snake game Version-2 (using random number)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char comp_choice()
{
int i = 1, num, gnum;
srand(time(0));
num = rand() % 100 + 1;
if (num <33)
{
return 'w';
}
else if (num<66)
{
return 'g';
}
else if(num<100)
{
return 's';
}
}
int snakegame(char you, char comp)
{
if (comp == you)
{
return 0;
}
if (comp == 'w' && you == 's')
{
return 1;
}
else if (comp == 's' && you == 'w')
{
return -1;
}
if (comp == 'w' && you == 'g')
{
return -1;
}
else if (comp == 'g' && you == 'w')
{
return 1;
}
if (comp == 'g' && you == 's')
{
return -1;
}
else if (comp == 's' && you == 'g')
{
return 1;
}
}
void result(int value)
{
if (value == 0)
{
printf("Game draw!");
}
else if (value == 1)
{
printf("You win!");
}
else
{
printf("You Lose!");
}
}
int main()
{
char you, comp;
comp = comp_choice();
int value;
printf("Enter 'w' for water,'s' for snake and 'g' for gun:\n");
scanf("%c", &you);
value = snakegame(you, comp);
printf("Computer chose %c and you chose %c: So ", comp, you);
result(value);
return 0;
}
Comments
Post a Comment