/* * board.java * * Created on February 16, 2006, 4:49 PM * * $ID$ */ /** *A TicTacToe board along with methods to determine *who's turn it is and allow them to make a move. * * @author dgg */ public class board { String[][] currentBoard = new String[3][3]; String turn; /** Creates a new instance of board */ //public board(int size) { //String[][] currentBoard = new String[size][size]; //This commented section, as well as the strange definiton //of Main.boardSize, has to do with my attempts to make the //board size variable. I never figured out how to initialize //an array after declaration, so it didn't work. public board(){ } /** Alternates turns, then makes a move for the current turn on the (zero-indexed)[i,j]th position on the board. */ public void move (int i, int j){ nextTurn();//nextTurn goes first so that we can use it to pick the //starting turn without having to define it explicitly currentBoard[i][j]=turn; } /** Used for debuging initially, left in for historical accuracy. not surprisingly, this just outputs the current content of the board. Prints one cell per line.*/ public void printBoard(){ int i; int j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ System.out.println(currentBoard[i][j]); } } } /** Returns the current turn */ public String whosTurn(){ return turn; } /** Moves to the next turn */ public void nextTurn(){ if (turn==null){//check this first, or we get nullPointer errors turn="X"; }else if (turn.equals("X")){ turn="O"; } else if (turn.equals("O")){ turn="X"; } else{ turn="X";//just in case "turn" gets screwed somehow } } }