Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <stdio.h>
  10. #include <cstdlib>
  11. #include<string.h>
  12. //---------------------------------------------------
  13. // Purpose: Header file for the Tic4 class
  14. //
  15. // Author: John Gauch - Created class definition.
  16. // Kendall Clancy - Completed implementation.
  17. //---------------------------------------------------
  18.  
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. #ifndef TIC4
  23. #define TIC4
  24.  
  25. // Global constants
  26. const int SIZE = 4;
  27.  
  28. // Class definition
  29. class Tic4
  30. {
  31. public:
  32. // Constructor and destructor
  33. Tic4();
  34. ~Tic4();
  35.  
  36. // Public methods
  37. void ClearBoard();
  38. bool SetBoard(const int row, const int col, const char player);
  39. bool GetBoard(const int row, const int col, char & player);
  40. //bool GetBoard2(const int row, const int col, char & player);
  41. bool CheckWin(const char player);
  42. void PrintBoard();
  43. bool IsFull();
  44. bool SimpleAI(char & player);
  45. bool RandomAI(char & player);
  46. bool CleverAI(char & player);
  47. bool FancyAI(char & player);
  48.  
  49. private:
  50. // Private variables
  51. char board[SIZE][SIZE];
  52. int board_count;
  53. };
  54. #endif
  55.  
  56. //---------------------------------------------------
  57. // Purpose: Implementation of the Tic4 class
  58. //
  59. // Author: John Gauch - Created skeleton program.
  60. // YOUR NAME - Completed implementation.
  61. //---------------------------------------------------
  62.  
  63. //#include "tic4.h"
  64.  
  65. //---------------------------------------------------
  66. // Constructor function
  67. //---------------------------------------------------
  68. Tic4::Tic4()
  69. {
  70. ClearBoard();
  71. }
  72.  
  73. //---------------------------------------------------
  74. // Destructor function
  75. //---------------------------------------------------
  76. Tic4::~Tic4()
  77. {
  78. // Intentionally empty
  79. }
  80.  
  81. //---------------------------------------------------
  82. // Clear the Tic4 board
  83. //---------------------------------------------------
  84. void Tic4::ClearBoard()
  85. {
  86. // Initialize Tic4 board
  87. for (int c = 0; c < SIZE; c++)
  88. for (int r = 0; r < SIZE; r++)
  89. board[r][c] = ' ';
  90. board_count = 0;
  91. }
  92.  
  93. //---------------------------------------------------
  94. // Set value of board at location [row][column]
  95. //---------------------------------------------------
  96. bool Tic4::SetBoard(int row, int col, char player)
  97. {
  98. // Error checking
  99. if ((row < 0) || (row >= SIZE) ||
  100. (col < 0) || (col >= SIZE) ||
  101. (board[row][col] != ' '))
  102. return false;
  103.  
  104. // Set value of board
  105. board[row][col] = player;
  106. board_count++;
  107. return true;
  108. }
  109.  
  110. //---------------------------------------------------
  111. // Get value of board at location [row][column]
  112. //---------------------------------------------------
  113. bool Tic4::GetBoard(const int row, const int col, char & player)
  114. {
  115. // Error checking
  116. if ((row < 0) || (row >= SIZE) ||
  117. (col < 0) || (col >= SIZE))
  118. return false;
  119.  
  120. // Get value of board
  121. player = board[row][col];
  122. return true;
  123. }
  124. /*
  125. bool Tic4::GetBoard2(const int row, const int col, char & player)
  126. {
  127. int newrow = row;
  128. int newcol = col;
  129. char testingvalue = 'F';
  130. testingvalue = board[newrow,newcol];
  131. int resultz = strcmp(" ", testingvalue);
  132. if (resultz == 0)
  133. {
  134. return true;
  135. }
  136. return false;
  137. }
  138. */
  139. //---------------------------------------------------
  140. // Check to see if player has won the game
  141. //---------------------------------------------------
  142. bool Tic4::CheckWin(const char player)
  143. {
  144. // Check all the rows
  145. for (int r = 0; r < SIZE; r++)
  146. {
  147. // Count player pieces
  148. int count = 0;
  149. for (int c = 0; c < SIZE; c++)
  150. if (board[r][c] == player)
  151. count++;
  152. if (count == SIZE)
  153. return true;
  154. }
  155.  
  156. // Check all the cols
  157. for (int c = 0; c < SIZE; c++)
  158. {
  159. // Count player pieces
  160. int count = 0;
  161. for (int r = 0; r < SIZE; r++)
  162. if (board[r][c] == player)
  163. count++;
  164. if (count == SIZE)
  165. return true;
  166. }
  167.  
  168. // Check first diagonal
  169. int count = 0;
  170. for (int r = 0; r < SIZE; r++)
  171. {
  172. int c = r;
  173. if (board[r][c] == player)
  174. count++;
  175. }
  176. if (count == SIZE)
  177. return true;
  178.  
  179. // Check second diagonal
  180. count = 0;
  181. for (int r = 0; r < SIZE; r++)
  182. {
  183. int c = SIZE-1-r;
  184. if (board[r][c] == player)
  185. count++;
  186. }
  187. if (count == SIZE)
  188. return true;
  189.  
  190. // The player did not win
  191. return false;
  192. }
  193.  
  194. //---------------------------------------------------
  195. // Print the Tic4 board
  196. //---------------------------------------------------
  197. void Tic4::PrintBoard()
  198. {
  199. // Draw column numbers
  200. cout << "\n ";
  201. for (int c = 0; c < SIZE; c++)
  202. cout << c << " ";
  203. cout << "\n";
  204. //cout << "The size is " << SIZE << endl;
  205. // Print the Tic4 board
  206. for (int r = 0; r < SIZE; r++)
  207. {
  208. // Draw dashed line
  209. cout << " +";
  210. for (int c = 0; c < SIZE; c++)
  211. cout << "---+";
  212. cout << "\n";
  213.  
  214. // Draw board contents
  215. cout << " " << r << " | ";
  216. for (int c = 0; c < SIZE; c++)
  217. cout << board[r][c] << " | ";
  218. cout << "\n";
  219. }
  220.  
  221. // Draw dashed line
  222. cout << " +";
  223. for (int c = 0; c < SIZE; c++)
  224. cout << "---+";
  225. cout << "\n\n";
  226. }
  227.  
  228. //---------------------------------------------------
  229. // Check if board is full
  230. //---------------------------------------------------
  231. bool Tic4::IsFull()
  232. {
  233. return (board_count == SIZE * SIZE);
  234. }
  235.  
  236. //#include "tic4.h"
  237.  
  238. //-------------------------------------------------------------------- AI CODE
  239. bool Tic4::SimpleAI(char & player)
  240. {
  241. cout << "Called SimpleAI" << endl;
  242. Tic4 bob;
  243.  
  244. bool Truth {false};
  245. int counter = 0;
  246. for(int row = 0; row < 4; row++)
  247. {
  248.  
  249. for(int column = 0; column < 4; column++)
  250. {
  251. Truth = GetBoard(row, column, player);
  252. counter++;
  253. if (Truth == true)
  254. {
  255. bob.SetBoard(row,column,player);
  256. return true;
  257. }
  258.  
  259. }
  260. }
  261.  
  262. //bob.SetBoard(row, column, player);
  263. if (counter >=15 && Truth == false)
  264. {
  265. return false;
  266. }
  267. return true;
  268. }
  269.  
  270. bool Tic4::RandomAI(char & player)
  271. {
  272. cout << "Called RandomAI" << endl;
  273. Tic4 bob;
  274. int rand();
  275. int randomnumber;
  276. int randomnumber2;
  277. int counter = 0;
  278. bool Truth {false};
  279.  
  280. while(counter <= 50)
  281. {
  282. randomnumber = rand()%4;
  283. randomnumber2 = rand()%4;
  284. Truth = GetBoard(randomnumber, randomnumber2, player);
  285. if(Truth == true)
  286. {
  287. bob.SetBoard(randomnumber,randomnumber2,player);
  288. return true;
  289. } //end if
  290. }//end while
  291.  
  292. if(counter >= 50 && Truth == false)
  293. {
  294. cout << "This method assumes the board is full" << endl;
  295. return false;
  296. }
  297. //bob.SetBoard(row, column, playerTurn);
  298. return true;
  299. }
  300.  
  301. bool Tic4::CleverAI(char & player) //search the 2 diagonals, else, search the non-diagonals.
  302. //non-diagonals are SimpleAI so I can just call that if the diagonals don't work out
  303. {
  304. cout << "Called CleverAI" << endl;
  305. Tic4 bob;
  306. bool Truth {false};
  307. //check the first diagonal
  308. for (int r = 0; r < SIZE; r++)
  309. {
  310. int c = r;
  311. Truth = GetBoard(r,c,player);
  312. if(Truth == true)
  313. {
  314. bob.SetBoard(r,c,player);
  315. return true;
  316. }
  317. }
  318.  
  319.  
  320. // Check second diagonal
  321.  
  322. for (int row = 0; row < SIZE; row++)
  323. {
  324. int col = SIZE-1-row;
  325. Truth = GetBoard(row,col,player);
  326. if(Truth == true)
  327. {
  328. bob.SetBoard(row,col,player);
  329. return true;
  330. }
  331.  
  332. }
  333. int counter = 0;
  334. if (Truth == false)
  335. {
  336.  
  337. for(int rowz = 0; rowz < 4; rowz++)
  338. {
  339.  
  340. for(int column = 0; column < 4; column++)
  341. {
  342. Truth = GetBoard(rowz, column, player);
  343. counter++;
  344. if (Truth == true)
  345. {
  346. bob.SetBoard(rowz,column,player);
  347. return true;
  348. }
  349.  
  350. }
  351. }
  352.  
  353. //bob.SetBoard(row, column, player);
  354. if (counter >=15 && Truth == false)
  355. {
  356. return false;
  357. }
  358.  
  359. }
  360. return true;
  361. }
  362.  
  363. bool Tic4::FancyAI(char & player)
  364. {
  365. Tic4 bob;
  366. //will call simple random or clever at random
  367. cout << "called FancyAI" << endl;
  368. bool Truth {false};
  369. int rand();
  370. int randomnumber;
  371. randomnumber = rand()%3 + 1;
  372. cout << "The random number is " << randomnumber << endl;
  373. randomnumber = 1; // TESTINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
  374. if(randomnumber == 1)
  375. {
  376. Truth = SimpleAI(player);
  377. }
  378.  
  379. if(randomnumber == 2)
  380. {
  381. Truth = RandomAI(player);
  382. }
  383.  
  384. if(randomnumber == 3)
  385. {
  386. Truth = CleverAI(player);
  387. }
  388. if(Truth == true)
  389. {
  390. return true;
  391. }
  392. if(Truth == false)
  393. {
  394. return false;
  395. }
  396. }
  397.  
  398. //---------------------------------------------------------------------------
  399.  
  400. //---------------------------------------------------
  401. // Main program to play Tic4 game
  402. //---------------------------------------------------
  403. int main()
  404. {
  405. cout << "Hello user. Please input answers like so: 2,1" << endl;
  406. cout << "Do not enter any other way, and do not try put the same answer twice or go where someone else has gone " << endl;
  407. bool player1win = false;
  408. bool player2win = false;
  409. bool BoardFull = false;
  410. char Player1 = 'X';
  411. char Player2 = 'O';
  412. char str[] = "2 2";
  413. //cout << str ;
  414. string message1 = "Enter X move: ";
  415. string message2 = "Enter O move: ";
  416. string Answer = "";
  417. char player = 'X';
  418. bool GoodAnswer = true;
  419. int row = 0;
  420. int column = 0;
  421. bool currentwin = false;
  422.  
  423. Tic4 bob;
  424.  
  425. cout << "Welcome to 4x4 tic-tac-toe";
  426. int turn_counter = 1;
  427. bool Truth {false};
  428. while(currentwin == false && BoardFull == false)
  429. {
  430. bob.PrintBoard();
  431. //cout << "player turn = " << turn_counter%2 << " welp" << endl;
  432. if(turn_counter%2==0)
  433. {
  434. player = 'O';
  435. }
  436. else
  437. {
  438. player = 'X';
  439. }
  440.  
  441. currentwin = bob.CheckWin(player); //this doesn't work well
  442. if (currentwin == false)
  443. {
  444. cout << "No one has won the game yet" << endl;
  445. }
  446. BoardFull = bob.IsFull();
  447. if(turn_counter%2==0)
  448. {
  449. player = 'O';
  450. //cout << message2; //edit code here to call the fancy AI
  451. cout << "Now it's the AI turn" << endl;
  452. Truth = bob.FancyAI(player);
  453. if(Truth == false)
  454. {
  455. cout << "The board is full and it's a tie" << endl;
  456. return 0;
  457. }
  458.  
  459. }//end turn counter if
  460. else
  461. {
  462. player = 'X';
  463. //if(turn_counter%2 == 0) // --------------------------------------created for test
  464. //{
  465. //cout << "THE TURN COUNTER IS CORRECT BUT WTF LOL" << endl;
  466. //}
  467. cout << message1 ;
  468. cin >> Answer;
  469. //turn_counter++; // ---------------------------------------------------------- created this line for test
  470. //cout << "THE TURN COUNTER IS NOW " << turn_counter << endl; //same
  471. }
  472. turn_counter++;
  473.  
  474.  
  475.  
  476. cout << endl;
  477. ;
  478. int i;
  479. for(i=0;i<sizeof(Answer);i++)
  480. {
  481. str[i] = Answer[i];
  482. }
  483. //cout << "This is what you just entered " << str << endl;
  484. row = (int)str[0] - 48;
  485. column = (int)str[2] - 48;
  486.  
  487. bob.SetBoard(row, column, player);
  488. }
  489. if(currentwin == true)
  490. {
  491. if(player == 'O')
  492. {
  493. cout << "Player O Wins!";
  494. }
  495. else
  496. {
  497. cout <<"Player X Wins!";
  498. }
  499. }
  500. if(BoardFull == true && currentwin == false)
  501. {
  502. cout << "It's a draw!";
  503. }
  504. return 0;
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement