Advertisement
Guest User

PUT COCK HERE

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