In this tutorial, I will walk you through my code for the game of rock paper scissors. The tutorial assumes that you are going to compile & run this with gcc, on the Linux command terminal. This code has been tested with Slackware & Mint.
1st, we have our header file. In the header file I like to create two simple variables. The version number, & the string version number. These are used for output only, & are my own personal preference.
Since we are using a custom header file, I went ahead & defined all of the ascii values here. Though I only use a few, you can feel free to get creative in the main body of the code.
#define VER ".01" #define STR_VER "Rock Paper Scissors : Version .01" #define RESET 0 #define BRIGHT 1 #define DIM 2 #define UNDERLINE 3 #define BLINK 4 #define REVERSE 7 #define HIDDEN 8 #define BLACK 0 #define RED 1 #define GREEN 2 #define YELLOW 3 #define BLUE 4 #define MAGENTA 5 #define CYAN 6 #define WHITE 7
Now then, with the ground work out of the way, we setup our includes.
#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include "rps.h"
First is stdio.h, required as always
The ctype.h is required for some of the input translation. Ascii to integer, lower case to upper case, & so on.
Ahh, the always important time.h! This is required to randomize the timer, for our random seed.
Wrapping things up are types.h, used for the color console settings, & our own header file, created above.
int getrand(int min,int max); void textcolor(int attr, int fg, int bg); void menu(void); void rock(void), paper(void), scissor(void);
Now we define our functions. My favorite, get rand. It's nice for customizing the rand() function, & not having garbage code all over. This is, again, just my personal preference.
The function textcolor is used similarly to getrand(). It's simply for customizing & making simple usage out of the base functionality.
Obviously, looping through the code we'll throw up the menu quite a lot. Might as well make a function out of it
Lastly, rock paper scissors... these just throw out the ascii code for the words rock, paper, & scissor.
/* Global Variables */ int go=0;
Our one & only global value.
We shouldn't ever get to the point where go doesn't equal zero. But users always find a way to break everything.
int main(void) {
#ifdef WINDOWS
system("CLS");
#elif defined(WIN32)
system("CLS");
#elif defined(linux)
system("clear");
#elif defined(__CYGWIN32)
system("clear");
#else
system("clear");
#endif
//menu();
rock();
// Put the colors back
textcolor(BRIGHT, WHITE, BLACK);
return 0;
}
Lets figure out how we can ask the OS to clear the screen for us. Then call function rock(), which calls paper(), which calls scissor().
void menu(void) {
char buff1[32];
char buff2[32];
char ch;
int yc=0; // Your choice
int cc=0; // Computers choice
int err=0; // Error
int win;
Now we're getting into some code!
We've got two 32 bit buffers. I always like to overdue things.
The ch character variable will be used to gather the users input.
The integers are comments, but I'll go ahead & spell it out here. yc is Your Choice, & cc is the Computer's Choice.
We go ahead & set error to zero, as there have not been any errors yet
Lastly, our variable win is used in determining the winner.
srand(time(NULL)); cc=getrand(1,4);
Randomize the timer... & have the computer take it's first pick.
if(go==0) {
textcolor(REVERSE, MAGENTA, BLACK);
printf("%s\n\n",STR_VER);
textcolor(BRIGHT, BLACK, CYAN);
printf("Pick one...\n\n");
printf("A.) Rock\n");
printf("B.) Paper\n");
printf("C.) Scissor\n");
}
scanf("%c", &ch);
ch=toupper(ch);
Here we see if we are playing, by checking go for zero. As long as we are playing, then go ahead & prompt the user for their choice. Lastly, we alter it to uppercase to assure we are all on the same page. Never trust the end user to read or follow instructions.
if(go==0) {
printf("Your choice:\n");
switch(ch) {
case 'A':
printf("Rock....\n");
sprintf(buff1,"Rock");
yc=1;
break;
case 'B':
printf("Paper....\n");
sprintf(buff1,"Paper");
yc=2;
break;
case 'C':
printf("Scissors...\n");
sprintf(buff1,"Scissors");
yc=3;
break;
default: // We shouldn't get this far,
// with proper error checking!
printf("You didn't type in 1 or 2!\n");
break;
}
This should be rather self explanatory. As long as we are still playing (again, by checking go to zero) then determine what the user entered, & fill yc with the appropriate value. As well, if we got some garbage value, let the user know they didn't follow the directions correctly by using the default switch case option.
printf("\nComputers choice:\n");
switch(cc) {
case 1:
printf("Rock....\n");
sprintf(buff2,"Rock");
break;
case 2:
printf("Paper....\n");
sprintf(buff2,"Paper");
break;
case 3:
printf("Scissors...\n");
sprintf(buff2,"Scissors");
break;
default: // We shouldn't get this far,
// with proper error checking!
printf("You didn't type in 1 or 2!\n");
break;
}
Here we do the exact same thing for the computers choice. Thus there should never be an error. But remember, coding isn't about functionality, it's about trapping errors
if(yc!=cc) {
if(yc==1) {
if(cc==2) {
printf("%s covers %s!\n",buff2,buff1);
win=1;
}
if(cc==3) {
printf("%s smashes %s!\n",buff1,buff2);
win=0;
}
}
if(yc==2) {
if(cc==1) {
printf("%s covers %s!\n",buff1,buff2);
win=0;
}
if(cc==3) {
printf("%s cut %s!\n",buff2,buff1);
win=1;
}
}
if(yc==3) {
if(cc==1) {
printf("%s smashes %s!\n",buff2,buff1);
win=1;
}
if(cc==2) {
printf("%s cuts %s!\n",buff1,buff2);
win=0;
}
}
if(win==0) {
textcolor(BRIGHT, BLACK, GREEN);
printf("You Win!\n\n");
}
else {
textcolor(BRIGHT, BLACK, RED);
printf("You Loose!\n\n");
}
}
else {
printf("%s ties with %s!\n\n",buff1,buff2);
}
textcolor(BRIGHT, BLACK, WHITE);
printf("Thank you for playing...\n\n");
}
else {
ch='\n';
//printf("I didn't attempt to go because 'go' equals %d\n\n",go);
}
Determine who won, & then rub it in if necessary.
if(go==0) go++; else go=0; menu(); }
We finally get to change the value of go, because we arn't playing anymore. That is, until we prompt them with the menu, & they continue to play.
int getrand(int min,int max){
return(rand()%(max-min)+min);
}
This is the getrand() function I was talking about earlier. It takes in the minimum & maximum values as arguments, & returns a random value between the two.
void textcolor(int attr, int fg, int bg) {
char command[13];
/* Command is the control command to the terminal */
sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
printf("%s", command);
}
& here we set the color options for the output to the terminal.
void rock(void) {
textcolor(REVERSE, MAGENTA, BLACK);
printf(" ____ ____ ____ _ __\n");
printf(" / __\\/ _ \\/ _X |/ /\n");
printf(" | \\/|| / \\|| / | / \n");
printf(" | /| \\_/|| \\_| \\ \n");
printf(" \\_/\\_\\\\____/\\____X_|\\_\\\n\n");
paper();
}
void paper(void) {
printf(" ____ ____ ____ _____ ____ \n");
printf(" / __\\/ _ \\/ __\\/ __// __\\\n");
printf(" | \\/|| / \\|| \\/|| \\ | \\/|\n");
printf(" | __/| |-||| __/| /_ | /\n");
printf(" \\_/ \\_/ \\|\\_/ \\____\\\\_/\\_\\ \n\n");
scissor();
}
void scissor(void) {
printf(" ____ ____ _ ____ ____ ____ ____ \n");
printf(" / ___\\/ _X \\/ ___\\/ ___\\/ _ \\/ __\\\n");
printf(" | \\| / | || \\| \\| / \\|| \\/|\n");
printf(" \\___ || \\_| |\\___ |\\___ || \\_/|| /\n");
printf(" \\____/\\____X_/\\____/\\____/\\____/\\_/\\_\\\n");
printf("\n--#2pencil--\n\n");
menu();
}
Last but not least, we make the ascii all pretty like. Feel free to change this however you see fit!
& before I share the uncut code with you, here are two example screen shots. Enjoy!
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include "rps.h"
int getrand(int min,int max);
void textcolor(int attr, int fg, int bg);
void menu(void);
void rock(void), paper(void), scissor(void);
/* Global Variables */
int go=0;
int main(void) {
#ifdef WINDOWS
system("CLS");
#elif defined(WIN32)
system("CLS");
#elif defined(linux)
system("clear");
#elif defined(__CYGWIN32)
system("clear");
#else
system("clear");
#endif
//menu();
rock();
// Put the colors back
textcolor(BRIGHT, WHITE, BLACK);
return 0;
}
void menu(void) {
char buff1[32];
char buff2[32];
char ch;
int yc=0; // Your choice
int cc=0; // Computers choice
int err=0; // Error
int magic;
int win;
srand(time(NULL));
cc=getrand(1,4);
if(go==0) {
textcolor(REVERSE, MAGENTA, BLACK);
printf("%s\n\n",STR_VER);
textcolor(BRIGHT, BLACK, CYAN);
printf("Pick one...\n\n");
printf("A.) Rock\n");
printf("B.) Paper\n");
printf("C.) Scissor\n");
}
scanf("%c", &ch);
ch=toupper(ch);
if(go==0) {
printf("Your choice:\n");
switch(ch) {
case 'A':
printf("Rock....\n");
sprintf(buff1,"Rock");
yc=1;
break;
case 'B':
printf("Paper....\n");
sprintf(buff1,"Paper");
yc=2;
break;
case 'C':
printf("Scissors...\n");
sprintf(buff1,"Scissors");
yc=3;
break;
default: // We shouldn't get this far,
// with proper error checking!
printf("You didn't type in 1 or 2!\n");
break;
}
printf("\nComputers choice:\n");
switch(cc) {
case 1:
printf("Rock....\n");
sprintf(buff2,"Rock");
break;
case 2:
printf("Paper....\n");
sprintf(buff2,"Paper");
break;
case 3:
printf("Scissors...\n");
sprintf(buff2,"Scissors");
break;
default: // We shouldn't get this far,
// with proper error checking!
printf("You didn't type in 1 or 2!\n");
break;
}
if(yc!=cc) {
if(yc==1) {
if(cc==2) {
printf("%s covers %s!\n",buff2,buff1);
win=1;
}
if(cc==3) {
printf("%s smashes %s!\n",buff1,buff2);
win=0;
}
}
if(yc==2) {
if(cc==1) {
printf("%s covers %s!\n",buff1,buff2);
win=0;
}
if(cc==3) {
printf("%s cut %s!\n",buff2,buff1);
win=1;
}
}
if(yc==3) {
if(cc==1) {
printf("%s smashes %s!\n",buff2,buff1);
win=1;
}
if(cc==2) {
printf("%s cuts %s!\n",buff1,buff2);
win=0;
}
}
if(win==0) {
textcolor(BRIGHT, BLACK, GREEN);
printf("You Win!\n\n");
}
else {
textcolor(BRIGHT, BLACK, RED);
printf("You Loose!\n\n");
}
}
else {
printf("%s ties with %s!\n\n",buff1,buff2);
}
textcolor(BRIGHT, BLACK, WHITE);
printf("Thank you for playing...\n\n");
}
else {
ch='\n';
//printf("I didn't attempt to go because 'go' equals %d\n\n",go);
}
if(go==0) go++;
else go=0;
menu();
}
int getrand(int min,int max){
return(rand()%(max-min)+min);
}
void textcolor(int attr, int fg, int bg) {
char command[13];
/* Command is the control command to the terminal */
sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
printf("%s", command);
}
void rock(void) {
textcolor(REVERSE, MAGENTA, BLACK);
printf(" ____ ____ ____ _ __\n");
printf(" / __\\/ _ \\/ _X |/ /\n");
printf(" | \\/|| / \\|| / | / \n");
printf(" | /| \\_/|| \\_| \\ \n");
printf(" \\_/\\_\\\\____/\\____X_|\\_\\\n\n");
paper();
}
void paper(void) {
printf(" ____ ____ ____ _____ ____ \n");
printf(" / __\\/ _ \\/ __\\/ __// __\\\n");
printf(" | \\/|| / \\|| \\/|| \\ | \\/|\n");
printf(" | __/| |-||| __/| /_ | /\n");
printf(" \\_/ \\_/ \\|\\_/ \\____\\\\_/\\_\\ \n\n");
scissor();
}
void scissor(void) {
printf(" ____ ____ _ ____ ____ ____ ____ \n");
printf(" / ___\\/ _X \\/ ___\\/ ___\\/ _ \\/ __\\\n");
printf(" | \\| / | || \\| \\| / \\|| \\/|\n");
printf(" \\___ || \\_| |\\___ |\\___ || \\_/|| /\n");
printf(" \\____/\\____X_/\\____/\\____/\\____/\\_/\\_\\\n");
printf("\n--#2pencil--\n\n");
menu();
}
This post has been edited by no2pencil: 29 September 2010 - 11:11 PM
Reason for edit:: Added image




MultiQuote










|