#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define ROCK 3
#define SCISSORS 4
#define PAPER 5
#define ERROR 8
#define END 6
#define LOOP 7
#define WIN 2
#define LOSE 1
#define DRAW 0
void judge(char you, char opp)
{
int jg = (you - opp + 3) % 3;
switch(jg){
case WIN:
puts("\nYou win!");
break;
case LOSE:
puts("\nYou lose!");
break;
case DRAW:
puts("\nDraw.");
break;
}
}
void opponent(int *opp)
{
*opp = (int)((rand() % 3) + 3);
switch(*opp){
case ROCK:
puts("Opponent chose ROCK.");
break;
case PAPER:
puts("Opponent chose PAPER.");
break;
case SCISSORS:
puts("Opponent chose SCISSORS.");
break;
}
}
int janken(void)
{
int you, opp, yn;
puts("\nChoose.");
printf("ROCK : %d, SCISSORS : %d, PAPER : %d, END : %d\n", ROCK, SCISSORS, PAPER, END);
printf("INPUT = ");
fflush(stdin);
scanf("%d", &you);
switch(you){
case ROCK:
puts("\nYou chose ROCK.");
break;
case PAPER:
puts("\nYou chose PAPER.");
break;
case SCISSORS:
puts("\nYou chose SCISSORS.");
break;
case END:
return END;
default:
puts("\nERROR");
return ERROR;
}
opponent(&opp);
judge(you, opp);
return LOOP;
}
int main(void)
{
int flg;
srand((unsigned)time(NULL));
puts("Let's play janken.");
do{
flg = janken();
}while(flg != END);
puts("\nPress any key to quit the program.");
getch();
return 0;
}