#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
bool isConsecutiveFour(char grid[][7]) {
// checking rows
for (int i = 0; i < 6; i++)
{
char current = grid[i][0];
int rowCnsec = 0;
for (int j = 0; j < 7; j++)
{
if (current == grid[i][j] && grid[i][j] != ' ')
{
rowCnsec++;
if (rowCnsec == 4) return true;
}
else
{
current = grid[i][j];
rowCnsec = 1;
}
}
}
// check columns
for (int j = 0; j < 7; j++)
{
char current = grid[0][j];
int colConsec = 0;
for (int i = 0; i < 6; i++)
{
if (current == grid[i][j] && grid[i][j] != ' ')
{
colConsec++;
if (colConsec == 4) return true;
}
else
{
current = grid[i][j];
colConsec = 1;
}
}
}
// check top Left side: going upright
for (int i = 5; i >= 0 ; i--)
{
int consecutive = 0;
int y = i;
int x = 0;
char current = grid[y][x];
while (y >= 0)
{
if (grid[y][x] == current && grid[y][x] != ' ')
{
consecutive++;
if (consecutive == 4) return true;
}
else
{
consecutive = 1;
current = grid[y][x];
}
x++;
y--;
}
}
// check bottom right side: going upright
for (int j = 0; j < 7; j++) {
int y = 6 - 1;
int x = j;
int consecutive = 0;
int current = grid[y][x];
while (x < 6 && y >= 0) {
if (grid[y][x] == current && grid[y][x] != ' ') {
consecutive++;
if (consecutive == 4) return true;
}
else {
consecutive = 1;
current = grid[y][x];
}
x++;
y--;
}
}
// check bottom left side going up-left
for (int j = 7 - 1; j > 0; j--) {
int x = j;
int y = 6 - 1;
int current = grid[y][x];
int consecutiveCount = 0;
while (x >= 0 && y >= 0) {
if (grid[y][x] == current && grid[y][x] != ' ') {
consecutiveCount++;
if (consecutiveCount == 4) return true;
}
else {
consecutiveCount = 1;
current = grid[y][x];
}
x--;
y--;
}
}
// check bottom right side going up-left
for (int row = 1; row < 6; row++) {
int x = 7 - 1;
int y = row;
int consecutive = 0;
int current = grid[y][x];
while (y >= 0) {
if (grid[y][x] == current && grid[y][x] != ' ') {
consecutive++;
if (consecutive == 4) return true;
}
else {
consecutive = 1;
current = grid[y][x];
}
x--;
y--;
}
}
return false;
}
// a function to show the grid
void show_grid(char grid[][7])
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
cout << "|" << grid[i][j];
}
cout << "|" << endl;
}
}
void player1(char grid[][7], int x, int y)
{
show_grid(grid);
cout << "Drop a red disk at column (0–6): ";
cin >> y;
if (grid[x][y] == ' ')
grid[x][y] = 'R';
else {
//make sure not to target the same row
while (grid[x][y] != ' ')x--;
grid[x][y] = 'R';
}
}
void player2(char grid[][7],int x,int y)
{
show_grid(grid);
cout << "Drop a yellow disk at column (0–6): ";
cin >> y;
if (grid[x][y] == ' ')
grid[x][y] = 'Y';
else {
//make sure not to target the same row
while (grid[x][y] != ' ')x--;
grid[x][y] = 'Y';
}
}
//check if it's a draw
bool is_draw(char grid[][7])
{
for (int i = 0; i < 7; i++)
{
int count{0};
if (grid[0][i] != ' ' && grid[1][i] != ' ' && grid[2][i] != ' '
&& grid[3][i] != ' ' && grid[4][i] != ' ' && grid[5][i] != ' ')
count++;
if(count == 6)
return true;
return false;
}
}
int main()
{
char grid[6][7];
//fill the grid with blank space
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
grid[i][j] = ' ';
}
}
//to check the winner
bool winner1 = false;
bool winner2 = false;
while (!is_draw(grid))
{
int x{ 5 };
int y{ 0 };
player1(grid, x, y);
if (isConsecutiveFour(grid))
{
winner1 = true;
break;
}
player2(grid, x, y);
if (isConsecutiveFour(grid))
{
winner2 = true;
break;
}
}
show_grid(grid);
if (winner1 == true)
cout << "player 1 wins ";
else if (winner2 == true)
cout << "player 2 wins ";
else
cout << "It's a draw ";
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter