我有一个问题,使游戏的石头,布,剪刀在代码块(c语言)

rryofs0p  于 2023-02-15  发布在  其他
关注(0)|答案(2)|浏览(183)

除了while循环外,代码中的所有内容都可以正常工作,我不知道如何修复它(代码有点乱):

float paper1 = 1, rock1 = 3, scissors1 = 2, paper2 = -1, rock2 = -3,
      scissors2 = -2, life1 = 3, life2 = 3, number1, number2, result;
printf("first player, choose a number\n");
scanf("%f", &number1);
if (number1 == 3) {
    printf("you choose rock\n");
} else if (number1 == 2) {
    printf("you choose scissors\n");
} else if (number1 == 1) {
    printf("you choose paper\n");
} else {
    printf("you choose the wrong number\n");
}
printf("second player, choose a number\n");
scanf("%f", &number2);
if (number2 == -3) {
    printf("you choose rock\n");
} else if (number2 == -2) {
    printf("you choose scissors\n");
} else if (number2 == -1) {
    printf("you choose paper\n");
} else {
    printf("you choose the wrong number\n");
}
if (number1 == number2) {
    life2 = -1;
    life1 = -1;
} else if (number1 > 3) {
    life2 = -1;
    life1 = -1;
} else if (number2 < -3) {
    life2 = -1;
    life1 = -1;
} else if (number1 < 0) {
    life2 = -1;
    life1 = -1;
} else if (number2 > 0) {
    life2 = -1;
    life1 = -1;
} else if (number1 / number2 == -1) {
    life2 = -1;
    life1 = -1;
} else {
    life1 = 3;
    life2 = 3;
}
result = scissors2 - paper1 + 10;
result = rock2 - scissors1 + 10;
result = paper2 - rock1 + 12;
result = scissors1 - paper2 + 10;
result = rock1 - scissors2 + 10;
result = paper1 - rock2 + 15;
while (life1 > 0, life2 > 0) {
    if (result == 7) {
        life1 = life1 - 1;
        printf("second player won a round\n");
    } else if (result == 6) {
        life1 = life1 - 1;
        printf("second player won a round\n");
    } else if (result == 8) {
        life1 = life1 - 1;
        printf("second player won a round\n");
    } else if (result == 13) {
        life2 = life2 - 1;
        printf("first player won a round\n");
    } else if (result == 15) {
        life2 = life2 - 1;
        printf("first player won a round\n");
    } else if (result == 18) {
        life2 = life2 - 1;
        printf("first player won a round\n");
    }
}
if (life1 == 0) {
    printf("player 1 lost\n");
} else if (life2 == 0) {
    printf("player 2 lost\n");
} else {
    printf("error\n");
}
if (life1 == -1 && life2 == -1) {
    printf("program not working\n");
}

我试着为while循环设置一个条件,它从life1或life2中减去1,然后重复相同的过程,直到它达到0,如果life1达到0,第一个玩家1输了,同样的道理也适用于第二个玩家life2

m1m5dgzv

m1m5dgzv1#

我建议您将处理一轮游戏的代码放到它自己的函数中。我们将该函数称为do_game_round。根据这一轮的结果,该函数应该返回一个enum,其值为

  • ROUND_RESULT_PLAYER1_WINS
  • ROUND_RESULT_PLAYER2_WINS
  • ROUND_RESULT_TIE.

调用此函数的代码可能如下所示:

#include <stdio.h>
#include <stdlib.h>

enum round_result
{
    ROUND_RESULT_PLAYER1_WINS,
    ROUND_RESULT_PLAYER2_WINS,
    ROUND_RESULT_TIE
};

enum round_result do_game_round( void )
{
    //In this function, you should put all the code which handles
    //one game round. For simplicity, I am using the following
    //placeholer code which assumes that player 1 always wins.
    printf( "Performing game round, which Player 1 wins.\n");
    return ROUND_RESULT_PLAYER1_WINS;
}

int main( void )
{
    int life1 = 3, life2 = 3;

    for (;;) //infinite loop, equivalent to while(1)
    {
        switch ( do_game_round() )
        {
            case ROUND_RESULT_PLAYER1_WINS:
                life2--;
                if ( life2 == 0 )
                {
                    printf( "Player 2 is out of lives, Player 1 wins the entire game!\n" );
                    return EXIT_SUCCESS;
                }
                else
                {
                    printf( "Player 2 now has %d lives.\n", life2 );
                }
                break;
            case ROUND_RESULT_PLAYER2_WINS:
                life1--;
                if ( life1 == 0 )
                {
                    printf( "Player 1 is out of lives, Player 2 wins the entire game!\n" );
                    return EXIT_SUCCESS;
                }
                else
                {
                    printf( "Player 1 now has %d lives.\n", life1 );
                }
                break;
            case ROUND_RESULT_TIE:
                //do nothing
                break;
            default:
                fprintf( stderr, "Unexpected return value!\n" );
                exit( EXIT_FAILURE );
        }
    }
}

此演示程序具有以下输出:

Performing game round, which Player 1 wins.
Player 2 now has 2 lives.
Performing game round, which Player 1 wins.
Player 2 now has 1 lives.
Performing game round, which Player 1 wins.
Player 2 is out of lives, Player 1 wins the entire game!
8iwquhpp

8iwquhpp2#

关于while循环的问题,你有一个循环,循环的结果是单轮的,所以它不会迭代多轮,而且测试:

while (life1 > 0, life2 > 0)

语法上有效,但语义上无意义。life2 > 0被求值但被丢弃。您需要&&(布尔AND)运算符,而不是逗号运算符。

while (life1 > 0 && life2 > 0)

不过,首先我建议你修复"* 凌乱 *"代码。凌乱的代码更难调试,其他人更难理解你的意图,而且统计上会包含更多的bug。代码和 * 算法 * 都很混乱--而且非常难理解。老实说,我没有费心去理解它--它比这个非常简单的游戏所需要的要复杂得多。
例如,如果您分别用整数值0、1和2表示石头、布和剪刀,那么单个游戏的结果可以由下式确定:

int winner = ((player1 - player2) + 3) % 3 ;

这是因为:

一个简单的"模3"循环算术关系,其中平局的结果为零,如果玩家1获胜则为1,如果玩家2获胜则为2。
我进一步建议您将玩家选择封装到一个函数中,并对两个玩家使用相同的代码。例如:

int playerSelection()
{
    #define INVALID_SELECTION sizeof(rps)
    static const char rps[] = {'r', 'p', 's'} ;
    int selection = INVALID_SELECTION ;
        
    // While selection is not one of R,P,S,r,p or s...
    while( selection == INVALID_SELECTION )
    {
        printf("Enter R for Rock, P for Paper, or S for Scissors: ");
    
        char ch = 0 ;
        scanf("%c", &ch ) ;
        while( ch != '\n' && getchar() != '\n' ) ;
    
        // Transform input to one of 0,1,2 (for R,P,S respectively)
        for( selection = 0; 
             selection < INVALID_SELECTION && tolower(ch) != rps[selection] ; 
             selection++ )
        {
            // do nothing
        }
    }
    
    return selection ;
}

那么您的玩家选择可能如下所示:

// Get player selections    
    int player1 = playerSelection() ;
    int player2 = playerSelection() ;

现在给定玩家选择和"赢家"的确定,你可以把整个放在一个循环中,在那里你递减 * loosing * 玩家的生命计数,直到一个玩家剩下零生命。例如:

int main()
{
    int player1_lives = 3 ;
    int player2_lives = 3 ;
    int rounds_played = 0 ;
        
    // While both players live...
    while(player1_lives > 0 && player2_lives > 0 )
    {
        // Get player selections  
        printf( "Player 1: " ) ;
        int player1 = playerSelection() ;

        printf( "Player 2: " ) ;
        int player2 = playerSelection() ;

        rounds_played++ ;
        
        // Report player selections in full text
        static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ;
        printf( "Player one played %s\n", play_lookup[player1] ) ;
        printf( "Player two played %s\n", play_lookup[player2] ) ;
    
        // Calculate and report result
        int winner = ((player1 - player2) + 3) % 3 ;
        if( winner == 0 )
        {
            printf( "Round %d was a draw!\n\n", rounds_played ) ;
        }
        else
        {
            printf( "Player %d wins round %d\n\n", winner, rounds_played ) ; 
            if( winner == 1 )
            {
                player2_lives-- ;
            }
            else
            {
                player1_lives-- ;
            }
        }
    }
    
    printf( "Player %d won the game in %d rounds.", player1_lives != 0 ? 1 : 2, 
                                                    rounds_played ) ;
    
    return 0;
}

游戏示例:

Player 1: Enter R for Rock, P for Paper, or S for Scissors: r
Player 2: Enter R for Rock, P for Paper, or S for Scissors: p
Player one played Rock
Player two played Paper
Player 2 wins round 1

Player 1: Enter R for Rock, P for Paper, or S for Scissors: p
Player 2: Enter R for Rock, P for Paper, or S for Scissors: s
Player one played Paper
Player two played Scissors
Player 2 wins round 2

Player 1: Enter R for Rock, P for Paper, or S for Scissors: s
Player 2: Enter R for Rock, P for Paper, or S for Scissors: p
Player one played Scissors
Player two played Paper
Player 1 wins round 3

Player 1: Enter R for Rock, P for Paper, or S for Scissors: s
Player 2: Enter R for Rock, P for Paper, or S for Scissors: s
Player one played Scissors
Player two played Scissors
Round 4 was a draw!

Player 1: Enter R for Rock, P for Paper, or S for Scissors: p
Player 2: Enter R for Rock, P for Paper, or S for Scissors: r
Player one played Paper
Player two played Rock
Player 1 wins round 5

Player 1: Enter R for Rock, P for Paper, or S for Scissors: r
Player 2: Enter R for Rock, P for Paper, or S for Scissors: p
Player one played Rock
Player two played Paper
Player 2 wins round 6

Player 2 won the game in 6 rounds.

把所有这些放在一起:

#include <stdio.h>
#include <ctype.h>

static int playerSelection() ;

int main()
{
    int player1_lives = 3 ;
    int player2_lives = 3 ;
    int rounds_played = 0 ;
        
    // While both players live...
    while(player1_lives > 0 && player2_lives > 0 )
    {
        // Get player selections  
        printf( "Player 1: " ) ;
        int player1 = playerSelection() ;

        printf( "Player 2: " ) ;
        int player2 = playerSelection() ;

        rounds_played++ ;
        
        // Report player selections in full text
        static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ;
        printf( "Player one played %s\n", play_lookup[player1] ) ;
        printf( "Player two played %s\n", play_lookup[player2] ) ;
    
        // Calculate and report result
        int winner = ((player1 - player2) + 3) % 3 ;
        if( winner == 0 )
        {
            printf( "Round %d was a draw!\n\n", rounds_played ) ;
        }
        else
        {
            printf( "Player %d wins round %d\n\n", winner, rounds_played ) ; 
            if( winner == 1 )
            {
                player2_lives-- ;
            }
            else
            {
                player1_lives-- ;
            }
        }
    }
    
    printf( "Player %d won the game in %d rounds.", player1_lives != 0 ? 1 : 2, rounds_played ) ;
    
    return 0;
}

int playerSelection()
{
    #define INVALID_SELECTION sizeof(rps)
    static const char rps[] = {'r', 'p', 's'} ;
    int selection = INVALID_SELECTION ;
        
    // While selection is not one of R,P,S,r,p or s...
    while( selection == INVALID_SELECTION )
    {
        printf("Enter R for Rock, P for Paper, or S for Scissors: ");
    
        char ch = 0 ;
        scanf("%c", &ch ) ;
        while( ch != '\n' && getchar() != '\n' ) ;
    
        // Transform input to one of 0,1,2 (for R,P,S respectively)
        for( selection = 0; 
             selection < INVALID_SELECTION && tolower(ch) != rps[selection] ; 
             selection++ )
        {
            // do nothing
        }
    }
    
    return selection ;
}

改变"赢"的规则,例如"* 最好的5 ,"_第一到10 ",或增加生命的胜利以及减少松散,例如,使用这个通用框架很容易实现,所以如果我误解了预期的" 规则 *",你应该能够适应它,以适应。

相关问题