This is what I have as a start to my R/P/S game. The only parameters are:You must use the rand() function and a switch statement in the random selection of the computer’s action.
If you could just point me in the right direction, that would be helpful.
// This program plays you in a game of Rock/Paper/Scissors
#include <iostream>
#include <cstdlib>
#include <limits>
#include <ctime>
usingnamespace std;
#define win Congratulations, you won!
#define loose Sorry, you lost. Please try again.
int main()
{
int choice;
int i;
int comp;
char res;
unsigned seed;
cout << "Rock/Paper/scissors is a simple game. The rules are: \n Rock beats scissors. \n Scissors beats Paper. \n Paper beats rock.\n";
//The choices
cout << "Game Choices.\n\n";
cout << "1. Rock\n";
cout << "2. Paper\n";
cout << "3. Scissors\n";
cout << "4. Quit, exits the game.\n\n";
cout << "Please enter your choice.";
cin >> choice;
//Now for the IF/Else statements.
if (choice == 1) //Rock
{
cout << "You picked Rock.\n";
cout << "Now here was my choice.\n";
}
elseif (choice == 2) //Paper
{
cout << "You picked Paper.\n";
cout << "Now here was my choice.\n";
}
elseif (choice == 3) //Scissors
{
cout << "You picked Scissors.\n";
cout << "Now here was my choice.\n";
}
elseif ( choice == 4)
{
return 0;
}
//*************************************************
seed = time(0);
srand(seed); //For the random generator.
comp = rand() % 3 + 1; //Computers pick.
if (comp==1) //Computer rock
{
res = 1;
cout << "Rock!\n";
}
elseif (comp==2) //Computer paper
{
res = 2;
cout << "Paper!\n";
}
elseif (comp==3) // Computer scissors
{
res = 3
cout << "Scissors!\n";
}
switch (res)
{
case'1': cout << "test";
case'2': cout << "tests";
case'3': cout << "test";
}
system("pause");
return 0;
}
This is what I have now, I think I did something wrong with the random function, could some one explain?
Also, I now need it to display how many wins/losses/ties and how many rounds I played. This is what I have so far, I need help getting the switch function for the WIN/Lose/Tie, the counter, and getting it to keep playing until you choose the quit option.
Your rand statements generate numbers 1-3, inclusively. When the player chooses rock, paper, or scissors, you have a chain of if statements to determine whether the player chose rock, paper, or scissors. Just move the code for the computer's choice out of the statements, and do the same thing as you did for the player.
I've played around with rock paper scissors concepts in C++ earlier on. A good way to compare values and determine wins,losses or ties is to use logical combinations in your if statements, so that they check for all wins, or losses, and are not dependent upon an individual choice, or require multiple if statements to account for multiple possibilities.
1 2 3 4 5 6
//scans for all winning combinations
if ((choice==1 && comp==3 ) || (choice==2 && comp==1) || (choice==3 && comp==2))
{
cout<<"You win!";
wins++;
}
You can use a similar statement with the numeric values reversed to account for losses, and a simple (choice==comp) based if statement for ties. If the game is embedded in a loop, so that multiple games can be played, you can also use increments to count the number of wins,losses or ties (such as the wins++ in this example).