Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 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.  
  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();
  44. bool RandomAI();
  45. bool CleverAI();
  46. bool FancyAI();
  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.  
  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.  
  227.  
  228. bob.SetBoard(row, column, playerTurn);
  229. return true;
  230. }
  231.  
  232. bool Tic4::RandomAI(char player)
  233. {
  234.  
  235.  
  236. bob.SetBoard(row, column, playerTurn);
  237. return true;
  238. }
  239.  
  240. bool Tic4::CleverAI(char player)
  241. {
  242.  
  243.  
  244. bob.SetBoard(row, column, playerTurn);
  245. return true;
  246. }
  247.  
  248. bool Tic4::FancyAIchar player()
  249. {
  250. //will call simple random or clever at random
  251. /*
  252. if(randomnumber == 1)
  253. {
  254. SimpleAI();
  255. }
  256.  
  257. if(randomnumber == 2)
  258. {
  259. RandomAI();
  260. }
  261.  
  262. if(randomnumber == 3)
  263. {
  264. CleverAI();
  265. }
  266. */
  267. }
  268.  
  269. //---------------------------------------------------------------------------
  270.  
  271. //---------------------------------------------------
  272. // Main program to play Tic4 game
  273. //---------------------------------------------------
  274. int main()
  275. {
  276. cout << "Hello user. Please input answers like so: 2,1" << endl;
  277. cout << "Do not enter any other way, and do not try put the same answer twice or go where someone else has gone " << endl;
  278. bool player1win = false;
  279. bool player2win = false;
  280. bool BoardFull = false;
  281. char Player1 = 'X';
  282. char Player2 = 'O';
  283. char str[] = "2 2";
  284. //cout << str ;
  285. string message1 = "Enter X move: ";
  286. string message2 = "Enter O move: ";
  287. string Answer = "";
  288. char playerTurn = 'X';
  289. bool GoodAnswer = true;
  290. int row = 0;
  291. int column = 0;
  292. bool currentwin = false;
  293.  
  294. Tic4 bob;
  295.  
  296. cout << "Welcome to 4x4 tic-tac-toe";
  297. int turn_counter = 1;
  298. while(currentwin == false && BoardFull == false)
  299. {
  300. bob.PrintBoard();
  301. if(turn_counter%2==0)
  302. {
  303. playerTurn = 'O';
  304. }
  305. else
  306. {
  307. playerTurn = 'X';
  308. }
  309.  
  310. currentwin = bob.CheckWin(playerTurn); //this doesn't work well
  311. if (currentwin == false)
  312. {
  313. cout << "No one has won the game yet" << endl;
  314. }
  315. BoardFull = bob.IsFull();
  316. if(turn_counter%2==0)
  317. {
  318. playerTurn = 'O';
  319. cout << message2;
  320. }
  321. else
  322. {
  323. playerTurn = 'X';
  324. cout << message1 ; //edit code here to call the fancy AI
  325. }
  326. turn_counter++;
  327. cin >> Answer;//edit code here to call the fancy AI
  328.  
  329.  
  330. cout << endl;
  331. ;
  332. int i;
  333. for(i=0;i<sizeof(Answer);i++)
  334. {
  335. str[i] = Answer[i];
  336. }
  337. //cout << "This is what you just entered " << str << endl;
  338. row = (int)str[0] - 48;
  339. column = (int)str[2] - 48;
  340.  
  341. bob.SetBoard(row, column, playerTurn);
  342. }
  343. if(currentwin == true)
  344. {
  345. if(playerTurn == 'O')
  346. {
  347. cout << "Player O Wins!";
  348. }
  349. else
  350. {
  351. cout <<"Player X Wins!";
  352. }
  353. }
  354. if(BoardFull == true && currentwin == false)
  355. {
  356. cout << "It's a draw!";
  357. }
  358. return 0;
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement