/**
*Alex Chen
*Thomas Jefferson High School for Science and Technology
*Junior Division
*Contest#3 2008-2009
*PROGRAM: ACSL Blokus
**/
import
java.util.*;
public class
ACSLBlokus_AlexChen
{ //piece arrays for tile locations: {tile1,
tile2, etc.}
//if each piece were on a 3x3 array
with the top left corner being index (1, 1)
//then the tile location equals the
tile's index (as a two digit number, e.g. [1][1] is 11)
static int[] a
= {11, 12};
static int[] b
= {11, 12, 22};
static int[] c = {12, 21, 22, 23};
static int[] d
= {12, 21, 22, 31};
static int[][]
pieces = {a, b, c, d};
static
boolean[][] board = new boolean[10][10]; //false means you can't place a tile
there
static int row,
col, linkTo, linkWith, count;
public static
void main(String[] alex)
{
Scanner
dinosaur = new Scanner(System.in);
System.out.println("Please enter the inputs using spaces and commas
as delimiters:");
StringTokenizer parser;
String input, pos, output;
for(int i =
1; i <= 5; i++)
{
resetBoard();
count =
0;
output =
"";
System.out.println("#" + i + ":");
try{
input
= dinosaur.nextLine();
parser
= new StringTokenizer(input, ", ");
pos =
parser.nextToken();
linkTo
= Integer.parseInt(parser.nextToken());
linkWith = Integer.parseInt(parser.nextToken());
row =
74 - (int)(pos.charAt(0)); //A = row 9, B = row 8, ... J = row 0
col =
Integer.parseInt(pos.substring(1, pos.length())) - 1;
board[row][col] = board[row][col + 1] = false; //the piece's squares
if(col
> 0)
board[row][col - 1] = false; //space to the left of the piece
if(col
< 8)
board[row][col + 2] = false; //space to the right of the piece
if(row
> 0)
board[row - 1][col] = board[row - 1][col + 1] = false; //spaces above
the piece
if(row
< 9)
board[row + 1][col] = board[row + 1][col + 1] = false; //spaces below
the piece
}
catch(Exception
e)
{//nothing
}
if(linkTo
== 1)
col--;
else
col +=
2;
row--; //row and col now
represent the to-be-added tile's location
for(int
test = 0; test < 4; test++)
if(fits(pieces[test]))
output += (char)(test + 65) + ", ";
if(count
== 0)
System.out.println("NONE");
else
System.out.println(output.substring(0,
output.length() - 2));
}
System.exit(0);
}
public static
void resetBoard()
{
for(int r =
0; r < 10; r++)
for(int c
= 0; c < 10; c++)
board[r][c] = true;
}
public static
boolean fits(int[] piece)
{
boolean
itFits = true;
if(linkWith
> piece.length)
return
false;
int changeR
= row - (piece[linkWith - 1] / 10); //how much to add to the
"coordinates" of the
int changeC
= col - (piece[linkWith - 1] % 10); //target linking tile to put it in the
target location
int r, c;
for(int i =
0; i < 2; i++) //test piece fit at two locations
{
itFits =
true;
for(int f
: piece) //for each tile of the piece
{
r = f
/ 10 + changeR; //location of the current tile
c = f
% 10 + changeC; //based on the "change in rows" and the "changes
in columns"
try{
if(board[r][c]) //if the square is "true" then the tile
placement is valid
continue;
else
throw new ArrayIndexOutOfBoundsException();
//if tile overlaps original then it fails
}
catch(ArrayIndexOutOfBoundsException fail) //fails if tile is out of
array or it overlaps another tile
{
itFits = false;
break;
}
}
if(itFits)
break;
changeR
+= 2; //move location down two rows and test for piece fit there
}
if(itFits)
count++;
return
itFits;
}
}