Guest User

Untitled

a guest
Apr 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. private static Scanner scanner = new Scanner(System.in);
  6.  
  7. private static char[][] initiateGameField() {
  8. char[][] gameField = new char[3][3];
  9. int inputIndex = 0;
  10. // System.out.print("Enter cells: ");
  11. // String inputString = scanner.nextLine();
  12. String inputString = "_________";
  13. for (int i = 0; i < 3; i++) {
  14. for (int j = 0; j <3; j++) {
  15. gameField[i][j] = inputString.charAt(inputIndex);
  16. inputIndex++;
  17. }
  18. }
  19. drawGameField(gameField);
  20. return gameField;
  21. }
  22.  
  23. private static void drawGameField(char[][] gameField) {
  24. System.out.print("---------\n");
  25. for (int i = 0; i < 3; i++) {
  26. System.out.printf("| %s %s %s |\n",
  27. gameField[i][0], gameField[i][1], gameField[i][2]);
  28. }
  29. System.out.print("---------\n");
  30. }
  31.  
  32. private static String analyzeGameField(char[][] gameField) {
  33. String outcome = "Game not finished";
  34. int wins = 0;
  35. int countX = 0;
  36. int countY = 0;
  37. int emptyCells = 0;
  38.  
  39. for (int i = 0; i < 3; i++) {
  40. for (int j = 0; j < 3; j++) {
  41. if (gameField[i][j] == 'X') {
  42. countX++;
  43. }
  44. if (gameField[i][j] == 'O') {
  45. countY++;
  46. }
  47. if (gameField[i][j] == '_') {
  48. emptyCells++;
  49. }
  50. }
  51. }
  52.  
  53. for (int i = 0; i < 3; i++) {
  54. if (gameField[i][0] == gameField[i][1] && gameField[i][1] == gameField[i][2] && gameField[i][0] != '_') {
  55. outcome = String.format("%s wins", gameField[i][0]);
  56. wins++;
  57. }
  58. if (gameField[0][i] == gameField[1][i] && gameField[1][i] == gameField[2][i] && gameField[0][i] != '_') {
  59. outcome = String.format("%s wins", gameField[0][i]);
  60. wins++;
  61. }
  62. }
  63.  
  64. if (gameField[0][0] == gameField[1][1] && gameField[1][1] == gameField[2][2] && gameField[1][1] != '_') {
  65. outcome = String.format("%s wins", gameField[0][0]);
  66. wins++;
  67. }
  68. if (gameField[0][2] == gameField[1][1] && gameField[1][1] == gameField[2][0] && gameField[1][1] != '_') {
  69. outcome = String.format("%s wins", gameField[0][2]);
  70. wins++;
  71. }
  72.  
  73. if (emptyCells == 0 && wins == 0) {
  74. outcome = "Draw";
  75. }
  76. if (wins > 2 || Math.abs(countX - countY) > 1) {
  77. outcome = "Impossible";
  78. }
  79.  
  80. return outcome;
  81. }
  82.  
  83. private static char[][] playerMove(char[][] gameField, char player) { // player - X or O
  84. boolean moveFinished = false;
  85. int x;
  86. int y;
  87. while (!moveFinished) {
  88. System.out.print("Enter the coordinates: ");
  89. String input = scanner.nextLine();
  90. try {
  91. x = Integer.parseInt(input.substring(0, 1));
  92. y = Integer.parseInt(input.substring(2, 3));
  93. if (x < 0 || x > 3 || y < 0 || y > 3) {
  94. System.out.println("Coordinates should be from 1 to 3!");
  95. continue;
  96. }
  97.  
  98. // I Have no idea how to an make algorithm for that
  99. if (x == 1 && y == 1) {x = 2; y = 0;}
  100. if (x == 1 && y == 2) {x = 1; y = 0;}
  101. if (x == 1 && y == 3) {x = 0; y = 0;}
  102. if (x == 2 && y == 1) {x = 2; y = 1;}
  103. if (x == 2 && y == 2) {x = 1; y = 1;}
  104. if (x == 2 && y == 3) {x = 0; y = 1;}
  105. if (x == 3 && y == 1) {x = 2; y = 2;}
  106. if (x == 3 && y == 2) {x = 1; y = 2;}
  107. if (x == 3 && y == 3) {x = 0; y = 2;}
  108.  
  109. } catch (Exception e){
  110. System.out.println("You should enter numbers!");
  111. continue;
  112. }
  113. if (gameField[x][y] == '_') {
  114. gameField[x][y] = player;
  115. } else {
  116. System.out.println("This cell is occupied! Choose another one!");
  117. continue;
  118. }
  119. moveFinished = true;
  120. }
  121. return gameField;
  122. }
  123.  
  124. static void clear() {
  125. try {
  126. new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  127. } catch (Exception E) {
  128. System.out.println(E);
  129. }
  130. }
  131.  
  132. private static void game() {
  133. char[][] gameField = initiateGameField();
  134. boolean inProgress = true;
  135. char[] players = new char[] {'X', 'O'};
  136.  
  137. while (inProgress) {
  138. for (char player : players) {
  139. gameField = playerMove(gameField, player);
  140. clear();
  141. drawGameField(gameField);
  142. String outcome = analyzeGameField(gameField);
  143. if (!outcome.equals("Game not finished")) {
  144. System.out.println(outcome);
  145. inProgress = false;
  146. break;
  147. }
  148. }
  149. }
  150. }
  151.  
  152. public static void main(String[] args) {
  153. game();
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment