array values

I wrote a little connect 4 game (unfinished).
If i enter 0, he also throws a token into row 6, and vice-versa.
EDIT: The question is: Why is he doing that?
The array values get manipulated by "Eingabe", and nowhere else.
And Eingabe itself doesn't get manipulated, it always stays "original".
Iam a Beginner, both in C++ and english. Feel free to improve both :)

The whole "input -48" thing has something to do with used characted set, i guess. I didn't know how to change it, so i worked around.

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
#include <iostream>		//cout
#include <conio.h>		//getche
using namespace std;
void Ausgabe();
int table[5][6];		//The array representing the game

void Fallen(int Eingabe)
{
	if (Eingabe >= 0 && Eingabe <= 6)
	{
		if (table[0][Eingabe] == 1 || table[0][Eingabe] == 2)		//gridcolumn full?
		{
			cout << "Diese Reihe ist bereits voll!\n";
			return;
		}
		for (int i = 5; i >= 0; i--)
			if (table[i][Eingabe] == 0)                      //change the desired token location: Remember, the token falls down
				{
					table[i][Eingabe] = 1;
					Ausgabe();
					break; 
				}
	}
	else cout << "Bitte eine Zahl zwischen 0 und 6 eingeben!\n";
}



void Ausgabe()
{
	system("cls");				//clear the console. I know using "system" is a bad idea, but it works for me.
	cout << "     \xc9\xcd\xcb\xcd\xcb\xcd\xcb\xcd\xcb\xcd\xcb\xcd\xcb\xcd\xbb\n";	//First gridrow
	for (int i1 = 0; i1 <= 5; i1++)
	{
		cout << "     \xba";
		for (int i2 = 0; i2 <= 6; i2++)								//Draw the Tokens
		{	
			if (table[i1][i2] == 0) cout << " \xba";				// nothing for an empty space
			else if (table[i1][i2] == 1) cout << "O\xba";			// O for Player 1
			else if (table[i1][i2] == 2) cout << "X\xba";			// X for Player 2, currently not implemented
			else cout << "E\xba";
			if ((i1 == 5) && (i2 == 6)) 
			{
				cout << "\n     \xc8\xcd\xca\xcd\xca\xcd\xca\xcd\xca\xcd\xca\xcd\xca\xcd\xca\n      0 1 2 3 4 5 6\n";	//Last gridrow
				return;
			}
		}
		cout << "\n     \xCC\xcd\xce\xcd\xce\xcd\xce\xcd\xce\xcd\xce\xcd\xce\xcd\xb9\n";	//gridrow between each "tokenline"
	}
}

void leeren()		//fill table with 0
{for (int i1 = 0; i1 <= 5; i1++) {
		for (int i2 = 0; i2 <= 6; i2++) 
			table[i1][i2] = 0;}}



int main()
{		
	cout << "Willkommen zu meinem kleinen 4gewinnt-Projekt.\nZurzeit koennen nur 2 menschliche Spieler gegeneinander antreten.\n\n\n";    
	leeren();
	/*for (int i1 = 0; i1 <= 5; i1++) {
		for (int i2 = 0; i2 <= 6; i2++)
		{
			cout << "Table[" << i1 << "][" << i2 << "]=" << table[i1][i2] << endl;
		}
	}
	getchar(); */ //array value check
	Ausgabe();
	while (true)
		Fallen(getche()-48);
}
Last edited on
Do you have any question?
Arrays in C++ are accessed starting at zero, so int table[5][6] has valid values at table[0][0] through table[4][5]. Your code assumes valid values in table[0][0] through table[5][6]. If that's what you want, then you need to declare table as int table[6][7] at line 5.

Line 72 would be better written as Fallen(getche()-'0');. On this line, the program calls getche() which returns a single ASCII character, presumably '0' through '9'. Fallen() expects its argument to be an integer, so to convert the ASCII character to the integer 0, you subtract '0'. If the user enters '0', the result is 0. If they enter '1', the result is 1, etc.

Your English is pretty good. :)
Topic archived. No new replies allowed.