Created
July 8, 2019 21:17
-
-
Save khayyamsaleem/fc7eade49e8a8cb6c2bb1499e24f5835 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// width and height of the board | |
const int width = 7, height = 6; | |
//pieces | |
const char EMPTY = '_', X = 'X', O = 'O'; | |
//board is represented with a 2-dimensional char array | |
char board[height][width]; | |
// this function initializes the board to have all empty spots | |
void setup(){ | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
board[i][j] = EMPTY; | |
} | |
} | |
} | |
//this function prints the board in a user-friendly way | |
void printBoard(){ | |
for (int i = 0; i < height; i++) { | |
cout << "|"; | |
for (int j = 0; j < width; j++) { | |
cout << " " << board[i][j] << " " << "|"; | |
} | |
cout << endl; | |
} | |
for (int i = 0; i < width; i++) { | |
cout << " " << i << " "; | |
} | |
cout << endl; | |
cout << endl; | |
} | |
// this function allows a player to add a move to a column | |
void playMove(char piece, int column) { | |
for (int i = 0; i < height; i++) { | |
if (board[i][column] != EMPTY) { | |
if (i == 0) { | |
// if the TOP row of the column that the player chose is NOT EMPTY, | |
// then the column is FULL and the user is not allowed to go there. | |
cout << "cannot move there! Skipping your turn." << endl; | |
return; | |
} else { | |
board[i-1][column] = piece; | |
return; | |
} | |
} else { | |
// if the bottom of the column is empty, then the player is putting the first piece there | |
if (i == height-1) { | |
board[i][column] = piece; | |
return; | |
} | |
} | |
} | |
} | |
//this function checks if the player has won after a move is made | |
bool checkWin(char player) { | |
int win = 0; | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
//checking for horizontal win | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((j+k) < width) { | |
if (board[i][j+k] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
//checking for a vertical win | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height) { | |
if (board[i+k][j] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking up and right | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height && (j+k) < width) { | |
if (board[i+k][j+k] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking up and left | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height && (j-k) >= 0) { | |
if (board[i+k][j-k] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking down and left | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i-k) >= 0 && (j-k) >= 0) { | |
if (board[i-k][j-k] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking down and right | |
win = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i-k) >= 0 && (j+k) < width) { | |
if (board[i-k][j+k] == player) { | |
win++; | |
} | |
} | |
} | |
if (win == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
} | |
} | |
int tieCount = 0; | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
if (board[i][j] != EMPTY) { | |
tieCount++; | |
} | |
} | |
} | |
if (tieCount == width*height) { | |
cout << "Both players tied!" << endl; | |
return true; | |
} | |
return false; | |
} | |
void playGame(){ | |
char player = X; | |
while (!(checkWin(X) || checkWin(O))) { | |
int column; | |
cout << "Player " << player << ", enter a column: "; | |
cin >> column; | |
cout << endl; | |
while(!cin) { | |
// user didn't input a number | |
cin.clear(); // reset failbit | |
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input | |
// next, request user reinput | |
cout << "Invalid input! Player " << player << ", enter a number: "; | |
cin >> column; | |
cout << endl; | |
} | |
if (column >= width) { | |
cout << "Invalid column! Try again." << endl; | |
} else { | |
playMove(player, column); | |
printBoard(); | |
if (player == X) { | |
player = O; | |
} else { | |
player = X; | |
} | |
} | |
} | |
} | |
int main() { | |
// setup | |
cout << "Welcome to Connect 4!" << endl; | |
cout << "Hit ctrl+c at any time to quit the game." << endl; | |
int replay = 0; | |
do { | |
setup(); | |
printBoard(); | |
playGame(); | |
cout << "Enter 0 to quit or 1 to play again: "; | |
cin >> replay; | |
cout << endl; | |
while (!cin || (replay != 1 && replay != 0)) { | |
// user didn't input a number | |
cin.clear(); // reset failbit | |
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input | |
// next, request user reinput | |
cout << "Invalid input! Please enter either 1 or 0: "; | |
cin >> replay; | |
cout << endl; | |
} | |
} while (replay == 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment