Guest User

Untitled

a guest
Aug 12th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 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 realcheck_win(char board[19][19]) {
  15. char lop;
  16. for (int r = 0; r < 15; r++) {
  17. for (int c = 0; c < 15; c++) {
  18. if (board[r][c] != '.' ) {
  19. lop = board[r][c];
  20. cout<<lop<<endl;
  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 1;
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33. if (board[r][c+1] == lop){
  34. if (board[r][c+2] == lop){
  35. if (board[r][c+3] == lop){
  36. if (board[r][c+4] == lop){
  37. if (board[r][c+5] == lop){
  38. cout<<"mooooooo";
  39. return 1;
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. if (board[r+1][c+1] == lop){
  47. if (board[r+2][c+2] == lop){
  48. if (board[r+3][c+3] == lop){
  49. if (board[r+4][c+4] == lop){
  50. if (board[r+5][c+5] == lop){
  51. return 1;
  52. }
  53. }
  54. }
  55. }
  56. }
  57.  
  58. }
  59. }
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. char check_win(char board[19][19]) {
  66. char lop;
  67. for (int r = 0; r < 17; r++) {
  68. for (int c = 0; c < 17; c++) {
  69. if (board[r][c] != '.' ) {
  70. lop = board[r][c];
  71.  
  72. if (board[r+1][c] == lop){
  73. if (board[r+2][c] == lop){
  74. if (board[r+3][c] == lop){
  75. return board[r][c];
  76. }
  77. }
  78. }
  79.  
  80. if (board[r][c+1] == lop){
  81. if (board[r][c+2] == lop){
  82. if (board[r][c+3] == lop){
  83. return board[r][c];
  84.  
  85. }
  86. }
  87. }
  88.  
  89. if (board[r+1][c+1] == lop){
  90. if (board[r+2][c+2] == lop){
  91. if (board[r+3][c+3] == lop){
  92. return board[r][c];
  93. }
  94. }
  95. }
  96.  
  97. }
  98. }
  99. }
  100.  
  101. return 0;
  102. }
  103.  
  104.  
  105. int negamax(char board[19][19], char player2, char player1,int negamaxno) {
  106.  
  107. int best_move_score = -9999;
  108. int score_for_this_move = 0;
  109.  
  110.  
  111.  
  112.  
  113.  
  114. negamaxno++;
  115. if (negamaxno>100){
  116. negamaxno--;
  117. return 0;
  118. }
  119. //If player 1 wins, then the score is high (good for player1)
  120. if (check_win(board) == player1){
  121. return 600;
  122. }
  123.  
  124. //If player 2 loses, then the score is low (bad for player1)
  125. else if (check_win(board) == player2)
  126. return -600;
  127.  
  128. for (int r = 0; r < 19; r++) {
  129. for (int c = 0; c < 19; c++) {
  130. if (board[r][c] == player2 || board[r][c] == player1 ) {
  131.  
  132. if (board[r+1][c] == '.' ){
  133. board[r+1][c] = player1; //Try test move.
  134. int negamaxno=0;
  135. score_for_this_move = -(negamax(board, player1, player2,negamaxno));
  136. board[r+1][c] = '.'; //Put back test move.
  137. if (score_for_this_move >= best_move_score) {
  138. best_move_score = score_for_this_move;
  139. }
  140.  
  141. }
  142.  
  143. if (board[r-1][c] == '.' ){
  144. board[r-1][c] = player1; //Try test move.
  145. int negamaxno=0;
  146. score_for_this_move = -(negamax(board, player1, player2,negamaxno));
  147. board[r-1][c] = '.'; //Put back test move.
  148. if (score_for_this_move >= best_move_score) {
  149. best_move_score = score_for_this_move;
  150. }
  151.  
  152. }
  153.  
  154. if (board[r][c+1] == '.' ){
  155. board[r][c+1] = player1; //Try test move.
  156. int negamaxno=0;
  157. score_for_this_move = -(negamax(board, player1, player2,negamaxno));
  158. board[r][c+1] = '.'; //Put back test move.
  159. if (score_for_this_move >= best_move_score) {
  160. best_move_score = score_for_this_move;
  161. }
  162.  
  163. }
  164.  
  165. if (board[r][c-1] == '.' ){
  166. board[r][c-1] = player1; //Try test move.
  167. int negamaxno=0;
  168. score_for_this_move = -(negamax(board, player1, player2,negamaxno));
  169. board[r][c] = '.'; //Put back test move.
  170. if (score_for_this_move >= best_move_score) {
  171. best_move_score = score_for_this_move;
  172. }
  173.  
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. }
  183. }
  184. }
  185.  
  186. if (best_move_score == -9999 || best_move_score == 0)
  187. return 0;
  188.  
  189. else if (best_move_score < 0)
  190. return best_move_score + 1;
  191.  
  192. else if (best_move_score > 0)
  193. 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.
  194.  
  195. }
  196.  
  197.  
  198. int main() {
  199. gomoku game;
  200. for (int i = 0; i < 19; i++) {
  201. for (int j = 0; j < 19; j++) {
  202. game.board[i][j] = '.';
  203. }
  204. }
  205. cout << "Hello, I am the computer you will be playing." << endl;
  206. cout << "If you chose x's, I will be o's. If you chose o's, I will be x's." << endl;
  207. cout << "If you chose neither x or o, I will default to x." << endl << endl;
  208. while (1) {
  209. string choice;
  210. cout << "Player 1" << ": What would you like your character to be? ";
  211. cin >> choice;
  212. if (choice.size() > 1) {
  213. cout << "You inputted more than one character. Please try again." << endl;
  214. continue;
  215. }
  216. cout << endl;
  217. game.player1 = choice[0];
  218. break;
  219. }
  220.  
  221. if (game.player1 == 'x' || game.player1 == 'X')
  222. game.cpu = 'o';
  223.  
  224. else
  225. game.cpu = 'x';
  226.  
  227.  
  228. for (int i = 0; i < 19; i++) {
  229. cout << endl;
  230. for (int j = 0; j < 19; j++) {
  231. cout << " ";
  232. cout << game.board[i][j];
  233. }
  234. }
  235. cout << endl;
  236.  
  237. int moves = 0;
  238.  
  239.  
  240. while (moves < 361) {
  241.  
  242. while (1) {
  243. string string_row, string_col;
  244. int row = 0, col = 0;
  245. while (1) {
  246. cout << "Where would you like to play? " << endl << "Enter the row: ";
  247. cin >> string_row;
  248. row = atoi(string_row.c_str());
  249.  
  250. if (row >= 1 && row <= 19)
  251. break;
  252. cout << "You need to enter a row on the board (between 1 and 19, inclusive)." << endl;
  253. }
  254.  
  255. while (1) {
  256. cout << "Enter the column: ";
  257. cin >> string_col;
  258. col = atoi(string_col.c_str());
  259.  
  260. if (col >= 1 && col <= 19)
  261. break;
  262. cout << "You need to enter a column on the board (between 1 and 19, inclusive)." << endl;
  263. }
  264. if (game.board[row-1][col-1] == '.') {
  265. game.board[row-1][col-1] = game.player1;
  266. break;
  267. }
  268. else
  269. cout << "Someone already played there." << endl << endl;
  270. }
  271.  
  272. moves++;
  273. for (int i = 0; i < 19; i++) {
  274. cout << endl;
  275. for (int j = 0; j < 19; j++) {
  276. cout << " ";
  277. cout << game.board[i][j];
  278. }
  279. }
  280. cout << endl << endl;
  281. if (moves == 361)
  282. break;
  283. int best_move_score = -9999;
  284. int best_move_row = -9999;
  285. int best_move_col = -9999;
  286. int score_for_this_move = 0;
  287. for (int r = 0; r < 19; r++) {
  288. for (int c = 0; c < 19; c++) {
  289. if (game.board[r][c] == game.player1 || game.board[r][c] == game.cpu ) {
  290.  
  291. if (game.board[r+1][c] == '.' ){
  292. game.board[r+1][c] = game.cpu; //Try test move.
  293. int negamaxno=0;
  294. score_for_this_move = -(negamax(game.board, game.cpu, game.player1,negamaxno));
  295. game.board[r+1][c] = '.'; //Put back test move.
  296. if (score_for_this_move >= best_move_score) {
  297. best_move_score = score_for_this_move;
  298. best_move_row = r+1;
  299. best_move_col = c;
  300. }
  301.  
  302. }
  303.  
  304. if (game.board[r-1][c] == '.' ){
  305. game.board[r-1][c] = game.cpu; //Try test move.
  306. int negamaxno=0;
  307. score_for_this_move = -(negamax(game.board, game.cpu, game.player1,negamaxno));
  308. game.board[r-1][c] = '.'; //Put back test move.
  309. if (score_for_this_move >= best_move_score) {
  310. best_move_score = score_for_this_move;
  311. best_move_row = r-1;
  312. best_move_col = c;
  313. }
  314.  
  315. }
  316.  
  317. if (game.board[r][c+1] == '.' ){
  318. game.board[r][c+1] = game.cpu; //Try test move.
  319. int negamaxno=0;
  320. score_for_this_move = -(negamax(game.board, game.cpu, game.player1,negamaxno));
  321. game.board[r][c+1] = '.'; //Put back test move.
  322. if (score_for_this_move >= best_move_score) {
  323. best_move_score = score_for_this_move;
  324. best_move_row = r;
  325. best_move_col = c+1;
  326. }
  327.  
  328. }
  329.  
  330. if (game.board[r][c-1] == '.' ){
  331. game.board[r][c-1] = game.cpu; //Try test move.
  332. int negamaxno=0;
  333. score_for_this_move = -(negamax(game.board, game.cpu, game.player1,negamaxno));
  334. game.board[r][c] = '.'; //Put back test move.
  335. if (score_for_this_move >= best_move_score) {
  336. best_move_score = score_for_this_move;
  337. best_move_row = r;
  338. best_move_col = c-1;
  339. }
  340.  
  341. }
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349. }
  350. }
  351. }
  352. game.board[best_move_row][best_move_col] = game.cpu;
  353.  
  354. moves++;
  355. for (int i = 0; i < 361; i++) {
  356. cout << endl;
  357. for (int j = 0; j < 361; j++) {
  358. cout << " ";
  359. cout << game.board[i][j];
  360. }
  361. }
  362.  
  363. if (realcheck_win(game.board)) {
  364. cout << game.cpu << " won!" <<endl;
  365. exit(1);
  366. }
  367. cout << endl;
  368. }
  369. cout << "Cat's game!" << endl;
  370.  
  371. return 0;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment