Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 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.  
  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 CheckWin(const char player);
  41. void PrintBoard();
  42. bool IsFull();
  43. bool SimpleAI(char & player);
  44. bool RandomAI(char & player);
  45. bool CleverAI(char & player);
  46. bool FancyAI(char & player);
  47.  
  48. private:
  49. // Private variables
  50. char board[SIZE][SIZE];
  51. int board_count;
  52. };
  53. #endif
  54.  
  55. //---------------------------------------------------
  56. // Purpose: Implementation of the Tic4 class
  57. //
  58. // Author: John Gauch - Created skeleton program.
  59. // YOUR NAME - Completed implementation.
  60. //---------------------------------------------------
  61.  
  62. //#include "tic4.h"
  63.  
  64. //---------------------------------------------------
  65. // Constructor function
  66. //---------------------------------------------------
  67. Tic4::Tic4()
  68. {
  69. ClearBoard();
  70. }
  71.  
  72. //---------------------------------------------------
  73. // Destructor function
  74. //---------------------------------------------------
  75. Tic4::~Tic4()
  76. {
  77. // Intentionally empty
  78. }
  79.  
  80. //---------------------------------------------------
  81. // Clear the Tic4 board
  82. //---------------------------------------------------
  83. void Tic4::ClearBoard()
  84. {
  85. // Initialize Tic4 board
  86. for (int c = 0; c < SIZE; c++)
  87. for (int r = 0; r < SIZE; r++)
  88. board[r][c] = ' ';
  89. board_count = 0;
  90. }
  91.  
  92. //---------------------------------------------------
  93. // Set value of board at location [row][column]
  94. //---------------------------------------------------
  95. bool Tic4::SetBoard(int row, int col, char player)
  96. {
  97. // Error checking
  98. if ((row < 0) || (row >= SIZE) ||
  99. (col < 0) || (col >= SIZE) ||
  100. (board[row][col] != ' '))
  101. return false;
  102.  
  103. // Set value of board
  104. board[row][col] = player;
  105. board_count++;
  106. return true;
  107. }
  108.  
  109. //---------------------------------------------------
  110. // Get value of board at location [row][column]
  111. //---------------------------------------------------
  112. bool Tic4::GetBoard(const int row, const int col, char & player)
  113. {
  114. // Error checking
  115. if ((row < 0) || (row >= SIZE) ||
  116. (col < 0) || (col >= SIZE))
  117. return false;
  118.  
  119. // Get value of board
  120. player = board[row][col];
  121. return true;
  122. }
  123.  
  124. //---------------------------------------------------
  125. // Check to see if player has won the game
  126. //---------------------------------------------------
  127. bool Tic4::CheckWin(const char player)
  128. {
  129. // Check all the rows
  130. for (int r = 0; r < SIZE; r++)
  131. {
  132. // Count player pieces
  133. int count = 0;
  134. for (int c = 0; c < SIZE; c++)
  135. if (board[r][c] == player)
  136. count++;
  137. if (count == SIZE)
  138. return true;
  139. }
  140.  
  141. // Check all the cols
  142. for (int c = 0; c < SIZE; c++)
  143. {
  144. // Count player pieces
  145. int count = 0;
  146. for (int r = 0; r < SIZE; r++)
  147. if (board[r][c] == player)
  148. count++;
  149. if (count == SIZE)
  150. return true;
  151. }
  152.  
  153. // Check first diagonal
  154. int count = 0;
  155. for (int r = 0; r < SIZE; r++)
  156. {
  157. int c = r;
  158. if (board[r][c] == player)
  159. count++;
  160. }
  161. if (count == SIZE)
  162. return true;
  163.  
  164. // Check second diagonal
  165. count = 0;
  166. for (int r = 0; r < SIZE; r++)
  167. {
  168. int c = SIZE-1-r;
  169. if (board[r][c] == player)
  170. count++;
  171. }
  172. if (count == SIZE)
  173. return true;
  174.  
  175. // The player did not win
  176. return false;
  177. }
  178.  
  179. //---------------------------------------------------
  180. // Print the Tic4 board
  181. //---------------------------------------------------
  182. void Tic4::PrintBoard()
  183. {
  184. // Draw column numbers
  185. cout << "\n ";
  186. for (int c = 0; c < SIZE; c++)
  187. cout << c << " ";
  188. cout << "\n";
  189. //cout << "The size is " << SIZE << endl;
  190. // Print the Tic4 board
  191. for (int r = 0; r < SIZE; r++)
  192. {
  193. // Draw dashed line
  194. cout << " +";
  195. for (int c = 0; c < SIZE; c++)
  196. cout << "---+";
  197. cout << "\n";
  198.  
  199. // Draw board contents
  200. cout << " " << r << " | ";
  201. for (int c = 0; c < SIZE; c++)
  202. cout << board[r][c] << " | ";
  203. cout << "\n";
  204. }
  205.  
  206. // Draw dashed line
  207. cout << " +";
  208. for (int c = 0; c < SIZE; c++)
  209. cout << "---+";
  210. cout << "\n\n";
  211. }
  212.  
  213. //---------------------------------------------------
  214. // Check if board is full
  215. //---------------------------------------------------
  216. bool Tic4::IsFull()
  217. {
  218. return (board_count == SIZE * SIZE);
  219. }
  220.  
  221. //#include "tic4.h"
  222.  
  223. //-------------------------------------------------------------------- AI CODE
  224. bool Tic4::SimpleAI(char & player)
  225. {
  226. Tic4 bob;
  227. //GetBoard(row, column, player)
  228. bool Truth = False;
  229. for(int row = 0; row < 4) //this is wrong, gotta check top left first
  230. {
  231. Truth
  232. for(int column = 0; column < 4)
  233. {
  234.  
  235. }
  236. }
  237.  
  238. //bob.SetBoard(row, column, playerTurn);
  239. return true;
  240. }
  241.  
  242. bool Tic4::RandomAI(char & player)
  243. {
  244. Tic4 bob;
  245.  
  246. //bob.SetBoard(row, column, playerTurn);
  247. return true;
  248. }
  249.  
  250. bool Tic4::CleverAI(char & player)
  251. {
  252. Tic4 bob;
  253.  
  254. //bob.SetBoard(row, column, player);
  255. return true;
  256. }
  257.  
  258. bool Tic4::FancyAI(char & player)
  259. {
  260. Tic4 bob;
  261. //will call simple random or clever at random
  262.  
  263. int rand();
  264. int randomnumber;
  265. randomnumber = rand()%3 + 1;
  266. if(randomnumber == 1)
  267. {
  268. SimpleAI(player);
  269. }
  270.  
  271. if(randomnumber == 2)
  272. {
  273. RandomAI(player);
  274. }
  275.  
  276. if(randomnumber == 3)
  277. {
  278. CleverAI(player);
  279. }
  280.  
  281. }
  282.  
  283. //---------------------------------------------------------------------------
  284.  
  285. //---------------------------------------------------
  286. // Main program to play Tic4 game
  287. //---------------------------------------------------
  288. int main()
  289. {
  290. cout << "Hello user. Please input answers like so: 2,1" << endl;
  291. cout << "Do not enter any other way, and do not try put the same answer twice or go where someone else has gone " << endl;
  292. bool player1win = false;
  293. bool player2win = false;
  294. bool BoardFull = false;
  295. char Player1 = 'X';
  296. char Player2 = 'O';
  297. char str[] = "2 2";
  298. //cout << str ;
  299. string message1 = "Enter X move: ";
  300. string message2 = "Enter O move: ";
  301. string Answer = "";
  302. char player = 'X';
  303. bool GoodAnswer = true;
  304. int row = 0;
  305. int column = 0;
  306. bool currentwin = false;
  307.  
  308. Tic4 bob;
  309.  
  310. cout << "Welcome to 4x4 tic-tac-toe";
  311. int turn_counter = 1;
  312. while(currentwin == false && BoardFull == false)
  313. {
  314. bob.PrintBoard();
  315. if(turn_counter%2==0)
  316. {
  317. player = 'O';
  318. }
  319. else
  320. {
  321. player = 'X';
  322. }
  323.  
  324. currentwin = bob.CheckWin(player); //this doesn't work well
  325. if (currentwin == false)
  326. {
  327. cout << "No one has won the game yet" << endl;
  328. }
  329. BoardFull = bob.IsFull();
  330. if(turn_counter%2==0)
  331. {
  332. player = 'O';
  333. cout << message2;
  334. }
  335. else
  336. {
  337. player = 'X';
  338. cout << message1 ; //edit code here to call the fancy AI
  339. }
  340. turn_counter++;
  341. cin >> Answer;//edit code here to call the fancy AI
  342.  
  343.  
  344. cout << endl;
  345. ;
  346. int i;
  347. for(i=0;i<sizeof(Answer);i++)
  348. {
  349. str[i] = Answer[i];
  350. }
  351. //cout << "This is what you just entered " << str << endl;
  352. row = (int)str[0] - 48;
  353. column = (int)str[2] - 48;
  354.  
  355. bob.SetBoard(row, column, player);
  356. }
  357. if(currentwin == true)
  358. {
  359. if(player == 'O')
  360. {
  361. cout << "Player O Wins!";
  362. }
  363. else
  364. {
  365. cout <<"Player X Wins!";
  366. }
  367. }
  368. if(BoardFull == true && currentwin == false)
  369. {
  370. cout << "It's a draw!";
  371. }
  372. return 0;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement