Advertisement
Guest User

BETA CUCKS ONLY

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. //Andrew Barnes Programming assignment Connect 4
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int player = 1;
  12. int mainMenuInput;
  13. int movement;
  14. int gameBoard[7][7] = { 0 };
  15. bool gameWon = false;
  16. bool check(int a, int b);
  17. int Winner;
  18.  
  19. //Menu Screen
  20. void mainMenu()
  21. {
  22.  
  23. system("CLS");
  24. cout << "--------------------------------------------------------Main Menu-------------------------------------------------------" << endl;
  25. cout << "1. Play game" << endl;
  26. cout << "2. Leaderboard" << endl;
  27. cout << "3. Close application" << endl;
  28. cin >> mainMenuInput;
  29. cout << endl;
  30. //Menu Screen input validation
  31. if (mainMenuInput <= 0 || mainMenuInput >= 4)
  32. {
  33. cout << "That isn't a valid input" << endl;
  34. cout << "Please type a valid input" << endl;
  35. cin.clear();
  36. cin.ignore();
  37. _getch();
  38. return mainMenu();
  39. }
  40. }
  41.  
  42.  
  43. void boardLayout()
  44. {
  45. //Display board
  46.  
  47.  
  48. cout << " 1 2 3 4 5 6 7\n";
  49. for (int j = 0; j < 7; j++)
  50. {
  51.  
  52. for (int i = 0; i < 7; i++)
  53. {
  54. char temp1;
  55. char temp2 = gameBoard[j][i];
  56. if (temp2 == 0)
  57. {
  58. temp1 = ' ';
  59. }
  60. else if (temp2 == 1)
  61. {
  62. temp1 = 'X';
  63. }
  64. else if (temp2 == 2)
  65. {
  66. temp1 = 'O';
  67. }
  68. cout << "|";
  69. cout << "[" << temp1 << "]";
  70.  
  71. }
  72. cout << endl;
  73.  
  74. }
  75. }
  76.  
  77. bool check(int a, int b)
  78. {
  79. int vertical = 1;//(|)
  80. int horizontal = 1;//(-)
  81. int diagonal1 = 1;//(\)
  82. int diagonal2 = 1;//(/)
  83. char player = gameBoard[a][b];
  84. int l;//vertical
  85. int z;//horizontal
  86.  
  87. //vertical checks
  88. for (l = a + 1; gameBoard[l][b] == player && l <= 6; l++, vertical++);//down
  89. for (l = a - 1; gameBoard[l][b] == player && l >= 0; l--, vertical++);//up
  90. if (vertical >= 4)return true;
  91. //horizontal checks
  92. for (z = b - 1; gameBoard[a][z] == player && z >= 0; z--, horizontal++);//left
  93. for (z = b + 1; gameBoard[a][z] == player && z <= 6; z++, horizontal++);//right
  94. if (horizontal >= 4) return true;
  95. //diagonal 1 checks (\)
  96. for (l = a - 1, z = b - 1; gameBoard[l][z] == player && l >= 0 && z >= 0; diagonal1++, l--, z--);//up and left
  97. for (l = a + 1, z = b + 1; gameBoard[l][z] == player && l <= 6 && z <= 6; diagonal1++, l++, z++);//down and right
  98. if (diagonal1 >= 4) return true;
  99. //diagonal 2 checks(/)
  100. for (l = a - 1, z = b + 1; gameBoard[l][z] == player && l >= 0 && z <= 6; diagonal2++, l--, z++);//up and right
  101. for (l = a + 1, z = b - 1; gameBoard[l][z] == player && l <= 6 && z >= 0; diagonal2++, l++, z--);//up and left
  102. if (diagonal2 >= 4) return true;
  103. return false;
  104. }
  105. //The board input
  106.  
  107. void boardInput()
  108. {
  109. int userInput;
  110. cout << "Please enter the column you wish to place your counter" << endl;
  111. cin >> userInput;
  112. //User input validation
  113. while (!cin.good() || userInput <= 0 || userInput >= 8)
  114. {
  115.  
  116. cout << "That isn't a valid input" << endl;
  117. cout << "Please type a valid input" << endl;
  118. cin.clear();
  119. cin.ignore();
  120. cin >> userInput;
  121.  
  122. }
  123. //Player movement always starting with player 1 as X
  124. int freePlace = 0;
  125. cout << endl;
  126. movement = (userInput-1);
  127. for (int j = 6; j >= 0; j--)
  128. {
  129. if (gameBoard[j][movement] == 0)
  130. {
  131. gameBoard[j][movement] = player;
  132. gameWon = check(j, movement);
  133. if (gameWon == true)
  134. {
  135. Winner = player;
  136. }
  137. break;
  138. }
  139. else {
  140. freePlace += 1;
  141.  
  142. }
  143.  
  144. }
  145. if (freePlace == 7)
  146. {
  147. cout << "There is no room in #" << movement << endl;
  148. boardInput;
  149. }
  150. if (player == 1)
  151. {
  152. player = 2;
  153. }
  154. else
  155. {
  156. player = 1;
  157. }
  158.  
  159. }
  160.  
  161. int main()
  162. {
  163. //Menu Screen
  164. mainMenu();
  165.  
  166. //Menu choices
  167. switch (mainMenuInput)
  168. {
  169.  
  170. case 1:
  171. cout << "Play connect 4" << endl;
  172. cout << endl;
  173. do {
  174. boardLayout();
  175. boardInput();
  176. } while (!gameWon);
  177. boardLayout();
  178. cout << "The winner is Player " << Winner << "!" << endl;
  179.  
  180. break;
  181.  
  182. case 2:
  183. cout << "Leaderboard" << endl;
  184. cout << endl;
  185. break;
  186.  
  187. case 3:
  188. return 0;
  189. break;
  190.  
  191. default: cout << " " << endl;
  192.  
  193. }
  194.  
  195. system("CLS");
  196. main();
  197. _getch();
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement