// Method for switching figures on the board public Message switchFigures(Figure figure, coord position) { Player player = new Player(getFieldFigureColor(position)); Message result = new Message(false, "", "", player); this.fields [position.x, position.y] = figure; if (isCheck(player.next())) { result.msg = "check"; if (isCheckMate(player.next(), new coord(0, 0))) { result.msg = "checkmate"; } } return(result); }
//check if the king will be in check after this move and don't allow it if it is private bool isCheck(Player player, coord start, coord end) { coord king = new coord(); bool noKing = true; Figure removedObj = new Figure(); bool res = false; //move the figure to the new position if the new positon is not the same as the old if (!start.Equals(end)) { removedObj = this.fields [end.x, end.y]; this.fields [end.x, end.y] = this.fields [start.x, start.y]; this.fields [start.x, start.y] = new Empty(); } //search for the king on the board for (int x = 0; x < this.size.x; x++) { for (int y = 0; y < this.size.y; y++) { if (this.fields [x, y].color == player.ToString() && getFieldFigureName(new coord(x, y)) == "King") { king = new coord(x, y); noKing = false; } } } //if a king was found, try to move all figures to the position of the king,if that is possibile it means that the king is in check if (!noKing) { for (int x = 0; x < this.size.x && !res; x++) { for (int y = 0; y < this.size.y && !res; y++) { //if a move is posiblie means the king is checked if (checkMove(player.next(), new coord(x, y), king)) { res = true; } } } } //revert the move so that the state of the board is the same as at the start of this function if (!start.Equals(end)) { this.fields [start.x, start.y] = this.fields [end.x, end.y]; this.fields [end.x, end.y] = removedObj; } return(res); }
// Message for the initial state public Message initialState() { return(new Message(false, "", "", player.next())); }
// Method that return some message to player,for example if it is under check or under checkmate public Message Move(Player player, coord start, coord end) { Message result = new Message(false, "", "", player); if (doCastling(player, start, end)) { return(result); } else if (doEnPassant(player, start, end)) { return(result); } else if (checkMove(player, start, end) && !isCheck(player, start, end)) { if (this.fields [end.x, end.y].GetType().Name != "Empty") { this.removedFigures.Add(this.fields [end.x, end.y]); } this.fields [end.x, end.y] = this.fields [start.x, start.y]; this.fields [start.x, start.y] = new Empty(); //check if with this move the other player will be in check or even checkmate if (isCheck(player.next())) { result.msg = "check"; if (isCheckMate(player.next(), new coord(0, 0))) { result.msg = "checkmate"; } } if (this.getFieldFigureName(end) == "Pawn") { ((Pawn)this.fields [end.x, end.y]).justMoved = true; } //remove all justMoved for (int x = 0; x < this.size.x; x++) { for (int y = 0; y < this.size.y; y++) { if (this.fields [x, y].color == player.ToString() && getFieldFigureName(new coord(x, y)) == "Pawn") { ((Pawn)this.fields [x, y]).justMoved = false; } } } //set moved Pawn's justMoved to true; if (this.getFieldFigureName(end) == "Pawn" && (start.y + 2 == end.y || start.y - 2 == end.y)) { ((Pawn)this.fields [end.x, end.y]).justMoved = true; } if (this.getFieldFigureName(end) == "Pawn" && (end.y == 0 || end.y == 7)) { result.action = "chooser"; } this.fields [end.x, end.y].hasMoved = true; isDraw(player.next(), new coord(0, 0)); return(result); } result.error = true; return(result); }