// Textbook fragment 03.11 /** Returns a simple character string showing the current board */ public String toString() { String s = ""; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { switch (board[i][j]) { case X: s += "X"; break; case O: s += "O"; break; case EMPTY: s += " "; break; } if (j < 2) s += "|"; // column boundary } if (i < 2) s += "\n-----\n"; // row boundary } return s; } /** Test run of a simple game */ public static void main(String[] args) { TicTacToe game = new TicTacToe(); /* X moves: */ /* O moves: */ game.putMark(1,1); game.putMark(0,2); game.putMark(2,2); game.putMark(0,0); game.putMark(0,1); game.putMark(2,1); game.putMark(1,2); game.putMark(1,0); game.putMark(2,0); System.out.println(game.toString()); int winningPlayer = game.winner(); if (winningPlayer != 0) System.out.println(winningPlayer + " wins"); else System.out.println("Tie"); } }