Rock, Paper, Scissor using seeds

I am now having a difficulty trying to understand this 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
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
// Bjarne Stroustrup 4/4/2009
// Chapter 4 Exercise 10

/*
	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 do next.
*/

#include "iostream"	
#include "vector"
using namespace std;
// note that different compilers/SDEs keep header files in different places
// so that you may have to use "../std_lib_facilities.h" or "../../std_lib_facilities.h"
// the ../ notation means "look one directly/folder up from the current directory/folder"

/*
	Somehow, we have to get the computer to be a bit unpredicatble.
	There are many ways of doing that. All of the good ones involves random numbers,
	but here I'll just do somthing simple that does not use any advanced programming.

	The computer will play based on a series of (Fibbonacci) numbers that we generate using the
	function next_play().

	To avoid having the computer always play the same game we ask the player to enter a "seed";
	different seeds can give different games.
*/

int v1 = 1;
int v2 = 2;

int fib()	// generate the next element of a (Fibbonacci) series:
			// 1 2 3 5 8 13 21 34
{
	int s = v1+v2;
	if (s<=0) s = 1;	// how could s become less than zero?
	v1 = v2;
	v2 = s;
	return s;
}

void generate(int seed)
	// use the seed to choose where in the sequence the game starts
{
		if (seed<0) seed = -seed;	// don't want a negative number
		seed %=10;					// don't want a number larger than 9
		if (seed==0) return;		// don't bother: use the default
		for (int i=0 ; i<seed; ++i) fib();	// move seed steps forward
}

int next_play()	// generate a reasonably obscure sequence of 0s, 1s, and 2s
{
	return fib()%3;	// we are only interested in a value 0, 1, or 2 (% is the modulus/remainder operation)
}


int main()
{
	cout << "enter an integer \"seed\" to help me play: ";
	int seed = 0;
	cin >> seed;
	generate(seed);	// get the computer ready to play

	// let's keep track of who's winning:
	int count1 = 0;	// user's score
	int count2 = 0;	// computer's score
	int draws = 0;	// number of draws/ties

	cout << "enter \"rock\", \"paper\", or \"scissors\"\n"
		<< "(I'll do the same and promises not to cheat by peeping at your input): ";
	string s;
	while(cin >> s) {	// we'll as long as we get "good" input and then stop

						// the computer prefers numbers, so convert string representations to numbers
						// we prefer strings (except when wet ype), so convert abbreviations to full words
		int x = 0;
		if (s=="scissors" || s=="s") {
			x = 0;
			s = "scissors";
		}
		else if (s=="rock" || s=="r") {
			x = 1;
			s = "rock";
		}
		else if (s=="paper" || s=="p") {
			x = 2;
			s = "paper";
		}
		else
		cout<<"Error!";

		int xx = next_play();
		string ss;	// computers play
		switch(xx) {	// we prefer strings, so convert numeric representations to strings
		case 0: ss = "scissors"; break;
		case 1:	ss = "rock"; break;
		case 2:	ss = "paper"; break;
		}
	
		if (x==xx) {
			cout << "a draw!\n";
			++draws;
		}
		else {
			string res = "I win!";
			if (xx==0 && x==1) {
				res = "You win!";	// rock beats sissors
				++count1;
			}
			else if (xx==1 && x==2) {
				res = "You win!";	// paper beats rock
				++count1;
			}
			else if (xx==2 && x==0) {
				res = "You win!";	// scissors beat paper
				++count1;
			}
			else
				++count2;

			cout << "you said \"" << s << "\" I said \"" << ss << "\": " << res ;
			cout << " score: you==" << count1 << " me==" << count2 << " same==" << draws << "\n";
		}
		cout << "Please try again: ";
	}
	cout << "exit because of bad input\n";
}



I don't understand the way "seed" is used. The program first takes input "seed" from the user, right? But that seed input by the user really depends on the function next_play? I don't think so. In my opinion, the function next_play takes value only from the function fib(). Everytime it is called, the fib() generates a number and gives this value to the next_play. In that case, the function generate(seed) is useless, right? Why this function is included in this problem? Can any seniors or experts tell me in detail?
Last edited on
I don't understand the way "seed" is used. The program first takes input "seed" from the user, right? But that seed input by the user really depends on the function next_play?

The seed is independent of all functions. generate is dependent on seed. If fib is called after generate the initial value returned may also be seen as dependent on seed.


In my opinion, the function next_play takes value only from the function fib().

This is true, but as I mentioned, seed is not dependent on fib or next_play.


Everytime it is called, the fib() generates a number and gives this value to the next_play. In that case, the function generate(seed) is useless, right?

No, it's not. See above. Have you tried running the code? Does the sequence of numbers generated depend on the number fed to generate?

Simple experimentation should prove to you that generate is not useless.


Ok, I think I am getting close to it. But can I ask you a question? For example if I input 10 for seed. And the value is given to the function generate. Then "for" loop works 10 times. It means the last value(the 10th fibo number) is passed to the next_play or each time the loop works, the value is passed to the next_play each?
Thanks in advance, #circe
If you feed 10 to generate then seed %= 10; makes the seed 0, which on the next line you can see is as if generate wasn't called at all. It returns without calling fibo at all, so the first value in the fibonacci sequence is used the first time next_play is called.
I got this! Thanks :)
Topic archived. No new replies allowed.