Guest User

Untitled

a guest
Apr 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class TicTacToe {
  4.  
  5. public static void main(String[] args) {
  6. int[][]board= new int [3][3];
  7. int turn= 1;
  8. while(full(board)== false && winner(board)==0)
  9. {
  10. printBoard(board);
  11. System.out.println("Human, play!");
  12. humanPlay(board,turn);
  13. if(turn ==1)
  14. {
  15. turn=2;
  16. }
  17. else
  18. {
  19. turn=1;
  20. }
  21. int winner = winner(board);
  22. //printBoard(board);
  23. if(winner==1)
  24. {
  25. System.out.println("Congrats X! You won!");
  26. }
  27. else
  28. if(winner==2)
  29. {
  30. System.out.println("Congrats O! You won!");
  31. }
  32. //else
  33. //{
  34. // System.out.println("It's a draw!");
  35. //}
  36. }
  37. if (winner(board) == 0)
  38. {
  39. System.out.println("It's a draw!");
  40. }
  41. }
  42. public static boolean full(int[][] board) {
  43. for(int row = 0; row < board.length; row++)
  44. {
  45. for(int col = 0;col < board[0].length; col++)
  46. {
  47. if(board[row][col]==0)
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. public static void printBoard(int[][] board)
  54. {
  55. // String hbar = "|";
  56. // String vbar = "----";
  57. //
  58. // for (int row=0; row < board.length; row++)
  59. // {
  60. // for (int col=0; col < board[0].length; col++)
  61. // {
  62. // if (col > 0)
  63. // {
  64. // System.out.print(hbar);
  65. // }
  66. // if (board[row][col] == 1)
  67. // {
  68. // System.out.print("X");
  69. // }
  70. // else if (board[row][col] == 2)
  71. // {
  72. // System.out.print("O");
  73. // }
  74. // else
  75. // {
  76. // System.out.print(" ");
  77. // }
  78. // if (col == 2)
  79. // {
  80. // System.out.println("");
  81. // }
  82. // if (row<2)
  83. // {
  84. // System.out.print(hbar);
  85. // }
  86. // }
  87. // }
  88. for (int row=0; row < board.length; row++)
  89. {
  90. //if (row == 2)
  91. //{
  92. if (board [row][0]== 0)
  93. {
  94. System.out.print(" ");
  95. }
  96. else
  97. if (board [row][0]== 1)
  98. {
  99. System.out.print(" X ");
  100. }
  101. else
  102. if (board [row][0]== 2)
  103. {
  104. System.out.print(" O ");
  105. }
  106. if (board [row][1] ==0)
  107. {
  108. System.out.print("| |");
  109. }
  110. else
  111. if (board [row][1] ==1)
  112. {
  113. System.out.print("| X |");
  114. }
  115. else
  116. if (board [row][1] ==2)
  117. {
  118. System.out.print("| O |");
  119. }
  120. if (board [row][2] == 0)
  121. {
  122. System.out.print(" ");
  123. }
  124. else
  125. if (board [row][2] == 1)
  126. {
  127. System.out.print(" X ");
  128. }
  129. else
  130. if (board [row][2] == 2)
  131. {
  132. System.out.print(" O ");
  133. }
  134. //}
  135. System.out.println("");
  136. System.out.println("-----------");
  137. //{
  138. // if (board [row][0] == 0)
  139. // {
  140. // System.out.print(" |");
  141. // System.out.println("----");
  142. // }
  143. // else
  144. // if (board [row][0] == 1)
  145. // {
  146. // System.out.println(" X |");
  147. // System.out.println("----");
  148. // }
  149. // else
  150. // if (board [row][0] == 2)
  151. // {
  152. // System.out.println(" O |");
  153. // System.out.println("----");
  154. // }
  155. // if (board [row][1] == 0)
  156. // {
  157. // System.out.println("| |");
  158. // System.out.println("----");
  159. // }
  160. // else
  161. // if (board [row][1] == 1)
  162. // {
  163. // System.out.println("| X |");
  164. // System.out.println("----");
  165. // }
  166. // else
  167. // if (board [row][1] == 2)
  168. // {
  169. // System.out.println("| O |");
  170. // System.out.println("----");
  171. // }
  172. // if (board [row][2] == 0)
  173. // {
  174. // System.out.println("| ");
  175. // System.out.println("----");
  176. // }
  177. // else
  178. // if (board [row][2] == 1)
  179. // {
  180. // System.out.println("| X ");
  181. // System.out.println("----");
  182. // }
  183. // else
  184. // if (board [row][2] == 2)
  185. // {
  186. // System.out.println("| O ");
  187. // System.out.println("----");
  188. // }
  189. //}
  190. }
  191. }
  192. public static int winner(int[][] board) {
  193. int[]players = new int[2];
  194. int winner = 0; // if winner remains 0 then is a draw
  195. players[0] = 1; // add player one to the array of player to check for winners
  196. players[1] = 2; // add player two to the array of players to check for winners
  197. // check for the winner or draw
  198. for (int p=0; p < players.length; p++)
  199. {
  200. int player = players[p];
  201. if (board[0][0] == player && board[0][1] == player && board[0][2] == player){
  202. winner = player;
  203. }
  204. if (board[1][0] == player && board[1][1] == player && board[1][2] == player){
  205. winner = player;
  206. }
  207. if (board[2][0] == player && board[2][1] == player && board[2][2] == player){
  208. winner = player;
  209. }
  210.  
  211. if (board[0][0] == player && board[1][0] == player && board[2][0] == player){
  212. winner = player;
  213. }
  214.  
  215. if (board[0][1] == player && board[1][1] == player && board[2][1] == player){
  216. winner = player;
  217. }
  218.  
  219. if (board[0][2] == player && board[1][2] == player && board[2][2] == player){
  220. winner = player;
  221. }
  222. //diagonal wins
  223. if (board[0][0] == player && board[1][1] == player && board[2][2] == player){
  224. winner = player;
  225. }
  226. if (board[2][0] == player && board[1][1] == player && board[0][2] == player){
  227. winner = player;
  228. }
  229. }
  230. return winner;
  231. }
  232.  
  233. //for(int row = 0; row < board.length; row++)
  234. //{
  235. // for(int col = 0;col < board[0].length; col++)
  236. // {
  237. // if(board [row][col] == 1)
  238. // {
  239. // row++;
  240. // if(board [row][col]== 1)
  241. // row++;
  242. // {
  243. // if(board [row][col]== 1)
  244. // System.out.println("X is the winner! ");
  245. // return 1;
  246. // }
  247. // }
  248. // else
  249. // if(board [row][col] == 1)
  250. // {
  251. // col++;
  252. // if(board [row][col]== 1)
  253. // col++;
  254. // {
  255. // if(board [row][col]== 1)
  256. // System.out.println("X is the winner! ");
  257. // return 1;
  258. // }
  259. // }
  260. // else
  261. // if(board [row][col] == 2)
  262. // {
  263. // row++;
  264. // if(board [row][col]== 2)
  265. // row++;
  266. // {
  267. // if(board [row][col]== 2)
  268. // System.out.println("O is the winner! ");
  269. // return 2;
  270. // }
  271. // }
  272. // else
  273. // if(board [row][col] == 2)
  274. // {
  275. // col++;
  276. // if(board [row][col]== 2)
  277. // col++;
  278. // {
  279. // if(board [row][col]== 2)
  280. // System.out.println("O is the winner! ");
  281. // return 2;
  282. // }
  283. // }
  284. // else
  285. // if(board [1][1]== 1)
  286. // {
  287. // if(board [0][0]==1)
  288. // {
  289. // if(board [2][2]==1)
  290. // System.out.println("X is the winner!!");
  291. // return 1;
  292. // }
  293. // else
  294. // if(board [0][2]== 1)
  295. // {
  296. // if(board [2][0]== 1)
  297. // System.out.println("X is the winner!!");
  298. // return 1;
  299. // }
  300. // }
  301. // else
  302. // if(board [1][1]== 2)
  303. // {
  304. // if(board [0][0]==2)
  305. // {
  306. // if(board [2][2]==2)
  307. // System.out.println("O is the winner!!");
  308. // return 2;
  309. // }
  310. // else
  311. // if(board [0][2]== 2)
  312. // {
  313. // if(board [2][0]== 2)
  314. // System.out.println("O is the winner!!");
  315. // return 2;
  316. // }
  317. // }
  318. // }
  319. //}
  320. //return 0;
  321. //}
  322.  
  323. public static void humanPlay(int[][] board, int playNum) {
  324.  
  325. Scanner sc = new Scanner(System.in);
  326. int row = -1;
  327. int col = -1;
  328. while(row >= board.length || row < 0 || col >= board[0].length || col < 0 || board[row][col] != 0)
  329. //while(row < board.length && col < board[0].length && board[row][col]==0)
  330. {
  331. if(playNum == 1)
  332. System.out.print("Player X, select a row: ");
  333. else
  334. System.out.print("Player O, select a row: ");
  335. row = sc.nextInt();
  336. if(playNum == 1)
  337. System.out.print("Player X, select a column: ");
  338. else
  339. System.out.print("Player O, select a column: ");
  340. col = sc.nextInt();
  341. }
  342. System.out.println("returninig");
  343. board[row][col] = playNum;
  344. }
  345. //bonus
  346. public static void randomPlay(int[][] board, int playNum) {
  347. }
  348.  
  349. //bonus
  350. public static void intelligentPlay(int[][] board, int playNum) {
  351. }
  352.  
  353. }
Add Comment
Please, Sign In to add comment