Advertisement
Richard_Sekol

project7.6

Nov 16th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. package chap7;
  2. import java.util.*;
  3. public class tictac {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. public class TTTConsoleNonOO2P {
  8. int EMPTY = 0;
  9. int CROSS = 1;
  10. int OTHER = 2;
  11.  
  12. int PLAYING = 0;
  13. int DRAW = 1;
  14. int CROSS_WON = 2;
  15. int OTHER_WON = 3;
  16.  
  17. int ROWS = 3;
  18. int COLS = 3;
  19. int[][] board = new int[ROWS][COLS];
  20.  
  21. int currentState;
  22.  
  23. int currentPlayer;
  24. int currntRow, currentCol;
  25.  
  26. public static Scanner in = new Scanner(System.in);
  27.  
  28. public void main(String[] args) {
  29. initGame();
  30. do {
  31. playerMove(currentPlayer);
  32. updateGame(currentPlayer, currntRow, currentCol);
  33. printBoard();
  34. if (currentState == CROSS_WON) {
  35. System.out.println("'X' wins.");
  36. } else if (currentState == OTHER_WON) {
  37. System.out.println("'O' wins.");
  38. } else if (currentState == DRAW) {
  39. System.out.println("draw");}
  40. currentPlayer = (currentPlayer == CROSS) ? OTHER : CROSS;}
  41. while (currentState == PLAYING);}
  42. public void initGame() {
  43. for (int row = 0; row < ROWS; ++row) {
  44. for (int col = 0; col < COLS; ++col) {
  45. board[row][col] = EMPTY;
  46. }
  47. }
  48. currentState = PLAYING;
  49. currentPlayer = CROSS;
  50. }
  51.  
  52. public void playerMove(int theSeed) {
  53. boolean validInput = false;
  54. do {
  55. if (theSeed == CROSS) {
  56. System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
  57. } else {
  58. System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
  59. }
  60. int row = in.nextInt() - 1; // array index starts at 0 instead of 1
  61. int col = in.nextInt() - 1;
  62. if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
  63. currntRow = row;
  64. currentCol = col;
  65. board[currntRow][currentCol] = theSeed;
  66. validInput = true;
  67. } else {
  68. System.out.println("This move at (" + (row + 1) + "," + (col + 1)
  69. + ") is not valid. Try again...");
  70. }
  71. } while (!validInput);
  72. }
  73. public void updateGame(int theSeed, int currentRow, int currentCol) {
  74. if (hasWon(theSeed, currentRow, currentCol)) {
  75. currentState = (theSeed == CROSS) ? CROSS_WON : OTHER_WON;
  76. } else if (isDraw()) {
  77. currentState = DRAW;
  78. }
  79.  
  80. }
  81.  
  82. public boolean isDraw() {
  83. for (int row = 0; row < ROWS; ++row) {
  84. for (int col = 0; col < COLS; ++col) {
  85. if (board[row][col] == EMPTY) {
  86. return false;
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92.  
  93.  
  94. public boolean hasWon(int theSeed, int currentRow, int currentCol) {
  95. return (board[currentRow][0] == theSeed
  96. && board[currentRow][1] == theSeed
  97. && board[currentRow][2] == theSeed
  98. || board[0][currentCol] == theSeed
  99. && board[1][currentCol] == theSeed
  100. && board[2][currentCol] == theSeed
  101. || currentRow == currentCol
  102. && board[0][0] == theSeed
  103. && board[1][1] == theSeed
  104. && board[2][2] == theSeed
  105. || currentRow + currentCol == 2
  106. && board[0][2] == theSeed
  107. && board[1][1] == theSeed
  108. && board[2][0] == theSeed);}
  109. public void printBoard() {
  110. for (int row = 0; row < ROWS; ++row) {
  111. for (int col = 0; col < COLS; ++col) {
  112. printCell(board[row][col]);
  113. if (col != COLS - 1) {
  114. System.out.print("|");
  115. }
  116. }
  117. System.out.println();
  118. if (row != ROWS - 1) {
  119. System.out.println("-----------");
  120. }
  121. }
  122. System.out.println();
  123. }
  124. public void printCell(int content) {
  125. switch (content) {
  126. case EMPTY: System.out.print(" "); break;
  127. case OTHER: System.out.print(" O "); break;
  128. case CROSS: System.out.print(" X "); break;
  129. }
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement