Rock-Paper-Scissors Game for Stroustrup's PPP2 Book - Help Needed

Here are the specs:
Write a program that plays the game “Rock, Paper, Scissors.” If you are not
familiar with the game do some research (e.g., on the web using Google).
Research is a common task for programmers. Use a switch-statement to
solve this exercise. Also, the machine should give random answers (i.e.,
select the next rock, paper, or scissors randomly). Real randomness is too
hard to provide just now, so just build a vector with a sequence of values
to be used as “the next value.” If you build the vector into the program,
it will always play the same game, so maybe you should let the user enter
some values. Try variations to make it less easy for the user to guess
which move the machine will make next.


It says to do it without true (pseudo) randomness, which I take to mean that it's saying to not use rand(). But I wonder if I'm still allowed to use it if I already know about it? I asked on the Google Group for the readers of the book, but I thought I'd ask here as well just in case.

I'd also like some tips on actually creating the game itself. This is what I have so far (I'm planning to split it up into separate functions after I've made it in main(), so that I can first see what parameters I should pass to which function and then create the functions):

1
2
3
4
5
6
7
8
9
10
11
#include "std_lib_facilities.h"

int main()
{
	srand(static_cast<unsigned int>(time(NULL)));
	rand();
	cout << "Welcome to my Rock-Paper-Scissors Game!\n\n";
	cout << "For this game, 1 means \"Rock\", 2 means \"Paper\", and 3 means \"Scissors\".\n";
	int computer_move = 0, player_move = 0;
	
}


I used rand() like that beforehand to discard the first value it returns, since I read before that there's a bug with rand() in Windows that requires discarding its return value at least once before using it.

After this, there are some exercises coming next in the book where I have to write programs that find prime numbers. For the first one, it says to make a vector to store all of the primes I can think of first and then write code that has the computer generate primes. I have to compare the list the computer generates against mine. But I'm not sure I know the right list, so I might need help there.

But yeah, I'll be sure to get this game out of the way first before getting to the prime numbers programs. I just thought I'd give you guys a heads-up that I'm going to ask about that as well. I'm sorry I keep asking too many questions, though.

Edit: I attempted to do it myself using rand() and srand(), but there's a bug in there. It's fine when I win on the first try and the program exits, but then when I get a draw (I haven't seen what happens when I actually lose), the game gets stuck with just me entering numbers and the computer staying silent. Then it eventually goes into an infinite loop while printing out what move I played last. Here's the code:

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
#include "std_lib_facilities.h"

bool play_game(int computer_move, int player_move);

int main()
{
	cout << "Welcome to my Rock-Paper-Scissors Game!\n\n";
	cout << "For this game, 1 means \"Rock\", 2 means \"Paper\", and 3 means \"Scissors\".\n";
	int computer_move = 0, player_move = 0;
	if (play_game(computer_move, player_move))
	{
		cout << "You won! Congratulations!\n";
	}
	else
	{
		cout << "You lose (unless it was a tie)! Try again later\n";
	}
}

bool play_game(int computer_move, int player_move)
{
	bool won = false;
	srand(static_cast<unsigned int>(time(NULL)));
	rand();
	computer_move += rand() % 3 + 1;
	cin >> player_move;
	if (computer_move == 1)
	{
		cout << "Computer used Rock\n";
	}
	else if (computer_move == 2)
	{
		cout << "Computer used Paper\n";
	}
	else if (computer_move == 3)
	{
		cout << "Computer used Scissors\n";
	}

	switch (player_move)
	{
	case 1:
		cout << "Player used Rock\n";
		if (computer_move == player_move)
		{
			cout << "It's a tie!  Try again later\n";
			won = false;
		}
		else if (computer_move != player_move && computer_move == 2)
		{
			won = false;
		}
		else if (computer_move != player_move && computer_move == 3)
		{
			won = true;
		}
		break;
	case 2:
		cout << "Player used Paper\n";
		if (computer_move == player_move)
		{
			cout << "It's a tie!  Try again later\n";
			won = false;
		}
		else if (computer_move != player_move && computer_move == 1)
		{
			won = true;
		}
		else if (computer_move != player_move && computer_move == 3)
		{
			won = false;
		}
		break;
	case 3:
		cout << "Player used Scissors\n";
		if (computer_move == player_move)
		{
			cout << "It's a tie!  Try again later\n";
			won = false;
		}
		else if (computer_move != player_move && computer_move == 1)
		{
			won = false;
		}
		else if (computer_move != player_move && computer_move == 2)
		{
			won = true;
		}
		break;
	default:
		cout << "Please enter a 1, 2 or 3!\n";
	}
	return won;
}


Since the specs say to not use rand() and to use just switch-case statements, I'd like to know of a good way to do it like that as well. Thanks in advance.
Last edited on
Topic archived. No new replies allowed.