Rock, Paper, Sissors Game
Jan 5, 2014 at 6:25am UTC
I am writing a rock, paper, scissors game. The code I have complies but doesn't work very long before. the game quits! I have been trying to debut it but I am not having much luck. If anyone can help me figure out were I went wrong with this please let me know that would be awesome!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
//C++ rock paper sissors game
//by BP Jan 2014
#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
//functions
void stop();
string get_winner(string Player, string cpu);
string get_cpu_move();
string ask_Player( string prompt);
char ask_Text(string promt);
void welcome();
void cls();
//end of functions
int main()
{
welcome();
bool play_game = true ;
while (play_game = true )
{
cls();
cout<<"Pick your move or press 1 to quit!\n" ;
cout<<"====================================\n" ;
cout<<"R.) Rock" <<endl;
cout<<"P.) Paper" <<endl;
cout<<"S.) Scissors" <<endl;
cout<<"1.) Quit\n" <<endl;
cout<<"===================================\n" ;
string player = ask_Player("What is your Choice?: " );
string cpu = get_cpu_move();
string winner = get_winner(player, cpu);
if (player == "1" ){play_game = false ; break ;}
else {
cls();
cout<<"==========Results=================\n" ;
cout<<"your pick was: " <<player<<endl;
cout<<"comp pick was: " <<cpu<<endl;
cout<<"==================================\n\n" ;
cout<<"The winner is: " <<winner<<"\n\n\n\n" ;
stop();
player = " " ;
cpu = " " ;
winner = " " ;
continue ;
}
}
return 0;
}
string get_winner(string Player, string cpu)
{
if (Player == "Paper" && cpu == "Rock" ) {return "Player" ;}
else if (Player == "Scissors" && cpu == "Paper" ) {return "Player" ;}
else if (Player == "Rock" && cpu == "Scissors" ) {return "Player" ;}
else if (Player == "Paper" && cpu == "Scissors" ) {return "CPU" ;}
else if (Player == "Scissors" && cpu == "Rock" ) {return "CPU" ;}
else if (Player == "Rock" && cpu == "Paper" ) {return "CPU" ;}
else if (Player == cpu) {return "Tie" ;}
}
string get_cpu_move()
{
const int NUM = 3;
srand( time(NULL));
int bam = (rand() % NUM);
if (bam == 1) {return "Rock" ;}
else if (bam == 2) {return "Paper" ;}
else if (bam == 3) {return "Scissors" ;}
}
string ask_Player(string prompt)
{
char temp;
cout<<prompt;
cin>>temp;
temp = toupper(temp);
if (temp == 'R' ) {return "Rock" ;}
else if (temp == 'P' ) {return "Paper" ;}
else if (temp == 'S' ) {return "Scissors" ;}
}
char ask_Text(string prompt)
{
char temp;
cout<<prompt;
cin>>temp;
temp = toupper(temp);
if (temp == 'Y' ){return 'Y' ;}
if (temp == 'N' ){return 'N' ;}
else { cls(); cout<<"ivalid response" ; Sleep(1000); cls(); }
}
void welcome()
{
bool go = true ;
do {
cout<<" Welcome to the C++ Game\n\n " ;
cout<<" ( R )ock ( P )aper ( S )cissors " ;
cout<<"\n\n\n" ;
char YesNo = ask_Text("Would you like to play?(Y/N): " );
if (YesNo == 'Y' ){cls(); go = false ; break ;}
if (YesNo == 'N' ){exit(0);}
}while (go = true );
}
void cls ()
{
system("cls" );
}
void stop ()
{
system("pause" );
}
Jan 5, 2014 at 9:20am UTC
int bam = (rand() % NUM);
should be int bam = (rand() % NUM + 1);
Jan 5, 2014 at 10:52am UTC
Topic archived. No new replies allowed.