i am not very good with LLD.
need your reviews on this Chess LLD of mine.
class Timer{
bool timeUp, reset;
public:
bool getTimeUp(){
return timeUp;
}
bool setReset(){
reset = true;
}
void start(int sec){
reset = timeUp = false;
time startTime = getCurrentTime();
while(getCurrentTime() < startTime){
if(reset == true){
reset = false;
return;
}
}
}
};
enum Color{
Black,
White;
};
class User{
int id;
string email, name;
};
class Player : private User{
int num;
Color color;
void setColor(){}
void setNum(){}
};
class Piece{
Color color;
public: virtual unordered_set<string> getPossiblePositions(string currentPos) = 0;
};
class Queen : public Piece{
virtual unordered_set<string> getPossiblePositions(string currentPos){
//core logic
}
};
class Board{
int boardLength;
unordered_map<string,Piece> positions; //map of position vs piece
vector<Piece> killedPieces;
Board(boardLength = 8)
:boardLength(boardLength)
{
fill_map();
}
Color getColor(string pos){}
unordered_set<string> getPossiblePositions(string pos, Color color){
if(getColor(pos) != color)
return {};
return positions[pos].getPossiblePositions();
}
bool move(string src, string dest){
//return true if all of the opponent's pieces are killed.
}
};
class Chess{
Player p1, p2, *currentPlayer, *winner;
Board board;
Timer timer;
thread timerThread(timer.start(120)); // this starts a countdown.
Chess(User p1, User p2, boardLength=8)
:p1(p1)
,p2(p2)
,board(boardLength)
{
p1.setColor(White);
p1.setNum(1);
p2.setColor(Blacl);
p2.setNum(2);
currentPlayer = &p1;
timerThread.start();
thread checkTimeUpThread(checkTimeUp).start(); //works continuously throught out the game.
}
void checkTimeUp(){
if(timer.getTimeUp()){
if(currentPlayer == &p1)
winner = &p2;
else
winner = &p1;
terminate();
}
}
unordered_set<string> getPossiblePositions(Player p, string pos){
if(p != *currentPlayer)
return {};
return board.getPossiblePositions(pos,p.getColor());
}
bool move(Player, p, string src, string dest){
mp = board.getPossiblePositions(src,p.getColor);
if(validatePlayer(p) and mp.find(dest) != mp.end()){
isWinner = board.move(src,dest);
if(isWinner)
terminate();
timer.setReset(); //timerThread gets stopped now.
change currentPlayer;
timerThread.start(); //start timer again.
}
return false;
}
};