Guest User

Untitled

a guest
Oct 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package Homework3;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class DotsAndDaggers {
  7.  
  8. private static char map[][];
  9. private static int size = 3;
  10.  
  11. private static char empty = '*';
  12. private static char dot = 'O';
  13. private static char dagger = 'X';
  14. private static byte charInLineToWin = 3;
  15. private static byte score = 0;
  16.  
  17. private static Scanner scanner = new Scanner(System.in);
  18. private static Random random = new Random();
  19.  
  20.  
  21. public static void main(String[] args) {
  22. initMap();
  23. printMap();
  24. while (true) {
  25. userTurn();
  26. if (isEndGame(dagger) == true) {
  27. break;
  28. }
  29. computerTurn();
  30. if (isEndGame(dot)) {
  31. break;
  32. }
  33. }
  34.  
  35. System.out.printf("Game over");
  36. }
  37.  
  38. public static void initMap() {
  39. map = new char[size][size];
  40. for (int i = 0; i < size; i++) {
  41. for (int j = 0; j < size; j++) {
  42. map[i][j] = empty;
  43. }
  44. }
  45. }
  46.  
  47. public static void printMap(){
  48. System.out.print("\t");
  49. for (int i = 1; i < size + 1; i++) {
  50. System.out.print(i + "\t");
  51. }
  52. System.out.println();
  53. System.out.println();
  54. for (int i = 0; i < size; i++) {
  55. System.out.print((i + 1) + "\t");
  56. for (int j = 0; j < size; j++) {
  57. System.out.print(map[i][j] + "\t");
  58. }
  59. System.out.println();
  60. }
  61. System.out.println();
  62. }
  63.  
  64. public static void userTurn(){
  65. System.out.println("User turn");
  66. int x, y;
  67. do {
  68. System.out.print("Enter coordinates of cell: ");
  69. x = scanner.nextInt() - 1;
  70. y = scanner.nextInt() - 1;
  71. } while (!isCellValid(y, x));
  72. map[x][y] = dagger;
  73. }
  74.  
  75. public static void computerTurn(){
  76. int good[][] = new int[size][size];
  77. int x, y;
  78. do {
  79. x = random.nextInt(size);
  80. y = random.nextInt(size);
  81.  
  82. } while (map[x][y] != empty);
  83.  
  84.  
  85.  
  86. System.out.println("computer made it's turn in cell " + (x + 1) + " " + (y + 1));
  87. System.out.println();
  88. map[x][y] = dot;
  89. }
  90.  
  91.  
  92. private static boolean isCellValid(int x, int y){
  93. boolean result = true;
  94. if ((map[y][x] != empty) || x >= size || x < 0 || y >= size || y < 0) {
  95. result = false;
  96. }
  97. return result;
  98. }
  99.  
  100. public static boolean isEndGame(char symbol){
  101. boolean result = false;
  102. printMap();
  103. if (isAnyWon(symbol)) {
  104. System.out.println(symbol + " won");
  105. result = true;
  106. }
  107.  
  108. if (isMapFull() == true && isAnyWon(symbol) != true){
  109. System.out.println("nobody won");
  110. result = true;
  111. }
  112. return result;
  113. }
  114.  
  115. public static boolean isAnyWon(char symbol){
  116.  
  117. score = 0;
  118.  
  119. for (int i = 0; i < map.length; i++) {
  120. if (map[i][i] == symbol) {
  121. score++;
  122. if (score == charInLineToWin) {
  123. return true;
  124. }
  125. }
  126. }
  127.  
  128. score = 0;
  129.  
  130. for (int i = 0; i < map.length; i++) {
  131. if (map[i][map.length - i - 1] == symbol) {
  132. score++;
  133. if (score == charInLineToWin) {
  134. return true;
  135. }
  136. }
  137. }
  138.  
  139. score = 0;
  140.  
  141. if (winInCaseColumn(3, symbol) == true) {
  142. return true;
  143. }
  144.  
  145. score = 0;
  146.  
  147. if (winInCaseColumn(2, symbol) == true) {
  148. return true;
  149. }
  150.  
  151. score = 0;
  152.  
  153. if (winInCaseColumn(1, symbol) == true) {
  154. return true;
  155. }
  156.  
  157. score = 0;
  158.  
  159. if (winInCaseLine(3, symbol) == true) {
  160. return true;
  161. }
  162.  
  163. score = 0;
  164.  
  165. if (winInCaseLine(2, symbol) == true) {
  166. return true;
  167. }
  168.  
  169. score = 0;
  170.  
  171. if (winInCaseLine(1, symbol) == true) {
  172. return true;
  173. }
  174.  
  175. return false;
  176. }
  177.  
  178. public static boolean winInCaseColumn(int x, char symbol){
  179. boolean result = false;
  180. for (int i = 0; i < size; i++) {
  181. if (map[i][map.length - x] == symbol) {
  182. score++;
  183. }
  184. if (score == charInLineToWin) {
  185. result = true;
  186. }
  187. }
  188. return result;
  189. }
  190.  
  191. public static boolean winInCaseLine(int x, char symbol){
  192. boolean result = false;
  193. for (int i = 0; i < size; i++) {
  194. if (map[map.length - x][i] == symbol) {
  195. score++;
  196. }
  197. if (score == charInLineToWin) {
  198. result = true;
  199. }
  200. }
  201. return result;
  202. }
  203.  
  204. public static boolean isMapFull(){
  205. boolean result = true;
  206. for (int i = 0; i < size; i++) {
  207. for (int j = 0; j < size; j++) {
  208. if (map[i][j] == empty) {
  209. result = false;
  210. }
  211. }
  212. }
  213. return result;
  214. }
  215. }
Add Comment
Please, Sign In to add comment