繁体   English   中英

计算数组中的特定字符,如何在 C 中循环 function?

[英]Counting a specific character inside an array, How to make a loop function in C?

我正在做一个棋盘游戏,它有一个棋子计数器。 但是,我无法弄清楚如何使用 for 循环制作一个,每次一块被另一块(字符)吃掉时递减。 最初红色和蓝色棋子是 12,随着游戏的进行,它们会被吃掉,所以游戏棋子应该更新。 我也不建议使用全局变量,除非用户定义 function。我该怎么办?

#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;
}

`

在你的问题中,你 state 你不想使用全局变量,但你已经在使用全局变量board

我建议您定义一个包含整个游戏 state 的struct ,其中包括棋盘的二维数组以及蓝色和红色棋子的数量。 然后,您可以在 function main中创建此struct的实例,并将指向此struct的指针传递给需要读取或更改游戏 state 的所有函数。这样,您就没有使用全局变量。 通常建议不要使用它们。

这是一个例子:

#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 );
}

该程序具有以下 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 -

如您所见,声明

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

在 function main中成功地使board[2][1]的蓝色棋子吃掉board[5][2]的红色棋子,这导致红色棋子的数量从12减少到11

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM