Created
July 8, 2013 05:57
-
-
Save mutoo/5946500 to your computer and use it in GitHub Desktop.
A simple Rock-paper-scissors Game implement with Table-Driven Approach
This file contains hidden or 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> | |
| #include<time.h> | |
| int main(int argc, char **argv) { | |
| /* | |
| U\C R P S | |
| R 1 0 2 | |
| P 2 1 0 | |
| S 0 2 1 | |
| */ | |
| int code[3][3] = { { 1, 0, 2 }, { 2, 1, 0 }, { 0, 2, 1 } }; | |
| char* weapon[3] = {"Rock","Paper","Scissors"}; | |
| char* result[3] = {"You lose!","Draw","You win!"}; | |
| int user,computer; | |
| srand((unsigned)time(NULL)); | |
| printf("Rock-Paper-Scissors Game\n"); | |
| printf("Rule:\n 0: rock\n 1: paper\n 2: scissors\n"); | |
| for(;;){ | |
| printf("Select your weapon: "); | |
| scanf("%d", &user); | |
| switch(user){ | |
| case 0: | |
| case 1: | |
| case 2: | |
| computer = rand()%3; | |
| printf("You choosed %s, while Computer used %s\n", | |
| weapon[user], weapon[computer]); | |
| printf("Result: %s\n", result[code[user][computer]]); | |
| break; | |
| default: | |
| printf("Can't find this weapon! Retry~\n"); | |
| break; | |
| } | |
| } | |
| return 0; | |
| } |
This file contains hidden or 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 <iostream> | |
| #include<string> | |
| #include<ctime> | |
| using namespace std; | |
| int main(int argc, const char * argv[]) | |
| { | |
| /* | |
| U\C R P S | |
| R 1 0 2 | |
| P 2 1 0 | |
| S 0 2 1 | |
| */ | |
| int code[3][3] = { { 1, 0, 2 }, { 2, 1, 0 }, { 0, 2, 1 } }; | |
| string weapon[3] = {"Rock","Paper","Scissors"}; | |
| string result[3] = {"You lose!","Draw","You win!"}; | |
| int user,computer; | |
| srand((unsigned)time(NULL)); | |
| cout<<"Rock-Paper-Scissors Game"<<endl; | |
| cout<<"Rule:"<<endl; | |
| cout<<" 0: rock"<<endl; | |
| cout<<" 1: paper"<<endl; | |
| cout<<" 2: sissors"<<endl; | |
| for(;;){ | |
| cout<<"Select your weapon: "; | |
| cin>>user; | |
| switch(user){ | |
| case 0: | |
| case 1: | |
| case 2: | |
| computer = rand()%3; | |
| cout<<"You choosed "<<weapon[user]<<", while Computer used "<<weapon[computer]<<endl; | |
| cout<<"Result: "<<result[code[user][computer]]<<endl; | |
| break; | |
| default: | |
| cout<<"Can't find this weapon! Retry~"<<endl; | |
| break; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment