// Method for the opening of popup public void open(Player player, coord position) { close (); foreach (Figure fig in this.figures) { TileWidget tile = new TileWidget ("", fig.name (), player.ToString (), this.tileSize); tile.position = position; fig.color = player.ToString (); box.PackStart (tile); tile.ButtonPressEvent += onTileClicked; } this.ShowAll (); }
// 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); }
//special move: castling private bool doCastling(Player player, coord start, coord end) { //castling is only possible if the figure on the start position is a king witch hasn' t moved if (this.getFieldFigureName(start) == "King" && this.fields [start.x, start.y].color == player.ToString() && this.fields [start.x, start.y].hasMoved == false) { //left side castling if (start.x + 2 == end.x && this.getFieldFigureName(new coord(start.x + 1, start.y)) == "Empty" && !Move(player, start, new coord(start.x + 1, start.y)).error) { //move the king a secound time if (!Move(player, new coord(start.x + 1, start.y), new coord(start.x + 2, start.y)).error) { //save kings position to allow the rock to move Figure tmpKingPosition = this.fields [end.x, end.y]; this.fields [end.x, end.y] = new Empty(); //move rock if (!Move(player, new coord(7, start.y), new coord(4, start.y)).error) { this.fields [end.x, end.y] = tmpKingPosition; return(true); } else { tmpKingPosition.hasMoved = false; this.fields [start.x, start.y] = tmpKingPosition; } } else { this.fields [start.x, start.y] = this.fields [start.x + 1, start.y]; this.fields [start.x, start.y].hasMoved = false; this.fields [start.x + 1, start.y] = new Empty(); } } //right side castling else if (start.x - 2 == end.x && this.getFieldFigureName(new coord(start.x - 1, start.y)) == "Empty" && !Move(player, start, new coord(start.x - 1, start.y)).error) { //move the king a secound time if (!Move(player, new coord(start.x - 1, start.y), new coord(start.x - 2, start.y)).error) { //save kings position to allow the rock to move Figure tmpKingPosition = this.fields [end.x, end.y]; this.fields [end.x, end.y] = new Empty(); //move rock if (!Move(player, new coord(0, start.y), new coord(2, start.y)).error) { this.fields [end.x, end.y] = tmpKingPosition; return(true); } else { tmpKingPosition.hasMoved = false; this.fields [start.x, start.y] = tmpKingPosition; } } else { this.fields [start.x, start.y] = this.fields [start.x - 1, start.y]; this.fields [start.x, start.y].hasMoved = false; this.fields [start.x - 1, start.y] = new Empty(); } } } return(false); }
// Method that return a message public Message Move(Player player, coord start) { return(new Message(this.getFieldFigureName(start) == "Empty" || this.getFieldFigureColor(start) != player.ToString(), "firstClick", "", player)); }