Basic Game: can someone tell me if I can write this code a little more efficient...or any pointers

Hello. This is just a basic game: rock, paper, scissors and it started to get a little cloudy to code after a while. I just want to know if this code can be written more efficient or condensed. Appreciate any feedback

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
//I had to use the variable counter as a middle man for playerWin because I couldn't find a better way to end the game loop
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void instructions();
void gameStatistics(int x, int y, int z);

int main()
{
    char play;

    do
    {
    int playerWin=0, computerWin=0, tie=0, counter = 0, playerChoice, computerChoice;
    srand(time(0));


    cout << setw(4) << "------------------ Rock Paper Scissors ------------------" << endl << endl;

    instructions();

    while (playerWin < 5)     //loop until somebody wins
    {
        cout << "1 for rock - 2 for paper - 3 for scissors" << endl << endl;
        cin >> playerChoice;
        cout << endl;

        switch(playerChoice)
            {
                case 1: cout << "You: Rock" << endl;
                    break;
                case 2: cout << "You: Paper" << endl;
                    break;
                case 3: cout << "You: Scissors" << endl;
                    break;
            }

        computerChoice = rand() % 3 + 1;

        switch(computerChoice)
            {
                case 1: cout << "Computer: Rock" << endl;
                    break;
                case 2: cout << "Computer: Paper" << endl;
                    break;
                case 3: cout << "Computer: Scissors" << endl;
                    break;
            }
        if((playerChoice == 1 && computerChoice == 1) || (playerChoice == 2 && computerChoice == 2) || (playerChoice == 3 && computerChoice == 3))
        {
            cout << "\n--Tie!--" << endl << endl;
            tie++;
        }

        else if((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1))
        {
            cout << "\n--Computer Wins--" << endl << endl;
            computerWin++;
        }

        else if((playerChoice == 1 && computerChoice == 3) || (playerChoice == 2 && computerChoice == 1) || (playerChoice == 3 && computerChoice == 2))
        {
            cout << "\n--You Win--" << endl << endl;
            playerWin++;
            counter = playerWin;
        }

        if(computerWin == 5)
        {
            playerWin = 100;
            cout << "\nTHE COMPUTER WON!!!" << endl;
        }

        if(playerWin == 5)
        {
            cout << "\nYOU WON!!!" << endl;
        }
    }

    cout << "\nGAME OVER" << endl;
    cout << "Do you want to play again[y/n]: ";
    cin >> play;
    cout << endl << endl;

    gameStatistics(tie, counter, computerWin);    //output game stats to void function

    }
    while((play == 'y') || (play == 'Y'));
    return 0;
}
//end the main function


//start the other functions
void instructions()
{
    cout<< "The same rules apply to the game as they've always have " << endl << endl
        << "* Paper beats rock" << endl
        << "* Rock beats scissors" << endl
        << "* Scissors beats paper" << endl << endl
        << "First person to five points wins!!!" << endl << endl
        << "_________________________________________________________" << endl << endl << endl;
}

void gameStatistics(int x, int y, int z)
{
    cout << endl << endl;
    cout << "---------- Game Statistics ----------" << endl;
    cout << "Ties: " << x << endl;
    cout << "Player Wins: " << y << endl;
    cout << "Computer Wins: " << z << endl;
    cout << "_____________________________________" << endl << endl << endl;
}

This:
1
2
3
4
5
if((playerChoice == 1 && computerChoice == 1) || (playerChoice == 2 && computerChoice == 2) || (playerChoice == 3 && computerChoice == 3))
        {
            cout << "\n--Tie!--" << endl << endl;
            tie++;
        }

Could be:
1
2
3
4
5
if(playerChoice == computerChoice)
        {
            cout << "\n--Tie!--" << endl << endl;
            tie++;
        }


There is a few more things that could be improved, but not really much.
Much thanks!
I don't realy understand your loop.
while (playerWin < 5)
It only checks if the player. What if the computer gets to 5 first? So you would want to check if either win unless its only game over if player gets 5.
while (playerWin < 5 && computerWin < 5)

Instead of writing this twice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
switch(playerChoice)
            {
                case 1: cout << "You: Rock" << endl;
                    break;
                case 2: cout << "You: Paper" << endl;
                    break;
                case 3: cout << "You: Scissors" << endl;
                    break;
            }

        computerChoice = rand() % 3 + 1;

        switch(computerChoice)
            {
                case 1: cout << "Computer: Rock" << endl;
                    break;
                case 2: cout << "Computer: Paper" << endl;
                    break;
                case 3: cout << "Computer: Scissors" << endl;
                    break;
            }

You could make it a function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void PrintNames(int picked)
{
	switch(picked)
	{
		case 1:
			cout << "Paper";
				break;
		case 2:
			cout << "Scissors";
				break;
		case 3:
			cout << "Rock";
				break;
	}

}


Also instead of all the crazy comparisons you can use some simple math.
Assuming Paper = 1, Scissors = 2, Rock = 3
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
turnScore = playerChoice - comChoice;

	if(turnScore == 0) //Tie
	{
		cout << "Tie! You bolth picked ";  PrintNames(playerChoice); cout << endl;
		tie++;
	}
	else if(turnScore == 2) //Comp
	{
		Winner(1,comChoice,playerChoice);
		compWin++;
	}
	else if(turnScore == -2) //Player Wins
	{
		Winner(0,comChoice,playerChoice);
		playerWin++;
	}
	else if(turnScore < 0) //Comp Wins
	{
		Winner(1,comChoice,playerChoice);
		compWin++;
	}
	else //Player Wins
	{
		Winner(0,comChoice,playerChoice);
		playerWin++;
	}


Could get rid of this and the counter Var cuz its not realy doing anything but cloning playerWin.

1
2
3
4
5
6
7
8
9
10
if(computerWin == 5)
        {
            playerWin = 100;
            cout << "\nTHE COMPUTER WON!!!" << endl;
        }

        if(playerWin == 5)
        {
            cout << "\nYOU WON!!!" << endl;
        }

And just replace it with your function like this
1
2
3
4
5
6
7
8
9

if(playerWin == 5 || computerWin == 5){
    cout << endl << endl;
    cout << "---------- Game Statistics ----------" << endl;
    cout << "Ties: " << tie << endl;
    cout << "Player Wins: " << playerWin << endl;
    cout << "Computer Wins: " << computerWin << endl;
    cout << "_____________________________________" << endl << endl << endl;
}


You don't need that to be function you are only calling it once. Your just using more memory by cloning ( tie->x , playerWin->counter ->y and computerWins ->z)
Altho with as much memory and the processors we have now it doesn't matter much altho with a big project it could add up.


And add this function to stop the need to type the same thing over and over.

1
2
3
4
5
6
7
8
9
10
void Winner(int player , int comNum , int playerNum)
{
	if(player == 0)
	{
		cout << "You Wins! ";
	}else{
		cout << "Computer Wins! ";
	}
	 PrintNames(comNum); cout << " BEATS "; PrintNames(playerNum); cout << endl;
}


Good luck and hope some of this helps :)
Thanks bmiller. Helped a lot...especially the while game loop
Topic archived. No new replies allowed.