public class Matrix { /* * Initializes the board matrix * @param boardSize the current boardsize */ public Matrix(int boardSize) { this.boardSize = boardSize; boardMatrix = new int[boardSize][boardSize]; for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { // first part of the number (x) encodes the occupation of the field (0,1,2), second part (= y)the scoring (xyy) // black if (i == boardSize/2 - 1 && j == boardSize/2 - 1 || i == (boardSize/2) && j == (boardSize/2) ) { boardMatrix [i][j] = BLACK ; // white } else if (i == boardSize/2 - 1 && j == (boardSize/2) || i == (boardSize/2) && j == (boardSize/2 - 1) ) { boardMatrix [i][j] = WHITE; } else { // not occupied boardMatrix [i][j] = EMPTY; } } } } /* * Prints a generic int matrix, used for DEBUG * @param m the matrix to be printed */ public void printMatrix(int[][] m) { for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { if (j == (boardSize - 1)) System.out.println(m[j][i]+""); else System.out.print(m[j][i]+""); } } } /* * Updates the players' scores * @param oldMatrix the board as it was before the current move * @param newMatrix the current board * @return a Tuple, holding the scores */ public Tuple countScore(int[][] newMatrix) { int newW = 0, newB = 0; for (int i = 0; i != boardSize; i++) { for (int j = 0; j != boardSize; j++) { if (newMatrix[i][j] == WHITE) newW++; if (newMatrix[i][j] == BLACK) newB++; } } // return score Tuple return new Tuple(newW, newB); } /* * Returns the board matrix * @return the current board matrix */ public int[][] getMatrix() { return boardMatrix; } /* * Set the board matrix * @param matrix the matrix to be set as the board matrix */ public void setMatrix(int[][] matrix) { for (int i = 0; i < boardSize; ++i) { for (int j = 0; j < boardSize; ++j) { boardMatrix[i][j] = matrix[i][j]; } } } private int boardMatrix[][]; private int boardSize; private static final int BLACK = 0; private static final int WHITE = 1; private static final int EMPTY = 2; }