Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include<bits/stdc++.h>
  4. #include <mciavi.h>
  5. #pragma comment(lib, "Winmm.lib")
  6. #include <mmsystem.h>
  7. using namespace std;
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. char matrix[2][7] = { {'1','2','3','4','5','6','7'},{'a','b','c','d','e','f'} }; //just for denote each slot
  10. char arr[7][8]; //the place which players put in
  11. int counter = 0; //to count the number of terms
  12.  
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14.  
  15. void draw(int player) //function draw the game
  16. {
  17. mciSendString("play trance.mp3 repeat", NULL, 0, NULL);//to play the sound file
  18.  
  19. system("cls"); //to clear the screen after each term
  20.  
  21. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); //to make color green
  22. cout << "\t\tConnect Four" << endl; //name of the game
  23. cout << endl;
  24. cout << "Player 1 uses red \tPlayer 2 uses blue" << endl; //players
  25. cout << endl;
  26. cout << "insert the number of the column you want to insert your coin in." << endl;
  27. cout << endl;
  28. for (int i = 0; i < 7; i++) //draw
  29. {
  30. cout << " " << matrix[0][i] << " ";
  31. }
  32. cout << endl;
  33. cout << endl;
  34. for (int r = 0; r < 6; r++)
  35. {
  36. cout << "| | | | | | | |" << endl;
  37. cout << "|";
  38. for (int c = 0; c < 7; c++)
  39. {
  40. cout << " ";
  41. if(arr[r][c]=='R') //if player 1 so the color is red
  42. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
  43. else if(arr[r][c]=='B')//if player 2 so the color is blue
  44. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  45. cout << arr[r][c]; //else color still green
  46. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  47. cout << " |";
  48. }
  49. cout << " " << matrix[1][r] << endl;
  50. cout << "|_____|_____|_____|_____|_____|_____|_____|" << endl;
  51. }
  52. }
  53. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. int check_win() //function to check who is the winner
  55. {
  56. for (int i = 0; i < 6; i++) //diagonal (\)
  57. {
  58. for (int j = 0; j < 7; j++)
  59. {
  60. if (arr[i][j] == arr[i + 1][j + 1] && arr[i][j] == arr[i + 2][j + 2] && arr[i][j] == arr[i + 3][j + 3] && arr[i][j] != ' ')
  61. {
  62. return 1;
  63. }
  64. }
  65. for (int j = 0; j < 7; j++)
  66. {
  67. if (arr[i][j] == arr[i - 1][j - 1] && arr[i][j] == arr[i - 2][j - 2] && arr[i][j] == arr[i - 3][j - 3] && arr[i][j] != ' ')
  68. {
  69. return 1;
  70. }
  71. }
  72. }
  73. for (int i = 0; i < 6; i++) //diagonal (/)
  74. {
  75. for (int j = 0; j < 7; j++)
  76. {
  77. if (arr[i][j] == arr[i + 1][j - 1] && arr[i][j] == arr[i + 2][j - 2] && arr[i][j] == arr[i + 3][j - 3] && arr[i][j] != ' ')
  78. {
  79. return 1;
  80. }
  81. }
  82. for (int j = 0; j < 7; j++)
  83. {
  84. if (arr[i][j] == arr[i - 1][j + 1] && arr[i][j] == arr[i - 2][j + 2] && arr[i][j] == arr[i - 3][j + 3] && arr[i][j] != ' ')
  85. {
  86. return 1;
  87. }
  88. }
  89. }
  90. for (int i = 0; i < 6; i++) //Vertical (|)
  91. {
  92. for (int j = 0; j < 7; j++)
  93. {
  94. if (arr[i][j] == arr[i + 1][j] && arr[i][j] == arr[i + 2][j] && arr[i][j] == arr[i+3][j] && arr[i][j] != ' ')
  95. {
  96. return 1;
  97. }
  98. }
  99. }
  100. for (int i = 0; i < 6; i++) //horizontal (-)
  101. {
  102. for (int j = 0; j < 7; j++)
  103. {
  104. if (arr[i][j] == arr[i][j + 1] && arr[i][j] == arr[i][j + 2] && arr[i][j] == arr[i][j + 3] && arr[i][j] != ' ')
  105. {
  106. return 1;
  107. }
  108. }
  109. }
  110. if (counter == 42) //number of all terms
  111. {
  112. return 0;
  113. }
  114. else
  115. return -1;
  116. }
  117. //return 1 if each of them win
  118. //return 0 if nobody win
  119. //else return -1
  120. //------------------------------------------------------------------------------------------------------------------------------//
  121. int main()
  122. {
  123. int player = 2, check, choice; //define a variable to player //
  124. char mark;
  125. for (int i = 0; i < 7; i++) //to make this array empty ' '
  126. {
  127. for (int j = 0; j < 8; j++)
  128. {
  129. arr[i][j] = ' ';
  130. }
  131. }
  132. do {
  133. player = (player == 1) ? 2 : 1; //to switch between players
  134. mark = (player == 1) ? 'R' : 'B'; //put in mark R if player 1 //put in mark B if player 2
  135. retry: // return here if any player make invalid move
  136. draw(player); //calling function draw
  137. cout << "player " << player << " please insert a column :";
  138. cin >> choice;
  139. if (choice <= 0 || choice > 7) //if player enter number greater than columns or less
  140. {
  141. cout << "Invalid move" << endl;
  142. cin.ignore(); //waiting
  143. cin.get();//waiting
  144. goto retry; //return to retry to enter different column
  145. }
  146. for (int h=5; h >= 0; h--)
  147. {
  148. if (arr[h][choice-1] == ' ') //check the chosen slot if it empty or not
  149. {
  150. arr[h][choice-1] = mark; //make it here if true
  151. counter++; //count 1
  152. break; //break and go to the next term
  153. }
  154. else if(h==0 && arr[h][choice-1]!=' ') // if it doesn't empty
  155. {
  156. cout << "Invalid move" << endl;
  157. cin.ignore();
  158. cin.get();
  159. goto retry; //return to retry to check one more time
  160. }
  161. }
  162. check = check_win(); // give the returned value of function check_win
  163. } while (check == -1); // stop the game
  164. draw(player);
  165. if (check == 1) //check what is returned by function check_win
  166. {
  167. mciSendString("stop trance.mp3", NULL, 0, NULL);//to stop the sound file
  168. mciSendString("play winn.mp3 repeat", NULL, 0, NULL);//to play the other sound file
  169.  
  170. cout<<"\n\n";
  171. cout << "\t\tPlayer " << player << " won *__* \n\n"; // message which is appear when each player won
  172. cin.ignore(); // for waiting
  173. cin.get(); // for waiting
  174. }
  175. else
  176. {
  177. cout << "Game draw ";
  178. }
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement