Thread: Quick question: rock,paper,scissors

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Quick question: rock,paper,scissors

    Another simple issue, I am sure. The program runs fine but it skips the if statement at the end and just closes when the game is finished. I've been looking at that do/while loop and it seems fine to me, same with the if statement so I can't figure it out.

    here is the code:
    Code:
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    using namespace std;
    
    #define winscore 3
    
    char comppick (void) {
         char option;
         srand ( time (NULL) );
         int value = rand()%3;
         switch (value){
                case 0: option='r';
                break;
                case 1: option='s';
                break;
                case 2: option='p';
                break;
                }
         return option;
         }
    
    int whowins(char a, char b) {
        switch (a) {
               case 'r':
                    if (b=='p') {
                                return 1;
                                }
                    else if (b=='s') {
                                     return 2;
                                     }
                    else {
                         return 0;
                         }
               case 'p':
                    if (b=='s') {
                                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;
        }
    }
    
    int main () {
        char player, comp;
        int winner;
        int comppoints=0;
       int playerpoints=0;
        cout<<"Let's play rock, paper, scissors!\n\n";
        do {
            cout<<"Pick (r) rock, (p) paper, or (s) scissors.\n\n";
            cin>> player;
            cout<<"\n";
            comp=comppick();
            cout<<"I pick: "<<comp;
            cout<<"\n\n";
            winner = whowins (player,comp);
            if (winner==0) {
                            cout<<"It's a tie.";
                            cout<<"\n\n";
                            }
            else if (winner==1) {
                                 cout<<"I win!";
                                 comppoints++;
                                 cout<<"\n\n";
                                 }
            else if (winner==2) {
                                 cout<<"You win, this time."; 
                                 playerpoints++;
                                 cout<<"\n\n";
                                 }
            cout<<"Points: You: "<<playerpoints;
            cout<<" Me: "<<comppoints;
            cout<<"\n\n";
            } while (comppoints<winscore && playerpoints<winscore);
            if (playerpoints<comppoints) {
                                         cout<<"I win the match!";
                                         }
            else {
                 cout<<"You win the match.";
                 }
            cin.get();
        }
    Thanks for all the help so far.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Its probably cause there is a '/n' left in the cin buffer. And that is what the cin.get, gets.

    Throw a cin.ignore in there as well.

    Side note. On your whowins function you could just throw a simple if check in the begining to see if the chars equal and return zero, instead of having that else on every case.
    Woop?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're using srand wrong. You're only supposed to call it once in the whole program.

    Now to demonstrate how unnecessarily long your program is - here is a much shorter way of implementing compick:
    Code:
    char comppick () {
        return "rsp"[rand() % 3];
    }
    The rest could be shortened almost as much too. E.g.
    Code:
    int whowins(char a, char b) {
    	switch ((a<<8) | b)
    	{
    		case ('r'<<8) | 'r': return 0;
    		case ('r'<<8) | 'p': return 1;
    		case ('r'<<8) | 's': return 2;
    		case ('p'<<8) | 'r': return 2;
    		case ('p'<<8) | 'p': return 0;
    		case ('p'<<8) | 's': return 1;
    		case ('s'<<8) | 'r': return 1;
    		case ('s'<<8) | 'p': return 2;
    		case ('s'<<8) | 's': return 0;
    		default: return -1;
    	}
    }
    Of course I forbid you to actually use that code without being able to explain how it works.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM