Advertisement
peter2396

ttt

Jan 11th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 KB | None | 0 0
  1. import java.util.Random;
  2. //bugs - who won, extra draw, because place method should print then check winner
  3. import javax.swing.*;
  4. public class TicTacToe
  5. {
  6. static Random gen = new Random();
  7. static int[] freeSpots;
  8. static String rowParts;
  9. static int position;
  10. static boolean over = false;
  11. static boolean[] occupation = new boolean[9];
  12. static String header = "Choose a number 1-9.";
  13. static char playerChar, aiChar;
  14. static String[] options = {"X", "O"};
  15. static int[][] winnings = {{0,0,0,2,0,0,0,4},
  16. {2,0,2,2,2,0,2,4},
  17. {4,0,4,2,4,0,4,4},
  18. {0,0,2,0,0,0,4,0},
  19. {0,2,2,2,0,2,4,2},
  20. {0,4,2,4,0,4,4,4},
  21. {0,0,2,2,0,0,4,4},
  22. {0,4,2,2,0,4,4,0}};
  23.  
  24.  
  25. static char[][] board =
  26. {{' ', '|', ' ', '|', ' '},
  27. {'-', '+', '-', '+', '-'},
  28. {' ', '|', ' ', '|', ' '},
  29. {'-', '+', '-', '+', '-'},
  30. {' ', '|', ' ', '|', ' '}};
  31.  
  32. public static void main(String[] args)
  33. {
  34. printGrid(board);
  35. initialize();
  36. do
  37. {
  38. do
  39. {
  40. position = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number!", header, JOptionPane.INFORMATION_MESSAGE));
  41. if (occupation[position-1] == true)
  42. {
  43. header = "This spot is occupied!";
  44. }
  45. else header = "Pick a number";
  46. } while(occupation[position-1] != false);
  47.  
  48. place(board, true, position, true);
  49. if (over == false)
  50. {
  51. aiMove();
  52. checkWinner();
  53. }
  54.  
  55. } while (over != true);
  56. }
  57.  
  58. public static void initialize()
  59. {
  60. switch (JOptionPane.showOptionDialog(null, "Pick a letter!", "TicTacToe", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]))
  61. {
  62. case 0:
  63. {
  64. playerChar = 'X';
  65. aiChar = 'O';
  66. break;
  67. }
  68. case 1:
  69. {
  70. playerChar = 'O';
  71. aiChar = 'X';
  72. break;
  73. }
  74. }
  75.  
  76. for (int i = 0; i<occupation.length; i++)
  77. {
  78. occupation[i] = false;
  79. }
  80. }
  81.  
  82. public static void place(char[][] gameBoard, boolean player, int pos, boolean truePlacement)
  83. {
  84. if (player == true)
  85. gameBoard[2*((int)(pos*.33))][(int)(Double.parseDouble(((((Double.toString((pos%3)-1)))).replace("-1", "2")))*2)] = playerChar;
  86. else gameBoard[2*((int)(pos*.33))][(int)(Double.parseDouble(((((Double.toString((pos%3)-1)))).replace("-1", "2")))*2)] = aiChar;
  87.  
  88. if (truePlacement == true)
  89. {
  90. occupation[pos-1] = true;
  91. if (player == false) printGrid(board);
  92. }
  93. }
  94.  
  95. public static void printGrid(char[][] grid)
  96. {
  97. clear();
  98. for (char[] row : grid)
  99. {
  100. rowParts="";
  101. for (char c : row)
  102. rowParts+=c;
  103. System.out.println(rowParts);
  104. }
  105. }
  106. public static void aiMove()
  107. {
  108. //make array of available spots. see if putting a point in each spot will make you win, or make them win. if so, place there with intelligence percent
  109. freeSpots = null;
  110. freeSpots = new int[numFreeSpots()];
  111. int freeSpotsFound = 0;
  112. for (int i=0; i<9; i++)
  113. {
  114. if (occupation[i] == false)
  115. {
  116. freeSpots[freeSpotsFound] = i+1;
  117. freeSpotsFound++;
  118. }
  119. }
  120. char[][] testBoard = board.clone();
  121. testBoard = board; //Copy the board to a new variable to test winning
  122.  
  123. for (int spot: freeSpots)
  124. {
  125. place(testBoard, false, spot, false);
  126. for (int i = 0; i<8; i++)
  127. {
  128. if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
  129. {
  130. if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
  131. {
  132. place(board, false, spot, true);
  133. return;
  134. }
  135.  
  136. }
  137. }
  138. testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
  139. }
  140. //At this point, no game-winning spots were found. Next, we will try to avoid a loss.
  141. for (int spot: freeSpots)
  142. {
  143. place(testBoard, true, spot, false);
  144. for (int i = 0; i<8; i++)
  145. {
  146. if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
  147. {
  148. if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
  149. {
  150. place(board, false, spot, true);
  151. return;
  152. }
  153.  
  154. }
  155. }
  156. testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
  157. }
  158. //At this point, we couldn't win the game OR avoid a loss. Now we will look into the future and plan a path by placing TWO fake points in available locations.
  159. //If this path can cause us to win, pick one of the potential spots at random, so that we don't just draw a straight line and reveal our plan.
  160. for (int j = 0; j<numFreeSpots()*10; j++)
  161. {
  162. freeSpots = null;
  163. freeSpots = new int[numFreeSpots()];
  164. freeSpotsFound = 0;
  165. for (int i=0; i<9; i++)
  166. {
  167. if (occupation[i] == false)
  168. {
  169. freeSpots[freeSpotsFound] = i+1;
  170. freeSpotsFound++;
  171. }
  172. }
  173.  
  174. int index = 0;
  175. int[] freeSpotsCopy = freeSpots.clone();
  176. int spot = freeSpotsCopy[gen.nextInt(freeSpotsCopy.length)];
  177.  
  178. freeSpotsCopy = null;
  179. freeSpotsCopy = new int[freeSpots.length-1];
  180.  
  181. for (int s: freeSpots)
  182. {
  183. if (s != spot)
  184. {
  185. freeSpotsCopy[index] = s;
  186. index++;
  187. }
  188. }
  189. int spot2 = freeSpotsCopy[gen.nextInt(freeSpotsCopy.length)];
  190. place(testBoard, false, spot, false);
  191. place(testBoard, false, spot2, false);
  192.  
  193. for (int i = 0; i<8; i++)
  194. {
  195. if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
  196. {
  197. if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
  198. {
  199. testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
  200. testBoard[2*((int)(spot2*.33))][(int)(Double.parseDouble(((((Double.toString((spot2%3)-1)))).replace("-1", "2")))*2)] = ' ';
  201. place(board, false, spot, true);
  202. return;
  203. }
  204. }
  205. }
  206. testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
  207. testBoard[2*((int)(spot2*.33))][(int)(Double.parseDouble(((((Double.toString((spot2%3)-1)))).replace("-1", "2")))*2)] = ' ';
  208. }
  209. //At this point, we couldn't win, block, or find a winning path. So now, we will pick a point at random and go there.
  210.  
  211. freeSpots = null;
  212. freeSpots = new int[numFreeSpots()];
  213. freeSpotsFound = 0;
  214. for (int i=0; i<9; i++)
  215. {
  216. if (occupation[i] == false)
  217. {
  218. //System.out.println("found a free spot at "+(i+1));
  219. freeSpots[freeSpotsFound] = i+1;
  220. freeSpotsFound++;
  221. }
  222. }
  223. //System.out.println(freeSpots.length);
  224. place(board, false, freeSpots[gen.nextInt(freeSpots.length)], true);
  225. return;
  226. }
  227. public static void checkWinner()
  228. {
  229. for (int i = 0; i<8; i++)
  230. {
  231. if (board[winnings[i][0]][winnings[i][1]] == board[winnings[i][2]][winnings[i][3]] && board[winnings[i][4]][winnings[i][5]] == board[winnings[i][6]][winnings[i][7]])
  232. {
  233. if (board[winnings[i][0]][winnings[i][1]] != ' ' && board[winnings[i][2]][winnings[i][3]] != ' ' && board[winnings[i][6]][winnings[i][7]] != ' ')
  234. {
  235. printGrid(board);
  236. over = true;
  237. if (boardFull() == true)
  238. {
  239. JOptionPane.showMessageDialog(null, "Tie!");
  240. }
  241. else if (board[winnings[i][0]][winnings[0][i]] == playerChar)
  242. JOptionPane.showMessageDialog(null, "You win!");
  243. else JOptionPane.showMessageDialog(null, "You lose!");
  244. }
  245. }
  246. }
  247. }
  248. public static int numFreeSpots()
  249. {
  250. int spots = 0;
  251. for (boolean b: occupation)
  252. {
  253. if (b == false)
  254. {
  255. spots++;
  256. }
  257. }
  258. return spots;
  259. }
  260. public static boolean boardFull()
  261. {
  262. for (boolean b: occupation)
  263. {
  264. if (b == false)
  265. {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. public static void clear()
  272. {
  273. for (int i = 0; i<=10; i++)
  274. {
  275. System.out.println(" ");
  276. }
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement