Chess

Hey Guys,
I am writing a program for a chess game. I created classes for Game, Board and Piece, and derived classes under base class Piece for the different pieces (pawn, horse, etc.). Here is my code 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
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
#include "stdafx.h"
#include <iostream>
using namespace std;
class Piece
{
protected:
	char Color;
	char PieceName;
	int Value;
public:
	friend class Board;
	friend class ChessGame;
	virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]);
	
};

class Pawn : public Piece
{
public:
	friend class Board;
	Pawn(char PieceColor)
	{
		Color = PieceColor;
		PieceName = 'P';
		Value = 1;
	}

	~Pawn(){ }

	virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]) //Checking valid move for white pawns
	{
		if(Color == 'W') // Checking valid move for white pawns
		{
			if(CurrRow == 6 && CurrRow - DestRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid moves for first move of a white pawn
			{
				return true;
			}
			else
			if(CurrRow - DestRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for normalwhite pawn move
			{
				return true;
			}
			else
			if(CurrRow - DestRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'W') //Checking valid move for killing move of white pawn
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else																											//Checking valid move for blackpawns
		{
			if(CurrRow == 1 && DestRow - CurrRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for first move of a black pawns
			{
				return true;
			}
			else
			if(DestRow - CurrRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0)				//Checking valid move for normal white pawn move
			{
				return true;
			}
			else
			if(DestRow - CurrRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'B') //Checking valid move for killing move of a white pawn
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
};



//Similar classes for the other pieces

class Board
{
	Piece* Square[8][8];	
public:

	friend class ChessGame;
	friend class Piece;

	friend bool CheckValidMove(int, int, int, int, Piece*);
	Board()
	{ }//set all the pieces on the board
			}

	~Board()
	{
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				Square[i][j] = 0;
			}
		}
	}

	void DisplayBoard()
	{
		cout << "\n\t\t\t\t      Chess" << endl << endl;
		cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl;		
		cout << "" << (char) 179 << "                                    " << (char)179 << endl; 
		
		cout << "" << (char) 179 << "  " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << "  " << (char)179 << endl;
		for(int row = 0; row < 8; row++)
		{
			cout << "" << (char) 179 << " " << (char) 179 << "";
			if(row%2 == 0)
			{
				cout << "    ::::    ::::    ::::    ::::" << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row))  << "" << (char) 179 << "";
				for(int col = 0; col < 8; col++)
				{
					if(col % 2 == 0)
					{
						cout << " ";
						if(Square[row][col] == 0)
						{ cout << "  ";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << " ";
					}
					else
					{
						cout << ":";
						if(Square[row][col] == 0)
						{ cout << "::";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << ":";
					}
					if(col == 7)
					{
						cout << (char)179 << " " << (char)179;
					}
				}
				cout << "\n" << (char) 179 << " " << (char) 179 << "    ::::    ::::    ::::    ::::" << (char)179 << " " << (char)179 << "" << endl;
				
			}
			else
			if(row % 2 == 1)
			{
				cout << "::::    ::::    ::::    ::::    " << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row))  << "" << (char) 179 << "";
				for(int col = 0; col < 8; col++)
				{
					if(col % 2 == 0)
					{
						cout << ":";
						if(Square[row][col] == 0)
						{ cout << "::";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << ":";
					}
					else
					{
						cout << " ";
						if(Square[row][col] == 0)
						{ cout << "  ";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << " ";
					}
					if(col == 7)
					{
						cout << (char)179 << " " << (char)179;
					}
				}
				cout << "\n" << (char) 179 << " " << (char) 179 << "::::    ::::    ::::    ::::    " << (char)179 << " " << (char)179 << "" << endl;
				
			}
		}
		cout << "" << (char) 179 << "  " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << "  " << (char)179 << endl;
		cout << "" << (char) 179 << "    1   2   3   4   5   6   7   8   " << (char)179 << endl; 
		cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl << endl;
	}

	};

class ChessGame
{ } //Game content

int main()
{}


I am getting an error in the CheckValidMove() function in the Pawn class on line 48 and 69 that the Color member of piece class is protected and cannot be accessed through a Piece* pointer. However, on line 130 and 140, I am doing the same thing but no error is coming. Can someone please help me ?
In class piece you say that Board and ChessGame are friends of Piece which means that these 2 classes can access what's protected there. But you don't say the same thing about Pawn, so how do you expect it to be able to access it if you don't let him?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Piece
{
protected:
	char Color;
	char PieceName;
	int Value;
public:
        friend class Pawn; //add this
	friend class Board;
	friend class ChessGame;
	virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]);
	
};


aside that you have a lot of errors. Code a feature and test it!!! then move forward, after you see it works fine...don't code and after a few days compile only to see nothing works.

This code works fine:

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
#include "stdafx.h"
#include <iostream>
using namespace std;
class Piece
{
protected:
	char Color;
	char PieceName;
	int Value;
public:
	friend class Pawn;
	friend class Board;
	friend class ChessGame;
	virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]);

};

class Pawn : public Piece
{
public:
	friend class Board;
	Pawn(char PieceColor)
	{
		Color = PieceColor;
		PieceName = 'P';
		Value = 1;
	}

	~Pawn(){ }

	virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]) //Checking valid move for white pawns
	{
		if(Color == 'W') // Checking valid move for white pawns
		{
			if(CurrRow == 6 && CurrRow - DestRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid moves for first move of a white pawn
			{
				return true;
			}
			else
				if(CurrRow - DestRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for normalwhite pawn move
				{
					return true;
				}
				else
					if(CurrRow - DestRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'W') //Checking valid move for killing move of white pawn
					{
						return true;
					}
					else
					{
						return false;
					}
		}
		else																											//Checking valid move for blackpawns
		{
			if(CurrRow == 1 && DestRow - CurrRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for first move of a black pawns
			{
				return true;
			}
			else
				if(DestRow - CurrRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0)				//Checking valid move for normal white pawn move
				{
					return true;
				}
				else
					if(DestRow - CurrRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'B') //Checking valid move for killing move of a white pawn
					{
						return true;
					}
					else
					{
						return false;
					}
		}
	}
};



//Similar classes for the other pieces

class Board
{
	Piece* Square[8][8];	
public:

	friend class ChessGame;
	friend class Piece;

	friend bool CheckValidMove(int, int, int, int, Piece*);

	Board()
	{ }//set all the pieces on the board


	~Board()
	{
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				Square[i][j] = 0;
			}
		}
	}

	void DisplayBoard()
	{
		cout << "\n\t\t\t\t      Chess" << endl << endl;
		cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl;		
		cout << "" << (char) 179 << "                                    " << (char)179 << endl; 

		cout << "" << (char) 179 << "  " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << "  " << (char)179 << endl;
		for(int row = 0; row < 8; row++)
		{
			cout << "" << (char) 179 << " " << (char) 179 << "";
			if(row%2 == 0)
			{
				cout << "    ::::    ::::    ::::    ::::" << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row))  << "" << (char) 179 << "";
				for(int col = 0; col < 8; col++)
				{
					if(col % 2 == 0)
					{
						cout << " ";
						if(Square[row][col] == 0)
						{ cout << "  ";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << " ";
					}
					else
					{
						cout << ":";
						if(Square[row][col] == 0)
						{ cout << "::";} 
						else 
						{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
						cout << ":";
					}
					if(col == 7)
					{
						cout << (char)179 << " " << (char)179;
					}
				}
				cout << "\n" << (char) 179 << " " << (char) 179 << "    ::::    ::::    ::::    ::::" << (char)179 << " " << (char)179 << "" << endl;

			}
			else
				if(row % 2 == 1)
				{
					cout << "::::    ::::    ::::    ::::    " << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row))  << "" << (char) 179 << "";
					for(int col = 0; col < 8; col++)
					{
						if(col % 2 == 0)
						{
							cout << ":";
							if(Square[row][col] == 0)
							{ cout << "::";} 
							else 
							{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
							cout << ":";
						}
						else
						{
							cout << " ";
							if(Square[row][col] == 0)
							{ cout << "  ";} 
							else 
							{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
							cout << " ";
						}
						if(col == 7)
						{
							cout << (char)179 << " " << (char)179;
						}
					}
					cout << "\n" << (char) 179 << " " << (char) 179 << "::::    ::::    ::::    ::::    " << (char)179 << " " << (char)179 << "" << endl;

				}
		}
		cout << "" << (char) 179 << "  " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << "  " << (char)179 << endl;
		cout << "" << (char) 179 << "    1   2   3   4   5   6   7   8   " << (char)179 << endl; 
		cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl << endl;
	}

};

class ChessGame
{ }; //Game content

int main()
{

	return 0;
}
Thanks a lot .... but why should I include Pawn as a friend class ? Pawn is a derived class with Piece as the superclass. Shouldn't Pawn be able to access the "protected" member variables .... but anyways, your code works.
Pawn is a derived class with Piece as the superclass.

Yes

Shouldn't Pawn be able to access the "protected" member variables

You can...and you're doing it.

1
2
3
4
5
6
class Pawn : public Piece
{
public:
	virtual bool CheckValidMove(blah blah blah)
	{
		if(Color == 'W')  //this is actually Piece::Color you're calling, so it works 


But square is an independent variable of type Piece* which can't access protected/private members.

1
2
3
4
5
6
7
8
class Pawn : public Piece
{
public:

	virtual bool CheckValidMove(blah blah blah, Piece* Square[][8]) 	
{
                if(Color != 'W') //legal (Piece::Color)
		if(Square[DestRow][DestCol]->Color != 'W')  //this is illegal 


What you're trying to do is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

class A
{
public:

protected: 
	int v;
};

class B: public A
{
public:
	B()
	{
		v = 10; //legal as A is the base class for B
	}
};

int main()
{
	A *a;
	a->v; //illegal - can't access protected or private members;
	// does encapsulation ring a bell?
}

Last edited on
Ok thanks, got it
Topic archived. No new replies allowed.