Rock, Paper, Scissors Game Help

Need help with this code:

MAIN.CPP
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
 
#include "game.h"


int main()
{
	while (points>0){

		string choice, play;
		cout << "Please enter your choice:(Rock, Paper, Scissors)" << endl;
		getline(cin, choice);//get input for choice
		srand((unsigned int) time(0));
		unsigned int randomNumber=(rand()%2)+1;
		
		
		
		if (randomNumber==0){
			play == "Rock";
		}
		else if (randomNumber==1){
			play=="Paper";
		}
		else if (randomNumber>1)
			play=="Scissors";
		
		game_func(choice, play);//send choice to game_function
	}
		cout << "THE GAME ENDED!" << endl;

		system("PAUSE");
		return 0;
}


GAME.H
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
#ifndef __FUNC__
#define __FUNC__
#include <time.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
int points=3;
int enemypoints=3;


bool compare_plays(string a, string b)
{
	if ((a=="Rock" && b=="Scissors") || (a=="Paper" && b=="Rock") || (a=="Scissors" && b=="Paper"))
	{
		return true;
	}
	else
		return false;


}
void game_func(string c, string p)
{
	
	// Playing rules!
	if (c == p)
	{
		cout << "Enemy: " << p << "------------" << "Player: " << c << endl;
		cout << "IT WAS A TIE!" << endl;
	}
	else if (c!=p)
	{
		cout << "Enemy: " << p << "------------" << "Player: " << c << endl;
		compare_plays(c, p);
		if (compare_plays(c,p))
		{
			enemypoints-=1;
			cout << "ENEMY LOST A POINT AND NOW HAS " << enemypoints << " left!" << endl;
		}
		else
		{
			points-=1;
			cout << "YOU LOST A POINT AND NOW HAVE " << points << " left!" << endl;
		}
	}
		



}

#endif 


I get no errors, but the problem seems to be in the main.cpp file. I am having trouble with the rand() conditional (where it generates a random number between 0 and 3 and then based on that tells if variable "play" is rock paper or scissors). Thanks in advance
two problems:

First: Line 12 must be before line 7 (srand() only once!)
Second: Line13: Since you add 1 to the value you will get always values > 1 (and hence never "Rock")
Topic archived. No new replies allowed.