Created
July 8, 2019 17:28
-
-
Save khayyamsaleem/411720ba06234553750804245d0af667 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; | |
const int width = 7, height = 6; | |
const char EMPTY = '_', X = 'X', O = 'O'; | |
char board[height][width]; | |
void setup(){ | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
board[i][j] = EMPTY; | |
} | |
} | |
} | |
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; | |
} | |
void playMove(char piece, int column) { | |
for (int i = 0; i < height; i++) { | |
if (board[i][column] != EMPTY) { | |
if (i == 0) { | |
cout << "cannot move there! Skipping your turn." << endl; | |
return; | |
} else { | |
board[i-1][column] = piece; | |
return; | |
} | |
} else { | |
if (i == height-1) { | |
board[i][column] = piece; | |
return; | |
} | |
} | |
} | |
} | |
bool checkWin(char player) { | |
//checking for horizontal win | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
int horizontalWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((j+k) < width) { | |
if (board[i][j+k] == player) { | |
horizontalWin++; | |
} | |
} | |
} | |
if (horizontalWin == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
} | |
} | |
//checking for vertical win | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
int verticalWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height) { | |
if (board[i+k][j] == player) { | |
verticalWin++; | |
} | |
} | |
} | |
if (verticalWin == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
} | |
} | |
//checking for diag wins | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
// checking up and right | |
int diagWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height && (j+k) < width) { | |
if (board[i+k][j+k] == player) { | |
diagWin++; | |
} | |
} | |
} | |
if (diagWin == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking up and left | |
diagWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i+k) < height && (j-k) >= 0) { | |
if (board[i+k][j-k] == player) { | |
diagWin++; | |
} | |
} | |
} | |
if (diagWin == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking down and left | |
diagWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i-k) >= 0 && (j-k) >= 0) { | |
if (board[i-k][j-k] == player) { | |
diagWin++; | |
} | |
} | |
} | |
if (diagWin == 4) { | |
cout << "Player " << player << " won!" << endl; | |
return true; | |
} | |
// checking down and right | |
diagWin = 0; | |
for (int k = 0; k < 4; k++) { | |
if ((i-k) >= 0 && (j+k) < width) { | |
if (board[i-k][j+k] == player) { | |
diagWin++; | |
} | |
} | |
} | |
if (diagWin == 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 | |
setup(); | |
printBoard(); | |
playGame(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment