[go: up one dir, main page]

0% found this document useful (0 votes)
11 views3 pages

Connect Four CPP Game

This document contains a C++ implementation of the Connect Four game. It includes functions for printing the game board, dropping pieces into columns, checking for wins, and determining if the game is a draw. The main function manages player turns and game flow until a player wins or the game ends in a draw.

Uploaded by

sanjayragu05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Connect Four CPP Game

This document contains a C++ implementation of the Connect Four game. It includes functions for printing the game board, dropping pieces into columns, checking for wins, and determining if the game is a draw. The main function manages player turns and game flow until a player wins or the game ends in a draw.

Uploaded by

sanjayragu05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Connect Four Game in C++

#include <iostream>
#include <vector>
using namespace std;

const int ROWS = 6;


const int COLS = 7;
vector<vector<char>> board(ROWS, vector<char>(COLS, '.'));

// Function to print colored tokens (X = red, O = yellow)


void printBoard() {
system("clear"); // For Windows use: system("CLS");
cout << "\n CONNECT FOUR \n\n";
for (int i = 0; i < ROWS; i++) {
cout << " | ";
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 'X')
cout << "\033[1;31mX\033[0m "; // Red
else if (board[i][j] == 'O')
cout << "\033[1;33mO\033[0m "; // Yellow
else
cout << ". ";
}
cout << "|\n";
}
cout << " ";
for (int j = 1; j <= COLS; j++)
cout << j << " ";
cout << "\n";
}

// Drop piece into column


bool dropPiece(int col, char symbol) {
if (col < 0 || col >= COLS) return false;
for (int i = ROWS - 1; i >= 0; i--) {
if (board[i][col] == '.') {
board[i][col] = symbol;
return true;
}
}
return false;
}

// Win check logic


bool checkWin(char sym) {
for (int r = 0; r < ROWS; r++)
for (int c = 0; c < COLS - 3; c++)
if (board[r][c] == sym && board[r][c + 1] == sym && board[r][c + 2] == sym &&
board[r][c + 3] == sym)
return true;

for (int r = 0; r < ROWS - 3; r++)


for (int c = 0; c < COLS; c++)
if (board[r][c] == sym && board[r + 1][c] == sym && board[r + 2][c] == sym && board[r
+ 3][c] == sym)
return true;

for (int r = 0; r < ROWS - 3; r++)


for (int c = 0; c < COLS - 3; c++)
if (board[r][c] == sym && board[r + 1][c + 1] == sym && board[r + 2][c + 2] == sym &&
board[r + 3][c + 3] == sym)
return true;

for (int r = 3; r < ROWS; r++)


for (int c = 0; c < COLS - 3; c++)
if (board[r][c] == sym && board[r - 1][c + 1] == sym && board[r - 2][c + 2] == sym &&
board[r - 3][c + 3] == sym)
return true;

return false;
}

bool isDraw() {
for (int j = 0; j < COLS; j++)
if (board[0][j] == '.') return false;
return true;
}

int main() {
char currentPlayer = 'X';
int col;

while (true) {
printBoard();
cout << "\n Player X (Red) Player O (Yellow)\n";
cout << "Player " << currentPlayer << ", choose column (1-7): ";
cin >> col;

if (!cin || col < 1 || col > 7 || !dropPiece(col - 1, currentPlayer)) {


cin.clear(); cin.ignore(10000, '\n');
cout << " Invalid input. Try again!\n";
continue;
}

if (checkWin(currentPlayer)) {
printBoard();
cout << "\n Player " << currentPlayer << " wins! \n";
break;
}

if (isDraw()) {
printBoard();
cout << "\n It's a draw!\n";
break;
}

currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';


}

return 0;
}

You might also like