Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Andrew Barnes Programming assignment Connect 4
- #include <conio.h>
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- int player = 1;
- int mainMenuInput;
- int movement;
- int gameBoard[7][7] = { 0 };
- bool gameWon = false;
- bool check(int a, int b);
- int Winner;
- //Menu Screen
- void mainMenu()
- {
- system("CLS");
- cout << "--------------------------------------------------------Main Menu-------------------------------------------------------" << endl;
- cout << "1. Play game" << endl;
- cout << "2. Leaderboard" << endl;
- cout << "3. Close application" << endl;
- cin >> mainMenuInput;
- cout << endl;
- //Menu Screen input validation
- if (mainMenuInput <= 0 || mainMenuInput >= 4)
- {
- cout << "That isn't a valid input" << endl;
- cout << "Please type a valid input" << endl;
- cin.clear();
- cin.ignore();
- _getch();
- return mainMenu();
- }
- }
- void boardLayout()
- {
- //Display board
- cout << " 1 2 3 4 5 6 7\n";
- for (int j = 0; j < 7; j++)
- {
- for (int i = 0; i < 7; i++)
- {
- char temp1;
- char temp2 = gameBoard[j][i];
- if (temp2 == 0)
- {
- temp1 = ' ';
- }
- else if (temp2 == 1)
- {
- temp1 = 'X';
- }
- else if (temp2 == 2)
- {
- temp1 = 'O';
- }
- cout << "|";
- cout << "[" << temp1 << "]";
- }
- cout << endl;
- }
- }
- bool check(int a, int b)
- {
- int vertical = 1;//(|)
- int horizontal = 1;//(-)
- int diagonal1 = 1;//(\)
- int diagonal2 = 1;//(/)
- char player = gameBoard[a][b];
- int l;//vertical
- int z;//horizontal
- //vertical checks
- for (l = a + 1; gameBoard[l][b] == player && l <= 6; l++, vertical++);//down
- for (l = a - 1; gameBoard[l][b] == player && l >= 0; l--, vertical++);//up
- if (vertical >= 4)return true;
- //horizontal checks
- for (z = b - 1; gameBoard[a][z] == player && z >= 0; z--, horizontal++);//left
- for (z = b + 1; gameBoard[a][z] == player && z <= 6; z++, horizontal++);//right
- if (horizontal >= 4) return true;
- //diagonal 1 checks (\)
- for (l = a - 1, z = b - 1; gameBoard[l][z] == player && l >= 0 && z >= 0; diagonal1++, l--, z--);//up and left
- for (l = a + 1, z = b + 1; gameBoard[l][z] == player && l <= 6 && z <= 6; diagonal1++, l++, z++);//down and right
- if (diagonal1 >= 4) return true;
- //diagonal 2 checks(/)
- for (l = a - 1, z = b + 1; gameBoard[l][z] == player && l >= 0 && z <= 6; diagonal2++, l--, z++);//up and right
- for (l = a + 1, z = b - 1; gameBoard[l][z] == player && l <= 6 && z >= 0; diagonal2++, l++, z--);//up and left
- if (diagonal2 >= 4) return true;
- return false;
- }
- //The board input
- void boardInput()
- {
- int userInput;
- cout << "Please enter the column you wish to place your counter" << endl;
- cin >> userInput;
- //User input validation
- while (!cin.good() || userInput <= 0 || userInput >= 8)
- {
- cout << "That isn't a valid input" << endl;
- cout << "Please type a valid input" << endl;
- cin.clear();
- cin.ignore();
- cin >> userInput;
- }
- //Player movement always starting with player 1 as X
- int freePlace = 0;
- cout << endl;
- movement = (userInput-1);
- for (int j = 6; j >= 0; j--)
- {
- if (gameBoard[j][movement] == 0)
- {
- gameBoard[j][movement] = player;
- gameWon = check(j, movement);
- if (gameWon == true)
- {
- Winner = player;
- }
- break;
- }
- else {
- freePlace += 1;
- }
- }
- if (freePlace == 7)
- {
- cout << "There is no room in #" << movement << endl;
- boardInput;
- }
- if (player == 1)
- {
- player = 2;
- }
- else
- {
- player = 1;
- }
- }
- int main()
- {
- //Menu Screen
- mainMenu();
- //Menu choices
- switch (mainMenuInput)
- {
- case 1:
- cout << "Play connect 4" << endl;
- cout << endl;
- do {
- boardLayout();
- boardInput();
- } while (!gameWon);
- boardLayout();
- cout << "The winner is Player " << Winner << "!" << endl;
- break;
- case 2:
- cout << "Leaderboard" << endl;
- cout << endl;
- break;
- case 3:
- return 0;
- break;
- default: cout << " " << endl;
- }
- system("CLS");
- main();
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement