Writing code for Rock, Paper, Scissors game.

So I've been teaching myself to code for the past three days and have hit a road block in my code. When I run the code it only executes one of the outcomes rather than all of them. Can someone help me figure out what the issue is and how to avoid it in the future?

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
#include <iostream>
#include <stdlib.h>
#include <time.h>

void rock()
{
	std::cout << "You chose rock.\n";
}
void paper()
{
	std::cout << "You chose paper.\n";
}
void scissors()
{
	std::cout << "You chose scissors.\n";
}
void quit()
{
	std::cout << "Play again soon!/n";
}

void ai()
{

}

int main(){
	
	int input;
	std::cout << "Choose one of the options.\n";
	std::cout << "1. Rock\n";
	std::cout << "2. Paper\n";
	std::cout << "3. Scissors\n";
	std::cout << "4. Quit\n";
	std::cin >> input;
	switch (input)
	{
		case 1:
		rock();
		break;
		
		case 2:
		paper();
		break;
		
		case 3:
		scissors();
		break;
		
		case 4:
		quit();
		break;
		
		default:
		std::cout << "Invalid input.\n";
		break;
	}
	
	
	int pick;                    //I think the problem us somewhere in here?
	srand (time(NULL));
	pick = rand() % 3 + 1;
	
	if(pick == 1 && input == 2){
		std::cout << "You win!\n\n";
	}else if(pick == 1 && input == 3){
		std::cout << "You lose!\n\n";
	}else if(pick == 1 && input == 1){
		std::cout << "You tied!\n\n";
	}else if(pick == 2 && input == 3){
		std::cout << "You win!\n\n";
	}else if(pick == 2 && input == 1){
		std::cout << "You lose!\n\n";
	}else if(pick == 2 && input == 2){
		std::cout << "You tied!\n\n";
	}else if(pick == 3 && input == 1){
		std::cout << "You win!\n\n";
	}else if(pick == 3 && input == 2){
		std::cout << "You lose!\n\n";
	}else if(pick == 3 && input == 3){
		std::cout << "You tied!\n\n";
	}
	
	main();
	
	return 0;
}
Why main() at 84?
I don't think main() can call itself or should call itself.

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
#include <iostream>
#include <stdlib.h>
#include <time.h>

void rock()
{
	std::cout << "You chose rock.\n";
}
void paper()
{
	std::cout << "You chose paper.\n";
}
void scissors()
{
	std::cout << "You chose scissors.\n";
}
void quit()
{
	std::cout << "Play again soon!/n";
}

void ai()
{

}

int main(){

	int input;
	std::cout << "Choose one of the options.\n";
	std::cout << "1. Rock\n";
	std::cout << "2. Paper\n";
	std::cout << "3. Scissors\n";
	std::cout << "4. Quit\n";
	std::cin >> input;
	switch (input)
	{
		case 1:
		rock();
		break;

		case 2:
		paper();
		break;

		case 3:
		scissors();
		break;

		case 4:
		quit();
		break;

		default:
		std::cout << "Invalid input.\n";
		break;
	}


	int pick;                    //I think the problem us somewhere in here?
	srand (time(NULL));
	pick = rand() % 3 + 1;
	if (pick == 1)
            std::cout << "computer picked rock " << std::endl;
        if (pick == 2)
            std::cout << "computer picked paper " << std::endl;
        if (pick == 3)
            std::cout << "computer picked scissors" << std::endl;

	if(pick == 1 && input == 2){
		std::cout << "You win!\n\n";
	}else if(pick == 1 && input == 3){
		std::cout << "You lose!\n\n";
	}else if(pick == 1 && input == 1){
		std::cout << "You tied!\n\n";
	}else if(pick == 2 && input == 3){
		std::cout << "You win!\n\n";
	}else if(pick == 2 && input == 1){
		std::cout << "You lose!\n\n";
	}else if(pick == 2 && input == 2){
		std::cout << "You tied!\n\n";
	}else if(pick == 3 && input == 1){
		std::cout << "You win!\n\n";
	}else if(pick == 3 && input == 2){
		std::cout << "You lose!\n\n";
	}else if(pick == 3 && input == 3){
		std::cout << "You tied!\n\n";
	}

	return 0;
}
Last edited on
@Kenota

Here is your program, shortened. And it can be compressed even more by using a string array in the ai() function, thereby removing all of the other functions.


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
// Rock - Paper - Scissors.cpp : main project file.

#include <iostream>
#include <stdlib.h>
#include <ctime> // better 

void rock()
{
	std::cout << "You chose rock.\n";
}
void paper()
{
	std::cout << "You chose paper.\n";
}
void scissors()
{
	std::cout << "You chose scissors.\n";
}
void quit()
{
	std::cout << "Play again soon!\n\n\n";
}

void ai(int input)
{
	int pick;                    //I think the problem us somewhere in here?

	pick = rand() % 3 + 1;
	if (pick == 1)
		std::cout << "computer picked rock " << std::endl;
	if (pick == 2)
		std::cout << "computer picked paper " << std::endl;
	if (pick == 3)
		std::cout << "computer picked scissors" << std::endl;

	if (pick == input)
		std::cout << "You tied!\n\n";
	if ((pick == 1 && input == 2) || (pick == 2 && input == 3) || (pick == 3 && input == 1))
		std::cout << "You win!\n\n";
	if (pick == 1 && input == 3 || (pick == 2 && input == 1) || (pick == 3 && input == 2))
		std::cout << "You lose!\n\n";
}

int main()
{

	int input, computer_choice;
	srand((unsigned)time(0));
	do
	{
		std::cout << "Choose one of the options.\n";
		std::cout << "1. Rock\n";
		std::cout << "2. Paper\n";
		std::cout << "3. Scissors\n";
		std::cout << "4. Quit\n";
		std::cin >> input;
		switch (input)
		{
		case 1:
			rock();
			break;

		case 2:
			paper();
			break;

		case 3:
			scissors();
			break;

		case 4:
			quit();
			break;

		default:
			std::cout << "Invalid input.\n";
			break;
		}
		if (input != 4)
			ai(input); // Computer plays ONLY if player is not quitting
	} while (input != 4);




	return 0;
}

Last edited on
Registered users can post here. Sign in or register to post.