AceInfinity
Emeritus, Contributor
Code:
[NO-PARSE]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int getmove(const size_t len)
{
char *user = malloc(sizeof(char)*len);
printf("Choose:\n\t0 - Rock\n\t1 - Paper\n\t2 - Scissors\nInput: ");
if (fgets(user, len, stdin) != NULL)
{
while ((user[0] < '0' || user[0] > '2') || strlen(user) != 2)
{
printf("Invalid choice, try again: ");
fgets(user, len, stdin);
}
int n = user[0] - '0';
free(user);
return n;
} else return 0;
}
int main()
{
char ch;
do
{
int choice = getmove(256);
printf("\nYou chose: %d\n", choice);
srand(time(NULL));
char cpu = rand() % 3;
if (cpu)
{
bool b = cpu & 1;
printf("You %s\n", b ? "Win!" : "Lose...");
cpu = b ? (choice == 0 ? 2 : choice - 1) : (choice +1) % 3;
}
else
{
printf("Draw.\n");
cpu = choice;
}
printf("Computer chose: %d\n\n", cpu);
printf("Press [Q] to quit, otherwise any other key to continue...\n");
} while ((ch = getchar()) != 'Q' && ch != 'q');
return 0;
}
[/NO-PARSE]
Here's an implementation of this game I wrote in C. BTW, I see a "C++" thread prefix, what about "C" or any of the others? :S
:thumbsup2:
Last edited: