hi everyone...
i know there were a lot of RPS related topics, but non of them seems to be having the problems i'm having. my problems are :
1- the program completely ignores the "cout"s in the PlayersChoice function.
(somebody told me to add cin.ignore for this matter, but the problem is still there.)
2- the program always says that it's a tie!
srand() should only be called once, i.e. put it in main, not in a function that might potentially later be called repetitively. (Just a suggestion.)
You want and's not or's (&& not ||.)
Also, a tie happen whenever x == y; you can check just once instead of three times. Also wins happen whenever x == (y + 1) % 3... think about it! (Look at the way you defined Rock, Paper, and Scissors.)
All you need to do to fix your cout problem is put single quotes around the 'r', 'p' and 's'. This returns the ascii value of that letter. Otherwise you are returning the "variables" r, p, and s, which are declared but not initialized so they are returning garbage.
you don't know how much you've helped me guys... THANK YOU A THOUSAND TIMES! (that might sound gay, but what you did really helped me!)
@mathhead200 : thank you for caring that much... really...
I did what "stewbond" told me to do, and all the program started working perfectly... and later i am going to add a loop which ends the game after one side wins the game three times... do you think what you said : "the srand might potentially later be called repetitively. " would be a problem later on? and also, how would you suggest to end the program that way?? using an "IF" to end it after 3 times or "WHILE"??
@stewbond : thanks for caring man... you were a huge help.
constint WINS = 3;
int myWins = 0,
comWins = 0;
while( myWins < WINS && comWins < WINS ) {
//Let's play a games...
//(Remember to increment either myWins or comWins in here after each game, so the loop ends!)
}
if( myWins > comWins ) //or "if( myWins >= WINS )" works too
//I win!
else
//Aww... computer wins
Something like that. And yes about srand(). You only seed the PRNG once! This is very important as it's one of those logical errors and it can be very hard to track down. Using rand() in a function (over and over) is fine (it's designed to be used tat way in fact,) just remember to use srand() before you invoke that function, and exactly once; (srand() somewhere very near the beginning of the program should be fine for this one.) Same goes for srand() in a loop, no!
//
// main.cpp
// RPS
//
// Created by Amirali Monfared on 11/23/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
usingnamespace std;
void player (int& a)
{
char playerschoice;
cout << "Enter R for Rock, P for Paper and S for Scissors: ";
cin >> playerschoice;
cin.ignore();
if ( playerschoice == 'r' )
{
cout << "You are going with: Rock..." << endl;
a = 0;
}
elseif ( playerschoice == 'p' )
{
cout << "You are going with: Paper..." << endl;
a = 1;
}
elseif ( playerschoice == 's' )
{
cout << "You are going with: Scissors..." << endl;
a = 2;
}
}
void computer (int& b)
{
int ComputersChioce;
srand(time(NULL));
ComputersChioce = rand() % 3;
if ( ComputersChioce == 0 )
{
cout << "And the Computer's Chioce is: Rock! " << endl;
b = 0;
}
elseif ( ComputersChioce == 1 )
{
cout << "And the Computer's Choice is: Paper! " << endl;
b = 1;
}
elseif ( ComputersChioce == 2 )
{
cout << "And the Computer's Choice is: Scissors! " << endl;
b = 2;
}
}
int main ()
{
constint TotalWins = 3;
int PlayerWins = 0;
int ComputerWins = 0;
int x, y;
int i = 0, j = 0;
cout << "Welcome... This is a minimal Rock-Paper-Scissors game." << endl;
cout << "Let the game begin... " << endl;
while (PlayerWins < TotalWins && ComputerWins < TotalWins)
{
player(x);
computer(y);
if (x == 0 && y == 0)
{
cout << "It's a TIE...";
cout << endl;
cout << endl;
}
elseif (x == 0 && y == 1)
{
cout << "You LOSE...";
ComputerWins ++;
cout << endl;
cout << endl;
j++;
}
elseif (x == 0 && y == 2)
{
cout << "You WIN!";
PlayerWins ++;
cout << endl;
cout << endl;
i++;
}
elseif (x == 1 && y == 0)
{
cout << "You WIN!";
PlayerWins ++;
cout << endl;
cout << endl;
i++;
}
elseif (x == 1 && y == 1)
{
cout << "It's a TIE...";
cout << endl;
cout << endl;
}
elseif (x == 1 && y == 2)
{
cout << "You LOSE...";
ComputerWins ++;
cout << endl;
cout << endl;
j++;
}
elseif (x == 2 && y == 0)
{
cout << "You LOSE...";
ComputerWins ++;
cout << endl;
cout << endl;
j++;
}
elseif (x == 2 && y == 1)
{
cout << "You WIN!";
PlayerWins ++;
cout << endl;
cout << endl;
i++;
}
elseif (x == 2 && y == 2)
{
cout << "It's a TIE...";
cout << endl;
cout << endl;
}
if (PlayerWins == 3)
cout << "You Beat The Computer... That Was Unlikely." << endl;
else
cout << "Computer Won... Haha... " << endl;
}
}
i know there are a lot of newbie problems... but at least, it works... thank you guys. any suggestions for polishing it?? i really appreciate what you guys did...
Put a single condition for TIE! TIE occurs when x==y... So no need for 3 different if loops... Also, make it idiot proof... Like, what if the player enters a letter other than r, p or s? or what if he's got CAPSLOCK on? Put the conditions as
1 2 3 4
if (playerschoice=='r'||playerschoice=='R')
{
//Blah blah
}
...and so on. Also put a check for a letter input other than r, p or s, and assign it a default value. Like, keep a default choice as a random choice b/w r, p or s. So even if the user presses some other letter or number, your program won't crash...
thank you so much caprico... i changed the TIE logic...
i was playing around with the program, and then i sadly opened a can of worms...
the problem is i wanted to give the player the choice to continue the game if they want to... but i don't know where exactly to put the while loop... i put it at the beginning of the main (), and it didn't work... i also tried the end... didn't work either... any idea?
Simpler would be to put a do at the beginning, at line 98, and then before you end the do, ask for user choice to continue, close do, and then put while condition (ch=='y' || ch=='Y');... :)