Guest User

Untitled

a guest
Oct 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToeGame
  4. {
  5.  
  6. private static final char PLAYERX = 'X'; // Helper constant for X player
  7.  
  8. private static final char PLAYERO = 'O'; // Helper constant for O player
  9.  
  10. private static final char SPACE = ' '; // Helper constant for spaces
  11.  
  12. private String playerX;
  13.  
  14. private String playerO;
  15.  
  16. public String getPlayerX() {
  17. return playerX;
  18. }
  19.  
  20. public String getPlayerO() {
  21. return playerO;
  22. }
  23.  
  24. /*
  25. TicTacToe Board
  26. 0 | 1 | 2
  27. -----------
  28. 3 | 4 | 5
  29. -----------
  30. 6 | 7 | 8
  31. */
  32.  
  33. private char[][] ar=new char[3][3];
  34.  
  35. public TicTacToeGame() //constructor
  36. {
  37. for(int i=0;i<3;i++)
  38. {
  39. for(int j=0;j<3;j++)
  40. {
  41. ar[i][j]=' ';
  42. }
  43. }
  44. }
  45.  
  46. public TicTacToeGame(String player1, String player2){
  47. this();
  48. this.playerX=player1;
  49. this.playerO=player2;
  50. }
  51.  
  52. public void printsamplebox() //prints sample box before the game starts
  53. {
  54. System.out.println(" Sample TicTacToe Board\n" +
  55. " 0 | 1 | 2\n" +
  56. " -----------\n" +
  57. " 3 | 4 | 5\n" +
  58. " -----------\n" +
  59. " 6 | 7 | 8");
  60. }
  61.  
  62. public static String getSampleBox() //prints sample box before the game starts
  63. {
  64. return String.format(" Sample TicTacToe Board\n" +
  65. " 0 | 1 | 2\n" +
  66. " -----------\n" +
  67. " 3 | 4 | 5\n" +
  68. " -----------\n" +
  69. " 6 | 7 | 8");
  70. }
  71.  
  72.  
  73.  
  74. public void printbox() //prints tictactoe box after every move
  75. {
  76. System.out.println(" TicTacToe Board\n" +
  77. " "+ar [0][0]+ " | " +ar[0][1]+ " | "+ ar[0][2]+"\n" +
  78.  
  79. " "+ ar[1][0] +" | "+ ar[1][1]+" | "+ar[1][2]+"\n" +
  80.  
  81. " "+ar[2][0] + " | "+ar[2][1] +" | "+ ar[2][2]+"");
  82. }
  83.  
  84. @Override
  85. public String toString() //prints tictactoe box after every move
  86. {
  87. return String.format(" TicTacToe Board\n" +
  88. " "+ar [0][0]+ " | " +ar[0][1]+ " | "+ ar[0][2]+"\n" +
  89.  
  90. " "+ ar[1][0] +" | "+ ar[1][1]+" | "+ar[1][2]+"\n" +
  91.  
  92. " "+ar[2][0] + " | "+ar[2][1] +" | "+ ar[2][2]+"");
  93. }
  94.  
  95.  
  96. /*
  97. checkwinner function
  98. returns 1 if "0" wins
  99. returns 2 if "X" wins
  100. returns 0 if draw-game
  101. */
  102. public boolean isOver(){
  103. if(checkwinner()!=0){
  104. return true;
  105. }else{
  106. for(int i=0;i<3;i++){
  107. for(int j=0;j<3;j++){
  108. if(ar[i][j]==' '){
  109. return false;
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. }
  116.  
  117. public int checkwinner()
  118. {
  119. for(int i=0;i<3;i++)
  120. {
  121. if(ar[i][0]=='0' && ar[i][1]=='0' && ar[i][2]=='0')
  122. {
  123. return 1;
  124. }
  125. if(ar[i][0]=='X' && ar[i][1]=='X' && ar[i][2]=='X')
  126. {
  127. return 2;
  128. }
  129. }
  130. for(int j=0;j<3;j++)
  131. {
  132. if(ar[0][j]=='0' && ar[1][j]=='0' && ar[2][j]=='0')
  133. {
  134. return 1;
  135. }
  136. if(ar[0][j]=='X' && ar[1][j]=='X' && ar[2][j]=='X')
  137. {
  138. return 2;
  139. }
  140.  
  141. }
  142.  
  143. if(ar[0][0]=='0' && ar[1][1]=='0' && ar[2][2]=='0')
  144. {
  145. return 1;
  146. }
  147. if(ar[0][0]=='X' && ar[1][1]=='X' && ar[2][2]=='X')
  148. {
  149. return 2;
  150. }
  151.  
  152. if(ar[0][2]=='0' && ar[1][1]=='0' && ar[2][0]=='0')
  153. {
  154. return 1;
  155. }
  156. if(ar[0][2]=='X' && ar[1][1]=='X' && ar[2][0]=='X')
  157. {
  158. return 2;
  159. }
  160.  
  161. return 0;
  162.  
  163. }
  164.  
  165.  
  166. public int cou=1;
  167. /* This is the counter variable which check whose turn it is .
  168. If cou is even it '0' turn
  169. Else it is 'x' turn
  170. So when you implement socket in this program and when a player takes 2 turns consequently then
  171. you just need to do counter minus 1 or you can make a if statement in controller() method that doesn't let to use place() method if a player takes 2 turns
  172. */
  173.  
  174. public int getCou() {
  175. return cou;
  176. }
  177.  
  178. public char getSpace(int space){
  179. return ar[space/3][space%3];
  180. }
  181.  
  182. public boolean isTurn(String player){
  183. if(getCou()%2==1){
  184. return this.playerX.equalsIgnoreCase(player);
  185. }else{
  186. return this.playerO.equalsIgnoreCase(player);
  187. }
  188. }
  189.  
  190. public int place(int a, String player){
  191. if(!isTurn(player)){
  192. return -1;
  193. }else if(ar[a/3][a%3]!=' '){
  194. return -2;
  195. }else{
  196. place(a);
  197. return 0;
  198. }
  199. }
  200.  
  201. public void place(int a) // this function place the X or 0 to the place where player tells i.e "int a"
  202.  
  203. {
  204.  
  205. if(cou%2==0)
  206. {
  207. if (a == 0) {
  208. ar[0][0] = '0';
  209. } else if (a == 1) {
  210. ar[0][1] = '0';
  211. } else if (a == 2) {
  212. ar[0][2] = '0';
  213. } else if (a == 3) {
  214. ar[1][0] = '0';
  215. } else if (a == 4) {
  216. ar[1][1] = '0';
  217.  
  218. } else if (a == 5) {
  219. ar[1][2] = '0';
  220.  
  221. } else if (a == 6) {
  222. ar[2][0] = '0';
  223.  
  224. } else if (a == 7) {
  225. ar[2][1] = '0';
  226. } else if (a == 8) {
  227. ar[2][2] = '0';
  228. }
  229.  
  230. }
  231.  
  232. else
  233. { if (a == 0) {
  234. ar[0][0] = 'X';
  235. } else if (a == 1) {
  236. ar[0][1] = 'X';
  237. } else if (a == 2) {
  238. ar[0][2] = 'X';
  239. } else if (a == 3) {
  240. ar[1][0] = 'X';
  241. } else if (a == 4) {
  242. ar[1][1] = 'X';
  243.  
  244. } else if (a == 5) {
  245. ar[1][2] = 'X';
  246.  
  247. } else if (a == 6) {
  248. ar[2][0] = 'X';
  249.  
  250. } else if (a == 7) {
  251. ar[2][1] = 'X';
  252. } else if (a == 8) {
  253. ar[2][2] = 'X';
  254. }
  255. }
  256. cou++;
  257. }
  258.  
  259. public void gamecontroller() // this method basically calls all other method to make a game as whole working
  260. {
  261. TicTacToeGame o=new TicTacToeGame();
  262. o.printsamplebox();
  263. int pos;
  264. Scanner s= new Scanner(System.in);
  265.  
  266. int[] p=new int[9];
  267. for(int h=0;h<p.length;h++)
  268. {p[h]=-1;}
  269.  
  270. for(int i=0;i<9;i++)
  271. {
  272.  
  273. System.out.println("Enter place where you want to place your symbol");
  274. pos=s.nextInt();
  275. boolean shouldIplace=true;
  276. for(int c=0;c<p.length;c++)
  277. {
  278. if(p[c]==-1 || p[c]<0 ||pos>8)
  279. {
  280. shouldIplace=true;
  281. p[c]=pos;
  282. break;
  283. }
  284. else if(p[c]==pos)
  285. {
  286. System.out.println("This place is already been used. Enter a valid other place");
  287. i--;
  288. shouldIplace=false;
  289. break;
  290. }
  291.  
  292.  
  293. }
  294. if (shouldIplace)
  295. {
  296. if(pos >=0 && pos <9)
  297. {
  298. o.place(pos);
  299. }
  300.  
  301. else
  302. {
  303. System.out.println("Please enter a valid space");
  304. --i;
  305. }
  306.  
  307. o.printbox();
  308.  
  309. int a=o.checkwinner();
  310. if (a==1)
  311. {
  312. System.out.println("'0' player wins");break;
  313. }
  314. if (a==2)
  315. {
  316. System.out.println("'X' player wins`");
  317. }
  318. if(i==8 && a==0)
  319. {
  320. System.out.println("Draw game");
  321. }
  322. }
  323. }
  324.  
  325. }
  326.  
  327.  
  328. /*
  329. you can apply all socket functions in the main() method or create other methods of your own and you can change p1 and p2 below to sockets
  330.  
  331. */
  332.  
  333. public static void main(String[] args)
  334.  
  335. {
  336. TicTacToeGame c=new TicTacToeGame();
  337.  
  338.  
  339. Scanner s=new Scanner(System.in);
  340. String p1,p2;
  341. System.out.println("Enter Player 1's name: ");
  342. p1=s.nextLine();
  343. System.out.println("Enter Player 2's name: ");
  344. p2=s.nextLine();
  345.  
  346. c.gamecontroller(); // calls game controller method
  347.  
  348.  
  349. }
  350.  
  351. }
Add Comment
Please, Sign In to add comment