All pastes #284940 Raw Edit

Mine

public text v1 · immutable
#284940 ·published 2006-12-19 13:32 UTC
rendered paste body
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

#define WINSCORE 3

// Function PickRandomOption
// * Returns a random character between 's', 'x', and 'p'
char PickRandomOption (void)
{
   char option;
   srand ( time (NULL) );      // (re)initialize random number generator
   int value = rand()%3;       // Generate random number between 0 and 2

   switch (value) {
      case 0: option='r'; break;
      case 1: option='p'; break;
      case 2: option='s'; break;
   }
   return option;
}

// Function WhoWins
// * check which of the characters passed wins.
// return values:
//   0= tie, 1=the first, 2=the second, -1=error
int WhoWins (char a, char b)
{
   switch (a)
   {
      case 'r':
         if (b=='s') return 1;
         else if (b=='p') return 2;
         else return 0;
      case 'p':
         if (b=='p') return 1;
         else if (b=='r') return 2;
         else return 0;
      case 's':
         if (b=='r') return 1;
         else if (b=='p') return 2;
         else return 0;
      default:
         return -1;
   }
   // NOTE: no break instructions were included in this switch statement
   // because a break instruction at the end of a case would never
   // been executed because there would always be a return statement
   // executed before.
   // For the same reason this peculiar function has no explicit ending
   // return statement.
}

main ()
{
   char you, me;
   int mypoints=0;
   int yourpoints=0;
   int winner;
   cout << "Rock papper scissors by Paul KERR\n";
   do {
      //prompt user.
      cout << "\nEnter r, p or s ";
      cout << "(r=Rock, p=Papepr, s=Scissors): ";
      cin >> you;

      //decide computer's option and say it
      me = PickRandomOption();
      cout << "I say: " << me << "\n";

      // check who is the winner
      winner = WhoWins (you,me);

      // show appropiate message:
      if (winner==0) cout << "Tied\n";
      else if (winner==1) { cout << "You win\n"; yourpoints++; }
      else if (winner==2) { cout << "I win\n"; mypoints++; }
      else cout << "Sorry. You entered an Invalid option\n";

      // show current scoreboard.
      cout << "POINTS: You:" << yourpoints;
      cout << " Me:" << mypoints << "\n";

   } while (yourpoints<WINSCORE && mypoints<WINSCORE);

   if (yourpoints>mypoints) cout << "You win the competition!\n";
   else cout << "I win the competition!\n";
   cout << "\n\n"; system("PAUSE"); // Pauses the application so it doesn't automaticaly close!
   return 0;
}