Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. /*--------------------------------------------
  2. name: Tyler Hann
  3. nsid: tch435
  4. lecture section : 05A
  5. lab section: T07
  6. student number: 11081310
  7.  
  8. Assignment X
  9. Question Y
  10.  
  11. Notes or comments to marker:
  12.  
  13.  
  14. ----------------------------------------------*/
  15. #include <iostream>
  16. #include <stdlib.h>
  17. #include <string>
  18.  
  19. //put additional include lines here as needed
  20. //include <packageName>
  21.  
  22. using namespace std;
  23.  
  24. void checkWin(char gameGrid[3][3])
  25. { int p1=0, p11=0, p111=0, p1111=0, p2=0, p22=0, p222=0, p2222=0, a=0, b=1, c=0;
  26. if (gameGrid[0][0]=='X')
  27. {
  28. p1++;
  29. }
  30. if (gameGrid[0][1]=='X')
  31. {
  32. p1++;
  33. }
  34. if (gameGrid[0][2]=='X')
  35. {
  36. p1++;
  37. }
  38. if (p1==3)
  39. {
  40. cout << "Player 1 wins!" << endl;
  41. a++;
  42. b=0;
  43. }
  44.  
  45.  
  46. if (gameGrid[0][0]=='X')
  47. {
  48. p11++;
  49. }
  50. if (gameGrid[1][0]=='X')
  51. {
  52. p11++;
  53. }
  54. if (gameGrid[2][0]=='X')
  55. {
  56. p11++;
  57. }
  58. if (p11==3)
  59. {
  60. cout << "Player 1 wins!" << endl;
  61. a++;
  62. b=0;
  63. }
  64.  
  65.  
  66. if (gameGrid[0][0]=='X')
  67. {
  68. p111++;
  69. }
  70. if (gameGrid[1][1]=='X')
  71. {
  72. p111++;
  73. }
  74. if (gameGrid[2][2]=='X')
  75. {
  76. p111++;
  77. }
  78. if (p111==3)
  79. {
  80. cout << "Player 1 wins!" << endl;
  81. a++;
  82. b=0;
  83. }
  84.  
  85.  
  86. if (gameGrid[2][0]=='X')
  87. {
  88. p1111++;
  89. }
  90. if (gameGrid[1][1]=='X')
  91. {
  92. p1111++;
  93. }
  94. if (gameGrid[0][2]=='X')
  95. {
  96. p1111++;
  97. }
  98. if (p1111==3)
  99. {
  100. cout << "Player 1 wins!" << endl;
  101. a++;
  102. b=0;
  103. }
  104.  
  105.  
  106.  
  107. if (gameGrid[0][1]=='X' && gameGrid[1][1]=='X' && gameGrid[2][1]=='X')
  108. {
  109. cout << "Player 1 wins!" << endl;
  110. a++;
  111. b=0;
  112. }
  113.  
  114. if (gameGrid[0][2]=='X' && gameGrid[1][2]=='X' && gameGrid[2][2]=='X')
  115. {
  116. cout << "Player 1 wins!" << endl;
  117. a++;
  118. b=0;
  119. }
  120.  
  121. if (gameGrid[1][0]=='X' && gameGrid[1][1]=='X' && gameGrid[1][2]=='X')
  122. {
  123. cout << "Player 1 wins!" << endl;
  124. a++;
  125. b=0;
  126. }
  127.  
  128. if (gameGrid[2][0]=='X' && gameGrid[2][1]=='X' && gameGrid[2][2]=='X')
  129. {
  130. cout << "Player 1 wins!" << endl;
  131. a++;
  132. b=0;
  133. }
  134.  
  135.  
  136.  
  137. /*------------------------------------------------------------*/
  138.  
  139.  
  140. if (gameGrid[0][0]=='O')
  141. {
  142. p2++;
  143. }
  144. if (gameGrid[0][1]=='O')
  145. {
  146. p2++;
  147. }
  148. if (gameGrid[0][2]=='O')
  149. {
  150. p2++;
  151. }
  152. if (p2==3)
  153. {
  154. cout << "Player 2 wins!" << endl;
  155. c++;
  156. b=0;
  157. }
  158.  
  159.  
  160.  
  161. if (gameGrid[0][0]=='O')
  162. {
  163. p22++;
  164. }
  165. if (gameGrid[1][0]=='O')
  166. {
  167. p22++;
  168. }
  169. if (gameGrid[2][0]=='O')
  170. {
  171. p22++;
  172. }
  173. if (p22==3)
  174. {
  175. cout << "Player 2 wins!" << endl;
  176. c++;
  177. b=0;
  178. }
  179.  
  180.  
  181.  
  182. if (gameGrid[0][0]=='O')
  183. {
  184. p222++;
  185. }
  186. if (gameGrid[1][1]=='O')
  187. {
  188. p222++;
  189. }
  190. if (gameGrid[2][2]=='O')
  191. {
  192. p222++;
  193. }
  194. if (p222==3)
  195. {
  196. cout << "Player 2 wins!" << endl;
  197. c++;
  198. b=0;
  199. }
  200.  
  201.  
  202.  
  203.  
  204. if (gameGrid[2][0]=='O')
  205. {
  206. p2222++;
  207. }
  208. if (gameGrid[1][1]=='O')
  209. {
  210. p2222++;
  211. }
  212. if (gameGrid[0][2]=='O')
  213. {
  214. p2222++;
  215. }
  216. if (p2222==3)
  217. {
  218. cout << "Player 2 wins!" << endl;
  219. c++;
  220. b=0;
  221. }
  222.  
  223.  
  224. if (gameGrid[0][1]=='O' && gameGrid[1][1]=='O' && gameGrid[2][1]=='O')
  225. {
  226. cout << "Player 2 wins!" << endl;
  227. c++;
  228. b=0;
  229. }
  230.  
  231. if (gameGrid[0][2]=='O' && gameGrid[1][2]=='O' && gameGrid[2][2]=='O')
  232. {
  233. cout << "Player 2 wins!" << endl;
  234. c++;
  235. b=0;
  236. }
  237.  
  238. if (gameGrid[1][0]=='O' && gameGrid[1][1]=='O' && gameGrid[1][2]=='O')
  239. {
  240. cout << "Player 2 wins!" << endl;
  241. c++;
  242. b=0;
  243. }
  244.  
  245. if (gameGrid[2][0]=='O' && gameGrid[2][1]=='O' && gameGrid[2][2]=='O')
  246. {
  247. cout << "Player 2 wins!" << endl;
  248. c++;
  249. b=0;
  250. }
  251.  
  252. //Thought of grouping the if statements with &&s part way through as you can tell
  253.  
  254. if (a!=0 && c!=0)
  255. {
  256. cout << "Both players technically won, therefore it is a draw." << endl;
  257. cout << "In real tic tac toe this is an impossible scenario." << endl;
  258. }
  259.  
  260. if (a>1)
  261. {
  262. cout << "Player 1 has managed to score multiple times in the same game!" << endl;
  263. }
  264.  
  265. if (c>1)
  266. {
  267. cout << "Player 2 has managed to score multiple times in the same game!" << endl;
  268. }
  269.  
  270. if (b==1)
  271. {
  272. cout << "It is a draw!" << endl;
  273. }
  274.  
  275.  
  276.  
  277. return;
  278. }
  279.  
  280.  
  281. // display the given array
  282. void displayGrid(char game[3][3])
  283. {
  284. cout << game[0][0] << '|' << game[0][1] << '|' << game[0][2] << endl;
  285. cout << "-+-+-" << endl;
  286. cout << game[1][0] << '|' << game[1][1] << '|' << game[1][2] << endl;
  287. cout << "-+-+-" << endl;
  288. cout << game[2][0] << '|' << game[2][1] << '|' << game[2][2] << endl;
  289.  
  290. checkWin(game);
  291. }
  292.  
  293.  
  294.  
  295. int main()
  296. { int stopend;
  297. char gameGrid[3][3] = {{'O','O','O'}, // declare and initialize a 3x3 grid, an arbitrary position
  298. {'O','O','O'}, // you should change this to test your functions
  299. {'O','O','O'}}; // or let the user input some symbols
  300.  
  301. char player1 = 'X';
  302. char player2 = 'O';
  303.  
  304. cout << "The position is: " << endl;
  305. displayGrid(gameGrid);
  306.  
  307. // use your function checkWin() here
  308. // tell the user if it is a win or not
  309. // if it is a win, say who won!
  310. // test with other posiible game positions
  311.  
  312. cin >> stopend;
  313. // "stopend" is so the console doesn't immediately close after the program is finished.
  314.  
  315. return 0;
  316. }
  317.  
  318.  
  319. // end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement