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.