Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. // tictactoe.txt - Purpose: To be able to play tic tac toe against a computer opponent.
  2. // Scott Hancock
  3. // CISP 360
  4. // November 6, 2015
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <ctime>
  9. using namespace std;
  10.  
  11. int freeSpaces = 9;
  12. int turn;
  13.  
  14. char board[3][3] =
  15. {
  16. {'1','2','3'},
  17. {'4','5','6'},
  18. {'7','8','9'}
  19. };
  20.  
  21. //////////////////
  22. //Display Board//
  23. /////////////////
  24.  
  25. void displayBoard()
  26. {
  27.  
  28.  
  29. cout << " _________________" << endl;
  30. cout << "| | | |" << endl;
  31. cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] <<" |" << endl;
  32. cout << "|_____|_____|_____|" << endl;
  33. cout << "| | | |" << endl;
  34. cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] <<" |" << endl;
  35. cout << "|_____|_____|_____|" << endl;
  36. cout << "| | | |" << endl;
  37. cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] <<" |" << endl;
  38. cout << "|_____|_____|_____|" << endl;
  39.  
  40. }
  41.  
  42. ///////////////////
  43. //Makes game move//
  44. ///////////////////
  45.  
  46. void gameMove()
  47. {
  48.  
  49.  
  50. if (turn == 1)
  51. {
  52.  
  53. char move;
  54. cout << endl << "Where would you like to make your next move?" << endl;
  55. cin >> move;
  56.  
  57. //Check space and set array to the move space
  58.  
  59. for (int i= 0; i < 3; i++)
  60. for (int j = 0; j < 3; j++)
  61.  
  62. {
  63.  
  64. if (board[i][j] == move)
  65. {
  66. board[i][j] = 'X';
  67. cout << "Move is okay." << endl;
  68. turn = turn + 1;
  69. freeSpaces = freeSpaces - 1;
  70. displayBoard();
  71.  
  72. }
  73.  
  74.  
  75.  
  76. }
  77.  
  78. }
  79. //////////////////
  80. //Computers turn//
  81. //////////////////
  82.  
  83. else
  84. {
  85.  
  86. int compMove;
  87. char newMove;
  88. srand( time(NULL) );
  89. compMove = rand() % 9 + 1;
  90. newMove = compMove + '0';
  91.  
  92.  
  93. //Check space and set array to the move space
  94.  
  95. for (int i = 0; i < 3; i++)
  96. for (int j = 0; j < 3; j++)
  97. {
  98.  
  99. if (board[i][j] == newMove)
  100. {
  101. board[i][j] = 'O';
  102. turn = turn - 1;
  103. freeSpaces = freeSpaces - 1;
  104. cout << endl << "The computer is making its move." << endl;
  105. displayBoard();
  106. }
  107.  
  108.  
  109. }
  110.  
  111. }
  112.  
  113. }
  114.  
  115. /////////////
  116. //Start Up//
  117.  
  118. void startUp()
  119. {
  120. cout << "This is a Tic Tac Toe Game. Choose the number for the space you want to take." << endl;
  121. //Choose Starting player
  122. cout << "Randomizing starting player." << endl;
  123. srand( time(NULL) );
  124. turn = rand() % 2 + 1;
  125.  
  126. if(turn == 1)
  127. {
  128. cout << "Player " << turn << " goes first. (Human Player or X)" << endl;
  129. }
  130.  
  131. else
  132. {
  133. cout << "Player " << turn << " goes first. (Computer or O)" << endl;
  134. }
  135. }
  136.  
  137. ///////////////////////////
  138. //Check for winner or tie//
  139. ///////////////////////////
  140.  
  141. bool checkWin()
  142. {
  143. char winner = 'N';
  144.  
  145. for(int x = 0; x < 3; x++)
  146. {
  147. winner = board[x][0];
  148. for(int y = 0; y < 3; y++)
  149. {
  150. if(board[x][y] != winner){
  151. winner = 'N';
  152. break;
  153. }
  154. }
  155. if(winner != 'N')
  156. {
  157. cout << "Winner is " << winner << endl;
  158. return true;
  159. }
  160. }
  161.  
  162. for(int x = 0; x < 3; x++)
  163. {
  164. winner = board[0][x];
  165. for(int y = 0; y < 3; y++)
  166. {
  167. if(board[y][x] != winner)
  168. {
  169. winner = 'N';
  170. break;
  171. }
  172. }
  173. if(winner != 'N')
  174. {
  175. cout << "Winner is " << winner << endl;
  176. return true;
  177. }
  178. }
  179.  
  180. //Check Diagonals
  181. if((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
  182. {
  183. cout << "Winner is " << board[1][1] << endl;
  184. return true;
  185. }
  186.  
  187. while (freeSpaces !=0)
  188. {
  189. gameMove();
  190. return false;
  191. }
  192.  
  193. if (freeSpaces == 0)
  194. {
  195. cout << "It is a tie!" << endl;
  196. return true;
  197. }
  198. return false;
  199. }
  200.  
  201. int main()
  202. {
  203. bool gameOver = false;
  204. startUp();
  205. displayBoard();
  206. while(!gameOver)
  207. {
  208. gameOver = checkWin();
  209. }
  210. return 0;
  211.  
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement