Chessboard detour

As I have posted in previous posts in this section- I take time away from the course to attempt to put things together based on the things we learned so far.

I thought that it would be fun to put together a c-prompt version of a board game classic- chess.

01

My code as of right now only constructs the layout of our board and pieces; I’ll continue to add to this and edit as we go through the course.

The main point of this practice today was revisiting the For and if statements.

Also; I touched lightly in ‘multidimensional’ arrays and enumerators. The " int Chessboard[8][8]" declared an array of 8 rows by 8 columns.

My next version will likely be built and changed in the header file via classes.

But for now; to carry on with our lectures. Hope you guys like it!

int Chessboard[8][8];
void PrintBoard();
void InitializeBoard();

int main()
{
	InitializeBoard();
	PrintBoard();

	
	std::string Hold;
	std::getline(std::cin, Hold);
	
	
	return 0;
}



void PrintBoard()
{
	for (int print = 0; print <= 7; print++)
	{
		std::cout << "\n";
		for (int print2 = 0; print2 <= 7; print2++)
		{
			if (Chessboard[print][print2] < 9)
			{
				std::cout << " ";
			};
			std::cout << "  " << Chessboard[print][print2];
		}

	};
	return;
}

void InitializeBoard()
{

	//setting values to black pieces

	enum Blackpiece
	{
		Empty = 0,
		Bpawn,
		Brook,
		Bknight,
		Bbishop,
		Bqueen,
		Bking,
	};
	// setting values to white pieces
	enum Whitepiece
	{
		Wpawn = 11,
		Wrook,
		Wknight,
		Wbishop,
		Wqueen,
		Wking,
	};
	//setting values to white pieces
	//Initialize values for the chessboard

	Chessboard[0][0] = Chessboard[0][7] = Wrook;
	Chessboard[0][1] = Chessboard[0][6] = Wknight;
	Chessboard[0][2] = Chessboard[0][5] = Wbishop;
	Chessboard[0][3] = Wqueen;
	Chessboard[0][4] = Wking;
	Chessboard[1][0] = Chessboard[1][1] = Chessboard[1][2] = Chessboard[1][3] = Chessboard[1][4] = Chessboard[1][5] = Chessboard[1][6] = Chessboard[1][7] = Wpawn;

	Chessboard[6][0] = Chessboard[6][1] = Chessboard[6][2] = Chessboard[6][3] = Chessboard[6][4] = Chessboard[6][5] = Chessboard[6][6] = Chessboard[6][7] = Bpawn;
	Chessboard[7][0] = Chessboard[7][7] = Brook;
	Chessboard[7][1] = Chessboard[7][6] = Bknight;
	Chessboard[7][2] = Chessboard[7][5] = Bbishop;
	Chessboard[7][3] = Bking;
	Chessboard[7][4] = Bqueen;


}
1 Like

UPDATE:

The board layout has been updated; and I figured out that an array can be strings - so the new look is much easier to understand.

Also; now features player 1 and player 2 input. With a check on validity of selection. As of right now; you do not need to input a valid move after selecting the piece. I’ll need to do that in the next version.

Here is the updated ‘Main.cpp’

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

int main()
{

	ChessTools ChessGame;
	ChessGame.Reset();

	ChessGame.PrintBoard();
	

	do
	{
		bool Player1 = false;
		bool Player2 = false;
		do
		{
			Player1 = ChessGame.Player1();
		} while (Player1);
		do
		{
			Player2 = ChessGame.Player2();
		} while (Player2);
		ChessGame.PrintBoard();
	}while (ChessGame.GameOver());

	std::string Hold;
	std::getline(std::cin, Hold);


	return 0;
}

Here is the new header file

#pragma once
#include <iostream>
#include <string>


enum class Errors
{
	Invalid_Move,
	Invalid_Piece,
	Okay,
};

class ChessTools
{
public:

	ChessTools();	 //Constructor
	bool GameOver();

	void Reset(); //Resets the initial board
	void PrintBoard();


	bool Player1();//TODO
	bool Player2();
	




private:
	int xcoord;
	int ycoord;
	std::string Chessboard[8][8];
};

and finally - the .cpp associated tot he header file

#include "Chess.h"

ChessTools::ChessTools()
{
}

bool ChessTools::GameOver()
{
	return true;
}

void ChessTools::Reset()
{
	Chessboard[0][0] = Chessboard[0][7] = "R";
	Chessboard[0][1] = Chessboard[0][6] = "N";
	Chessboard[0][2] = Chessboard[0][5] = "B";
	Chessboard[0][3] = "Q";
	Chessboard[0][4] = "K";
	Chessboard[1][0] = Chessboard[1][1] = Chessboard[1][2] = Chessboard[1][3] = Chessboard[1][4] = Chessboard[1][5] = Chessboard[1][6] = Chessboard[1][7] = "P";

	for (int blankboard = 2; blankboard <= 5; blankboard++)
	{
		for (int blank2 = 0; blank2 <= 7; blank2++)
		{
			Chessboard[blankboard][blank2] = "-";
		}
	}

	Chessboard[6][0] = Chessboard[6][1] = Chessboard[6][2] = Chessboard[6][3] = Chessboard[6][4] = Chessboard[6][5] = Chessboard[6][6] = Chessboard[6][7] = "d";
	Chessboard[7][0] = Chessboard[7][7] = "r";
	Chessboard[7][1] = Chessboard[7][6] = "n";
	Chessboard[7][2] = Chessboard[7][5] = "b";
	Chessboard[7][3] = "k";
	Chessboard[7][4] = "q";

	return;
}

void ChessTools::PrintBoard()
{
	for (int print = 0; print <= 7; print++)
	{
		std::cout << "\n\n";
		std::cout << (7 - print) << " ";
		for (int print2 = 0; print2 <= 7; print2++)
		{

			std::cout << "    " << Chessboard[7-print][print2];
		}

	};
	std::cout << "\n\n\n      0    1    2    3    4    5    6    7";
	return;
}

bool ChessTools::Player1()
{
	int x = 0;
	int y = 0;
	int x2 = 0;
	int y2 = 0;
	std::cout << "\n\n PLAYER 1 Select piece by coordinate: \nx =";
	std::cin >> x;
	std::cout << "y -";
	std::cin >> y;
	xcoord = x;
	ycoord = y;
	//Check validity of selection.
	
	if (Chessboard[y][x] == "R" || Chessboard[y][x] == "N" || Chessboard[y][x] == "Q" || Chessboard[y][x] == "B" || Chessboard[y][x] == "K" || Chessboard[y][x] == "P")
	{
		std::cout << "Select coordinate of desired location: \nx =";
		std::cin >> x2;
		std::cout << "y= ";
		std::cin >> y2;
	}
	else
	{
		std::cout << "\n not valid"; //decide how to loop properly
		return true;
	}

	//Set new position to previous
	Chessboard[y2][x2] = Chessboard[y][x];

	//set previous postion to empty
	Chessboard[y][x] = "-";

	return false;
}

bool ChessTools::Player2()
{

	int x = 0;
	int y = 0;
	int x2 = 0;
	int y2 = 0;
	std::cout << "player 2 - Select piece by coordinate: \nx =";
	std::cin >> x;
	std::cout << "y -";
	std::cin >> y;
	xcoord = x;
	ycoord = y;
	//Check validity of selection.

	if (Chessboard[y][x] == "r" || Chessboard[y][x] == "n" || Chessboard[y][x] == "q" || Chessboard[y][x] == "b" || Chessboard[y][x] == "k" || Chessboard[y][x] == "d")
	{
		std::cout << "Select coordinate of desired location: \nx =";
		std::cin >> x2;
		std::cout << "y= ";
		std::cin >> y2;
	}
	else
	{
		std::cout << "\n not valid"; //decide how to loop properly
		return true;
	}

	//Set new position to previous
	Chessboard[y2][x2] = Chessboard[y][x];

	//set previous postion to empty
	Chessboard[y][x] = "-";

	return false;
}
1 Like

Interesting.

1 Like

Another update:
This new version now features checks on pawn movement; and switching pawn pieces at the back row!

This proved to be quite a challenge - defining how the piece moves; and was great experience at using and reading the debug feature of VS!

032

And now for the part you’re eager to look over - the code.

main.cpp :

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

using int32 = int;

int main()
{

	ChessTools ChessGame;
	ChessGame.Reset(); //sets game to initial state
	
	do
	{

		ChessGame.PrintBoard();  //Prints the board for players to see
		bool BPlayer1 = false;
		bool BPlayer2 = false;
		do
		{
			do
			{
				BPlayer1 = ChessGame.CheckPlayer1Selection(); //Checks if player1 choice is valid
			} while (BPlayer1);
			BPlayer1 = ChessGame.CheckPlayer1Movement();
		} while (BPlayer1);
		do
		{
			do
			{
				BPlayer2=ChessGame.CheckPlayer2Selection();//checks if player choice is valid
			} while (BPlayer2);
			BPlayer2 = ChessGame.CheckPlayer2Movement();
		} while (BPlayer2);
	}while (ChessGame.GameOver());

	std::string Hold;
	std::getline(std::cin, Hold);


	return 0;
}

header (Chess.h):

#pragma once
#include <iostream>
#include <string>


enum class WPieces
{
	Pawn,
	Rook,
	Bishop,
	Knight,
	Queen,
	King,
};

enum class BPieces
{
	Pawn,
	Rook,
	Bishop,
	Knight,
	Queen,
	King,
};

class ChessTools
{
public:

	ChessTools();	 //Constructor
	bool GameOver();

	void Reset(); //Resets the initial board
	void PrintBoard();


	bool CheckPlayer1Selection();//TODO
	bool CheckPlayer2Selection();

	bool CheckPlayer1Movement();
	bool CheckPlayer2Movement();
	


private:
	int XC;
	int YC;
	int X2C;
	int Y2C;
	std::string TestPiece;
	std::string Chessboard[8][8];
};

And finally; the much updated Chess.cpp:

#include "Chess.h"

ChessTools::ChessTools()
{
}

bool ChessTools::GameOver()
{
	return true;
}

void ChessTools::Reset()
{
	Chessboard[0][0] = Chessboard[0][7] = "R";
	Chessboard[0][1] = Chessboard[0][6] = "N";
	Chessboard[0][2] = Chessboard[0][5] = "B";
	Chessboard[0][3] = "Q";
	Chessboard[0][4] = "K";
	Chessboard[1][0] = Chessboard[1][1] = Chessboard[1][2] = Chessboard[1][3] = Chessboard[1][4] = Chessboard[1][5] = Chessboard[1][6] = Chessboard[1][7] = "P";

	for (int blankboard = 2; blankboard <= 5; blankboard++)
	{
		for (int blank2 = 0; blank2 <= 7; blank2++)
		{
			Chessboard[blankboard][blank2] = "-";
		}
	}

	Chessboard[6][0] = Chessboard[6][1] = Chessboard[6][2] = Chessboard[6][3] = Chessboard[6][4] = Chessboard[6][5] = Chessboard[6][6] = Chessboard[6][7] = "d";
	Chessboard[7][0] = Chessboard[7][7] = "r";
	Chessboard[7][1] = Chessboard[7][6] = "n";
	Chessboard[7][2] = Chessboard[7][5] = "b";
	Chessboard[7][3] = "k";
	Chessboard[7][4] = "q";

	return;
}

void ChessTools::PrintBoard()
{
	for (int print = 0; print <= 7; print++)
	{
		std::cout << "\n\n";
		std::cout << (7 - print) << " ";
		for (int print2 = 0; print2 <= 7; print2++)
		{

			std::cout << "    " << Chessboard[7-print][print2];
		}

	};
	std::cout << "\n\n\n      0    1    2    3    4    5    6    7";
	return;
}

bool ChessTools::CheckPlayer1Selection()
{
	std::cout << "\n\n PLAYER 1 Select piece by coordinate: \nx =";
	std::cin >> XC;
	std::cout << "y -";
	std::cin >> YC;
	//Check validity of selection.

	if (Chessboard[YC][XC] == "R" || Chessboard[YC][XC] == "N" || Chessboard[YC][XC] == "Q" || Chessboard[YC][XC] == "B" || Chessboard[YC][XC] == "K" || Chessboard[YC][XC] == "P")
	{
		TestPiece = Chessboard[YC][XC];
		return false;
	}
	else
	{
		std::cout << "\n not valid"; //decide how to loop properly
		return true;
	}
	return false;
}

bool ChessTools::CheckPlayer2Selection()
{
	std::cout << "player 2 - Select piece by coordinate: \nx =";
	std::cin >> XC;
	std::cout << "y =";
	std::cin >> YC;

	//Check validity of selection.
		if (Chessboard[YC][XC] == "r" || Chessboard[YC][XC] == "n" || Chessboard[YC][XC] == "q" || Chessboard[YC][XC] == "b" || Chessboard[YC][XC] == "k" || Chessboard[YC][XC] == "d")
	{
		TestPiece = Chessboard[YC][XC];
		return false;
	}
	else
	{
		std::cout << "\n not valid"; //decide how to loop properly
		return true;
	}
	return false;

}

bool ChessTools::CheckPlayer1Movement()
{
	std::cout << "Select coordinate of desired location: \nx =";
	std::cin >> X2C;
	std::cout << "y= ";
	std::cin >> Y2C;

	WPieces Move = WPieces::King;

	//Set Enumerated for switch
	if (TestPiece == "P")
	{
		Move = WPieces::Pawn;
	}
	else if (TestPiece == "R")
	{
		Move = WPieces::Rook;
	}
	else if (TestPiece == "B")
	{
		Move = WPieces::Bishop;
	}
	else if (TestPiece == "N")
	{
		Move = WPieces::Knight;
	}
	else if (TestPiece == "Q")
	{
		Move = WPieces::Queen;
	}
	else
	{
		Move = WPieces::King;
	};
	

	switch (Move)
	{
	case WPieces::Pawn:
		if (YC == 1)
		{
			if ((Y2C == 3 && X2C==XC) && Chessboard[2][XC] == "-"  && Chessboard[3][XC]== "-")
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";
				return false;
			}
			else if ((Y2C==2 && (X2C==XC && Chessboard[Y2C][X2C] == "-")) || (Y2C == 2 && ((X2C == XC+1)|| (X2C == XC-1)) && (Chessboard[Y2C][X2C]=="d"|| Chessboard[Y2C][X2C] == "r" || Chessboard[Y2C][X2C] == "n" || Chessboard[Y2C][X2C] == "q" || Chessboard[Y2C][X2C] == "b" ) ) )
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";
				return false;
			}
			else
			{
				std::cout << "not a valid move -- no cheaters!";
				return true;
			};
		}
		else if (YC != 1)
		{
			if (((Y2C == (YC+1)) && (X2C == XC) && (Chessboard[Y2C][X2C] == "-")) || ((Y2C == (YC + 1)) && ((X2C == XC + 1) || (X2C == XC - 1)) && (Chessboard[Y2C][X2C] == "d" || Chessboard[Y2C][X2C] == "r" || Chessboard[Y2C][X2C] == "n" || Chessboard[Y2C][X2C] == "q" || Chessboard[Y2C][X2C] == "b")))
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";
				if (Y2C == 7)
				{
					bool BChangePiece(true);
					std::cout << "What would you like to change your pawn into? \n choose 'Q' - 'B' - 'R'- 'N' :";
					do
					{
						std::string TestPiece2="null";
						std::cin >> TestPiece2;

						if (TestPiece == "Q" || "B" || "R" || "N")
						{
							Chessboard[Y2C][X2C] = TestPiece2;
							return false;
						}
						else
						{
							std::cout << "Not a valid selection; choose again.";
							BChangePiece = true;
						}

					} while (BChangePiece);
				}
				else
				{
					return false;
				}
				return false;
			}
			else
			{
				std::cout << "not a valid move -- no cheaters!";
				return true;
			};
		}
		break;

	case WPieces::Rook:

		break;

	case WPieces::Bishop:

		break;

	case WPieces::Knight:

		break;

	case WPieces::Queen:

		break;

	case WPieces::King:

		break;
	default: return true;
		break;
	}



	return true;
}

bool ChessTools::CheckPlayer2Movement()
{
	std::cout << "Select coordinate of desired location: \nx =";
	std::cin >> X2C;
	std::cout << " y= ";
	std::cin >> Y2C;

	BPieces Move = BPieces::King;

	//Set Enumerated for switch
	if (TestPiece == "d")
	{
		Move = BPieces::Pawn;
	}
	else if (TestPiece == "r")
	{
		Move = BPieces::Rook;
	}
	else if (TestPiece == "b")
	{
		Move = BPieces::Bishop;
	}
	else if (TestPiece == "n")
	{
		Move = BPieces::Knight;
	}
	else if (TestPiece == "q")
	{
		Move = BPieces::Queen;
	}
	else
	{
		Move = BPieces::King;
	};


	switch (Move)
	{
	case BPieces::Pawn:
		if (YC == 6)
		{
			if ((Y2C == 4 && X2C == XC) && Chessboard[5][XC] == "-"  && Chessboard[4][XC] == "-")
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";
				return false;
			}
			else if (( (Y2C == 5) && (X2C == XC) && Chessboard[Y2C][X2C] == "-") || ((Y2C==5) && ((X2C == XC + 1) || (X2C== XC - 1)) && (Chessboard[Y2C][X2C] == "P" || Chessboard[Y2C][X2C] == "R" || Chessboard[Y2C][X2C] == "N" || Chessboard[Y2C][X2C] == "Q" || Chessboard[Y2C][X2C] == "B")))
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";
				return false;
			}
			else
			{
				std::cout << "not a valid move -- no cheaters!";
				return true;
			};
		}
		else if (YC != 6)
		{
			if (((Y2C == YC-1) && (X2C == XC) && (Chessboard[Y2C][X2C] == "-")) || ((Y2C == YC - 1) && ((X2C == XC + 1) || (X2C== XC - 1) ) && (Chessboard[Y2C][X2C] == "P" || Chessboard[Y2C][X2C]== "R" || Chessboard[Y2C][X2C] == "N" || Chessboard[Y2C][X2C] == "Q" || Chessboard[Y2C][X2C] == "B")))
			{
				Chessboard[Y2C][X2C] = Chessboard[YC][XC];
				Chessboard[YC][XC] = "-";

				if (Y2C == 0)
				{
					bool BChangePiece(true);
					std::cout << "What would you like to change your pawn into? \n choose 'q' - 'b' - 'r'- 'n' :";
					do
					{
						std::string TestPiece2 = "null";
						std::cin >> TestPiece2;

						if (TestPiece == "q" || "b" || "r" || "n")
						{
							Chessboard[Y2C][X2C] = TestPiece2;
							return false;
						}
						else
						{
							std::cout << "Not a valid selection; choose again.";
							BChangePiece = true;
						}
					} while (BChangePiece);

				}
				return false;
			}
			else
			{
				std::cout << "not a valid move -- no cheaters!";
				return true;
			};
		}
		break;

	case BPieces::Rook:

		break;

	case BPieces::Bishop:

		break;

	case BPieces::Knight:

		break;

	case BPieces::Queen:

		break;

	case BPieces::King:

		break;
	default: return true;
		break;
		return true;
	}
}

Privacy & Terms