Page 1 of 1

Rock Paper Scissors / Multi OS Console With Colors!

#1 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5316
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Posted 14 March 2009 - 08:54 PM

Rock Paper Scissors : by #2pencil (03/15/09)

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!

Posted Image

Posted Image

#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();
}



Posted Image

This post has been edited by no2pencil: 29 September 2010 - 11:11 PM
Reason for edit:: Added image


Is This A Good Question/Topic? 1
  • +

Replies To: Rock Paper Scissors / Multi OS Console

#2 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 27
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Posted 14 March 2009 - 09:00 PM

Very impressive! I never would have guessed someone to make a game with such simplicity and put so much thought behind it.

Way to go :^:
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Posted 16 March 2009 - 04:42 AM

I kinda hate you now. You made my tutorial pretty much superfluous. See, this is why I don't bother making tutorials.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5316
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Posted 16 March 2009 - 06:53 AM

They are for different languages.
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Posted 16 March 2009 - 07:18 AM

That still doesn't mean that I don't wanna kick you in the nuts for it :P
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5316
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Posted 16 March 2009 - 08:28 PM

So you are going to kick yourself in the chin? Because that's the last place that I remember putting them.

Thanks for being professional, btw.
Was This Post Helpful? 0
  • +
  • -

#7 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 27
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Posted 16 March 2009 - 11:13 PM

View Postno2pencil, on 16 Mar, 2009 - 07:28 PM, said:

So you are going to kick yourself in the chin? Because that's the last place that I remember putting them.

Thanks for being professional, btw.


Best comeback I have ever read on the net.
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2675
  • View blog
  • Posts: 15,719
  • Joined: 24-September 08

Posted 18 March 2009 - 07:09 AM

Lol.

And no problem. Well, my work here is done. *flys away superhero style*
Was This Post Helpful? 0
  • +
  • -

#9 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Posted 27 March 2009 - 05:59 AM

View Postcoden4fun, on 17 Mar, 2009 - 12:13 AM, said:

View Postno2pencil, on 16 Mar, 2009 - 07:28 PM, said:

So you are going to kick yourself in the chin? Because that's the last place that I remember putting them.

Thanks for being professional, btw.


Best comeback I have ever read on the net.


I agree. Well thought, and hilarious. As for regarding the tutorial. Nice tutorial, I actually learned more about changing the console color than RPS (I already knew how to do it), but regardless it was nice and simply laid out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1