Guest User

Untitled

a guest
May 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. //-----------------------------------------
  2. //Matthew Olson's program
  3. //Program: tick-tack-toe
  4. //
  5.  
  6. //EXTRA CREIDT make it work for a 4x4 (ask the user for the size of the grid)
  7.  
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <time.h>
  11.  
  12. using namespace std;
  13. //constants -------------------------------
  14. const int MAX_SIZE = 100; //size of board
  15. const char BLANK = ' '; //marker used for board
  16. const char PLAYER = 'X'; //marker used for player
  17. const char COMPUTER = '0'; //marker used for computer
  18.  
  19. //prototypes ------------------------------
  20. void initBoard(char board[][MAX_SIZE],const int size);
  21. //void dumpArray(const char arr[][MAX_SIZE]);
  22. void displayBoard(char board[][MAX_SIZE],const int size);
  23. void displayRow(char row[],const int size, int &count);
  24. char winner(const char board[][MAX_SIZE], bool &gameOver,const int size); // returns 0, PLAYER or COMPUTER
  25. bool boardFull(char board[][MAX_SIZE], const int size);
  26. void playerMove(char board[][MAX_SIZE],const int size);
  27. void computerMove(char board[][MAX_SIZE], const int size);
  28. bool row(const char board[][MAX_SIZE], bool &gameOver,const int size);
  29. bool column(const char board[][MAX_SIZE], bool &gameOver,const int size);
  30. bool diagonal(const char board[][MAX_SIZE], bool &gameOver,const int size);
  31. //-----------------------------------------
  32. void main() {
  33. char board[MAX_SIZE][MAX_SIZE];
  34. char answer;
  35. int size;
  36.  
  37. //todo:
  38. //winner
  39. // row col diag
  40.  
  41. //boardfull might not work
  42.  
  43. cout << "Would you like to play tic-tac-toe? (y/n): ";
  44. cin >> answer;
  45. while (answer == 'y' || answer == 'Y')
  46. {
  47. bool gameOver = false;
  48. //creat board
  49. cout << "how big would you like the board to be?: ";
  50. cin >> size;
  51. while (size < 2 || size > 25){
  52. cout << "\nPlease choose a new size: ";
  53. cin >> size;
  54. }
  55. initBoard(board,size);
  56. //dumpArray(board); //TEST
  57.  
  58. displayBoard(board,size);
  59. while (!winner(board,gameOver,size) && !boardFull(board,size)) {
  60. playerMove(board,size);
  61. displayBoard(board,size);
  62. if (!winner(board,gameOver,size) && !boardFull(board,size)){
  63. computerMove(board,size);
  64. displayBoard(board,size);
  65. }
  66. }
  67. if (!winner(board,gameOver,size) && boardFull(board,size))
  68. cout << "\nCat's game!" << endl;
  69. cout << "Would you like to play tic-tac-toe again? (y/n): ";
  70. cin >> answer;
  71. }
  72. cin.get();
  73. }
  74.  
  75. void initBoard(char board[][MAX_SIZE],int size) {
  76. for (int row=0;row<size;row++)
  77. for(int col=0; col<size; col++)
  78. board[row][col]=BLANK;
  79.  
  80. }
  81.  
  82. void displayBoard(char board[][MAX_SIZE],const int size) {
  83. system("cls");
  84. int count = 0;
  85.  
  86.  
  87. cout << endl <<endl<< endl<< endl;
  88. for(int x=0;x<size;x++){
  89. displayRow(board[x],size,count);
  90.  
  91. }
  92. }
  93.  
  94. void displayRow(char row[],const int size,int &count){
  95. int temp = size - 1;
  96.  
  97.  
  98. cout << "\n\t";
  99. for(int x=0;x<temp;x++){
  100. cout << row[x] << '|';
  101. }
  102. cout << row[temp];
  103.  
  104. if (count < temp){
  105. cout << "\n\t-";
  106.  
  107. for(int x=1;x<size;x++)
  108. cout <<"+-";
  109. count++;
  110. }
  111. }
  112.  
  113. //------------------------------------------------
  114. // returns 0, PLAYER or COMPUTER
  115. char winner(const char board[][MAX_SIZE], bool &gameOver,const int size){
  116.  
  117.  
  118. row(board,gameOver,size);
  119. column(board,gameOver,size);
  120. diagonal(board,gameOver,size);
  121.  
  122.  
  123. if (gameOver == true)
  124. return 1;
  125. else
  126. return 0;
  127. }
  128.  
  129. //------------------------------------------------
  130. bool boardFull(char board[][MAX_SIZE], const int size){
  131. for (int row=0;row<size;row++)
  132. for(int col=0; col<size; col++)
  133. //if the space is blank, returns that there is an empty space
  134. if (board[row][col]==BLANK)
  135. return false;
  136.  
  137. //board has no empty spaces
  138. return true;
  139. }
  140.  
  141.  
  142.  
  143. //------------------------------------------------
  144. void playerMove(char board[][MAX_SIZE],const int size){
  145. int row=0;
  146. int col=0;
  147.  
  148. //get a row and column for user
  149.  
  150. cout << "\n\tWhere would you like to go? (row and column): ";
  151. cin >> row >> col;
  152.  
  153. row--; col--;
  154.  
  155. //while row and column are invalid (already occupied or of bounds)
  156. //produce error message
  157. //reget row and column
  158. while(board[row][col] != BLANK || row > size || col > size){
  159. cout << "Place taken. Please select a new place to go: ";
  160. cin >> row >> col;
  161. row--; col--;
  162. }
  163.  
  164. //put Player maker in selected location
  165. board[row][col] = PLAYER;
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172. //------------------------------------------------
  173. void computerMove(char board[][MAX_SIZE], const int size){
  174. int row;
  175. int col;
  176. bool moved = false;
  177.  
  178. static bool firstRun = true;
  179. if(firstRun==true) //only seed the random generator once
  180. {
  181. srand(time(NULL));
  182. firstRun = false;
  183. }
  184.  
  185. cout << "thinking.....";
  186. for (time_t t=time(0)+1; time(0)<t; ) {}
  187.  
  188. while(moved == false){
  189. row = rand() % size;
  190. col = rand() % size;
  191.  
  192. while (board[row][col]==BLANK){
  193. board[row][col]=COMPUTER;
  194. moved= true;
  195. }
  196.  
  197. }
  198.  
  199.  
  200. }
  201.  
  202. //------------------------------------------------
  203.  
  204. //void dumpArray(const char arr[][MAX_SIZE]) {
  205. //
  206. // for(int row=0; row<MAX_SIZE; row++){
  207. // for(int col=0; col<MAX_SIZE; col++)
  208. // cout << static_cast<int>(arr[row][col]) << ' ';
  209. // cout << endl;
  210. // }
  211. //
  212. // cout<< endl;
  213. //}
  214.  
  215. bool row(const char board[][MAX_SIZE], bool &gameOver,const int size){
  216. int x;
  217. int y;
  218.  
  219. if (!gameOver){
  220. for (int col=0;col<size;col++){
  221. x = 0;
  222. y=0;
  223. for(int row=0;row<size;row++){
  224. if (board[row][col]==PLAYER){
  225. x++;
  226.  
  227. if (!gameOver && x == size){
  228. gameOver = true;
  229. std::cout << "\n\tYou are the winner!" << endl;
  230. }
  231. }
  232.  
  233.  
  234. if (board[row][col]==COMPUTER){
  235. y++;
  236.  
  237. if (!gameOver && y == size){
  238. gameOver = true;
  239. std::cout << "\n\tYou lost!" << endl;
  240. }
  241.  
  242. }
  243. }
  244. }
  245.  
  246. }
  247. return gameOver;
  248. }
  249.  
  250. bool column(const char board[][MAX_SIZE], bool &gameOver,const int size) {
  251. int x;
  252. int y;
  253.  
  254. if (!gameOver){
  255. for (int row=0;row<size;row++){
  256. x = 0;
  257. y=0;
  258. for(int col=0;col<size;col++){
  259. if (board[row][col]==PLAYER){
  260. x++;
  261.  
  262. if (!gameOver && x == size){
  263. gameOver = true;
  264. cout << "\n\tYou are the winner!" << endl;
  265. }
  266. }
  267.  
  268.  
  269. if (board[row][col]==COMPUTER){
  270. y++;
  271.  
  272. if (!gameOver && y == size){
  273. gameOver = true;
  274. cout << "\n\tYou lost!" << endl;
  275. }
  276.  
  277. }
  278. }
  279. }
  280. return gameOver;
  281. }
  282. }
  283.  
  284. bool diagonal(const char board[][MAX_SIZE], bool &gameOver,const int size) {
  285. int x;
  286. int y;
  287.  
  288. if (gameOver != true){
  289. x = 0;
  290. y = 0;
  291. for(int rowCol=0;rowCol<size;rowCol++){
  292. if (board[rowCol][rowCol]==PLAYER){
  293. x++;
  294. if (!gameOver && x == size){
  295. gameOver = true;
  296. cout << "\n\tYou are the winner!" << endl;
  297. }
  298. }
  299.  
  300. if (board[rowCol][rowCol]==COMPUTER){
  301. y++;
  302. if (!gameOver && y == size){
  303. gameOver = true;
  304. cout << "\n\tYou lost!" << endl;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. if (gameOver != true){
  311. x = 0;
  312. y = 0;
  313.  
  314. int rowSize = size; //starts at top right
  315. for(int colSize=0;colSize<size;colSize++){
  316. rowSize--;
  317. if (board[rowSize][colSize]==PLAYER){
  318. x++;
  319. if (!gameOver && x == size){
  320. gameOver = true;
  321. cout << "\n\tYou are the winner!" << endl;
  322. }
  323. }
  324.  
  325. if (board[rowSize][colSize]==COMPUTER){
  326. y++;
  327. if (!gameOver && y == size){
  328. gameOver = true;
  329. cout << "\n\tYou lost!" << endl;
  330. }
  331. }
  332. }
  333. }
  334.  
  335. return gameOver;
  336. }
Add Comment
Please, Sign In to add comment