Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. package HW_XO.Lesson_4;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7. public static char[][] map;
  8. public static final int SIZE = 5;
  9. // public static final int DOTS_TO_WIN = 4;
  10. public static final char DOTS_EMPTY = '.';
  11. public static final char DOTS_X = 'X';
  12. public static final char DOTS_O = 'O';
  13. public static Scanner sc = new Scanner(System.in);
  14. public static Random rand = new Random();
  15.  
  16.  
  17. public static void main(String[] args) {
  18. initMap();
  19. printMap();
  20. while (true) {
  21. humanTurn();
  22. printMap();
  23. if (chekcWin(DOTS_X)) {
  24. System.out.println("Человек победил");
  25. break;
  26. }
  27. if (isMapFull()) {
  28. System.out.println("Ничья");
  29. break;
  30. }
  31. aiTurn();
  32. printMap();
  33. if (chekcWin(DOTS_O)) {
  34. System.out.println("Компьютер победил");
  35. break;
  36. }
  37. if (isMapFull()) {
  38. System.out.println("Ничья");
  39. break;
  40. }
  41. }
  42. System.out.println("Игра закончена");
  43. sc.close();
  44. }
  45.  
  46. public static void initMap() {
  47. map = new char[SIZE][SIZE];
  48. for (int i = 0; i < SIZE; i++) {
  49. for (int j = 0; j < SIZE; j++) {
  50. map[i][j] = DOTS_EMPTY;
  51. }
  52. }
  53. }
  54.  
  55. public static void printMap() {
  56. for (int i = 0; i <= SIZE; i++) {
  57. System.out.print(i + " ");
  58. }
  59. System.out.println();
  60. for (int i = 0; i < SIZE; i++) {
  61. System.out.print(i+1 + " ");
  62. for (int j = 0; j < SIZE; j++) {
  63. System.out.print(map[i][j] + " ");
  64. }
  65. System.out.println();
  66. }
  67. System.out.println();
  68. }
  69.  
  70. public static void humanTurn() {
  71. int x, y;
  72. do {
  73. System.out.println("Ввведите координаты в формате X Y");
  74. x = sc.nextInt() - 1;
  75. y = sc.nextInt() - 1;
  76. } while (!isCellValid(x, y));
  77. map[y][x] = DOTS_X;
  78. }
  79.  
  80. public static boolean isCellValid(int x, int y) {
  81. if (x < 0 || x >=SIZE || y < 0 || y >= SIZE) return false;
  82. if (map[y][x] == DOTS_EMPTY) return true;
  83. return false;
  84. }
  85.  
  86. public static void aiTurn() {
  87. int x, y;
  88. do {
  89. x = rand.nextInt(SIZE);
  90. y = rand.nextInt(SIZE);
  91. } while (!isCellValid(x, y));
  92. System.out.println("Компьютер походил в точку " + (x + 1) + " " + (y + 1));
  93. map[y][x] = DOTS_O;
  94. }
  95.  
  96. public static boolean chekcWin(char symb) {
  97. int col = 0;
  98. int row = 0;
  99. int dg = 0;
  100. int dp = 0;
  101. for (int i = 0; i < SIZE; i++) {
  102. for (int j = 0; j < SIZE; j++) {
  103. if (map[i][j] == symb) {
  104. row++;
  105. }
  106. if (map[j][i] == symb) {
  107. col++;
  108. }
  109. if (i == j && map[i][j] == symb || (i == (j - 1)) && map[i][j] == symb || (i == (j + 1)) && map[i][j] == symb) {
  110. dg++;
  111. }
  112. if ((i + j) == (SIZE - 1) && map[i][j] == symb || ((i + j) == SIZE - 2) && map[i][j] == symb || (i + j) == (SIZE) && map[i][j] == symb) {
  113. dp++;
  114. }
  115. }
  116.  
  117. if (col == (SIZE - 1) || row == (SIZE - 1) || dg == (SIZE - 1) || dp == (SIZE - 1)) {
  118. return true;
  119. } else col = 0; row = 0;
  120. }
  121.  
  122. // if (map[0][0] == symb && map[0][1] == symb && map[0][2] == symb) return true;
  123. // if (map[1][0] == symb && map[1][1] == symb && map[1][2] == symb) return true;
  124. // if (map[2][0] == symb && map[2][1] == symb && map[2][2] == symb) return true;
  125. // if (map[0][0] == symb && map[1][0] == symb && map[2][0] == symb) return true;
  126. // if (map[0][1] == symb && map[1][1] == symb && map[2][1] == symb) return true;
  127. // if (map[0][2] == symb && map[1][2] == symb && map[2][2] == symb) return true;
  128. // if (map[0][0] == symb && map[1][1] == symb && map[2][2] == symb) return true;
  129. // if (map[2][0] == symb && map[1][1] == symb && map[0][2] == symb) return true;
  130. return false;
  131. }
  132.  
  133. public static boolean isMapFull() {
  134. for (int i = 0; i < SIZE; i++) {
  135. for (int j = 0; j < SIZE; j++) {
  136. if (map[i][j] == DOTS_EMPTY) return false;
  137. }
  138. }
  139. return true;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement