Rock, Paper, Scissors, Lizard, Spock completed but need help closing program

[/code] I've been taking this C++ course, I like it; however, there are still some things that I can't figure out. I created this game for an assignment. Rock, Paper, Scissors, Lizard, Spock; the game runs perfectly and built to my professors specs except for one thing. The problem is, the game is suppose to be played once and then the program closes (ends) by itself unless there is a tie, then you get to try again until you win/lose. I was using exit() but it would close out the program even it their was a tie. My game menu only has an exit button just so I can work with the code when I was making it, but I must delete it for my finished code. Could really use the help or some insight, please! :)

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROCK		= 1;
const int PAPER		= 2;
const int SCISSORS	= 3;
const int LIZARD    = 4; 
const int SPOCK     = 5;
const int Exit		= 6;
int getUserChoice();
int getComputerChoice();
void determineWinner(int, int);
void displayChoice(int); 

int main()
{
	int userChoice;		
	int computerChoice;	

	
	computerChoice = getComputerChoice();
	userChoice = getUserChoice();

	while (userChoice != 6)
	{
		determineWinner(userChoice, computerChoice);
		computerChoice = getComputerChoice();
		userChoice = getUserChoice();
	}

	return 0;
}

int getComputerChoice()
{
    unsigned seed = time(0);
    srand(seed);

    int number = 1 + rand() % 5;

    return number;
}

int getUserChoice()
{
    int choice;	

	cout << "================================\n";
        cout << "Rock, Paper, Scissors and Beyond\n";
       cout << "================================\n";
        cout << "1) Rock\n";
        cout << "2) Paper\n";
        cout << "3) Scissors\n";
	cout << "4) Lizard\n";
	cout << "5) Spock\n";
	cout << "6) Exit\n";
    cout << "Enter your choice: ";
    cin >> choice;

    while (choice < 1 || choice > 6)
    {
        cout << "Enter a number form 1-6 ";
        cin >> choice;
    }

    return choice;
}

void determineWinner(int user, int computer)
{
	
	cout << "You selected: ";
	displayChoice(user);
	cout << "The computer selected: ";
	displayChoice(computer);
    
	if (user == ROCK)
	{
		if (computer == SCISSORS)
			cout << "** YOU win! Rock breaks Scissors. **\n" <<endl;
		else if (computer == PAPER)
			cout << "** Computer wins! Paper covers Rock. **\n" <<endl;
		else if (computer == LIZARD)
			cout << "** You win! Rock crushes Lizard. **\n" <<endl;
		else if (computer == SPOCK)
			cout << "** Computer wins! Spock vaporizes Rock. **\n" <<endl;
	}
	else if (user == PAPER)
	{
		if (computer == SCISSORS)
			cout << "** Computer wins! Scissors cut Paper. **\n" <<endl;
		else if (computer == ROCK)
			cout << "** YOU win! Paper covers Rock. **\n" <<endl;
		else if (computer == LIZARD)
			cout << "** Computer wins! Lizard eats Paper. **\n" <<endl;
		else if (computer == SPOCK)
			cout << "** You win! Paper disproves Spock. **\n" <<endl;
	}
	else if (user == SCISSORS)
	{
		if (computer == ROCK)
			cout << "** Computer win! Rock smashes Scissors. **\n" <<endl;
		else if (computer == PAPER)
			cout << "** You win! Scissor cuts Paper. **\n" <<endl;
		else if (computer == LIZARD)
			cout << "** You win! Scissors decapitate Lizard. **\n" <<endl;
		else if (computer == SPOCK)
			cout << "** Computer wins! Spock melts Scissors. **\n" <<endl;
	}
	else if (user == LIZARD)
	{
		if (computer == SCISSORS)
			cout << "** Computer wins! Scissors decapitate Lizard. **\n" <<endl;
		else if (computer == ROCK)
			cout << "** Computer wins! Rock crushes Lizard. **\n" <<endl;
		else if (computer == PAPER)
			cout << "** You win! Lizard eats Paper. **\n" <<endl;
		else if (computer == SPOCK)
			cout << "** You win! Lizard poisons Spock. **\n" <<endl;
	}
	else if (user == SPOCK)
	{
		if (computer == ROCK)
			cout << "** You win! Spock vaporizes Rock.**\n" <<endl;
		else if (computer == PAPER)
			cout << "** Computer wins! Paper disproves Spock. **\n" <<endl;
		else if (computer == LIZARD)
			cout << "** Computer wins! Lizard poisons Spock. **\n" <<endl;
		else if (computer == SCISSORS)
			cout << "** You win! Spock melts Scissors. **\n" <<endl;
	}
	
	{
    if (user == ROCK && computer == ROCK)
			cout << "** Game is a tie!! Go again **\n" <<endl;
	else if (user == PAPER && computer == PAPER)
			cout << "** Game is a tie!! Go again **\n" <<endl;
	else if (user == SCISSORS && computer == SCISSORS)
			cout << "** Game is a tie!! Go again **\n" <<endl;
	else if (user == LIZARD && computer == LIZARD)
			cout << "** Game is a tie!! Go again **\n" <<endl;
	else if (user == SPOCK && computer == SPOCK)
			cout << "** Game is a tie!! Go again **\n" <<endl;
	}
		
}

void displayChoice(int choice)
{
    if (choice == ROCK)
        cout << "Rock\n";
    else if (choice == PAPER)
        cout << "Paper\n";
    else if (choice == SCISSORS)
        cout << "Scissors\n";
	else if (choice == LIZARD)
        cout << "Lizard\n";
    else if (choice == SPOCK)
        cout << "Spock\n";
    
}
Last edited on
closed account (j3Rz8vqX)
Your program, as of right now, isn't designed to exit unless the user picks 6, which is exit.

There is no mistake that I can see in the programs functionality.

If you want to exit the battle, then enable your determineWinner function to return a value.

My assumption is:

If they are tied, return anything but 6.
If they aren't tied, return 6; force exit.

There are better means out there, but that is the simplest that will correlate with your already developed code.

Edit: Line 135 and 136 needs to be swapped.
Last edited on
thank you for your feedback, will work on that and see how it works out. extra thxs for the edit note.
Still not getting it. kind of messed up my code trying to fix this, luckily I keep a back up :)
closed account (j3Rz8vqX)
Enable your function to return an int; I'll leave this to you.

Insert a "return 7" at the end of your function.

Modify your last conditions in your function:
Choice#1:
1
2
3
4
5
6
7
//Remove the last whole branch and insert this:
if(user == computer)
{
    cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
return 6;//<--- default return for everything else (6 - exit, done with playing) 

Or, your standard approach
Choice#2:
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

	//{ Edit 
    if (user == ROCK && computer == ROCK)
{
			cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
	else if (user == PAPER && computer == PAPER)
{
			cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
	else if (user == SCISSORS && computer == SCISSORS)
{
			cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
	else if (user == LIZARD && computer == LIZARD)
{
			cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
	else if (user == SPOCK && computer == SPOCK)
{
			cout << "** Game is a tie!! Go again **\n" <<endl;
    return 7;//<---6 is your exit key; I'd prefer true/false, but oh well.
}
	}

I'll leave the indentations to you.

The above code was written in the web text editor, no validation was done; it may not possibly work, but it has the general idea.

If you do correctly it should return a integer value, then all you'd need to do is create an escape path.

Simply assign the return value to userChoice?

 
userChoice = determineWinner(userChoice, computerChoice);

I appreciate your help, and thank you once again. I will go into the code and check it out and see how it works out; fingers crossed.
closed account (j3Rz8vqX)
I'll improvise because I feel I may have not given you enough information to solve this:
1
2
3
4
5
6
7
8
	while (userChoice != 6)
	{
		userChoice = determineWinner(userChoice, computerChoice);
		if(userChoice == 6)
			break;
		computerChoice = getComputerChoice();
		userChoice = getUserChoice();
	}

or:
1
2
3
4
5
6
7
	while (userChoice != 6)
	{
		if(determineWinner(userChoice, computerChoice)==6)
			break;
		computerChoice = getComputerChoice();
		userChoice = getUserChoice();
	}
been fiddling with the code but I get a error when I try to rearrange the code with the above improvisations. I'm still trying to get it though, I don't like giving up.

1>------ Build started: Project: Assignemt2Week6, Configuration: Debug Win32 ------
1> main.cpp
1>main.cpp(59): error C2440: '=' : cannot convert from 'void' to 'int'
1> Expressions of type void cannot be converted to other types
1. main.cpp(72): warning C4244: 'initializing' : conversion from 'time_t' to 'unsigned int', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
//Prototype:
int determineWinner(int, int);

//Header:
int determineWinner(int user, int computer)
{
    //... Your stuff...
}


Not sure what happened on line 72.

Try it.
Last edited on
Topic archived. No new replies allowed.