change game direction

Hello,
I'm trying to change the direction of a game. In my example code it works:

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
#include <iostream>
#include <string>
using namespace std;

int playernumber;
bool stopthegame;
bool maexle;
int i;
int direction = 1;
int main()
{
	i = 0;
	

	cin >> playernumber;
string* player = new string[playernumber];
	for (int i = 0; i < playernumber; i++)
	{
		cout << "Player " << i << ": ";
		cin >> player[i];
	}

	while (stopthegame == false)
	{
	
		cout << "Player " << player[i] << endl;
		cin >> maexle;

		if (maexle)
		{
			direction = -1;
		}

		i = i + direction;

		if (i == -1)
		{
			i = playernumber - 1;
		}
		if (i == playernumber)
		{
			i = 0;
		}

		cout << "Game stop?" << endl;
		cin >> stopthegame;

	} 

	system("pause");
	return 0;
}


Bit i can't spot the mistake in the real code...

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

vector <int> strafpunkte(mSpieler);

	int wuerfel1, wuerfel2;
	bool abbruch = false;
	bool maexle = false;
	bool aufdecken = false;
	int direction = 1;
	int i = 0;
	while(abbruch == false)
	{
		
			//cout << mMitspieler[3] << endl;
			CWuerfel::zufallszahl();
			cout << mMitspieler[i] << ", bitte geben sie ihren 1. Wuerfel bekannt: ";
			cin >> wuerfel1;
			cout << mMitspieler[i] << ", bitte geben sie ihren 2. Wuerfel bekannt: ";
			cin >> wuerfel2;
			ausgabe(wuerfel1, wuerfel2);	
			
			if (wuerfel1 == 2 && wuerfel2 == 1)	//Mäxle
			{
				maexle = true;
				
			}
			else {
				
				cout << mMitspieler[i+1] << ", möchten Sie dieser Behauptung glauben schenken?";
				cin >> proof;
				if (wahl == proof)
				{
					aufdecken = true;
					
				}
			}

			if (maexle == true)
			{
				if ((wuerfel1 == max) && (wuerfel2 == min))
				{
					cout << mMitspieler[i] << " hat die Wahrheit gesagt." << endl;
					strafpunkte.at(i+1) += 1;
					cout << mMitspieler[i+1] << " hat jetzt " << strafpunkte.at(i+1) << " Strafpunkte." << endl;
					
				}
				else 
				{
					cout << mMitspieler[i] << " hat gelogen" << endl;
					strafpunkte.at(i) += 1;
					cout << mMitspieler[i] << " hat jetzt " << strafpunkte.at(i) << " Strafpunkte." << endl;
					
				}
				direction = -1;
				maexle = false;
			}
		

			if (aufdecken == true)
			{
				if ((max == wuerfel1) && (min == wuerfel2))
				{
						cout << mMitspieler[i] << " hat die Wahrheit gesagt." << endl;
						strafpunkte.at(i+1) += 1;
						cout << mMitspieler[i+1] << " hat jetzt " << strafpunkte.at(i+1) << " Strafpunkte." << endl;
						
				}
				else 
				{
						cout << mMitspieler[i] << " hat gelogen" << endl;
						strafpunkte.at(i) += 1;
						cout << mMitspieler[i] << " hat jetzt " << strafpunkte.at(i) << " Strafpunkte." << endl;
						
				}
				direction = -1;
				aufdecken = false;
			}

			i =+ direction;

			if (i == -1)
			{
				i = mSpieler -1;	//Rückwärts
			}
			if (i == mSpieler)
			{
				i = 0;		//Vorwärts
			}
		
	} 


Last edited on
Your while loop sets direction to -1. It never changes to 1. Once you start going backwards, you never go forwards again.

That aside, what makes you think there's a mistake in the real code? How do you know it isn't working properly?
Last edited on
i =+ direction;
Did you mean i += direction?
What does i even mean here? If you're not using i as a simple iteration variable, it probably deserves a better name.
i =+ direction;
yeah fault by my side after testing for to long where the problem is, without having an idea and yes i should definitly rename i

Your while loop sets direction to -1. It never changes to 1
true to - thx


That aside, what makes you think there's a mistake in the real code? How do you know it isn't working properly?


I have a class in which i get a dynamic array with the names of player in it. Are there 3 Players the 4th is automatically the "Computer". Are there more, you can decide whether you want him or not. After the programm gets this array the following function plays into account:

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
void CStart::round()
{
	vector <int> penalties(mNumberOfPlayers); //tried the vector stuff, could also be a dynamic array

	int wuerfel1, wuerfel2; //numbers put in by user/player
	bool leave = false; // exit programm
	bool maexle; //special case for game winning number
	bool checking = false; // next player can check the player before him
	int direction = 1; //after "maxle" change game direction ( b is checking if a is having maexle so the last player or Computer starts the new round)
	int arrayposition = 0; //current player

	while (leave == false)
	{

		
		CWuerfel::randomnumber(); //two random numbers in int min and int max stored
		cout << mPlayer[arrayposition] << ", pls give your first number: "; 
		cin >> wuerfel1;
		cout << mPlayer[arrayposition] << ",pls give your second number: ";
		cin >> wuerfel2;
		output(wuerfel1, wuerfel2);

		if (wuerfel1 == 2 && wuerfel2 == 1)	//Mäxle, special case
		{
			maexle = true;

		}
		else {

			cout << mPlayer[arrayposition + 1] << ", will you trust?";
			cin >> proof;
			if (wahl == proof)
			{
				checking = true;

			}
		}

		if (maexle == true)
		{
			if ((wuerfel1 == max) && (wuerfel2 == min))
			{
				cout << mPlayer[arrayposition] << " told the truth." << endl;
				penalties.at(arrayposition + 1) += 1;
				cout << mPlayer[arrayposition + 1] << " has now " << penalties.at(arrayposition + 1) << " penalties." << endl;

			}
			else
			{
				cout << mPlayer[arrayposition] << " lied" << endl;
				penalties.at(arrayposition) += 1;
				cout << mPlayer[arrayposition] << " has now " << penalties.at(arrayposition) << " penalties." << endl;

			}
			direction = -1; //Change direction

		}


		else if (checking == true)
		{
			if ((max == wuerfel1) && (min == wuerfel2))
			{
				cout << mPlayer[arrayposition] << " told the truth" << endl;
				penalties.at(arrayposition + 1) += 1;
				cout << mPlayer[arrayposition + 1] << " has now " << penalties.at(arrayposition + 1) << " penalties." << endl;

			}
			else
			{
				cout << mPlayer[arrayposition] << " lied" << endl;
				penalties.at(arrayposition) += 1;
				cout << mPlayer[arrayposition] << " has now " << penalties.at(arrayposition) << " penalties." << endl;

			}
			direction = -1; //Change direction

		}
               else {
                         direction = 1; //normal direction
               }

		arrayposition += direction;

		if (arrayposition == -1)
		{
			arrayposition = mNumberOfPlayers - 1;	//Backwards
		}
		if (arrayposition == mNumberOfPlayers)
		{
			arrayposition = 0;		//Vorwards
		}

	}
}


The code works for more players then 3. But when the number is 3, so the "computer" is added, he gets overtaken when a change of direction accures. So if a has maexle, next round player c starts instead of "Computer", when I write:
cout << mPlayer[3] << endl; i get "Computer" as an output, so theoretically he should be there. Heres my Constructor function:

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
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
{
	mPlayer = new string[pPlayer];


	if (pPlayer == 3)
	{
		// special case: will actually be 4 players
		// with the 4th player being the computer
		mPlayer = new string[4];
		mPlayer[3] = computer;
	}
	else
	{
		//With Computer or without?
		mPlayer = new string[pPlayer];
		cout << "DO you like to play with Computer? 1 = yes; 0 = no ";
		cin >> wahl;
		if (wahl)	//Computer gewünscht
		{
			mPlayer = new string[pPlayer + 1];	//Array plus 1
			mPlayer[pPlayer] = computer;		//Computer adding
		}
	}
	//Name request
	for (int i = 0; i < pPlayer; i++)
	{
		cout << "Player " << i + 1 << ", pls input your name: ";
		cin >> mPlayer[i];

	}
	//choosed computer or pPlayer forces it -> ammount of Players +1
	if ((pPlayer == 3) || (wahl))
	{
		pPlayer += 1;
		cout << "Computer was added as a Player." << endl;
	}
}

I hope i made my problem more understandable,
thx for your answers
Last edited on
I changed the code a bit, its working until i get following error message:

Ausnahme ausgelöst bei 0x7B0FF2EF (ucrtbased.dll) in Hausarbeit_20.07.09.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00FA2000.


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
void CStart::runde()
{
	vector <int> strafpunkte(mSpieler);

	int wuerfel1, wuerfel2;
	bool abbruch = false;
	bool maexle = false;
	bool aufdecken = false;
	int direction = 1;
	int arrayposition = 0;
	while(abbruch == false)
	{
		

			

			//cout << mMitspieler[3] << endl;
			CWuerfel::zufallszahl();
			cout << mMitspieler[arrayposition] << ", bitte geben sie ihren 1. Wuerfel bekannt: ";
			cin >> wuerfel1;
			cout << mMitspieler[arrayposition] << ", bitte geben sie ihren 2. Wuerfel bekannt: ";
			cin >> wuerfel2;
			ausgabe(wuerfel1, wuerfel2);	
			
			if (wuerfel1 == 2 && wuerfel2 == 1)	//Mäxle
			{
				maexle = true;
				
			}
			else {
				
				cout << mMitspieler[arrayposition + direction] << ", möchten Sie dieser Behauptung glauben schenken?";
				cin >> proof;
				if (wahl == proof)
				{
					aufdecken = true;
					
				}
			}

			if (maexle == true)
			{
				if ((wuerfel1 == max) && (wuerfel2 == min))
				{
					cout << mMitspieler[arrayposition] << " hat die Wahrheit gesagt." << endl;
					strafpunkte.at(arrayposition + direction) += 1;
					cout << mMitspieler[arrayposition + direction] << " hat jetzt " << strafpunkte.at(arrayposition + direction) << " Strafpunkte." << endl;
					
				}
				else 
				{
					cout << mMitspieler[arrayposition] << " hat gelogen" << endl;
					strafpunkte.at(arrayposition) += 1;
					cout << mMitspieler[arrayposition] << " hat jetzt " << strafpunkte.at(arrayposition) << " Strafpunkte." << endl;
					
				}
				if (direction == -1)
				{
					direction = 1;
				}
				else
				{
					direction = -1;
				}
				maexle = false;
			}
		

			else if (aufdecken == true)
			{
					if ((max == wuerfel1) && (min == wuerfel2))
					{
						cout << mMitspieler[arrayposition] << " hat die Wahrheit gesagt." << endl;
						strafpunkte.at(arrayposition + direction) += 1;
						cout << mMitspieler[arrayposition + direction] << " hat jetzt " << strafpunkte.at(arrayposition + direction) << " Strafpunkte." << endl;
						
					}
					else 
					{
						cout << mMitspieler[arrayposition] << " hat gelogen" << endl;
						strafpunkte.at(arrayposition) += 1;
						cout << mMitspieler[arrayposition] << " hat jetzt " << strafpunkte.at(arrayposition) << " Strafpunkte." << endl;
						
					}
					if (direction == -1)
					{
						direction = 1;
					}
					else
					{
						direction = -1;
					}
					aufdecken = false;
			}
						
		arrayposition += direction;
		if (arrayposition == -1)
			{
				arrayposition = mSpieler -1;	//Rückwärts
			}
		if (arrayposition == mSpieler)
			{
				arrayposition = 0;		//Vorwärts
			}
		
	} 
}
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
Is that a typo? You throw away this value and set mPlayer in the first statement of the constructor. You also never save the value of pPlayer. Should you?

You also overwrite that value at line 10 or 16, which leaks memory.

And you overwrite the value from line 16 at line 21, leaking memory again.
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
Is that a typo? You throw away this value and set mPlayer in the first statement of the constructor. You also never save the value of pPlayer. Should you?

i give pPlayer to mPlayer, so its shouldnt be thrown away? So its saved in mPlayer?

You also overwrite that value at line 10 or 16, which leaks memory.

you mean i should use vectors? But I thought i just add one extra space to the Array, to add the player "Computer"
Last edited on
i give pPlayer to mPlayer, so its shouldnt be thrown away? So its saved in mPlayer?


This just makes no sense. You're leaking memory. What is pPlayer?

Whatever is going on here just makes no sense. Beginners shouldn't use new, shouldn't use arrays, shouldn't be manually managing memory. Just use a vector.
I thought i just add one extra space to the Array

No. when you say mPlayer = new string[pPlayer + 1]; it does exactly what it says: new string[pPlayer+1] creates an array of strings with pPlayer+1 items in it. Then it assigns the address of the array to mPlayer. If you wanted to save or delete the previous value of mPlayer, that's up to you.

1
2
3
CStart::CStart(int pPlayer) :mPlayer(pPlayer)
{
	mPlayer = new string[pPlayer];

On line1, you initialize mPlayer with pPlayer. The rest of the code seems to indicate that mPlayer is a pointer, so it's not clear that this initialization will compile. It certainly doesn't make sense.

Then line 3 creates an array of strings assigns mPlayer to the address of an array. So the assignment at line 1 has no effect.

Follow Repeater's advice and use a vector instead. It handles the memory management for you.

ok, but how do i handle this vectors? When i "initialize"? them in
1
2
3
4
5
6

CStart::CStart(int pSpieler){
vector <string> mPlayers(pSpieler); // like this
vector <int> mPenalties(pSpieler);


i cant use the vectors in e.g. :
1
2
3
4
5
6
7
void CStart::get()
{
 for(int i = 0; i < pSpieler; i++)
{
  cout << mMitspieler.at(i) << endl; // gives error, cant find it
}
}


so whats the correct way of doing it?
Don't post the same question in two different threads. It wastes people's time.
Last edited on
Topic archived. No new replies allowed.