Guest User

Untitled

a guest
Dec 10th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. public class TicTacToe {
  2.  
  3. public static char[][] board = new char[3][3];
  4. static Scanner scan = new Scanner(System.in);
  5. public static String keepPlaying;
  6. public static char yesOrNo;
  7. public static boolean playing;
  8. public static int row, col;
  9. public static User human1 = new User();
  10. public static User human2 = new User();
  11.  
  12. public static void main(String[] args) {
  13.  
  14. human1.getUser1Name();
  15. human2.getUser2Name();
  16.  
  17. System.out.println("nPlayer 1: " + human1.human1Name() + ": X's");
  18. System.out.println("Player 2: " + human2.human2Name() + ": O'sn");
  19.  
  20. initializeBoard();
  21. displayBoard();
  22.  
  23. playing = true;
  24.  
  25.  
  26. while (playing == true)
  27. {
  28.  
  29. System.out.println("n" + human1.human1Name() + " it's your turn.");
  30. human1.getChoice();
  31. human1.convert();
  32.  
  33.  
  34. if (human1.returnRow() == 0 && human1.returnCol() == 0/* && board[0][0] == ' '*/)
  35. {
  36. board[0][0] = 'X';
  37. displayBoard();
  38. }
  39. else if (human1.returnRow() == 0 && human1.returnCol() == 1 && board[1][0] == ' ')
  40. {
  41. board[1][0] = 'X';
  42. displayBoard();
  43. }
  44. else if (human1.returnRow() == 0 && human1.returnCol() == 2 && board[2][0] == ' ')
  45. {
  46. board[2][0] = 'X';
  47. displayBoard();
  48. }
  49. else if (human1.returnRow() == 1 && human1.returnCol() == 0 && board[0][1] == ' ')
  50. {
  51. board[0][1] = 'X';
  52. displayBoard();
  53. }
  54. else if (human1.returnRow() == 1 && human1.returnCol() == 1 && board[1][1] == ' ')
  55. {
  56. board[1][1] = 'X';
  57. displayBoard();
  58. }
  59. else if (human1.returnRow() == 1 && human1.returnCol() == 2 && board[2][1] == ' ')
  60. {
  61. board[2][1] = 'X';
  62. displayBoard();
  63. }
  64. else if (human1.returnRow() == 2 && human1.returnCol() == 0 && board[0][2] == ' ')
  65. {
  66. board[0][2] = 'X';
  67. displayBoard();
  68. }
  69. else if (human1.returnRow() == 2 && human1.returnCol() == 1 && board[1][2] == ' ')
  70. {
  71. board[1][2] = 'X';
  72. displayBoard();
  73. }
  74. else if (human1.returnRow() == 2 && human1.returnCol() == 2 && board[2][2] == ' ')
  75. {
  76. board[2][2] = 'X';
  77. displayBoard();
  78. }
  79.  
  80. if (checkForWinner() == true)
  81. {
  82. System.out.println("n" + human1.human1Name() + " has won the game!!!");
  83. playAgain();
  84. if (playing == false)
  85. {
  86. break;
  87. }
  88.  
  89. }
  90.  
  91. if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
  92. && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
  93. {
  94. System.out.println("It's a draw!!!");
  95. playAgain();
  96. if (playing == false)
  97. {
  98. break;
  99. }
  100. }
  101.  
  102.  
  103.  
  104. System.out.println("n" + human2.human2Name() + " it's your turn.");
  105. human2.getChoice();
  106. human2.convert();
  107.  
  108. if (human2.returnRow() == 0 && human2.returnCol() == 0 /*&& board[0][0] == ' '*/)
  109. {
  110. board[0][0] = 'O';
  111. displayBoard();
  112. }
  113. else if (human2.returnRow() == 0 && human2.returnCol() == 1 && board[1][0] == ' ')
  114. {
  115. board[1][0] = 'O';
  116. displayBoard();
  117. }
  118. else if (human2.returnRow() == 0 && human2.returnCol() == 2 && board[2][0] == ' ')
  119. {
  120. board[2][0] = 'O';
  121. displayBoard();
  122. }
  123. else if (human2.returnRow() == 1 && human2.returnCol() == 0 && board[0][1] == ' ')
  124. {
  125. board[0][1] = 'O';
  126. displayBoard();
  127. }
  128. else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1] == ' ')
  129. {
  130. board[1][1] = 'O';
  131. displayBoard();
  132. }
  133. else if (human2.returnRow() == 1 && human2.returnCol() == 2 && board[2][1] == ' ')
  134. {
  135. board[2][1] = 'O';
  136. displayBoard();
  137. }
  138. else if (human2.returnRow() == 2 && human2.returnCol() == 0 && board[0][2] == ' ')
  139. {
  140. board[0][2] = 'O';
  141. displayBoard();
  142. }
  143. else if (human2.returnRow() == 2 && human2.returnCol() == 1 && board[1][2] == ' ')
  144. {
  145. board[1][2] = 'O';
  146. displayBoard();
  147. }
  148. else if (human2.returnRow() == 2 && human2.returnCol() == 2 && board[2][2] == ' ')
  149. {
  150. board[2][2] = 'O';
  151. displayBoard();
  152. }
  153.  
  154.  
  155. if (checkForWinner() == true)
  156. {
  157. System.out.println("n" + human2.human2Name() + " has won the game!!!");
  158. playAgain();
  159. }
  160.  
  161. if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
  162. && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
  163. {
  164. System.out.println("It's a draw!!!");
  165. playAgain();
  166. }
  167.  
  168.  
  169. }
  170.  
  171.  
  172. }
  173.  
  174. public TicTacToe() {
  175.  
  176. board = new char[3][3];
  177.  
  178. }
  179.  
  180. public static void initializeBoard() {
  181.  
  182. for (int row = 0; row < 3; row++)
  183. {
  184. for (int col = 0; col < 3; col++)
  185. {
  186. board[row][col] = ' ';
  187. }
  188. }
  189. }
  190.  
  191. public static void displayBoard() {
  192.  
  193. System.out.println(" A B C");
  194. System.out.println(" -------------");
  195.  
  196. for (int row = 0; row < 3; row++)
  197. {
  198. System.out.print((row + 1) + " | ");
  199. for (int col = 0; col < 3; col++)
  200. {
  201. System.out.print(board[row][col] + " | ");
  202. }
  203. System.out.println();
  204. System.out.println(" -------------");
  205. }
  206. }
  207.  
  208. public static boolean checkForWinner() {
  209.  
  210. if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][1] != ' ')
  211. {
  212. return true;
  213. }
  214. if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ')
  215. {
  216. return true;
  217. }
  218. if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][1] != ' ')
  219. {
  220. return true;
  221. }
  222. if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[1][0] != ' ')
  223. {
  224. return true;
  225. }
  226. if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ')
  227. {
  228. return true;
  229. }
  230. if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[1][2] != ' ')
  231. {
  232. return true;
  233. }
  234. if (board[0][0] == board[1][1] && board [1][1] == board[2][2] && board[1][1] != ' ')
  235. {
  236. return true;
  237. }
  238. if (board[0][2] == board [1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
  239. {
  240. return true;
  241. }
  242.  
  243. return false;
  244. }
  245.  
  246. public static boolean playAgain() {
  247.  
  248. System.out.println("nWould you like to play again?");
  249.  
  250. keepPlaying = scan.nextLine();
  251.  
  252. yesOrNo = keepPlaying.toUpperCase().charAt(0);
  253.  
  254. if (yesOrNo == 'N')
  255. {
  256. System.out.println("nThanks for playing!");
  257. playing = false;
  258. initializeBoard();
  259. return playing;
  260. }
  261. else if (yesOrNo == 'Y')
  262. {
  263. initializeBoard();
  264. displayBoard();
  265. return true;
  266. }
  267. return playing;
  268. }
  269.  
  270. public static boolean notValid() {
  271.  
  272. if (human1.returnRow() > 2 || human1.returnRow() < 0 || human1.returnCol() > 2 || human1.returnCol() < 0 || human2.returnRow() > 2
  273. || human2.returnRow() < 0 || human2.returnCol() > 2 || human2.returnCol() < 0 || !isEmpty())
  274. {
  275. System.out.println("Invalid");
  276. return true;
  277. }
  278. else
  279. {
  280. return false;
  281. }
  282. }
  283.  
  284. public static boolean isEmpty() {
  285.  
  286. if (board[0][0] == ' ' && board[0][1] == ' ' && board[0][2] == ' ' && board[1][0] == ' ' && board[1][1] == ' '
  287. && board[1][2] == ' ' && board[2][0] == ' ' && board[2][1] == ' ' && board[2][2] == ' ')
  288. {
  289. return true;
  290. }
  291. else
  292. {
  293. System.out.println("That position is taken.n");
  294. return false;
  295. }
  296. }
  297.  
  298. else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1]==' ')
  299. {
  300. board[1][1] = 'O';
  301. displayBoard();
  302. }
  303.  
  304. boolean turnOver = false;
  305. while(!turnOver){
  306. if(board[human2.returnRow()][human2.returnCol()] != ' '){
  307. System.out.println("That space is taken")
  308. }else{
  309. if(human2.returnRow() == 1 && human2.returnCol() == 1){
  310. board[1][1] = 'O';
  311. displayBoard();
  312. }
  313.  
  314. //other cases
  315. turnOver = true;
  316. }
Add Comment
Please, Sign In to add comment