patterncppMinor
OOP chess game implementation in C++
Viewed 0 times
chessimplementationgameoop
Problem
I am attempting one of the questions from Cracking the Coding Interview:
"Design a chess game using object oriented principles."
The solutions are in Java and below is my attempt to create something similar in C++. This has brought up a few issues and gaps in my understanding. The below code compiles and gives me the desired logic however I know there is plenty of room for improvement, hence the post. In particular, I am looking for a nice way of removing the ChessPieces created in setup(). I am aware this may have something to do with ownership problems as I am trying to delete something from outside of the scope in which it was created, however I like the organisation of one method whose purpose it is to create and another whose purpose is to destroy.
```
#include
#include
#include
#include
class ChessPiece {
public:
const std::string position;
const std::string piece;
bool is_captured;
ChessPiece(std::string position, std::string piece, bool is_captured) :
position(position), piece(piece), is_captured(is_captured) {};
};
class PlayerBase {
public:
bool is_checked = false;
std::vector pieces;
char colour;
void print_pieces() {
for(auto &cp : pieces) {
std::cout piece is_captured << std::endl;
}
}
/*
* Add a ChessPiece to pieces as necessary.
*/
void add_pieces(const ChessPiece*);
/*
* Remove a ChessPiece to pieces as necessary.
*/
void remove_pieces(const ChessPiece&);
/*
* Process a player's desired next move (includes checks if it is a
* valid move and will call and erase methods if necessary
*/
virtual ChessPiece next_move(ChessPiece cp)
{
return cp;
};
};
void PlayerBase::add_pieces(const ChessPiece *cp) {
pieces.push_back(cp);
};
void PlayerBase::remove_pieces(const ChessPiece& cp) {
"Design a chess game using object oriented principles."
The solutions are in Java and below is my attempt to create something similar in C++. This has brought up a few issues and gaps in my understanding. The below code compiles and gives me the desired logic however I know there is plenty of room for improvement, hence the post. In particular, I am looking for a nice way of removing the ChessPieces created in setup(). I am aware this may have something to do with ownership problems as I am trying to delete something from outside of the scope in which it was created, however I like the organisation of one method whose purpose it is to create and another whose purpose is to destroy.
```
#include
#include
#include
#include
class ChessPiece {
public:
const std::string position;
const std::string piece;
bool is_captured;
ChessPiece(std::string position, std::string piece, bool is_captured) :
position(position), piece(piece), is_captured(is_captured) {};
};
class PlayerBase {
public:
bool is_checked = false;
std::vector pieces;
char colour;
void print_pieces() {
for(auto &cp : pieces) {
std::cout piece is_captured << std::endl;
}
}
/*
* Add a ChessPiece to pieces as necessary.
*/
void add_pieces(const ChessPiece*);
/*
* Remove a ChessPiece to pieces as necessary.
*/
void remove_pieces(const ChessPiece&);
/*
* Process a player's desired next move (includes checks if it is a
* valid move and will call and erase methods if necessary
*/
virtual ChessPiece next_move(ChessPiece cp)
{
return cp;
};
};
void PlayerBase::add_pieces(const ChessPiece *cp) {
pieces.push_back(cp);
};
void PlayerBase::remove_pieces(const ChessPiece& cp) {
Solution
Here are things to consider:
A chess piece in of itself has not state. It does not know where it is, it only knows what it is called and how it moves. It does not know if it is captured or anything of that sort. So the logic for its position and captured is probably not appropriate for it.
Then, who tracks the position of the game? It's the board. The board concept is missing. Game manager only manages who plays, turns, checks, etc. Board manages pieces on it. Player manage their names, info, etc.
Just suggestions.
A chess piece in of itself has not state. It does not know where it is, it only knows what it is called and how it moves. It does not know if it is captured or anything of that sort. So the logic for its position and captured is probably not appropriate for it.
Then, who tracks the position of the game? It's the board. The board concept is missing. Game manager only manages who plays, turns, checks, etc. Board manages pieces on it. Player manage their names, info, etc.
Just suggestions.
Context
StackExchange Code Review Q#141340, answer score: 7
Revisions (0)
No revisions yet.