fork download
  1. import java.util.Scanner;
  2. /**
  3.  * Tic-Tac-Toe: Two-player console, non-graphics, non-OO version.
  4.  * All variables/methods are declared as static (belong to the class)
  5.  * in the non-OO version.
  6.  */
  7. public class TTTConsoleNonOO2P {
  8. // Name-constants to represent the seeds and cell contents
  9. public static final int EMPTY = 0;
  10. public static final int CROSS = 1;
  11. public static final int NOUGHT = 2;
  12.  
  13. // Name-constants to represent the various states of the game
  14. public static final int PLAYING = 0;
  15. public static final int DRAW = 1;
  16. public static final int CROSS_WON = 2;
  17. public static final int NOUGHT_WON = 3;
  18.  
  19. // The game board and the game status
  20. public static final int ROWS = 3, COLS = 3; // number of rows and columns
  21. public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
  22. // containing (EMPTY, CROSS, NOUGHT)
  23. public static int currentState; // the current state of the game
  24. // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
  25. public static int currentPlayer; // the current player (CROSS or NOUGHT)
  26. public static int currntRow, currentCol; // current seed's row and column
  27.  
  28. public static Scanner in = new Scanner(System.in); // the input Scanner
  29.  
  30. /** The entry main method (the program starts here) */
  31. public static void main(String[] args) {
  32. // Initialize the game-board and current status
  33. initGame();
  34. // Play the game once
  35. do {
  36. playerMove(currentPlayer); // update currentRow and currentCol
  37. updateGame(currentPlayer, currntRow, currentCol); // update currentState
  38. printBoard();
  39. // Print message if game-over
  40. if (currentState == CROSS_WON) {
  41. System.out.println("'X' won! Bye!");
  42. } else if (currentState == NOUGHT_WON) {
  43. System.out.println("'O' won! Bye!");
  44. } else if (currentState == DRAW) {
  45. System.out.println("It's a Draw! Bye!");
  46. }
  47. // Switch player
  48. currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
  49. } while (currentState == PLAYING); // repeat if not game-over
  50. }
  51.  
  52. /** Initialize the game-board contents and the current states */
  53. public static void initGame() {
  54. for (int row = 0; row < ROWS; ++row) {
  55. for (int col = 0; col < COLS; ++col) {
  56. board[row][col] = EMPTY; // all cells empty
  57. }
  58. }
  59. currentState = PLAYING; // ready to play
  60. currentPlayer = CROSS; // cross plays first
  61. }
  62.  
  63. /** Player with the "theSeed" makes one move, with input validation.
  64.   Update global variables "currentRow" and "currentCol". */
  65. public static void playerMove(int theSeed) {
  66. boolean validInput = false; // for input validation
  67. do {
  68. if (theSeed == CROSS) {
  69. System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
  70. } else {
  71. System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
  72. }
  73. int row = in.nextInt() - 1; // array index starts at 0 instead of 1
  74. int col = in.nextInt() - 1;
  75. if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
  76. currntRow = row;
  77. currentCol = col;
  78. board[currntRow][currentCol] = theSeed; // update game-board content
  79. validInput = true; // input okay, exit loop
  80. } else {
  81. System.out.println("This move at (" + (row + 1) + "," + (col + 1)
  82. + ") is not valid. Try again...");
  83. }
  84. } while (!validInput); // repeat until input is valid
  85. }
  86.  
  87. /** Update the "currentState" after the player with "theSeed" has placed on
  88.   (currentRow, currentCol). */
  89. public static void updateGame(int theSeed, int currentRow, int currentCol) {
  90. if (hasWon(theSeed, currentRow, currentCol)) { // check if winning move
  91. currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
  92. } else if (isDraw()) { // check for draw
  93. currentState = DRAW;
  94. }
  95. // Otherwise, no change to currentState (still PLAYING).
  96. }
  97.  
  98. /** Return true if it is a draw (no more empty cell) */
  99. // TODO: Shall declare draw if no player can "possibly" win
  100. public static boolean isDraw() {
  101. for (int row = 0; row < ROWS; ++row) {
  102. for (int col = 0; col < COLS; ++col) {
  103. if (board[row][col] == EMPTY) {
  104. return false; // an empty cell found, not draw, exit
  105. }
  106. }
  107. }
  108. return true; // no empty cell, it's a draw
  109. }
  110.  
  111. /** Return true if the player with "theSeed" has won after placing at
  112.   (currentRow, currentCol) */
  113. public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
  114. return (board[currentRow][0] == theSeed // 3-in-the-row
  115. && board[currentRow][1] == theSeed
  116. && board[currentRow][2] == theSeed
  117. || board[0][currentCol] == theSeed // 3-in-the-column
  118. && board[1][currentCol] == theSeed
  119. && board[2][currentCol] == theSeed
  120. || currentRow == currentCol // 3-in-the-diagonal
  121. && board[0][0] == theSeed
  122. && board[1][1] == theSeed
  123. && board[2][2] == theSeed
  124. || currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
  125. && board[0][2] == theSeed
  126. && board[1][1] == theSeed
  127. && board[2][0] == theSeed);
  128. }
  129.  
  130. /** Print the game board */
  131. public static void printBoard() {
  132. for (int row = 0; row < ROWS; ++row) {
  133. for (int col = 0; col < COLS; ++col) {
  134. printCell(board[row][col]); // print each of the cells
  135. if (col != COLS - 1) {
  136. System.out.print("|"); // print vertical partition
  137. }
  138. }
  139. System.out.println();
  140. if (row != ROWS - 1) {
  141. System.out.println("-----------"); // print horizontal partition
  142. }
  143. }
  144. System.out.println();
  145. }
  146.  
  147. /** Print a cell with the specified "content" */
  148. public static void printCell(int content) {
  149. switch (content) {
  150. case EMPTY: System.out.print(" "); break;
  151. case NOUGHT: System.out.print(" O "); break;
  152. case CROSS: System.out.print(" X "); break;
  153. }
  154. }
  155. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class TTTConsoleNonOO2P is public, should be declared in a file named TTTConsoleNonOO2P.java
public class TTTConsoleNonOO2P {
       ^
1 error
stdout
Standard output is empty