fork(2) download
  1. /*TicTacToe.java*/
  2. public class TicTacToe{
  3. //private boolean isFull = false;//if true all the squares have been assigned.
  4. //private int playerCounter = 1;//if it is odd it's player1's turn if even it's player2's turn
  5. private TicTacToeEn[][] board = new TicTacToeEn[3][3];//to represent the 3-by-3 board
  6.  
  7. //private boolean isDraw;//to check if the game is draw
  8. //private boolean squareStatus;//returns the status of a square, if false the square is full if true the square is empty
  9. //private boolean victory;//if returns true one of the players has won
  10. //private int row;//row of the 3-by-3 board
  11. //private int column;//column of the 3-by-3 board
  12. //private int gameCounter;//counts the moves
  13.  
  14.  
  15. public enum TicTacToeEn{
  16. X, O, EMPTY
  17. }//end of enum
  18. TicTacToeEn theSymbol;//of type enum holds the current theSymbol, X or O
  19. //constructor
  20. public TicTacToe(){
  21. for( int i = 0; i < board.length; i++ ){
  22. for( int j = 0; j < board[i].length; j++ ){
  23. board[i][j] = TicTacToeEn.EMPTY;
  24. }//columns
  25. }//rows
  26. }//end of constructor
  27.  
  28. //checks if square is empty
  29. public boolean checkSquare( int row, int column ){
  30. if(board[row][column] == TicTacToeEn.EMPTY){
  31. return false;
  32. }
  33. else{
  34. return true;
  35. }
  36. }//end of checkSquare()
  37.  
  38. //assigns X or O to the square
  39. public void assignSymbol( int row, int column, int counter ){
  40. if( isOdd( counter )){//it's player1
  41. board[row][column] = TicTacToeEn.X;
  42. }
  43. else{//it's player2
  44. board[row][column] = TicTacToeEn.O;
  45. }
  46. }//end of assignSymbol()
  47.  
  48. //check if the game is draw
  49. public boolean isDraw(){
  50. for( int i = 0; i < board.length; i++ ){
  51. for(int j = 0; j < board[i].length; j++ ){
  52. if( board[i][j] == TicTacToeEn.EMPTY ){//if there is an empty square
  53. return false;
  54. }
  55. }//end of column array
  56. }//end of row array
  57. return true;//if there isn't an empty square
  58. }//end of isDraw()
  59.  
  60. //checks if anybody has won the game
  61. public boolean checkVictory( int row, int column, int counter ){
  62. if( isOdd( counter )){//it's player1
  63. theSymbol = TicTacToeEn.X;
  64. }
  65. else{//it's player2
  66. theSymbol = TicTacToeEn.O;
  67. }
  68. //determine victory
  69. if(
  70. ( board[row][0] == theSymbol && board[row][1] == theSymbol && board[row][2] == theSymbol )//check 3-in-the-row horizontally
  71. || ( board[0][column] == theSymbol && board[1][column] == theSymbol && board[2][column] == theSymbol )//check 3-in-the-row vertically
  72. || ( board[0][0] == theSymbol && board[1][1] == theSymbol && board[2][2] == theSymbol )//check 3-in-the-row 1st diagonal
  73. || ( board[0][2] == theSymbol && board[1][1] == theSymbol && board[2][0] == theSymbol )//check 3-in-the-row 2nd diagonal
  74. ){
  75. return true;
  76. }
  77.  
  78. else{
  79. return false;
  80. }
  81. }//end of checkVictory()
  82.  
  83. //determine if the argument is odd or even
  84. public boolean isOdd( int number){
  85. if( number % 2 == 0){//it's even
  86. return true;
  87. }
  88. else{//it's odd
  89. return false;
  90. }
  91. }//end of isOdd()
  92.  
  93. /*get the right input for the array,
  94. effectively subtracting 1 to the input so that the
  95. index is still within the array range*/
  96. public int optimizeInput( int theInput ){
  97. int newInput = theInput - 1;
  98. return newInput;
  99. }
  100.  
  101. //print board
  102. public void printBoard(){
  103. /* System.out.print("\n\n\n\n\n");
  104. System.out.println("| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |");
  105. System.out.println("| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |");
  106. System.out.println("| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |");
  107. System.out.print("\n\n\n\n\n"); */
  108. System.out.print("\n\n\n\n\n");
  109. System.out.printf("| %6s | %6s | %6s |", board[0][0], board[0][1], board[0][2]);
  110. System.out.printf("\n| %6s | %6s | %6s |", board[1][0], board[1][1], board[1][2]);
  111. System.out.printf("\n| %6s | %6s | %6s |", board[2][0], board[2][1], board[2][2]);
  112. System.out.print("\n\n\n\n\n");
  113. }
  114. }//end of class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty