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 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 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.
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...
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
C program with functions Make a dice game with the following characteristics: must be two players...
C program with functions Make a dice game with the following characteristics: must be two players with two dice. when player 1 rolls the marker is annotated when player 2 rolls the marker is annotated in other words, for each roll the added scores must be shown. Example: Round 1: player 1 score = 6 player 2 score = 8 Round 2: player 1 score = 14 player 2 score = 20 Whoever reaches 35 points first wins. if a...
Problem 5 In the 1998 video-games Pokémon Red Version and Pokémon Blue Version, the player was...
Problem 5 In the 1998 video-games Pokémon Red Version and Pokémon Blue Version, the player was able to create their own party of one to six total “pocket monsters”. In these games, there were 151 species of Pokémon available for capture, and it was possible to fill a party with multiple monsters of the same species. Determine the following: a) Ignoring order, how many combinations of party Pokémon were possible in these games? b) If a Pokémon party were to...
FOR C++ A friend wants you to start writing a video game. Write a class called...
FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT