Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct gomoku {
  8. char board[19][19];
  9. char player1;
  10. char cpu;
  11. };
  12.  
  13.  
  14. char check_win(char board[19][19]) {
  15. char lop;
  16. for (int r = 0; r < 14; r++) {
  17. for (int c = 0; c < 14; c++) {
  18. if (board[r][c] != '.' ) {
  19. lop = board[r][c];
  20.  
  21. if (board[r+1][c] == lop){
  22. if (board[r+2][c] == lop){
  23. if (board[r+3][c] == lop){
  24. if (board[r+4][c] == lop){
  25. if (board[r+5][c] == lop){
  26. return board[r][c];
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33. if (board[r][c+1] == lop){
  34. if (board[r][c+1] == lop){
  35. if (board[r][c+1] == lop){
  36. if (board[r][c+1] == lop){
  37. if (board[r][c+1] == lop){
  38. return board[r][c];
  39. }
  40. }
  41. }
  42. }
  43. }
  44.  
  45. if (board[r+1][c+1] == lop){
  46. if (board[r+1][c+1] == lop){
  47. if (board[r+1][c+1] == lop){
  48. if (board[r+1][c+1] == lop){
  49. if (board[r+1][c+1] == lop){
  50. return board[r][c];
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57. }
  58. }
  59. }
  60.  
  61. return 0;
  62. }
  63.  
  64.  
  65. int negamax(char board[19][19], char player1, char player2,int negamaxno) {
  66.  
  67. int best_move_score = -9999;
  68. int score_for_this_move = 0;
  69. if (negamaxno>100){
  70. return 0;
  71. }
  72. //If player 1 wins, then the score is high (good for player1)
  73. if (check_win(board) == player1)
  74. return 1000;
  75.  
  76. //If player 2 loses, then the score is low (bad for player1)
  77. else if (check_win(board) == player2)
  78. return -1000;
  79.  
  80. for (int r = 0; r < 19; r++) {
  81. for (int c = 0; c < 19; c++) {
  82. if (board[r][c] == '.') {
  83. board[r][c] = player1; //Try test move.
  84. score_for_this_move = -(negamax(board, player2, player1,negamaxno+1));
  85. board[r][c] = '.'; //Put back test move.
  86. if (score_for_this_move >= best_move_score) {
  87. best_move_score = score_for_this_move;
  88. }
  89. }
  90. }
  91. }
  92.  
  93. if (best_move_score == -9999 || best_move_score == 0)
  94. return 0;
  95.  
  96. else if (best_move_score < 0)
  97. return best_move_score + 1;
  98.  
  99. else if (best_move_score > 0)
  100. return best_move_score - 1; //As the game goes longer, and the recursion goes deeper, the moves near the end are less favorable than in the beginning.
  101.  
  102. }
  103.  
  104.  
  105. int main() {
  106. gomoku game;
  107. for (int i = 0; i < 19; i++) {
  108. for (int j = 0; j < 19; j++) {
  109. game.board[i][j] = '.';
  110. }
  111. }
  112. cout << "Hello, I am the computer you will be playing." << endl;
  113. cout << "If you chose x's, I will be o's. If you chose o's, I will be x's." << endl;
  114. cout << "If you chose neither x or o, I will default to x." << endl << endl;
  115. while (1) {
  116. string choice;
  117. cout << "Player 1" << ": What would you like your character to be? ";
  118. cin >> choice;
  119. if (choice.size() > 1) {
  120. cout << "You inputted more than one character. Please try again." << endl;
  121. continue;
  122. }
  123. cout << endl;
  124. game.player1 = choice[0];
  125. break;
  126. }
  127.  
  128. if (game.player1 == 'x' || game.player1 == 'X')
  129. game.cpu = 'o';
  130.  
  131. else
  132. game.cpu = 'x';
  133.  
  134.  
  135. for (int i = 0; i < 19; i++) {
  136. cout << endl;
  137. for (int j = 0; j < 19; j++) {
  138. cout << " ";
  139. cout << game.board[i][j];
  140. }
  141. }
  142. cout << endl;
  143.  
  144. int moves = 0;
  145.  
  146.  
  147. while (moves < 361) {
  148.  
  149. while (1) {
  150. string string_row, string_col;
  151. int row = 0, col = 0;
  152. while (1) {
  153. cout << "Where would you like to play? " << endl << "Enter the row: ";
  154. cin >> string_row;
  155. row = atoi(string_row.c_str());
  156.  
  157. if (row >= 1 && row <= 19)
  158. break;
  159. cout << "You need to enter a row on the board (between 1 and 19, inclusive)." << endl;
  160. }
  161.  
  162. while (1) {
  163. cout << "Enter the column: ";
  164. cin >> string_col;
  165. col = atoi(string_col.c_str());
  166.  
  167. if (col >= 1 && col <= 19)
  168. break;
  169. cout << "You need to enter a column on the board (between 1 and 19, inclusive)." << endl;
  170. }
  171. if (game.board[row-1][col-1] == '.') {
  172. game.board[row-1][col-1] = game.player1;
  173. break;
  174. }
  175. else
  176. cout << "Someone already played there." << endl << endl;
  177. }
  178.  
  179. moves++;
  180. for (int i = 0; i < 19; i++) {
  181. cout << endl;
  182. for (int j = 0; j < 19; j++) {
  183. cout << " ";
  184. cout << game.board[i][j];
  185. }
  186. }
  187. cout << endl << endl;
  188. if (moves == 361)
  189. break;
  190. int best_move_score = -9999;
  191. int best_move_row = -9999;
  192. int best_move_col = -9999;
  193. int score_for_this_move = 0;
  194. for (int r = 0; r < 361; r++) {
  195. for (int c = 0; c < 361; c++) {
  196. if (game.board[r][c] == '.') {
  197. game.board[r][c] = game.player1; //Try test move.
  198. int negamaxno=0;
  199. score_for_this_move = -(negamax(game.board, game.cpu, game.player1,negamaxno));
  200. game.board[r][c] = '.'; //Put back test move.
  201. if (score_for_this_move >= best_move_score) {
  202. best_move_score = score_for_this_move;
  203. best_move_row = r;
  204. best_move_col = c;
  205. }
  206. }
  207. }
  208. }
  209. game.board[best_move_row][best_move_col] = game.cpu;
  210. if (check_win(game.board)) {
  211. cout << game.cpu << " won!" <<endl;
  212. exit(1);
  213. }
  214. moves++;
  215. for (int i = 0; i < 361; i++) {
  216. cout << endl;
  217. for (int j = 0; j < 361; j++) {
  218. cout << " ";
  219. cout << game.board[i][j];
  220. }
  221. }
  222. cout << endl;
  223. }
  224. cout << "Cat's game!" << endl;
  225.  
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement