Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include<bits/stdc++.h>
- #include <mciavi.h>
- #pragma comment(lib, "Winmm.lib")
- #include <mmsystem.h>
- using namespace std;
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- char matrix[2][7] = { {'1','2','3','4','5','6','7'},{'a','b','c','d','e','f'} }; //just for denote each slot
- char arr[7][8]; //the place which players put in
- int counter = 0; //to count the number of terms
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void draw(int player) //function draw the game
- {
- mciSendString("play trance.mp3 repeat", NULL, 0, NULL);//to play the sound file
- system("cls"); //to clear the screen after each term
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); //to make color green
- cout << "\t\tConnect Four" << endl; //name of the game
- cout << endl;
- cout << "Player 1 uses red \tPlayer 2 uses blue" << endl; //players
- cout << endl;
- cout << "insert the number of the column you want to insert your coin in." << endl;
- cout << endl;
- for (int i = 0; i < 7; i++) //draw
- {
- cout << " " << matrix[0][i] << " ";
- }
- cout << endl;
- cout << endl;
- for (int r = 0; r < 6; r++)
- {
- cout << "| | | | | | | |" << endl;
- cout << "|";
- for (int c = 0; c < 7; c++)
- {
- cout << " ";
- if(arr[r][c]=='R') //if player 1 so the color is red
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
- else if(arr[r][c]=='B')//if player 2 so the color is blue
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
- cout << arr[r][c]; //else color still green
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
- cout << " |";
- }
- cout << " " << matrix[1][r] << endl;
- cout << "|_____|_____|_____|_____|_____|_____|_____|" << endl;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int check_win() //function to check who is the winner
- {
- for (int i = 0; i < 6; i++) //diagonal (\)
- {
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- }
- for (int i = 0; i < 6; i++) //diagonal (/)
- {
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- }
- for (int i = 0; i < 6; i++) //Vertical (|)
- {
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- }
- for (int i = 0; i < 6; i++) //horizontal (-)
- {
- for (int j = 0; j < 7; j++)
- {
- 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] != ' ')
- {
- return 1;
- }
- }
- }
- if (counter == 42) //number of all terms
- {
- return 0;
- }
- else
- return -1;
- }
- //return 1 if each of them win
- //return 0 if nobody win
- //else return -1
- //------------------------------------------------------------------------------------------------------------------------------//
- int main()
- {
- int player = 2, check, choice; //define a variable to player //
- char mark;
- for (int i = 0; i < 7; i++) //to make this array empty ' '
- {
- for (int j = 0; j < 8; j++)
- {
- arr[i][j] = ' ';
- }
- }
- do {
- player = (player == 1) ? 2 : 1; //to switch between players
- mark = (player == 1) ? 'R' : 'B'; //put in mark R if player 1 //put in mark B if player 2
- retry: // return here if any player make invalid move
- draw(player); //calling function draw
- cout << "player " << player << " please insert a column :";
- cin >> choice;
- if (choice <= 0 || choice > 7) //if player enter number greater than columns or less
- {
- cout << "Invalid move" << endl;
- cin.ignore(); //waiting
- cin.get();//waiting
- goto retry; //return to retry to enter different column
- }
- for (int h=5; h >= 0; h--)
- {
- if (arr[h][choice-1] == ' ') //check the chosen slot if it empty or not
- {
- arr[h][choice-1] = mark; //make it here if true
- counter++; //count 1
- break; //break and go to the next term
- }
- else if(h==0 && arr[h][choice-1]!=' ') // if it doesn't empty
- {
- cout << "Invalid move" << endl;
- cin.ignore();
- cin.get();
- goto retry; //return to retry to check one more time
- }
- }
- check = check_win(); // give the returned value of function check_win
- } while (check == -1); // stop the game
- draw(player);
- if (check == 1) //check what is returned by function check_win
- {
- mciSendString("stop trance.mp3", NULL, 0, NULL);//to stop the sound file
- mciSendString("play winn.mp3 repeat", NULL, 0, NULL);//to play the other sound file
- cout<<"\n\n";
- cout << "\t\tPlayer " << player << " won *__* \n\n"; // message which is appear when each player won
- cin.ignore(); // for waiting
- cin.get(); // for waiting
- }
- else
- {
- cout << "Game draw ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement