Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

ACSL Practice Problem :Pentominoes

// http://acsl.org/acsl/96-97/pdf/int/prog2.pdf
// Reads from a file "input.txt"
// Change path of file to whatever you want

import java.util.*;
import java.io.*;

class Board
{
// Class variables
  String printstr;
  int[][] plane = new int[8][8];
 
// Initialize board
  void drawempty(){
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
           plane[x][y] = 0;
        }
    }
  }
 
// Print board
  void print()
  {
    for (int y = 7; y >= 0; y--) {
        printstr = "";
        for (int x = 0; x <= 7; x++) {
             printstr = printstr + " " + plane[x][y];
        }
        System.out.println(printstr);
       }
  }
 
// Fill cells for Pentomino A
  void pentA (int x, int y){
    plane[x][y] = 1;
    plane[x][y-1] = 1;
    plane[x][y-2] = 1;
    plane[x+1][y-2] = 1;
    plane[x+2][y-2] = 1;
  }
 
// Fill cells for Pentomino B
  void pentB (int x, int y){
    plane[x][y] = 1;
    plane[x][y-1] = 1;
    plane[x][y-2] = 1;
    plane[x+1][y] = 1;
    plane[x+1][y - 1] = 1;
  }
 
// Fill cells for Pentomino C
  void pentC (int x, int y){
    plane[x][y] = 1;
    plane[x-1][y] = 1;
    plane[x-1][y-1] = 1;
    plane[x+1][y] = 1;
    plane[x+1][y-1] = 1;
  }
 
// Fill cells for Pentomino D
  void pentD (int x, int y){
    plane[x][y] = 1;
    plane[x][y+1] = 1;
    plane[x-1][y] = 1;
    plane[x-2][y] = 1;
    plane[x-3][y] = 1;
  }
 
// Fill cells for Pentomino E
  void pentE (int x, int y){
    plane[x][y] = 1;
    plane[x][y+1] = 1;
    plane[x-1][y] = 1;
    plane[x-2][y] = 1;
    plane[x-2][y - 1] = 1;
  }
 
  public int Count ()
  { int count = 0;
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
           if (plane[x][y] == 1)
              count = count + 1;            
        }
    }
    return count;
  }
}
public class Main {
public static void main(String[] args)
throws java.io.IOException{
           
String fileName = "C:\\Users\\Administrator\\Documents\\NetBeansProjects\\pentominoes\\input.txt" ;
String line,type,xcoor,ycoor;
int linecount = 1,p_count,x0,y0;
 
   try
   {    
     BufferedReader input = new BufferedReader(new FileReader(fileName));
     line = input.readLine();
     while ( line != null )  // continue until end of file
     {
   
// Initialize object
       Board aBoard = new Board();
       aBoard.drawempty();
      
//Create StringTokenizer object 
       StringTokenizer tokenizer = new StringTokenizer(line,",", false);
      
       p_count = Integer.parseInt(tokenizer.nextToken(","));
      
// Read strings from line
       for(int i=1;i <= p_count; i++){
         
           type = tokenizer.nextToken();
           type = type.trim();
         
           xcoor = tokenizer.nextToken();
           xcoor = xcoor.trim();
         
           ycoor = tokenizer.nextToken();
           ycoor = ycoor.trim();

// Values are decremented by one because arrays start at [0]         
           x0 = Integer.parseInt(xcoor) - 1;
           y0 = Integer.parseInt(ycoor) - 1;
         
           if (type.equals("A")) {
              aBoard.pentA(x0, y0);
           }
         
           if (type.equals("B")) {
              aBoard.pentB(x0, y0);
           }
           if (type.equals("C")) {
              aBoard.pentC(x0, y0);
           }
         
           if (type.equals("D")) {
              aBoard.pentD(x0, y0);
           }
           if (type.equals("E")) {
              aBoard.pentE(x0, y0);
           }
       }

       System.out.println("Output #" + linecount + ": " + aBoard.Count());
     
       line = input.readLine();
       linecount++;
     }
     input.close();
   }
   catch ( IOException iox )
   {
     System.out.println("Problem reading " + fileName );
   }
}
}

You need to create an account or log in to post comments to this site.


Click here to browse all 6643 code snippets