Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make the following program, when printed on the screen, make the X appear in red and the O in yellow. At the same time, the

Make the following program, when printed on the screen, make the "X" appear in red and the "O" in yellow. At the same time, the board should be blue (the game should have colours and not show white as in the image).

#include

#include

#include

#define ROWS 6

#define COLUMNS 7

#define CONNECT 4

void printBoard(char board[ROWS][COLUMNS]);

int dropPiece(char board[ROWS][COLUMNS], int column, char piece);

int checkWin(char board[ROWS][COLUMNS], char piece);

int main()

{

char board[ROWS][COLUMNS];

int i, j;

char currentPlayer = 'X';

int gameMode;

int column;

int winner = 0;

int draw = 0;

// Initialize the board to all empty spaces

for (i = 0; i < ROWS; i++)

{

for (j = 0; j < COLUMNS; j++)

{

board[i][j] = ' ';

}

}

// Choose who goes first randomly

srand(time(NULL));

if (rand() % 2 == 0)

{

currentPlayer = 'X';

}

else

{

currentPlayer = 'O';

}

printf("Welcome to Connect 4! ");

printf("Enter 1 to play against the computer or 2 to play against another player: ");

scanf("%d", &gameMode);

while (winner == 0 && draw == 0)

{

// Print the board

printBoard(board);

// Get the column choice from the current player

if (currentPlayer == 'X')

{

if (gameMode == 1)

{

// Player 1's turn (against the computer)

printf("Player 1's turn (X). Enter a column number (1-7): ");

scanf("%d", &column);

column--; // Convert the column number to an array index

}

else

{

// Player 1's turn (against another player)

printf("Player 1's turn (X). Enter a column number (1-7): ");

scanf("%d", &column);

column--; // Convert the column number to an array index

}

}

else

{

if (gameMode == 1)

{

// Computer's turn (Player 2)

printf("Computer's turn (O). ");

column = rand() % COLUMNS; // Choose a random column

}

else

{

// Player 2's turn (against another player)

printf("Player 2's turn (O). Enter a column number (1-7): ");

scanf("%d", &column);

column--; // Convert the column number to an array index

}

}

// Drop the piece into the column

if (dropPiece(board, column, currentPlayer) == 0)

{

printf("That column is full. Choose a different column. ");

}

else

{

// Switch players

if (currentPlayer == 'X')

{

currentPlayer = 'O';

}

else

{

currentPlayer = 'X';

}

// Check if the game has been won

winner = checkWin(board, currentPlayer);

if (winner == 1)

{

// The other player won

if (currentPlayer == 'X')

{

printf("Player 2 (O) has won the game! ");

}

else

{

printf("Player 1 (X) has won the game! ");

}

}

else

{

// Check for a draw

draw = 1;

for (i = 0; i < COLUMNS; i++)

{

if (board[0][i] == ' ')

{

// There is an empty space, so the game is not a draw

draw = 0;

break;

}

}

if (draw == 1)

{

printf("The game is a draw. ");

}

}

}

}

// Print the final board

printBoard(board);

return 0;

}

// Prints the current state of the board

void printBoard(char board[ROWS][COLUMNS])

{

int i, j;

printf(" ");

for (i = 0; i < ROWS; i++)

{

for (j = 0; j < COLUMNS; j++)

{

printf("| %c ", board[i][j]);

}

printf("| ");

}

printf("------------------- ");

printf(" 1 2 3 4 5 6 7 ");

printf(" ");

}

// Drops the piece into the specified column and returns 1 if successful, 0 if the column is full

int dropPiece(char board[ROWS][COLUMNS], int column, char piece)

{

int i;

// Check if the column is full

if (board[0][column] != ' ')

{

return 0;

}

// Find the first empty space in the column and place the piece there

for (i = ROWS - 1; i >= 0; i--)

{

if (board[i][column] == ' ')

{

board[i][column] = piece;

return 1;

}

}

return 0;

}

// Checks if the current player has won the game and returns 1 if they have, 0 otherwise

int checkWin(char board[ROWS][COLUMNS], char piece)

{

int i, j;

int consecutiveCount;

// Check for horizontal wins

for (i = 0; i < ROWS; i++)

{

consecutiveCount = 0;

for (j = 0; j < COLUMNS; j++)

{

if (board[i][j] == piece)

{

consecutiveCount++;

if (consecutiveCount == CONNECT)

{

return 1;

}

}

else

{

consecutiveCount = 0;

}

}

}

// Check for vertical wins

for (j = 0; j < COLUMNS; j++)

{

consecutiveCount = 0;

for (i = 0; i < ROWS; i++)

{

if (board[i][j] == piece)

{

consecutiveCount++;

if (consecutiveCount == CONNECT)

{

return 1;

}

}

else

{

consecutiveCount = 0;

}

}

}

// Check for diagonal wins

for (i = 0; i < ROWS - CONNECT + 1; i++)

{

for (j = 0; j < COLUMNS - CONNECT + 1; j++)

{

if (board[i][j] == piece && board[i+1][j+1] == piece && board[i+2][j+2] == piece && board[i+3][j+3] == piece)

{

return 1;

}

}

}

for (i = 0; i < ROWS - CONNECT + 1; i++)

{

for (j = COLUMNS - 1; j >= CONNECT - 1; j--)

{

if (board[i][j] == piece && board[i+1][j-1] == piece && board[i+2][j-2] == piece && board[i+3][j-3] == piece)

{

return 1;

}

}

}

// No win was found

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

Who responds to your customers complaint letters?

Answered: 1 week ago

Question

Under what circumstances do your customers write complaint letters?

Answered: 1 week ago