first of all, thanks for your help so far i think ive gotten ive figured this out for the most part, I just need help with two things. (ill paste my code below)
Number one, Ive got the intro screen to this paper rock scissors game to work and i think the mechanics of the game (best of three rounds) are worked out, but I cant seem to figure out where to put the CLS system statement, so that only the most recent user input and computer choice are shown. I tried putting it all over and seems whenever i put it ends up cutting out something..im looking to also add a counter that shows the computer and players score.
Number two, I want to have an option that allows the user to play again..do i just do the whole game in a huge loop equal to 0 then when the user inputs yes or no have it switched to 1?...
.any other suggestions on how to improve the game or shorten the code.??
Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define ROCK (0)
#define PAPER (1)
#define SCISSORS (2)
int convert_choice(const char *choice)
{
if (!strcasecmp(choice,"rock")) return ROCK;
if (!strcasecmp(choice,"paper")) return PAPER;
if (!strcasecmp(choice,"scissors")) return SCISSORS;
return -1;
}
const char *print_choice(int choice)
{
//gives word value to the random numbers
static char rock[] = "rock", paper[] = "paper", scissors[] = "scissors", unknown[] = "unknown";
switch (choice){
case 0: return rock;
case 1: return paper;
case 2: return scissors;
default: return unknown;
}
}
int computerchoice(void)
{
return rand()%3; // gives value of either 0,1 or 2
}
void who_wins(int compchoice, int playerchoice)
//Evaluates all the cases
{
switch (compchoice){
case ROCK:
switch (playerchoice){
cout<< " "<<endl;
case SCISSORS:
cout<<endl<<"The computer wins"<<endl;
break;
case PAPER:
break;
cout<< " "<<endl;
case ROCK:
break;
}
break;
cout<< " "<<endl;
case PAPER:
switch (playerchoice){
case ROCK:
break;
cout<< " "<<endl;
case SCISSORS:
break;
cout<< " "<<endl;
case PAPER:
break;
}
break;
case SCISSORS:
switch (playerchoice){
case ROCK:
break;
cout<< " "<<endl;
case PAPER:
break;
cout<< " "<<endl;
case SCISSORS:
break;
}
break;
}
}
int main()
{
char playerchoice[20];
int compchoice;
{
//initialize variables for intro screen
char yesOrno[10] = "";
int checkLoop = 0;
char roundnum[10]="";
int validChoice=0;
char playerPut[10]= "";
char computerPut[10]=" ";
//while loop is set to different values to identify whether to repeat the intro screen again or not
while(checkLoop == 0){
//intro screen
cout << " " << endl;
cout << " Rock Paper Scissors " << endl;
cout << " ____________________________________________________________________________" << endl;
cout << "| This is a game of rock, paper, scissors. You will the best of three rounds |" << endl;
cout << "| against the computer. To pick either rock, paper, or scissors simply | " << endl;
cout << "| type in your choice and hit enter. The computer will randomly pick its | " << endl;
cout << "| choice. At the end of the game type y to play again & n to end the game. | " << endl;
cout << "| Do you understand? please enter y or n | " << endl;
cout << "|____________________________________________________________________________| " << endl;
//user input
cin.getline(yesOrno, 10);
if((strcmp(yesOrno,"n")==0)||(strcmp(yesOrno,"y")==0)) //if the user input is a y or n go into this other loop
{
if(strcmp(yesOrno,"y")==0)
{
checkLoop=1;
system("CLS");
cout<< "Get ready to start the game"<<endl;
}
else //if the user input is no repeat the directions
{
system("CLS");
cout<< "Read the directions again";
checkLoop=0; //makes this loop repeat until y or n entered
}
}
else
{
;
cout<< " Enter a y or n "<<endl; //if the user enters something other than a y or n print this statement
cout<<endl;
cout<<endl;
}}
cout<<"Please type in rock, paper, or scissors"<<endl<<"and then press enter"<<endl;
for(int x=0;x<500;x++){
cin.getline(playerchoice, 9, '\n');
if (convert_choice(playerchoice) != -1){
compchoice = computerchoice();
cout<<"The computer chose "<< print_choice(compchoice)<<endl;
who_wins(compchoice,convert_choice(playerchoice));
} else {
cout << "Key in a valid choice" << endl;
}
}
return 0;
}
}