-1

I am working on a board game, which has a game piece counter. However, I cannot figure out how can I make one with for loop, decrementing every time a piece will be eaten by another piece(character). Initially red pieces and blue pieces are 12 and as the game progresses, they will be eaten and so the game piece should be updated. I am also not recommended to use global variables unless user-defined function. What can I do?

#include<stdio.h>
char board[8][8] = {
    {'-', 'B', '-', 'B', '-', 'B', '-', 'B'},
    {'B', '-', 'B', '-', 'B', '-', 'B', '-'},
    {'-', 'B', '-', 'B', '-', 'B', '-', 'B'},
    {' ', '-', ' ', '-', ' ', '-', ' ', '-'},
    {'-', ' ', '-', ' ', '-', ' ', '-', ' '},
    {'R', '-', 'R', '-', 'R', '-', 'R', '-'},
    {'-', 'R', '-', 'R', '-', 'R', '-', 'R'},
    {'R', '-', 'R', '-', 'R', '-', 'R', '-'}};

void 
printBoard()
{
    int i , j , k ; 
    
    printf("\n    "); 
    
    for(i=0;i<8;i++) 
        printf("    %d", i); 
        printf(" \n");
        
    for(k=0;k<8;k++) 
    {
        printf("     ");
        
        for(i=0;i<42;i++)
        { 
            printf("-"); 
        } 
        printf("  \n");
         
        printf("   %d ", k); 
        
        for(j=0;j<8;j++) 
        {
            printf("|| %c ", board[k][j]); 
        }
        printf("|| \n");
    }

    printf("     ");
    
    for(i=0;i<42;i++)
    { 
        printf("-");
    } 
    printf("  \n");
}

int gamePiece(int Player1, int BluePiece, int Player2, int RedPiece)
{
    int i, j;
       
    printf("\n\Game piece counter:");
    
    //for loop
    printf("\nPlayer %d = %d",Player1,BluePiece);
    
    printf("\nPlayer %d = %d",Player2,RedPiece); 
    
    return 0;
}

int main(){
    int Player1, BluePiece=12, Player2, RedPiece=12;
    
    gamePiece(Player1, BluePiece, Player2, RedPiece);
    
    return 0;
}

`

1 Answers1

0

In your question, you state that you don't want to use global variables, but you are already using the global variable board.

I recommend that you define a single struct that contains the entire game state, which includes the 2D array of the board as well as the number of blue and red pieces. You can then create an instance of this struct in the function main and pass a pointer to this struct to all functions that need to read or change the game state. That way, you are using no global variables. It is generally recommended not to use them.

Here is an example:

#include <stdio.h>

struct game_state
{
    char board[8][8];
    int blue_pieces;
    int red_pieces;
};

void eat_piece(
    struct game_state *p_gs,
    int predator_x,
    int predator_y,
    int prey_x,
    int prey_y
)
{
    char (*board)[8] = p_gs->board;
    char predator = board[predator_y][predator_x];
    char prey     = board[prey_y][prey_x];

    //remove the prey from the game and replace with predator
    if ( prey == 'R' )
        p_gs->red_pieces--;
    else if ( prey == 'B' )
        p_gs->blue_pieces--;
    board[prey_y][prey_x] = predator;

    //make the old board position of the predator empty
    board[predator_y][predator_x] = '-';
}

void print_board( struct game_state *p_gs )
{
    char (*board)[8] = p_gs->board;
    
    printf( "\nBoard Content:\n" );
    
    for ( int i = 0; i < 8; i++ )
    {
        for ( int j = 0; j < 8; j++ )
        {
            printf( " %c", board[i][j] );
        }

        printf( "\n" );
    }
}

void print_pieces( struct game_state *p_gs )
{
    printf( "Blue has %d pieces.\n", p_gs->blue_pieces );
    printf(  "Red has %d pieces.\n", p_gs->red_pieces  );
}

int main( void )
{
    //define and initialize the initial game state
    struct game_state gs = {
        {
            { '-', 'B', '-', 'B', '-', 'B', '-', 'B' },
            { 'B', '-', 'B', '-', 'B', '-', 'B', '-' },
            { '-', 'B', '-', 'B', '-', 'B', '-', 'B' },
            { ' ', '-', ' ', '-', ' ', '-', ' ', '-' },
            { '-', ' ', '-', ' ', '-', ' ', '-', ' ' },
            { 'R', '-', 'R', '-', 'R', '-', 'R', '-' },
            { '-', 'R', '-', 'R', '-', 'R', '-', 'R' },
            { 'R', '-', 'R', '-', 'R', '-', 'R', '-' }
        },
        12,
        12
    };
            
    //make the blue piece at board[2][1] eat the red piece at
    //board[5][2]
    eat_piece( &gs, 1, 2, 2, 5 );

    //print the number of pieces
    print_pieces( &gs );

    //print the board content
    print_board( &gs );
}

This program has the following output:

Blue has 12 pieces.
Red has 11 pieces.

Board Content:
 - B - B - B - B
 B - B - B - B -
 - - - B - B - B
   -   -   -   -
 -   -   -   -  
 R - B - R - R -
 - R - R - R - R
 R - R - R - R -

As you can see, the statement

eat_piece( &gs, 1, 2, 2, 5 );

in the function main successfully caused the blue piece at board[2][1] to eat the red piece at board[5][2], which caused the number of red pieces to be reduced from 12 to 11.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • what does *p_gs means and its use to the board? sorry im still having a hard time to understand c :( – Dustin Minrod Dec 04 '22 at 09:04
  • @DustinMinrod: `p_gs` is a pointer to an object of type `struct game_state`. The `*` operator will dereference this pointer. If you don't know what a pointer or a `struct` is, then I suggest that you buy [a good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) (Beginner's level) to learn the basics of C. – Andreas Wenzel Dec 04 '22 at 16:02
  • @DustinMinrod: You are probably also wondering what the line `char (*board)[8] = p_gs->board;` means. This declares a pointer to a sub-array of the 2D array, i.e a pointer to an array of 8 characters, and makes this pointer point to the first sub-array of the 2D array. – Andreas Wenzel Dec 04 '22 at 16:11