package contest3a; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; public class Petteia { static private String[][] board = new String[ 10 ][ 10 ]; static private int[][] OMarkers = new int[20][2]; static private ArrayList OCaptured = new ArrayList(); static private ArrayList xValuesRow = new ArrayList(); static private ArrayList xValuesCol = new ArrayList(); public static void main(String[] args) throws IOException { initializeBoard( board ); putPeicesOnBoard(board, xValuesRow, xValuesCol); printTheBoard( board ); Petteia.findCaptured(); Petteia.capturedByX(); } private static void initializeBoard( String[][] board ) { // all squares are initialized with '==' for( int i = 0; i < 10; i++ ) for( int j = 0; j < 10; j++ ) board[ i ][ j ] = "=="; } // end initializeBoard() private static void printTheBoard( String[][] board ) { // visuals representation of the board for( int i = 8; i > 0; i-- ) { System.out.print(i); for( int j = 1; j <= 8; j++ ) System.out.print(" " + board[ i ][ j ] ); System.out.println(); } System.out.println(" 1 2 3 4 5 6 7 8\n"); } // end printTheBoard() private static void putPeicesOnBoard( String[][] board, ArrayList xValuesRow, ArrayList xValuesCol ) throws IOException { BufferedReader in = new BufferedReader( new FileReader( "Petteia.in" ) ); String line = in.readLine(); StringTokenizer st = new StringTokenizer( line, ", " ); // delimiter on ", " int numO = Integer.parseInt( st.nextToken()); int col, row; for( int i = 0; i <1; i++ ) // adds the "O"s to the grid { for( int j = 0; j < numO; j++ ) { String piece = "O "; String coord1 = st.nextToken(); String coord2 = st.nextToken(); row = (int) coord1.charAt( 0 ) - 48; // Column coordinate col = (int) coord2.charAt( 0 ) - 48; // row coordinate //keeps track of the O values OMarkers[j][0] = row; // row in first column of array OMarkers[j][1] = col; // col in secound column board[ row ][ col ] = piece; // adds piece to the board } // end for j } // end for i // ads the "X"s to the grid line = in.readLine(); StringTokenizer ab = new StringTokenizer(line, ", "); int numX = Integer.parseInt( ab.nextToken()); for( int i = 0; i <1; i++ ) // ads the "X"s to the grid { for( int j = 0; j < numX; j++ ) { String piece = "X "; String coord1 = ab.nextToken(); String coord2 = ab.nextToken(); row = (int) coord1.charAt( 0 ) - 48; // col coordinate col = (int) coord2.charAt( 0 ) - 48; // row coordinate board[ row ][ col ] = piece; } // end for j } // end for i for(int i = 0; i<=4; i++) // keeps track of the 5 given "X" values that will be needed later { line = in.readLine(); StringTokenizer yz = new StringTokenizer(line, ", "); String coord1 = yz.nextToken(); String coord2 = yz.nextToken(); row = (int) coord1.charAt( 0 ) - 48; // col coordinate col = (int) coord2.charAt( 0 ) - 48; // row coordinate xValuesRow.add(row); //row in one arrayList xValuesCol.add(col); // corresponding column in other arrayList }//end for } // end outPeicesOnBoard() static void findCaptured() { // for(int i = 0; i < OMarkers.length; i++) { int row = OMarkers[i][0]; int col = OMarkers[i][1]; if(row != 0 && col != 0) { if(board[row+1][col].equals("X ") && board[row-1][col].equals("X ") || // captured from top and bottom board[row][col+1].equals("X ") && board[row][col-1].equals("X ") || // captured from sides board[row][col+1].equals("X ") && board[row][col-1].equals("O ") && board[row][col-2].equals("X ") || board[row][col-1].equals("X ") && board[row][col+1].equals("O ") && board[row][col+2].equals("X ") || board[row+1][col].equals("X ") && board[row-1][col].equals("O ") && board[row-2][col].equals("X ") || board[row-1][col].equals("X ") && board[row+1][col].equals("O ") && board[row+2][col].equals("X ")) OCaptured.add(true); else OCaptured.add(false); } } } static void capturedByX() { String outPut = ""; String toPrint = ""; // xValues, OCaptured for (int i = 0; i < xValuesRow.size(); i++) { int rowX = xValuesRow.get(i); int colX = xValuesCol.get(i); for(int j = 0; j< OCaptured.size(); j++) { if(OCaptured.get(j) == true) { int rowO= OMarkers[j][0]; int colO = OMarkers[j][1]; if(rowO+1 == rowX && colO == colX && board[rowO-1][colO].equals("X ")|| rowO-1 == rowX && colO == colX && board[rowO+1][colO].equals("X ")|| rowO == rowX && colO+1 == colX && board[rowO][colO-1].equals("X ")|| rowO == rowX && colO-1 == colX && board[rowO][colO+1].equals("X ")|| rowO == rowX && colO == colX+2 && board[rowO][colO+1].equals("X ") && board[rowO][colO-1].equals("O ")|| rowO == rowX && colO == colX-2 && board[rowO][colO-1].equals("X ") && board[rowO][colO+1].equals("O ")|| rowO == rowX && colO == colX-1 && board[rowO][colO-2].equals("X ") && board[rowO][colO-1].equals("O ")|| rowO == rowX && colO == colX+1 && board[rowO][colO+2].equals("X ") && board[rowO][colO+1].equals("O ")|| rowO == rowX-1 && colO == colX && board[rowO-1][colO].equals("O ") && board[rowO-2][colO].equals("X ")|| rowO == rowX-2 && colO == colX && board[rowO+1][colO].equals("O ") && board[rowO-1][colO].equals("X ") || rowO == rowX+1 && colO == colX && board[rowO+1][colO].equals("O ") && board[rowO+2][colO].equals("X ") || rowO ==rowX+2 && colO == colX && board[rowO-1][colO].equals("O ") && board[rowO+1][colO].equals("X ")) outPut = outPut + rowO+ ","+ colO + " and "; }// end if } //end for j if(outPut.equals("")) { System.out.println("NONE"); } else { toPrint = outPut.substring(0, outPut.length()-4); outPut = ""; System.out.println(toPrint); toPrint = ""; } }// end for i } // end capturedByX }// end class // 8 == == == == == == == == // 7 == == X == == == == == // 6 == == O == == == == == // 5 == == X O O X == == // 4 == == == == == == == == // 3 == == == == == == == X // 2 == == == == == == X O // 1 == == == == == == X O // 1 2 3 4 5 6 7 8 // // 6,3 // 5,4 and 5,5 // 5,4 and 5,5 and 6,3 // NONE // NONE