Here is an example command line game.
Rock, Paper, Scissors using Enum and Random Numbers.
Both were asked about in the last class.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
enum Signal{Rock,Paper,Scissors};
int win(int a,int b);
int main(void)
{
int i;
int your,mine, yourscore=0,myscore=0;
char *Words[]={"Rock","Paper","Scissors"}; srand(time(NULL));
printf(" To play enter the number 1 - 3 and 4 to quit.\n");
printf("Rock 1, Paper 2, Scissors 3 Quit 4\n\n"); your=getch()-49;
while(your<3)
{ mine=(rand() % 3 );
printf("Your %s\n",(your<3 && your>-1) ? Words[your]: "Fumble");
printf("My move %s\n",(mine<3 && mine>-1) ? Words[mine]:"Fumble");
yourscore+=win(your,mine);
myscore+=win(mine,your);
printf("Your score is %d and my score is %d.\n\n",yourscore,myscore);
your=getch()-49;
}
return 0;
}
int win(int a,int b)
{ if (a==Rock && b==Scissors)
{ printf("Rock wins\n");
return 1;
}
if (a==Paper && b==Rock)
{ printf("Paper wins\n");
return 1;
}
if (a==Scissors && b==Paper)
{ printf("Scissor wins\n");
return 1;
}
return 0;
}