C++ Alternating players

I want it to alternate from player to player but I cant figure out how. This is what ive got so far.


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
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>

using namespace std;

void diceone ();
void dicetwo ();
void dicethree ();
void dicefour ();
void dicefive ();
void dicesix ();
void gameover ();

void diceone ()
{
cout << " ----- " << endl;
cout << "|     |" << endl;
cout << "|  O  |" << endl;
cout << "|     |" << endl;
cout << " ----- " << endl;
}

void dicetwo ()
{
cout << " ----- " << endl;
cout << "|  0  |" << endl;
cout << "|     |" << endl;
cout << "|  0  |" << endl;
cout << " ----- " << endl;
}

void dicethree ()
{
cout << " ----- " << endl;
cout << "|  0  |" << endl;
cout << "|  0  |" << endl;
cout << "|  0  |" << endl;
cout << " ----- " << endl;
}

void dicefour ()
{
cout << " ----- " << endl;
cout << "|0   0|" << endl;
cout << "|     |" << endl;
cout << "|0   0|" << endl;
cout << " ----- " << endl;
}

void dicefive ()
{
cout << " ----- " << endl;
cout << "|0   0|" << endl;
cout << "|  0  |" << endl;
cout << "|0   0|" << endl;
cout << " ----- " << endl;
}

void dicesix ()
{
cout << " ----- " << endl;
cout << "|0   0|" << endl;
cout << "|0   0|" << endl;
cout << "|0   0|" << endl;
cout << " ----- " << endl;
}

    int main()
{
    int iSecret;
    char choice;
    srand (time(NULL));
    int Score[2];
    int cont;
    string current;

Score[0] = 0;
Score[1] = 0;

    std::string YourNameHere[2];{
        cout << "Player 1, please enter your name:" <<endl;
        cin >> YourNameHere[0];
        cout << "Hello " << YourNameHere[0] << endl;

        cout << "Player 2, Please enter your name:" <<endl;
         cin >> YourNameHere[1];
        cout << "Hello " << YourNameHere[1] << endl;

        cout << "Would you like to begin?(Y/N)" << endl;
        cin >> choice;
    }
    if (cont == 1){
        cout << "\n" << YourNameHere[0] << ":\n";}
    else{
        cout << "\n" << YourNameHere[1] << "\n";}

    do {
        iSecret = rand() % 6 + 1;
        cout << "You rolled the number: " << iSecret << endl;

    if (iSecret == 1){
        diceone();
        }
    else if (iSecret == 2){
        dicetwo();
        }
    else if (iSecret == 3){
        dicethree();
        }
    else if (iSecret == 4){
        dicefour();
        }
    else if (iSecret == 5){
        dicefive();
        }
    else if (iSecret == 6){
        dicesix();
        }
        cout << "Would you like to role again?(Y/N)" << endl;
        cin >> choice;
    if (choice == 'n')

{
    if (Score[0]>Score[1])
        cout<< (YourNameHere[0]) << " You win!" <<endl;
    else if(Score[1]>Score[0])
        cout<< (YourNameHere[1]) << " You win!" <<endl;
    else if (Score[2]>50)
        cout << "Game Over!!" << endl;
        cout << (YourNameHere[0]) << "'s score is " << Score[0] << endl;
        cout << (YourNameHere[1]) << "'s score is " << Score[1] << endl;
    return 0;
}

 Score[0] += iSecret;
 Score[1] += iSecret;
  } while (choice != 'n');
}
Use switch statements instead of so many ifs.
To change the player you could probably have a boolean variable that changes after every turn.
Can you give me an example browni? Would be greatly appreciated.
it hells you all about it here http://cplusplus.com/doc/tutorial/control/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

using namespace std;

int main()
{
    ...
    switch (iSecret)
    {
        case 1:
          diceone();
          break;
        case 2:
          dicetwo();
          break;
       ...


edit: @quirkyusername: You've got a small typo.
Last edited on
I personally dislike switch case statements... not that it matters, however if I were you I would turn that big block of code in the middle into one function.

1
2
3
4
5
void displayRoll(int number){
if (number == 1)
diceOne();
if (number...... you get the point
}


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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <math.h>

using namespace std;

void diceone ();
void dicetwo ();
void dicethree ();
void dicefour ();
void dicefive ();
void dicesix ();
void gameover ();

void diceone ()
{
	cout << " ----- \n"
		 << "|     |\n"
		 << "|  O  |\n"
		 << "|     |\n"
		 << " ----- \n";
}

void dicetwo ()
{
	cout << " ----- \n"
		 << "|  0  |\n"
		 << "|     |\n"
		 << "|  0  |\n"
		 << " ----- \n";
}

void dicethree ()
{
	cout << " ----- \n"
		 << "|  0  |\n"
		 << "|  0  |\n"
		 << "|  0  |\n"
		 << " ----- \n";
}

void dicefour ()
{
	cout << " ----- \n"
		 << "|0   0|\n"
		 << "|     |\n"
		 << "|0   0|\n"
		 << " ----- \n";
}

void dicefive ()
{
	cout << " ----- \n"
		 << "|0   0|\n"
		 << "|  0  |\n"
		 << "|0   0|\n"
		 << " ----- \n";
}

void dicesix ()
{
	cout << " ----- \n"
		 << "|0   0|\n"
		 << "|0   0|\n"
		 << "|0   0|\n"
		 << " ----- \n";
}

short rollDice(){
	short endRoll = rand() % 6 + 1;

	if (endRoll == 1){
		diceone();
	}
	else if (endRoll == 2){
		dicetwo();
	}
	else if (endRoll == 3){
		dicethree();
	}
	else if (endRoll == 4){
		dicefour();
	}
	else if (endRoll == 5){
		dicefive();
	}
	else if (endRoll == 6){
		dicesix();
	}
	return endRoll;
}

int main()
{
    int rollNumber;
	int *score;
	short playerNum = 2;
	
    char choice;
	
    srand (time(NULL));
	
    string current;
	
	cout << "How many players? : ";
	cin >> playerNum;
	
    string *players;
	players = new(string[playerNum]);
	score = new(int[playerNum]);
	
	for (int temp = 0; temp < playerNum; temp++){
		score[temp] = 0;	
	}
	
	for (int temp = 0; temp < playerNum; temp++){
        cout << "\n\nPlayer " << temp + 1 << ", please enter your name:" <<endl;
        cin >> players[temp];
        cout << "Hello " << players[temp] << endl;
        cout << "\n";
    }
	
	cout << "\nLets Go!\n";
	cin.get();
	while (choice != 'n'){
		
    for (int currentPlayer = 0; currentPlayer < playerNum; currentPlayer++){
		cout << endl << players[currentPlayer] << "'s turn";
		cout << "\nPush Enter To Roll\n";
		char input = cin.get();
		if (input != '\n'){
			input = cin.get();	
		}
		
        rollNumber = rollDice();
		
		score[currentPlayer] += rollNumber;
		
		cout << endl << players[currentPlayer] << "'s Total score = " << score[currentPlayer] << endl;
	}
		
        cout << "\n\nWould you like to role again?(Y/N)\n";
        cin >> choice;
		
		if (choice == 'n'){
			cout << "\nPlayer Scores";
			for (int temp = 0; temp < playerNum; temp++){
			cout << endl << players[temp] << "'s Total score = " << score[temp];
			}
			return 0;
		}
	}
	delete[] players;
	delete[] score;
	
}


sorry, got bored...


edit:whoops forgot to delete variables
Last edited on
C++0x allows this:

1
2
3
4
5
6
7
8
#include <functional>
#include <vector>

typedef std::function<void ()> dice_func_t;

std::vector<dice_func_t> dice = {diceone, dicetwo, dicethree, dicefour, dicefive, dicesix};
...
dice[iSecret - 1]();


This defines the function type of the diceXXX() functions, then creates a vector of dice functions initialized it to all of the defined functions. To execute the desired function, just look it up in the vector and call it.

This compiles with gcc 4.4 with "-std=c++0x". It may compile with VS2010 as well. Boost provides helpers to do most of that in C++98.

To answer your original question, you need a variable to track the player number. And then switch between 0 and 1. For example:

player = (player ? 0 : 1);

ultifinitus: better to use a vector rather than dynamic allocation.
Y'know something completely hilarious, the advanced computer programming class at my school literally just assigned this assignment. I'm starting to wonder if this is standard practice, do all comp prog teachers get their material from the same site? I'm sorry if this is seemingly pointless but I just couldn't help noticing...
Advanced? No offense, but this looks like more of a beginner assignment.
ultifinitus: better to use a vector rather than dynamic allocation.

I disagree.
@brownie3141: Hey man, I don't make the classes. It's also at a highschool level. And I'm not in the class, I just know the class and the teacher who teaches it. No offense taken.
Also: about vectors and dynamic allocation, vectors are harder to teach thouroughly (which means teaching the theory behind them) but dynamic allocation is harder to grasp the first time. As for use, I personally prefer dynamic allocation, but that's just me.
Topic archived. No new replies allowed.