2016-04-27 3 views
0

У меня есть задание, где я должен написать программу для бумажно-ножничного рока.C Программа не выходит?

Вы играете против «ПК», а первый забьет 2 очка. После забивания 2 очков программа должна остановить и распечатать победителя.

Вот мой код:

int main() 
{ 
srand(time(NULL)); 
int dinP = 0; 
int pcP = 0; 

printf("Welcome to rock paper scissor!\n"); 

while(dinP != 2 || pcP != 2){ 

int player, computer, letsgo; 

printf("Press any key to continue.\n"); 
scanf("%d", &letsgo); 

printf("Your turn!\n 0 for Paper\n 1 for Rock\n 2 for Scissor\n"); 
scanf("%d", &player); 

computer = rand() % 3; 
printf("computers picked",computer); 

if (player == 0 && computer == 0 || player == 1 && computer == 1 || player == 2 && computer == 2) 

{ 

// printf("Player picked %d\n", player); 
// printf("Computer picked %d\n", computer); 
    printf("Therefore the result is 0 you ended up equal!\n"); 

} 

if (player == 0 && computer == 1 || player == 1 &&computer == 2 || player == 2 && computer == 0) 

{ 

// printf("player picked %d\n", player); 
// printf("Computer picked %d\n", computer); 
    printf("Player wins!\n 1 point for Player\n"); 
    dinP++; 

} 


if(player == 0 && computer == 2 || player == 1 && computer == 0 || player == 2 && computer == 1) 

{ 

    // printf("player picked %d\n", player); 
// printf("Computer picked %d\n", computer); 
    printf("Computer wins!\n Computer Wins! 1 point for the Computer\n"); 
    pcP++; 

} 

if(player < 0 || player >= 3) 

{ 
    printf("Please enter a valid number\n In other words, pick either rock, paper or scissor.\n"); 
} 

printf("dinP : %d - - - - - pcP : %d", dinP, pcP); 

} 

if(dinP == 2){ 
    printf("You won\n"); 
    // printf("p: %d", dinP); 
}else if(pcP == 2){ 
    printf("Pc Won\n"); 
    // printf("p: %d", pcP); 
} 

return 0; 

}

Забив 2 очка, программа не останавливает и продолжает спрашивать моего входа. Любые предложения, почему это не заканчивается после 2 баллов?

+2

Возможно, вы хотели 'while (dinP! = 2 && pcP! = 2)'? –

+0

while (dinP! = 2 || pcP! = 2) всегда истинно, пока оба dinP и pcP равны 2. Попробуйте while (dinP <2 && pcP <2) – Striker

ответ

4

Подумайте об этой части:

while(dinP != 2 || pcP != 2) 

выражение будет ложным, когда они оба равны 2. Это будет продолжать работать, пока игрок и компьютер оба имеют 2 очка. Чтобы исправить это, просто измените || на &&.

+0

Спасибо! –

Смежные вопросы