I need help with my program :/ it wont run correctly

i need help with my code :/ i want it to run again if the user wants to play again. i think i need a do while in a do while but its not working. if i take away the first do while that has to do with playAgain, then it works. The second do while checks for if there is a tie, restart the game but i also want a user restart if there is no tie restart.
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
#include <iostream>
using namespace std;

/*
Name: Zander Cherry
Date: 10/11/2013
Project: make a Rock_Paper_Scissors game
*/

/*
rock
paper
scissors

r | p | s
---------
r > s done player 1 wins; battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'S' || battleOptionP2 == 's'
r < p done player 2 wins; battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'P' || battleOptionP2 == 'p'
r = r done no winner; battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'R' || battleOptionP2 == 'r'

p < s done player 2 wins; battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'S' || battleOptionP2 == 's'
p = p done no winner; battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'P' || battleOptionP2 == 'p'
p > r done player 1 wins; battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'R' || battleOptionP2 == 'r'

s = s done no winner; battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'S' || battleOptionP2 == 's'
s > p done player 1 wins; battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'P' || battleOptionP2 == 'p'
s < r done player 2 wins;
*/


int main()
{
    char playGame, battleOption, battleOptionP2;
    int playTime/*will be used*/, tie/*will be used*/, playAgain;

do
{

    cout << "Ready to play?[y]/[n]:    ";
    cin >> playGame;

	if(playGame == 'n' || playGame == 'N')
	{
		system("cls");
		cout << "Maybe later :[?" << endl;
		return 0;
	}

    cout << " " << endl;

    cout << "Rules:" << endl;
    cout << "1. Choose a battle option." << endl;
    cout << "2. Choose from [R]ock, [P]aper, or [S]cissors." << endl;
    cout << "3. Only type what is in the brakets" << endl;

    cout << " " << endl;
		do
		{
			cout << "Player one, choose your battle option:    ";
			cin >> battleOption;
			system("cls");

    
			cout << "Player two, choose your battle option:    ";
			cin >> battleOptionP2;
			system("cls");


			//rock

				if((battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'S' || battleOptionP2 == 's')) // rock beats scissors; r > s
				{
					cout << "Player 1 Wins" << endl;
				}
				else if((battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'P' || battleOptionP2 == 'p')) // rock looses over paper; r < p
				{
					cout << "Player 2 Wins" << endl;
				}
				else if((battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'R' || battleOptionP2 == 'r')) // rock ties rock; r = r
				{
					cout << "Oops, It seems to be a tie" << endl;
				}

			//paper

				else if((battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'S' || battleOptionP2 == 's')) // paper looses over scissors; p < s
				{
					cout << "Player 2 Wins" << endl;
				}
				else if((battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'P' || battleOptionP2 == 'p')) // paper ties paper; p = p
				{
					cout << "Oops, It seems to be a tie" << endl;
				}
				else if((battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'R' || battleOptionP2 == 'r')) // paper beats rock; p > r
				{
					cout << "Player 1 Wins" << endl;
				}

			//scissors

				else if((battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'S' || battleOptionP2 == 's')) // scissors ties scissors; s = s
				{
					cout << "Oops, It seems to be a tie" << endl;
				}
				else if((battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'P' || battleOptionP2 == 'p')) // scissors beats paper; s > p
				{
					cout << "Player 1 Wins" << endl;
				}
				else if((battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'R' || battleOptionP2 == 'r')) // scissors looses over rock; s < r
				{
					cout << "Player 2 Wins" << endl;
				}

			cout << " " << endl;
			cout << "Stats:" << endl;
			cout << "------------------" << endl;
			cout << "Player 1 choose: " << battleOption << endl;
			cout << "Player 2 choose: " << battleOptionP2 << endl;

			cout << " " << endl;

		}
		while(((battleOption == 'R' || battleOption == 'r') && (battleOptionP2 == 'R' || battleOptionP2 == 'r')) || ((battleOption == 'P' || battleOption == 'p') && (battleOptionP2 == 'P' || battleOptionP2 == 'p')) || ((battleOption == 'S' || battleOption == 's') && (battleOptionP2 == 'S' || battleOptionP2 == 's')));


cout << "Would you like to play again? [y]es, [n]o: ";
cin >> playAgain;
}

while(playAgain == 'y' || playAgain == 'Y');
}
Last edited on
edited it :9 pls can i get help?
i really need help D: please some one? any one?
closed account (D80DSL3A)
Declare playAgain as type char, not type int?
^_^ thx soo much
There is a lot of redundant code in your original post. I went through and made some optimizations for you to consider. I'm sure there is an even more optimal way to do this that I'm over looking but hopefully this helps you out in some fashion.... Slow day at work for me :P

Some notes:

using a string instead of char in a case like this is a lot better because you can use the index of 0 (the first letter in the string) and use that for all of your arguments. So, as you can check out in the code I listed below, someone can type "Yes" to the question, or type "Rock" and it just pulls that first letter and converts it to upper case using toupper(). Then all you have to do is check for that letter. Someone can enter Yess, or Roke (typos) and it will still work.

Anyway, hope this helps!

Edit: added some validation, but you could take it even further and test for the strings "rock", "paper" or "scissors"
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
#include <cstdlib>
#include <ctype.h>
#include <string>
#include <iostream>

using namespace std;

int main()
{

    string playGame, battleOption, battleOptionP2 = " ";
    int playTime, tie = 0;

    cout << "\n\t\t-------" << endl;
    cout << "\n\t\t Rules" << endl;
    cout << "\n\t\t-------" << endl;   
    cout << "\n*  Choose from Rock, Paper, or Scissors  *" << endl;
   
    do
    {
            cout << "\n\nAre you ready to start the game [Y/N]: ";
            cin >> playGame;
            
            //error checking 
            while((toupper(playGame[0]) != 'N') && (toupper(playGame[0]) != 'Y'))
            {
              cout << "\nError - " << playGame << " is not a valid option!" << endl;
              cout << "\n\nAre you ready to start the game [Y/N]: ";
              cin >> playGame;                         
            }
             
            
            if(toupper(playGame[0]) == 'N')
            {
              cout << "\nMaybe later :[ \n\n" << endl;
	     break;
            }
            
            
             cout << "\nPlayer one, choose your battle option: ";
             cin >> battleOption;
			
             //Error checking 
             while((toupper(battleOption[0]) != 'R') && (toupper(battleOption[0]) != 'P') && (toupper(battleOption[0]) != 'S'))
             {
                cout << "\nError - " << battleOption << " is not a valid selection!" << endl;
                cout << "\nPlayer one, choose a valid battle option: ";
	       cin  >> battleOption;            
             }
            
	    cout << "\nPlayer two, choose your battle option: ";
	    cin >> battleOptionP2;	
            		
	    while((toupper(battleOptionP2[0]) != 'R') && (toupper(battleOptionP2[0]) != 'P') && (toupper(battleOptionP2[0]) != 'S'))
	    {
                cout << "\nError - " << battleOptionP2 << " is not a valid selection!" << endl;
	       cout << "\nPlayer two, choose a valid battle option: ";
	       cin  >> battleOptionP2;            
             }			      
	    
			
             cout << "\n---------------------" << endl;
             cout << "\tStats" << endl;
             cout << "---------------------" << endl;
             cout << "Player 1 chose: " << battleOption << endl;
             cout << "Player 2 chose: " << battleOptionP2 << endl;
            
             if(battleOption[0] == battleOptionP2[0])
                    cout << "\nOops! Seems to be a tie!" << endl;
                   
             else if((toupper(battleOption[0]) == 'R') && (toupper(battleOptionP2[0]) == 'S') || // rock beats scissors; r > s
                    (toupper(battleOption[0]) == 'P') && (toupper(battleOptionP2[0]) == 'R') || // paper beats rock; p > r
                    (toupper(battleOption[0]) == 'S') && (toupper(battleOptionP2[0]) == 'P'))    // scissors beats paper; s > p  
             {
                    cout << "\nPlayer 1 Wins" << endl;
             }
             else 
             {
                    cout << "\nPlayer 2 Wins" << endl;
             }
			
             cout << "\nWould you like to play again?[Y/N]: ";
             cin  >> playGame;
            
             while((toupper(playGame[0]) != 'N') && (toupper(playGame[0]) != 'Y'))
             {
               cout << "\nError - " << playGame << " is not a valid option!" << endl;
               cout << "\n\nWould you like to play again?[Y/N]: ";
               cin >> playGame;                         
             }            
   }
   while(toupper(playGame[0]) != 'N');
    
   system("PAUSE");
   return 0;
}
Last edited on
Topic archived. No new replies allowed.