Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Have the following program written in C, when printed on the screen, make the "X" appear red and the "O" yellow. At the same time, the board must be blue (the game must have colours and not be displayed in white).

#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; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLUMNS; j++) { if (board[i][j] == 'X') { printf("\x1b[31mX\x1b[0m "); // Red text } else if (board[i][j] == 'O') { printf("\x1b[33mO\x1b[0m "); // Yellow text } else { printf("%c ", board[i][j]); } } printf(" "); }

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

More Books

Students also viewed these Databases questions

Question

What changes, if any, are projected for this environment?

Answered: 1 week ago

Question

How have these groups changed within the last three years?

Answered: 1 week ago