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.
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!
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;
}
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];
};