problem with game.

Gameboard not working.
code///
Last edited on
@Mikeyy222

You have choice as an int, but checking as a char. You also aren't checking if the player is choosing a space already used. I fiddled with your code, and now you can place your stone at a letter area on the board, and you must choose a different space if the one typed, was already used.

Your player variable is being used wrong, also. You keep increasing player number, but there are only two players. That's been corrected as well.

You also aren't keeping track of how many stones have been placed. Anyway, here's your code with my corrections.
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
using namespace std;

char points[8][3] =
{
	{'A','B','C'},
	{'D','E','F'},
	{'G','H','I'},
	{'J','K','L'},
	{'M','N','O'},
	{'P','Q','R'},
	{'S','T','U'},
	{'V','W','X'},
};

char stone[2] = {'#','$'};

void board()
{
	cout <<"Player 1: " << stone[0] << endl << "Player 2: " <<stone[1] <<endl <<endl <<endl;
	cout << points[0][0] << "--------" << points[0][1] << "--------" << points[0][2] <<endl
		<< "|        |        |" <<endl;
	cout << "|  " << points[1][0] << "-----" << points[1][1] << "-----" << points[1][2] << "  |" <<endl
		<< "|  |     |     |  |" <<endl;
	cout << "|  |  " << points[2][0] << "--" << points[2][1] << "--" << points[2][2] <<"  |  |"<<endl;
	cout << "|  |  |" <<"     " <<"|  |  |" <<endl;
	cout << points[3][0] <<"--" << points[3][1] <<"--" << points[3][2] <<"     " << points[4][0] <<"--" << points[4][1] <<"--" << points[4][2] <<endl;
	cout << "|  |  |" <<"     " <<"|  |  |" <<endl;
	cout << "|  |  " << points[5][0] << "--" << points[5][1] << "--" << points[5][2] <<"  |  |"<<endl 
		<< "|  |     |     |  |" <<endl;
	cout << "|  " << points[6][0] << "-----" << points[6][1] << "-----" << points[6][2] << "  |" <<endl
		<< "|        |        |" <<endl;
	cout << points[7][0] << "--------" << points[7][1] << "--------" << points[7][2] <<endl;
}

int checkwin();

int main()
{
	int player = 0,i=-1;
	bool ok=false;
	char choice;	
	do
	{
		board();
		cout << "Player " << player+1 << " [" << stone[player] << "], enter a letter: " << endl;
		choice = ' ';
		ok=false;
		cin >> choice;
		choice = toupper(choice);
		cout << choice;		
		if(choice >='A' && choice <='X')
		{
			if (choice == 'A' && points[0][0] == 'A')
			{
				ok=true;//Mark as a valid move
				points[0][0] = stone[player];
			}
			else if (choice == 'B' && points[0][1] == 'B')
			{
				ok=true;
				points[0][1] = stone[player];
			}    
			else if (choice == 'C' && points[0][2] == 'C')
			{
				ok=true;
				points[0][2] = stone[player];
			}  
			else if (choice == 'D' && points[1][0] == 'D')
			{
				ok=true;
				points[1][0] = stone[player];
			}    
			else if (choice == 'E' && points[1][1] == 'E')
			{
				ok=true;
				points[1][1] = stone[player];
			}    
			else if (choice == 'F' && points[1][2] == 'F')
			{
				ok=true;
				points[1][2] = stone[player];
			}   
			else if (choice == 'G' && points[2][0] == 'G')
			{
				ok=true;
				points[2][0] = stone[player];
			}   
			else if (choice == 'H' && points[2][1] == 'H')
			{
				ok=true;
				points[2][1] = stone[player];
			}    
			else if (choice == 'I' && points[2][2] == 'I')
			{
				ok=true;
				points[2][2] = stone[player];
			}    
			else if (choice == 'J' && points[3][0] == 'J')
			{
				ok=true;
				points[3][0] = stone[player];
			}    
			else if (choice == 'K' && points[3][1] == 'K')
			{
				ok=true;
				points[3][1] = stone[player];
			}    
			else if (choice == 'L' && points[3][2] == 'L')
			{
				ok=true;
				points[3][2] = stone[player];
			}
			else if (choice == 'M' && points[4][0] == 'M')
			{
				ok=true;
				points[4][0] = stone[player];
			}    
			else if (choice == 'N' && points[4][1] == 'N')
			{
				ok=true;
				points[4][1] = stone[player];
			}    
			else if (choice == 'O' && points[4][2] == 'O')
			{
				ok=true;
				points[4][2] = stone[player];
			}    
			else if (choice == 'P' && points[5][0] == 'P')
			{
				ok=true;
				points[5][0] = stone[player];
			}    
			else if (choice == 'Q' && points[5][1] == 'Q')
			{
				ok=true;
				points[5][1] = stone[player];
			}    
			else if (choice == 'R' && points[5][2] == 'R')
			{
				ok=true;
				points[5][2] = stone[player];
			}    
			else if (choice == 'S' && points[6][0] == 'S')
			{
				ok=true;
				points[6][0] = stone[player];
			}    
			else if (choice == 'T' && points[6][1] == 'T')
			{
				ok=true;
				points[6][1] = stone[player];
			}    
			else if (choice == 'U' && points[6][2] == 'U')
			{
				ok=true;
				points[6][2] = stone[player];
			}    
			else if (choice == 'V' && points[7][0] == 'V')
			{
				ok=true;
				points[7][0] = stone[player];
			}    
			else if (choice == 'W' && points[7][1] == 'W')
			{
				ok=true;
				points[7][1] = stone[player];
			}    
			else if (choice == 'X' && points[7][2] == 'X')
			{
				ok=true;
				points[7][2] = stone[player];
			}
			if(ok)
			{
				player = !player;
				// If valid, other player's turn
				// otherwise, same player
			}
		}
		if(!ok)
		{
			cout <<"Invalid move" << endl; // NOT a valid move
		}

	}while(i==-1);
	board();
	if(i==1)

		cout <<"Congratulations, \aPlayer "<<stone[player]<<" wins the game! ";
	else 
		cout <<"Wow! \aA draw!";      


	cin.ignore();
	cin.get();  
	system("PAUSE");   
	return 0;
}
how would i move the stones and remove the stone, what function would i need?
Are you required to input board positions by letter? It would be far easier to enter the row and column.

If you must enter positions by letter then write a function to convert the letter to row/column:
void getRowAndCol(char letter, int &row, int &col);
Then lines 54-130 become:
1
2
3
4
5
6
7
int row, col;
getRowAndCol(choice, row, col);
if (points[row][col] == choice) {
    points[row][col] = stone[player];
} else {
    cout <<"Invalid move" << endl
};

@dhayden

yea, that makes sense. It works. thanks.
now just trying the figure out the rest
Last edited on
Topic archived. No new replies allowed.