1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <stdlib.h>
#include <ctype.h>
/*
DEFINE FUNCTION GetInput( Input Text as string ) returns character
..declare result as character value ' '
..LOOP WHILE ( result is no R S or P )
....output (text) (Rock, Scissors, Paper)
....input result
..END LOOP
..return result
END FUNCTION
*/
char GetInput(const char *szPlayer)
{
char cRet = 0;
while(!(cRet == 'S' || cRet == 'R' || cRet == 'P')){
printf("%s: (R)ock, (S)cissors, (P)aper > ", szPlayer);
fflush(stdin);
scanf("%c", &cRet);
cRet = toupper(cRet);
}
return cRet;
}
int main()
{
const char *msg[] = {
"Paper covers Rock",
"Scissor cuts paper",
"Rock breaks Scissors",
"It's a Draw!"
};
const char *win[] = {
", Player 1 wins!\n",
", Player 2 wins!\n"
};
/* ..declare Player1 as character value ' '
..declare Player2 as character value ' '*/
char cPlayer1 = 0,
cPlayer2 = 0;
/*
MAIN ENTRY POINT
..Player1 = GetInput("Player 1")
..CLEAR SCREEN
..Player2 = GetInput("Player 2")
..CLEAR SCREEN
END MAIN
*/
cPlayer1 = GetInput("Player 1");
system("cls");
cPlayer2 = GetInput("Player 2");
system("cls");
/*
..IF( ( Player1 is Paper ) AND (Player2 is Rock) ) THEN output Paper covers Rock, Player 1 wins!
..ELSE IF( ( Player1 is Rock) AND (Player2 is Scissors) ) THEN output Rock breaks scissors, Player 1 wins!
..ELSE IF( ( Player1 is Scissors) AND (Player2 is Paper) ) THEN output Scissors cuts paper, Player 1 wins!
..ELSE IF( ( Player2 is Paper ) AND (Player1 is Rock) ) THEN output Paper covers Rock, Player 2 wins!
..ELSE IF( ( Player2 is Rock) AND (Player1 is Scissors) ) THEN output Rock breaks scissors, Player 2 wins!
..ELSE IF( ( Player2 is Scissors) AND (Player1 is Paper) ) THEN output Scissors cuts paper, Player 2 wins!
..ELSE output Its a Draw!
*/
if (cPlayer1 == 'P' && cPlayer2 == 'R') printf("%s%s", msg[0], win[0]);
else if (cPlayer1 == 'R' && cPlayer2 == 'S') printf("%s%s", msg[2], win[0]);
else if (cPlayer1 == 'S' && cPlayer2 == 'P') printf("%s%s", msg[1], win[0]);
else if (cPlayer2 == 'P' && cPlayer1 == 'R') printf("%s%s", msg[0], win[1]);
else if (cPlayer2 == 'R' && cPlayer1 == 'S') printf("%s%s", msg[2], win[1]);
else if (cPlayer2 == 'S' && cPlayer1 == 'P') printf("%s%s", msg[1], win[1]);
else printf("%s\n", msg[3]);
system("pause");
return 0;
}
|