Question

In: Computer Science

The following is a C program that is a video game version of Connect 4. The...

The following is a C program that is a video game version of Connect 4. The code works fine as is now but I want to change the way you input which column you drop a disk into. Currently, you type in 1 and it goes into column 1. If you type in 6, it goes into column 6 and so on. But I want to make it so you input A or a, then it goes into column 1 and if you input G or g then it goes into column 7. (A-G instead of 1-7 for the columns) Additionally I want to make it so if you press Q or q the game quits and exits. I want to implement this in the whoseTurnIsNext function. Help would be appreciated.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BOARD_ROWS 6
#define BOARD_COLS 7


void displayWorld(char *board);
int whoseTurnIsNext(char *board, int player, const char*);
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);


int main(int argc, char *argv[]){
const char *PIECES = "XO";
char board[BOARD_ROWS * BOARD_COLS];
memset(board, ' ', BOARD_ROWS * BOARD_COLS);

int turn, done = 0;

for(turn = 0; turn < BOARD_ROWS * BOARD_COLS && !done; turn++){
displayWorld(board);
while(!whoseTurnIsNext(board, turn % 2, PIECES)){
displayWorld(board);
puts("**Column full!**\n");
}
done = checkWin(board);
}
displayWorld(board);

if(turn == BOARD_ROWS * BOARD_COLS && !done){
puts("It's a tie!");
} else {
turn--;
printf("Player %d (%c) wins!\n", turn % 2 + 1, PIECES[turn % 2]);
}

return 0;

}
void displayWorld(char *board){
int row, col;

//system("clear");
puts("\n ****Connect Four****\n");
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS; col++){
printf("| %c ", board[BOARD_COLS * row + col]);
}
puts("|");
puts("-----------------------------");

}
puts(" 1 2 3 4 5 6 7\n");

}
int whoseTurnIsNext(char *board, int player, const char *PIECES){
int row, col = 0;
printf("Player %d (%c):\nEnter number coordinate: ", player + 1, PIECES[player]);

while(1){
if(1 != scanf("%d", &col) || col < 1 || col > 7 ){
while(getchar() != '\n');
puts("Number out of bounds! Try again.");
} else {
break;
}
}
col--;

for(row = BOARD_ROWS - 1; row >= 0; row--){
if(board[BOARD_COLS * row + col] == ' '){
board[BOARD_COLS * row + col] = PIECES[player];
return 1;
}
}
return 0;

}
int checkWin(char *board){
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));

}
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');

}
int horizontalCheck(char *board){
int row, col, idx;
const int WIDTH = 1;

for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS - 3; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3)){
return 1;
}
}
}
return 0;

}
int verticalCheck(char *board){
int row, col, idx;
const int HEIGHT = 7;

for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3)){
return 1;
}
}
}
return 0;

}
int diagonalCheck(char *board){
int row, col, idx, count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;

for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if(count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3) || count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3)){
return 1;
}
count++;
}
count = 0;
}
return 0;

}

Solutions

Expert Solution

I have made changes in the code from line 27 to 36 (main function) and from line 74 to 114 (whoseTurnIsNext function). And also added #include <ctype.h>

C code screenshot:

C code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define BOARD_ROWS 6
#define BOARD_COLS 7

void displayWorld(char *board);
int whoseTurnIsNext(char *board, int player, const char *);
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);

int main(int argc, char *argv[])
{
    const char *PIECES = "XO";
    char board[BOARD_ROWS * BOARD_COLS];
    memset(board, ' ', BOARD_ROWS * BOARD_COLS);

    int turn, done = 0, turnNext;

    for (turn = 0; turn < BOARD_ROWS * BOARD_COLS && !done; turn++)
    {
        displayWorld(board);
        turnNext = whoseTurnIsNext(board, turn % 2, PIECES);
        // If user enters q then the value should be -1
        if (turnNext == -1)
        {
            // exit the program
            printf("Quitting the game...\n");
            return 0;
        }

        while (!turnNext)
        {
            displayWorld(board);
            puts("**Column full!**\n");
        }
        done = checkWin(board);
    }
    displayWorld(board);

    if (turn == BOARD_ROWS * BOARD_COLS && !done)
    {
        puts("It's a tie!");
    }
    else
    {
        turn--;
        printf("Player %d (%c) wins!\n", turn % 2 + 1, PIECES[turn % 2]);
    }

    return 0;
}
void displayWorld(char *board)
{
    int row, col;

    //system("clear");
    puts("\n ****Connect Four****\n");
    for (row = 0; row < BOARD_ROWS; row++)
    {
        for (col = 0; col < BOARD_COLS; col++)
        {
            printf("| %c ", board[BOARD_COLS * row + col]);
        }
        puts("|");
        puts("-----------------------------");
    }
    puts(" 1 2 3 4 5 6 7\n");
}
int whoseTurnIsNext(char *board, int player, const char *PIECES)
{
    char ch;
    int row, col = 0;
again:
    printf("Player %d (%c):\nEnter letter coordinate (A-G): ", player + 1, PIECES[player]);

    while (1)
    {
        // get a letter from user and if the letter is Q or present in A to G then break the loop
        // toupper function changes the letter from lower alphabet to upper alphabet
        if (scanf("%c", &ch) == 1 && (toupper(ch) == 'Q' || (toupper(ch) >= 'A' && toupper(ch) <= 'G')))
        {
            // Removing extra characters after user's first character input
            while (getchar() != '\n')
                ;
            break;
        }
        else
        {
            // Removing extra characters after user's first character input
            while (getchar() != '\n')
                ;
            puts("Letter out of bounds! Try again.");
        }
    }
    // If user input is Q then return to main function with -1
    if (toupper(ch) == 'Q')
        return -1;

    col = (int)(toupper(ch) - 'A');

    for (row = BOARD_ROWS - 1; row >= 0; row--)
    {
        if (board[BOARD_COLS * row + col] == ' ')
        {
            board[BOARD_COLS * row + col] = PIECES[player];
            return 1;
        }
    }
    if (row == -1)
    {
        printf("\nSelected board coordinate is already full ! Please select different coordinate !\n");
        goto again;
    }
    return 0;
}
int checkWin(char *board)
{
    return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}
int checkFour(char *board, int a, int b, int c, int d)
{
    return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
int horizontalCheck(char *board)
{
    int row, col, idx;
    const int WIDTH = 1;

    for (row = 0; row < BOARD_ROWS; row++)
    {
        for (col = 0; col < BOARD_COLS - 3; col++)
        {
            idx = BOARD_COLS * row + col;
            if (checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3))
            {
                return 1;
            }
        }
    }
    return 0;
}
int verticalCheck(char *board)
{
    int row, col, idx;
    const int HEIGHT = 7;

    for (row = 0; row < BOARD_ROWS - 3; row++)
    {
        for (col = 0; col < BOARD_COLS; col++)
        {
            idx = BOARD_COLS * row + col;
            if (checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3))
            {
                return 1;
            }
        }
    }
    return 0;
}
int diagonalCheck(char *board)
{
    int row, col, idx, count = 0;
    const int DIAG_RGT = 6, DIAG_LFT = 8;

    for (row = 0; row < BOARD_ROWS - 3; row++)
    {
        for (col = 0; col < BOARD_COLS; col++)
        {
            idx = BOARD_COLS * row + col;
            if (count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3) || count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3))
            {
                return 1;
            }
            count++;
        }
        count = 0;
    }
    return 0;
}

Output 1:

Output 2:


Related Solutions

C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
In C, build a connect 4 game with no GUI. Use a 2D array as the...
In C, build a connect 4 game with no GUI. Use a 2D array as the Data structure. First, build the skeleton of the game. Then, build the game. Some guidelines include... SKELETON: Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a...
Write a LISP program to play either the game "Connect Three" on a size 4x4 game...
Write a LISP program to play either the game "Connect Three" on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (connect-3) The game is single player, human vs the computer AI.
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
Write a simple code for a simple connect the dots game in plain C coding.
Write a simple code for a simple connect the dots game in plain C coding.
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Upsetfowl inC++ knock­off version of the Angry Birds game. The starter program is a working first...
Upsetfowl inC++ knock­off version of the Angry Birds game. The starter program is a working first draft of the game. 1. Correct the first FIXME by moving the intro text to a function named PrintIntro. Development suggestion: Verify the program has the same behavior before continuing. 2. Correct the second FIXME. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel. 3. Correct the third FIXME. Notice that the function LaunchFowl only needs to return the...
4.Consider a modified version of the divide the dollar game in problem (3) in which player...
4.Consider a modified version of the divide the dollar game in problem (3) in which player 2 can make a counteroffer if she does not accept player 1’s offer. After player 2 makes her counteroffer –if she does– player 1 can accept or reject the counteroffer. As before, if there is no agreement after the two rounds of offers, neither player gets anything. If there is an agreement in either round then each player gets the amount agreed to.Represent the...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to parent pointer instead of child pointer, ie. parent of top is null) of Kruskal's Minimum Spanning Tree with adjacency list and min-heap as the additional data structure. Note: Please implement it in C and also keep the above conditions in mind. You can take your time. Thank you.
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT