Created
July 24, 2014 06:28
-
-
Save EvanDarwin/5e37b66a8471982fa975 to your computer and use it in GitHub Desktop.
rock paper scissors game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int roll_hand(); | |
char* get_hand_name(int hand); | |
void print_win(); | |
void print_lose(); | |
int main(int argc, char **argv) | |
{ | |
printf("Welcome to rock, paper, scissors.\n"); | |
printf("\nChoices:\n1) Rock\n2) Paper\n3) Scissors\n\nChoice: "); | |
int user_input; | |
scanf("%d", &user_input); | |
// Roll our own. | |
int ours = roll_hand(); | |
printf("You chose %s\n", get_hand_name(user_input)); | |
printf("We chose %s\n", get_hand_name(ours)); | |
if (user_input == ours) { | |
printf("It's a tie!\n"); | |
return 0; | |
} | |
if (user_input == 1) { | |
// Rock | |
if ( ours == 3) { | |
print_win(); | |
} else { | |
print_lose(); | |
} | |
} else if (user_input == 2) { | |
if ( ours == 1 ) { | |
print_win(); | |
} else { | |
print_lose(); | |
} | |
} else if (user_input == 3) { | |
if ( ours == 2 ) { | |
print_win(); | |
} else { | |
print_lose(); | |
} | |
} | |
return 0; | |
} | |
int roll_hand() | |
{ | |
srand(time(NULL)); | |
return (rand() % 3) + 1; | |
} | |
void print_win() { | |
printf("You won this round.\n"); | |
} | |
void print_lose() { | |
printf("The computer wins!\n"); | |
} | |
char* get_hand_name(int hand) | |
{ | |
if( hand == 1) { | |
return "rock"; | |
} else if (hand == 2) { | |
return "paper"; | |
} else { | |
return "scissors"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment