So I'm trying to code a Rock, Paper and Scissor game (my homework) the problem is that it runs but the output is not what I'm expecting Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main (){
int score1=0, score2=0;
char p1, p2;
printf ("ROCK, PAPER, SCISSOR GAME\n[R] ROCK, [P] PAPER, [S] SCISSOR\n");
while (1){
printf ("\nPlayer 1: ");
p1=getch(); printf ("*");
printf ("\nPlayer 2: ");
p2=getch(); printf ("*");
if ((p1=='R')||(p1=='r')&&(p2=='S')||(p2=='s'))
if ((p1=='P')||(p1=='p')&&(p2=='R')||(p2=='r'))
if ((p1=='S')||(p1=='s')&&(p2=='P')||(p2=='p'))
score1++;
printf ("\nWinner: Player 1\nPlayer 1: %d point%s\tPlayer 2: %d point%s\n", score1, (score1!=1) ? "s" : "", score2, (score2!=1) ? "s" : "");
if ((p1=='S')||(p1=='s')&&(p2=='R')||(p2=='r'))
if ((p1=='R')||(p1=='r')&&(p2=='P')||(p2=='p'))
if ((p1=='P')||(p1=='p')&&(p2=='S')||(p2=='s'))
score2=++;
printf ("\nWinner: Player 2\nPlayer 1: %d point%s\tPlayer 2: %d point%s\n", score1, (score1!=1) ? "s" : "", score2, (score2!=1) ? "s" : "");
if ((p1=='S')||(p1=='s')&&(p2=='S')||(p2=='s'))
if ((p1=='R')||(p1=='r')&&(p2=='R')||(p2=='r'))
if ((p1=='P')||(p1=='p')&&(p2=='R')||(p2=='r'))
printf ("\nWinner: Tie\nPlayer 1: %d point%s\tPlayer 2: %d point%s\n", score1, (score1!=1) ? "s" : "", score2, (score2!=1) ? "s" : "");
if ((score1==5)||(score2==5))
break;
}
return 0;
}
This program should run or loop and breaks if one of them got the score 5. Please correct this for me in C language and explain just a little so that i'll understand what the heck is happening. BTW, im using CodeBlocks.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire