粘在纸上的剪刀石头上的游戏

问题描述 投票:0回答:3

虽然每次循环重复两次,并且CPU仅在为剪刀选择'S'时才得分。另一个问题是,如何使它变得更高级以及在哪里添加该功能?我必须使用字符“ r”,“ s”和“ p”,而不是1,2,3来接受用户输入。不确定如何解决此问题

#include <stdlib.h> 
#include<time.h> 

int main () {

   char player1;
   int player2;
   int userScore = 0, cpuScore = 0;
   player2 = rand ( ) % 3 + 1;
   srand ((int) time (NULL));

int count = 0;
while(count <= 10) {
  printf("\nEnter p for Paper, r for rock, or S for scissor: ");
  scanf("%c", &player1);
   switch(player1) {
      case 'P' :
       if(player1 == 'P' && player2 == 1) {
         printf("Draw!");
         break;
       } else if(player1 == 'P' && player2 == 2) {
         userScore++;
         printf("User won this one!");
         break;
       } else {
         cpuScore++;
         printf("CPU won this one!");
         break;
       }
      case 'R': 
         if(player1 == 'R' && player2 == 2) {
         printf("Draw!");
         break;
       } else if(player1 == 'R' && player2 == 3) {
         userScore++;
         printf("User won this one!");
         break;
       } else {
         cpuScore++;
         printf("CPU won this one!");
         break;
       }
      case 'S':
        if(player1 == 'S' && player2 == 3) {
         printf("Draw!");
         break;
       } else if(player1 == 'S' && player2 == 1) {
         userScore++;
         break;
         printf("User won this one!");
       } else {
         cpuScore++;
         printf("CPU won this one!");
         break;
       }
      default :
         printf("\nInvalid Input");
         break;
   }
   printf("\nUser Score: %d", userScore);
   printf("\nCPU Score: %d", cpuScore);
   count++;
}
   if(userScore == cpuScore) {
     printf("\nDraw game!");
   } else if(userScore > cpuScore) {
     printf("\nYou win!");
   } else {
     printf("\nCPU wins!");
   }
   return 0;
} ``` 
c if-statement while-loop switch-statement
3个回答
1
投票

尝试一下:

#include <stdio.h>
#include <stdlib.h> 
int main () {
   char player1;
   int player2;
   int userScore = 0, cpuScore = 0;
   srand ((int) time (NULL));
   int count = 0;
   while(count != 10) {
       printf("Enter p for Paper, r for rock, or S for scissor:\n");
       fgets(&player1, 80, stdin);
       player2 = rand ( ) % 4;
       if(player1 == 'P' && player2 == 1) {
         printf("Draw!\n");
       }if(player1 == 'P' && player2 == 2) {
        userScore++;
        printf("User won this one!\n");
       }if(player1 == 'P' && player2 == 3){
         cpuScore++;
         printf("CPU won this one!\n");
       }
       if(player1 == 'R' && player2 == 2) {
         printf("Draw!\n");
       }if(player1 == 'R' && player2 == 3) {
         userScore++;
         printf("User won this one!\n");
       }if(player1 == 'R' && player2 == 1){
         cpuScore++;
         printf("CPU won this one!\n");
       }
       if(player1 == 'S' && player2 == 3) {
         printf("Draw!\n");
       }if(player1 == 'S' && player2 == 1) {
         userScore++;
         printf("User won this one!\n");
       }if(player1 == 'S' && player2 == 2){
         cpuScore++;
         printf("CPU won this one!\n");
       }
       if(player1!='S' || player1!='R' || player1!='P'){
         printf("Invalid Input\n");
       }
    printf("User Score: %d\n", userScore);
    printf("CPU Score: %d\n", cpuScore);
    count++;
}
   if(userScore == cpuScore) {
     printf("Draw game!\n");
   } else if(userScore > cpuScore) {
     printf("You win!\n");
   } else {
     printf("CPU wins!\n");
   }
   return 0;
}

我还应该补充一点,该程序是敏感的。例如,输入“ r”将不起作用,但输入“ R”将起作用。您可以自行决定更改此设置。


1
投票

我建议您修复它,以免使其复杂得多。剪刀石头布的获胜者可以通过算法确定。

比较起来比较简单,使用整数值可以使用更简单的算术解决方案。同样从算术上来说,使用0,1,2而不是1,2,3更简单。因此,首先将用户输入转换为0,1,2:

#define INVALID_SELECTION sizeof(rps)
static const char rps[] = {'r', 'p', 's'} ;
int human = INVALID_SELECTION ;

while( human == INVALID_SELECTION )
{
    printf("\nEnter R for Rock, P for Paper, or S for Scissors: ");

    char ch = 0 ;
    scanf("%c", &ch ) ;
    while( ch != '\n' && getchar() != '\n' ) ;

    for( human = 0; 
         human < INVALID_SELECTION && tolower(ch) != rps[human] ; 
         human++ )
    {
        // do nothing
    }
}

然后应确定计算机的播放方式:

srand( (int)time(NULL) ) ;
int computer = rand() % 3 ;

尽管请注意,您只需要调用一次srand(),所以如果将游戏置于循环中以重复播放,则srand()调用应出现在[[before重复循环中。

您可以这样报告播放情况:

static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ; printf( "Human played %s\n", play_lookup[human] ) ; printf( "Computer played %s\n", play_lookup[computer] ) ;

然后humancomputer在算术上是直接可比的,使得:

int battle = human - computer ; if( battle < 0 ) battle += 3 ; switch( battle ) { case 0 : printf( "Draw!\n" ) ; break ; case 1 : printf( "Human wins!\n" ) ; break ; case 2 : printf( "Computer wins!\n" ) ; break ; }

全部放在一起:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { // Randomize srand( (int)time(NULL) ) ; // Repeat play indefinitely for(;;) { #define INVALID_SELECTION sizeof(rps) static const char rps[] = {'r', 'p', 's'} ; int human = INVALID_SELECTION ; // While human input is not one of R,P,S,r,p or s... while( human == INVALID_SELECTION ) { printf("\nEnter 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( human = 0; human < INVALID_SELECTION && tolower(ch) != rps[human] ; human++ ) { // do nothing } } // Get computer's play int computer = rand() % 3 ; // Report human and computer plays in full text static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ; printf( "Human played %s\n", play_lookup[human] ) ; printf( "Computer played %s\n", play_lookup[computer] ) ; // Calculate and report result int battle = human - computer ; if( battle < 0 ) battle += 3 ; switch( battle ) { case 0 : printf( "Draw!\n" ) ; break ; case 1 : printf( "Human wins!\n" ) ; break ; case 2 : printf( "Computer wins!\n" ) ; break ; } } return 0; }

示例输出:

Enter R for Rock, P for Paper, or S for Scissors: r Human played Rock Computer played Rock Draw! Enter R for Rock, P for Paper, or S for Scissors: r Human played Rock Computer played Scissors Human wins! Enter R for Rock, P for Paper, or S for Scissors: r Human played Rock Computer played Rock Draw! Enter R for Rock, P for Paper, or S for Scissors: r Human played Rock Computer played Scissors Human wins! Enter R for Rock, P for Paper, or S for Scissors: r Human played Rock Computer played Paper Computer wins! Enter R for Rock, P for Paper, or S for Scissors: p Human played Paper Computer played Paper Draw! Enter R for Rock, P for Paper, or S for Scissors: p Human played Paper Computer played Scissors Computer wins! Enter R for Rock, P for Paper, or S for Scissors: p Human played Paper Computer played Rock Human wins! Enter R for Rock, P for Paper, or S for Scissors: s Human played Scissors Computer played Scissors Draw! Enter R for Rock, P for Paper, or S for Scissors: s Human played Scissors Computer played Paper Human wins! Enter R for Rock, P for Paper, or S for Scissors: s Human played Scissors Computer played Paper Human wins! Enter R for Rock, P for Paper, or S for Scissors: s Human played Scissors Computer played Scissors Draw! Enter R for Rock, P for Paper, or S for Scissors: s Human played Scissors Computer played Rock Computer wins! Enter R for Rock, P for Paper, or S for Scissors: R Human played Rock Computer played Scissors Human wins! Enter R for Rock, P for Paper, or S for Scissors: P Human played Paper Computer played Paper Draw! Enter R for Rock, P for Paper, or S for Scissors: S Human played Scissors Computer played Paper Human wins! Enter R for Rock, P for Paper, or S for Scissors: xx Enter R for Rock, P for Paper, or S for Scissors: yy Enter R for Rock, P for Paper, or S for Scissors: zz Enter R for Rock, P for Paper, or S for Scissors:


0
投票
问题是您正在循环外设置player2,而不更新它;它会一直不变。与其将其转换为toupper的中间值,不如将其直接转换为enum的典型情况。而不是将switchif一起使用,这会将您的问题空间缩小到3个值,这很容易放在查找表中。]

#include <stdlib.h> #include <stdio.h> /* `printf` requires this. */ #include <time.h> /* https://en.wikipedia.org/wiki/X_Macro just because I don't want to type. */ #define CHOICE(X) X(ROCK), X(PAPER), X(SCISSORS) #define OUTCOME(X) X(LOSS), X(TIE), X(WIN) #define NAME(A) A #define STR(A) #A enum Choice { CHOICE(NAME) }; static const char *const choice_names[] = { CHOICE(STR) }; enum Outcome { OUTCOME(NAME) }; static const char *const outcome_names[] = { OUTCOME(STR) }; static enum Outcome choice_outcomes[][3] = { { TIE, LOSS, WIN }, { WIN, TIE, LOSS }, { LOSS, WIN, TIE } }; int main(void) { /* <- `void` should be used to prototype. */ char input; enum Choice player1, player2; int score[] = { 0, 0, 0 }; /* <- Replace `cpuScore` and `userScore`. */ int count = 0; srand ((int) time (NULL)); /* <- `srand` should precede `rand`. */ while(count <= 10) { printf("\nEnter p for Paper, r for rock, or S for scissor: "); /* Ignores whitespace, (_viz_ enter); breaks on error, (_eg_ EOF.) */ if(scanf(" %c", &input) != 1) break; /* Convert it to `player1` immediately. */ switch(input) { case 'r': case 'R': player1 = ROCK; break; case 'p': case 'P': player1 = PAPER; break; case 's': case 'S': player1 = SCISSORS; break; default: printf("Unexpected input, '%c'.\n", input); continue; } player2 = rand() % 3; printf("User choses %s _vs_ Cpu choses %s: %s.\n", choice_names[player1], choice_names[player2], outcome_names[choice_outcomes[player1][player2]]); score[choice_outcomes[player1][player2]]++; printf("User Score: %d\n", score[WIN]); printf("CPU Score: %d\n", score[LOSS]); printf("Ties: %d\n", score[TIE]); count++; } /* One of the places was the `scanf` break, so check, (unlikely.) */ if(ferror(stdin)) { perror("stdin"); return 1; } /* Print the winner. */ if(score[WIN] == score[LOSS]) { printf("Draw game!\n"); } else if(score[WIN] > score[LOSS]) { printf("You win!\n"); } else { printf("CPU wins!\n"); } return 0; }

我还将userScorecpuScore放入由enum Outcome索引的数组中。
© www.soinside.com 2019 - 2024. All rights reserved.