2017-12-14 3 views
0

현재 코드를 컴파일 할 때마다 정수가 전달 되더라도 모든 가능한 시나리오가 인쇄됩니다. 이 문제를 해결하는 방법에 대한 도움과 이것이 발생하는 이유는 무엇입니까? 함수에서 문자열을 전달하여 승 조건 만들기

#include <stdio.h> 
void *get_plays(char *player1, char *player2) 
{ 
    printf ("Player 1 chooses to play: "); 
    scanf("%s", player1); 
    printf ("Player 2 chooses to play: "); 
    scanf("%s", player2); 
    return player1; 
    return player2; 
} 
int *check_for_winner(char *player1, char *player2, int condition) 
{ 

    if (player1==player2){ 
     condition = 0; 
    } 
    else if(player1=="rock" && player2 == "scissors"){ 
     condition = 1; 
    } 
    else if(player1=="paper" && player2 == "scissors"){ 
     condition = 2; 
    } 
    else if(player1=="rock" && player2 == "paper"){ 
     condition = 2; 
    } 
    else if(player1=="paper" && player2 == "rock"){ 
     condition = 1; 
    } 
    else if(player1=="scissors" && player2 == "papper"){ 
     condition = 1; 
    } 
    else (player1=="scissors" && player2 == "rock");{ 
     condition = 1; 
    } 
} 
void *print_plays(char *player1, char *player2) 
{ 
    printf("Player 1 has played %s \n",player1); 
    printf("Player 2 has played %s \n",player2); 
} 
void *print_winner(int condition) 
{ 
    if (condition = 0){ 
     printf("It's a tie\n"); 
    } 
    else if (condition = 1){ 
     printf("Player 1 wins\n"); 
    } 
    else (condition = 2);{ 
     printf("Player 2 wins\n"); 
    } 
} 
int main(void) 
{ 
    char player1[10]; 
    char player2[10]; 
    int win; 
    get_plays(player1, player2); 
    check_for_winner(player1, player2, win); 
    print_plays(player1, player2); 
    print_winner(win);  
    return; 
} 

나는 그것이 "플레이어 1 승"과 "플레이어 2는 승리"단서 이유 또는 해결 방법을하거나 발생하더라도, 왜 인쇄 컴파일

. 나는 아직도 C에 상당히 익숙하다. 그래서 어떤 도움도 인정 될 것이다. 고맙습니다.

+1

'void * get_plays (char * player1, char * player2) {... return player1; return player2;}'이것은 당신이 생각하는대로하지 않습니다. 'return player2'에 도달하지 못할 것입니다. –

+1

또한 여기'if (player1 == player2) {'당신이 문자열이 아닌 포인터를 비교하고 있습니다. 이 과정을 밟기 전에'c'를 더 이해해야합니다. 'c'를 추측하지 말고, 책에서 배워라. –

+0

'else if (player1 == "rock"&& player2 == "scissors") {'는 위와 같은 시나리오입니다. –

답변

0
#include <stdio.h> 
#include <string.h> 

void 
get_plays (char *choice1, char *choice2) 
{ 
    printf ("Choices: 'paper' - 'rock' - 'scissors'\n"); 
    printf ("Player 1 chooses to play: "); 
    scanf ("%s", choice1); 

    printf ("Player 2 chooses to play: "); 
    scanf ("%s", choice2); 
} 

int 
check_for_winner (const char *choice1, const char *choice2) 
{ 
    // paper wins rock but scissors does not and equals itself 
    if (strcmp (choice1, "paper") == 0) 
    { 
     if (strcmp (choice2, "rock") == 0) 
      return 1; 

     if (strcmp (choice2, "scissors") == 0) 
      return 2; 

     return 0; 
    } 

    // rock wins scissors but paper does not and equals itself 
    if (strcmp (choice1, "rock") == 0) 
    { 
     if (strcmp (choice2, "scissors") == 0) 
      return 1; 

     if (strcmp (choice2, "paper") == 0) 
      return 2; 

     return 0; 
    } 

    // scissors wins paper but rock does not and equals itself 
    if (strcmp (choice1, "scissors") == 0) 
    { 
     if (strcmp (choice2, "paper") == 0) 
      return 1; 

     if (strcmp (choice2, "rock") == 0) 
      return 2; 

     return 0; 
    } 

    return 0; 
} 

int main() 
{ 
    char choice1 [10]; 
    char choice2 [10]; 
    int winner; 
    char q; 

    char *result[3] = { 
     "No one is the winner", 
     "Player 1 is the winner!!!", 
     "Player 2 is the winner!!!" 
    }; 

    printf ("Choose 'q' to exit\n"); 
    printf ("------------------------------------------------------------\n"); 
    while (q != 'q') 
    { 
     get_plays (&choice1[0], &choice2[0]); 
     winner = check_for_winner (choice1, choice2); 

     printf ("------------------------------------------------------------\n"); 
     printf ("The result: %s\n", result[winner]); 
     printf ("------------------------------------------------------------\n"); 
     getc (stdin); 
     scanf ("%c", &q); 
    } 
    return 0; 
} 

여기 코드가 도움이되기를 바랍니다.