Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<ctype.h>
  3. using namespace std;
  4. struct Move
  5. {
  6. int row, col;
  7. };
  8. char player = 'O', opponent = 'X';
  9. char board[5][5];
  10. int turn=0;
  11. void print()
  12. {
  13. for(int i=1;i<=3;i++)
  14. {
  15. cout<<"\t\t\t\t";
  16.  
  17. for(int j=1;j<=3;j++)
  18. {
  19. if(j==1)
  20. cout<<"| ";
  21. cout<<board[i][j]<<" | ";
  22. }
  23.  
  24. cout<<"\n\n";
  25. }
  26. }
  27. int isover(char check)
  28. {
  29. if(board[1][1]==check&&board[1][2]==check&&board[1][3]==check)
  30. return 1;
  31. else if(board[1][1]==check&&board[2][1]==check&&board[3][1]==check)
  32. return 1;
  33. else if(board[3][1]==check&&board[3][2]==check&&board[3][3]==check)
  34. return 1;
  35. else if(board[1][3]==check&&board[2][3]==check&&board[3][3]==check)
  36. return 1;
  37. else if(board[1][2]==check&&board[2][2]==check&&board[3][2]==check)
  38. return 1;
  39. else if(board[2][1]==check&&board[2][2]==check&&board[2][3]==check)
  40. return 1;
  41. else if(board[1][1]==check&&board[2][2]==check&&board[3][3]==check)
  42. return 1;
  43. else if(board[1][3]==check&&board[2][2]==check&&board[3][1]==check)
  44. return 1;
  45. return 0;
  46. }
  47. int isgameover()
  48. {
  49. char check;
  50. if(turn==0)
  51. check='X';
  52. else
  53. check='O';
  54. if(isover(check))
  55. return 1;
  56. int c=0;
  57. for(int i=1;i<=3;i++)
  58. {
  59. for(int j=1;j<=3;j++)
  60. {
  61. if(board[i][j]=='-')
  62. c++;
  63. }
  64. }
  65. if(c==0)
  66. return -1;
  67. else
  68. return 0;
  69. }
  70. bool isMovesLeft(char board[5][5])
  71. {
  72. for (int i = 1; i<=3; i++)
  73. for (int j = 1; j<=3; j++)
  74. if (board[i][j]=='-')
  75. return true;
  76. return false;
  77. }
  78.  
  79. int evaluate(char b[5][5])
  80. {
  81. for (int row = 1; row<=3; row++)
  82. {
  83. if (b[row][1]==b[row][2] &&
  84. b[row][2]==b[row][3])
  85. {
  86. if (b[row][1]==player)
  87. return +10;
  88. else if (b[row][1]==opponent)
  89. return -10;
  90. }
  91. }
  92. for (int col = 1; col<=3; col++)
  93. {
  94. if (b[1][col]==b[2][col] &&
  95. b[2][col]==b[3][col])
  96. {
  97. if (b[1][col]==player)
  98. return +10;
  99.  
  100. else if (b[1][col]==opponent)
  101. return -10;
  102. }
  103. }
  104. if (b[1][1]==b[2][2] && b[3][3]==b[2][2])
  105. {
  106. if (b[1][1]==player)
  107. return +10;
  108. else if (b[1][1]==opponent)
  109. return -10;
  110. }
  111. if (b[1][3]==b[2][2] && b[2][2]==b[3][1])
  112. {
  113. if (b[1][3]==player)
  114. return +10;
  115. else if (b[1][3]==opponent)
  116. return -10;
  117. }
  118. return 0;
  119. }
  120. int minimax(char board[5][5], int depth, bool isMax)
  121. {
  122. int score = evaluate(board);
  123. if (score == 10)
  124. return score;
  125. if (score == -10)
  126. return score;
  127. if (isMovesLeft(board)==false)
  128. return 0;
  129. if (isMax)
  130. {
  131. int best = -1000;
  132. for (int i = 1; i<=3; i++)
  133. {
  134. for (int j = 1; j<=3; j++)
  135. {
  136. if (board[i][j]=='-')
  137. {
  138. board[i][j] = player;
  139. best = max( best,
  140. minimax(board, depth+1, !isMax) );
  141. board[i][j] = '-';
  142. }
  143. }
  144. }
  145. return best;
  146. }
  147. else
  148. {
  149. int best = 1000;
  150. for (int i = 1; i<=3; i++)
  151. {
  152. for (int j = 1; j<=3; j++)
  153. {
  154. if (board[i][j]=='-')
  155. {
  156. board[i][j] = opponent;
  157. best = min(best,
  158. minimax(board, depth+1, !isMax));
  159. board[i][j] = '-';
  160. }
  161. }
  162. }
  163. return best;
  164. }
  165. }
  166. Move findBestMove(char board[5][5])
  167. {
  168. int bestVal = -1000;
  169. Move bestMove;
  170. bestMove.row = 0;
  171. bestMove.col = 0;
  172. for (int i = 1; i<=3; i++)
  173. {
  174. for (int j = 1; j<=3; j++)
  175. {
  176. if (board[i][j]=='-')
  177. {
  178. board[i][j] = player;
  179. int moveVal = minimax(board, 1, false);
  180. board[i][j] = '-';
  181. if (moveVal > bestVal)
  182. {
  183. bestMove.row = i;
  184. bestMove.col = j;
  185. bestVal = moveVal;
  186. }
  187. }
  188. }
  189. }
  190. return bestMove;
  191. }
  192. int main()
  193. {
  194. cout<<"1:play game 2:instructions 3:exit"<<endl;
  195. int ch;
  196. while(cin>>ch)
  197. {
  198. system("cls");
  199. if(ch==3)
  200. break;
  201. else if(ch==2)
  202. {
  203. cout<<"The object of Tic Tac Toe is to get three in a row. You play on a three by three game board."<<
  204. "The first player is known as X and the second is O. Players alternate placing Xs and Os on the game board"<<
  205. "until either oppent has three in a row or all nine squares are filled. X always goes first,"<<
  206. "and in the event that no one has three in a row, the stalemate is called a cat game.\n\n\n";
  207. }
  208. else
  209. {
  210. cout<<"1: one player 2: two player"<<endl;
  211. int ch2;
  212. cin>>ch2;
  213. system("cls");
  214. if(ch2==1)
  215. {
  216. cout<<"\t\t\t TIC TAC TOE by Bhaskar Nihaal\n\n\n";
  217. for(int i=1;i<=3;i++)
  218. for(int j=1;j<=3;j++)
  219. board[i][j]='-';
  220. print();
  221. cout<<"\n\n Enter the location in the form of x and y (ex: 1,1)\n";
  222. int x,y;
  223. while(1)
  224. {
  225. if(turn==0)
  226. {
  227. cin>>x>>y;
  228. if(board[x][y]!='-'||x<1||x>3||y<1||y>3)
  229. {
  230. cout<<"Enter valid move"<<endl;continue;
  231. }
  232. board[x][y]='X';
  233. }
  234. else
  235. {
  236. Move bestMove = findBestMove(board);
  237. board[bestMove.row][bestMove.col]='O';
  238. }
  239. system("cls");
  240. cout<<"\t\t\t TIC TAC TOE by Bhaskar Nihaal\n\n\n";
  241. print();
  242. int result=isgameover();
  243. turn=1-turn;
  244. if(result==1)
  245. {
  246. if(turn==1)
  247. cout<<"Congratulations You Won"<<endl;
  248. else
  249. cout<<"You Lose Better Luck Next Time"<<endl;
  250. break;
  251. }
  252. else if(result==-1)
  253. {
  254. cout<<"Draw"<<endl;break;
  255. }
  256. cout<<"\n\n Enter the location in the form of x and y (ex: 1,1)\n";
  257. }
  258. }
  259. else
  260. {
  261. cout<<"\t\t\t TIC TAC TOE by Bhaskar Nihaal\n\n\n";
  262. for(int i=1;i<=3;i++)
  263. for(int j=1;j<=3;j++)
  264. board[i][j]='-';
  265. print();
  266. cout<<"\n\n Enter the location in the form of x and y (ex: 1,1)\n";
  267. int x,y;
  268. while(1)
  269. {
  270. cin>>x>>y;
  271. if(board[x][y]!='-'||x<1||x>3||y<1||y>3)
  272. {
  273. cout<<"Enter valid move"<<endl;continue;
  274. }
  275. if(turn==0)
  276. board[x][y]='X';
  277. else
  278. board[x][y]='O';
  279. system("cls");
  280. cout<<"\t\t\t TIC TAC TOE by Bhaskar Nihaal\n\n\n";
  281. print();
  282. int result=isgameover();
  283. turn=1-turn;
  284. if(result==1)
  285. {
  286. if(turn==1)
  287. cout<<"Congratulations Player 1 wins"<<endl;
  288. else
  289. cout<<"Congratulations Player 2 wins"<<endl;
  290. break;
  291. }
  292. else if(result==-1)
  293. {
  294. cout<<"Draw"<<endl;break;
  295. }
  296. cout<<"\n\n Enter the location in the form of x and y (ex: 1,1)\n";
  297. }
  298.  
  299. }
  300. //system("cls");
  301. }
  302. cout<<"1:play game 2:instructions 3:exit"<<endl;
  303. turn=0;
  304. }
  305. return 0;
  306. }
Add Comment
Please, Sign In to add comment