I need help with a checkers C++ program, I'm not sure what's wrong.

I'm fairly new to C++ and was trying to make a checkers program. I'm getting an error once I type the x and y coordinates for the piece I'm trying to move. Side note: It's not finished yet.

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Main.cpp:
#include <iostream>
#include <string>
#include "CheckerBoard.h"

using namespace std;

char turn = '1';

int main()
{
	bool isDone = false;

	CheckerBoard checkerBoard;

	while (isDone == false)
	{
		checkerBoard.initBoard();
		checkerBoard.placePieces();
		checkerBoard.printBoard();
		

		checkerBoard.movePieces(turn);

		if (turn == '1')

		system("PAUSE");
	}

	return 0;
}

CheckerBoard.h:
#pragma once
class CheckerBoard
{
public:
	CheckerBoard(void);
	void initBoard();
	void printBoard();

	int getXCoord();
	int getYCoord();
	int getXCoordOfPiece();
	int getYCoordOfPiece();

	void movePieces(char turn);
	void placePieces();


private: 
	char player1 = 'R';
	char player2 = 'B';

	char board[8][8];

	int yInput;
	int xInput;

	int yCoord;
	int xCoord;
};

CheckerBoard.cpp:
#include "CheckerBoard.h"
#include <iostream>

using namespace std;

CheckerBoard::CheckerBoard(void)
{
}

void CheckerBoard::initBoard()
{
	for (int y = 0; y < 8; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			if (((x + y + 1) % 2) == 0)
			{
				board[y][x] = ' ';
			}
			else if (((x + y + 1) % 2) == 1)
			{
				board[y][x] = ' ';
			}
			
		}
	}
}

void CheckerBoard::printBoard()
{ 
	cout << "  | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\n";
	cout << "-----------------------------------\n";

	for (int y = 0; y < 8; y++)
	{
		static int x = 0;
		cout << y + 1  << " | ";
		for (int x = 0; x < 8; x++)
		{
			cout << board[y][x] << " | ";
		}
		cout << endl << "-----------------------------------" << endl;
	}
}

int CheckerBoard::getXCoordOfPiece()
{
	int xInput;
	cout << "Enter X Coordinate of piece you'd like to move: ";
	cin >> xInput;
	return xInput;
}

int CheckerBoard::getYCoordOfPiece()
{
	int yInput;
	cout << "Enter Y Coordinate of piece you'd like to move: ";
	cin >> yInput;
	return yInput;

}

int CheckerBoard::getYCoord()
{
	int yCoord;
	cout << "Enter Y Coordinate of where you want to move.";
	cin >> yCoord;
	return yCoord;
}

int CheckerBoard::getXCoord()
{
	int xCoord;
	cout << "Enter X Coordinate of where you want to move.";
	cin >> xCoord;
	return xCoord;
}

void CheckerBoard::movePieces(char turn)
{
	if (turn == '1')
	{
		getXCoordOfPiece();
		getYCoordOfPiece();
		if (board[yInput][xInput] == player1)
		{
			getXCoord();
			getYCoord();
			if (board[yCoord][xCoord] == player1)
			{
				cout << "That move is invalid!";
				return;
			}
			else if (board[yCoord][xCoord] == player2)
			{
				cout << "That move is invalid!";
				return;
			}
			else
			{
				board[yCoord][xCoord] = player1;
			}
		}
	}

	if (turn == '2')
	{
		getXCoordOfPiece();
		getYCoordOfPiece();
		if (board[yInput][xInput] == player1)
		{
			getXCoord();
			getYCoord();
			if (board[yCoord][xCoord] == player1)
			{
				cout << "That move is invalid!";
				return;
			}
			else if (board[yCoord][xCoord] == player2)
			{
				cout << "That move is invalid!";
				return;
			}
			else
			{
				board[yCoord][xCoord] = player2;
			}
		}
	}
}

void CheckerBoard::placePieces()
{
	for (int y = 0; y < 3; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			if (((x + y) % 2) == 0)
			{
				board[y][x] = player1;
			}
		}
	}

	for (int y = 5; y < 8; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			if (((x + y) % 2) == 1)
			{
				board[y][x] = player2;
			}
		}
	}
}
Last edited on
Your code doesn't compile:

line 19: error C2864: 'CheckerBoard::player1' : only static const integral data members can be initialized within a class
line 20: error C2864: 'CheckerBoard::player2' : only static const integral data members can be initialized within a class


In getXCoordOfPiece() and getYCoordOfPiece() you store the input in local vars xInput and yInput.
In CheckerBoard::movePieces() you access the member variables with the same name which have a random value. Accessing board[yInput][xInput] will most of the time cause a crash.
@Thomas1965, in C++11, it is allowed to initialize class members this way. It depends on your compiler settings whether it will compile or not.
@Shadowwolf,
in C++11, it is allowed to initialize class members this way.

I didn't know that. VS2010 doesn't support all C++11
@Thomas1965
Visual Studio supports them as of VS2013, in earlier versions they were not supported.
@Shadowwolf,

I have VS 2013 Community but it is as sluggish as Eclipse - takes about 20 seconds to start. :(
I have VS 2015 Community if that helps. I still haven't figured out how to get the program to work and still need help. I've tried to fix it and figure it out but it still doesn't work.
main.cpp:

#include <iostream>
#include <string>
#include "CheckerBoard.h"

using namespace std;

int main()
{
	CheckerBoard checkerBoard;

	checkerBoard.playGame();

	return 0;
}


CheckerBoard.h:

#pragma once
class CheckerBoard
{
public:
	CheckerBoard(void);
	void initBoard();
	void printBoard();

	int getXCoord();
	int getYCoord();
	int getXCoordOfPiece();
	int getYCoordOfPiece();


	int xCoord = getXCoord();
	int yCoord = getYCoord();
	int yInput = getYCoordOfPiece();
	int xInput = getXCoordOfPiece();

	void movePieces(char turn, int  yInput, int xInput, int yCoord, int xCoord);
	void placePieces();

	void playGame();

private: 
	char turn = '1';

	char player1 = 'R';
	char player2 = 'B';

	char board[8][8];
};


CheckerBoard.cpp:

#include "CheckerBoard.h"
#include <iostream>

using namespace std;

CheckerBoard::CheckerBoard(void)
{
}

void CheckerBoard::initBoard()
{
	for (int y = 0; y < 8; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			board[y][x] = ' ';
		}
	}
}

void CheckerBoard::printBoard()
{ 
	cout << "  | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\n";
	cout << "-----------------------------------\n";

	for (int y = 0; y < 8; y++)
	{
		int x = 0;
		cout << y + 1  << " | ";
		for (int x = 0; x < 8; x++)
		{
			cout << board[y][x] << " | ";
		}
		cout << endl << "-----------------------------------" << endl;
	}
}

int CheckerBoard::getXCoordOfPiece()
{
	int xInput;
	cout << "Enter X Coordinate of piece you'd like to move: ";
	cin >> xInput;
	return xInput - 1;
}

int CheckerBoard::getYCoordOfPiece()
{
	int yInput;
	cout << "Enter Y Coordinate of piece you'd like to move: ";
	cin >> yInput;
	return yInput - 1;

}

int CheckerBoard::getYCoord()
{
	int yCoord;
	cout << "Enter Y Coordinate of where you want to move.";
	cin >> yCoord;
	return yCoord - 1;
}

int CheckerBoard::getXCoord()
{
	int xCoord;
	cout << "Enter X Coordinate of where you want to move.";
	cin >> xCoord;
	return xCoord - 1;
}

void CheckerBoard::movePieces(char turn, int  yInput, int xInput, int yCoord, int xCoord)
{
	if (turn == '1')
	{
		xInput = getXCoordOfPiece();
		yInput = getYCoordOfPiece();
		if (board[yInput][xInput] == player1)
		{
			xCoord = getXCoord();
			yCoord = getYCoord();
			if (board[yCoord][xCoord] == player1)
			{
				cout << "That move is invalid!";
				cout << yCoord << xCoord;
				system("PAUSE");
				return;
			}
			else if (board[yCoord][xCoord] == player2)
			{
				cout << "That move is invalid!";
				cout << yCoord << xCoord;
				system("PAUSE");
				return;
			}
			else
			{
				board[yInput][xInput] = ' ';
				board[yCoord][xCoord] = player1;
			}
		}
	}

	if (turn == '2')
	{
		xInput = getXCoordOfPiece();
		yInput = getYCoordOfPiece();
		if (board[yInput][xInput] == player1)
		{
			xCoord = getXCoord();
			yCoord = getYCoord();
			if (board[yCoord][xCoord] == player1)
			{
				cout << "That move is invalid!";
				cout << yCoord << xCoord;
				system("PAUSE");
				return;
			}
			else if (board[yCoord][xCoord] == player2)
			{
				cout << "That move is invalid!";
				cout << yCoord << xCoord;
				system("PAUSE");
				return;
			}
			else
			{
				board[yInput][xInput] = ' ';
				board[yCoord][xCoord] = player2;
			}
		}
	}
}

void CheckerBoard::placePieces()
{
	for (int y = 0; y < 3; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			if (((x + y) % 2) == 0)
			{
				board[y][x] = player1;
			}
		}
	}

	for (int y = 5; y < 8; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			if (((x + y) % 2) == 1)
			{
				board[y][x] = player2;
			}
		}
	}
}

void CheckerBoard::playGame()
{
	bool isDone = false;

	while (isDone == false)
	{
		
		initBoard();
		placePieces();
		printBoard();


		movePieces(turn, yInput, xInput, yCoord, xCoord);

		if (turn == '1') 
		{
			turn = '2';
		}
		else if (turn == '2')
		{
			turn = '1';
		}
			system("PAUSE");
	}
}
Last edited on
I'm getting an error once I type the x and y coordinates for the piece I'm trying to move.

Typically you'll want to be more specific than "I'm getting an error." If you're getting an error, presumably there is some sort of text or behavior you're seeing that leads you to believe this. Describing that would be helpful.

You have several members of your class that are never initialized or assigned values, but are used as if they have some meaningful value (xCoord, xInput, yCoord, etc.) Since they don't have meaningful values and you use them to index an array, you're likely to be accessing outside the bounds of that array which is a big no-no.

In:
1
2
3
4
5
6
7
int CheckerBoard::getXCoordOfPiece()
{
	int xInput;
	cout << "Enter X Coordinate of piece you'd like to move: ";
	cin >> xInput;
	return xInput;
}

xInput is a local variable which stops existing when the function returns. It is not the data member that is declared in the class definition. These functions return values. You ignore/discard the returned values.
Topic archived. No new replies allowed.