public static ArrayList FindAllPosibleMoves(ChessState[,] arrState, Point pos, eChessSide ownSide) { ArrayList arrMove = new ArrayList(); eChessSide side = arrState[pos.X, pos.Y].Side; Point[] How_To_Move = new Point[] { new Point(1, 1), new Point(1, -1), new Point(-1, 1), new Point(-1, -1), new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1) }; bool[,] can_Moves = Chess_Engine.FindAllKingCanMove(arrState, side, ownSide); for (int i = 0; i < How_To_Move.Length; i++) { Point pos_temp = new Point(pos.X + How_To_Move[i].X, pos.Y + How_To_Move[i].Y); if (can_Moves[pos_temp.X, pos_temp.Y] == true) { if (arrState[pos_temp.X, pos_temp.Y].Type == eChessPieceType.Null) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } if (arrState[pos_temp.X, pos_temp.Y].Side != side && arrState[pos_temp.X, pos_temp.Y].Type > 0) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Eating, new Point(pos_temp.X, pos_temp.Y), arrState[pos_temp.X, pos_temp.Y].Type, arrState[pos_temp.X, pos_temp.Y].Moves, arrState[pos.X, pos.Y].Moves)); } } } if (arrState[pos.X, pos.Y].Moves == 0) { //King Side castling if (arrState[pos.X, 6].Type == eChessPieceType.Null && arrState[pos.X, 7].Type == eChessPieceType.Null && arrState[pos.X, 8].Type == eChessPieceType.Rook && can_Moves[pos.X, 6] && can_Moves[pos.X, 7] && arrState[pos.X, 8].Moves == 0) { arrMove.Add(new ChessMove(new Point(pos.X, 5), new Point(pos.X, 7), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } //Queen Side castling if (arrState[pos.X, 4].Type == eChessPieceType.Null && arrState[pos.X, 3].Type == eChessPieceType.Null && arrState[pos.X, 2].Type == eChessPieceType.Null && arrState[pos.X, 1].Type == eChessPieceType.Rook && can_Moves[pos.X, 4] && can_Moves[pos.X, 3] && arrState[pos.X, 1].Moves == 0) { arrMove.Add(new ChessMove(new Point(pos.X, 5), new Point(pos.X, 3), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } //Promote Side castling if (pos.X == 8 && arrState[7, 5].Type == eChessPieceType.Null && arrState[6, 5].Type == eChessPieceType.Null && arrState[5, 5].Type == eChessPieceType.Null && arrState[4, 5].Type == eChessPieceType.Null && arrState[3, 5].Type == eChessPieceType.Null && arrState[2, 5].Type == eChessPieceType.Null && arrState[1, 5].Type == eChessPieceType.Rook && can_Moves[7, 5] && can_Moves[6, 5] && arrState[1, 5].Moves == 0) { arrMove.Add(new ChessMove(new Point(8, 5), new Point(6, 5), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } if (pos.X == 1 && arrState[7, 5].Type == eChessPieceType.Null && arrState[6, 5].Type == eChessPieceType.Null && arrState[5, 5].Type == eChessPieceType.Null && arrState[4, 5].Type == eChessPieceType.Null && arrState[3, 5].Type == eChessPieceType.Null && arrState[2, 5].Type == eChessPieceType.Null && arrState[8, 5].Type == eChessPieceType.Rook && can_Moves[2, 5] && can_Moves[3, 5] && arrState[8, 5].Moves == 0) { arrMove.Add(new ChessMove(new Point(1, 5), new Point(3, 5), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } } return(arrMove); }
private void Uc_ChessPiece_Click(object sender, EventArgs e) { // if we are in capturing Mode then exit if (Uc_ChessBoard._isCapturingMode) { return; } //Get the present Uc_ChessBoard Uc_ChessBoard uc_chessboard = (Uc_ChessBoard)this.Parent; //If this cell is clicked and highlighted => will be eated by uc_chessboard.PositionChoosen cell if (uc_chessboard.PositionChoosen.X != 0 && uc_chessboard.PositionChoosen.Y != 0) { //if choose this position again -> remove choose if (uc_chessboard.PositionChoosen.X == this.Position.X && uc_chessboard.PositionChoosen.Y == this.Position.Y) { uc_chessboard.UnHighLightMoves(); uc_chessboard.HighLightWhoCheckAndKingChecked(); uc_chessboard.PositionChoosen = new Point(0, 0); return; } foreach (ChessMove p in uc_chessboard.arrCanMove) { if (this._position.X == p.MoveTo.X && this._position.Y == p.MoveTo.Y) { uc_chessboard.DoMove(p); uc_chessboard.PositionChoosen = new Point(0, 0); //this cell was eated so there's no Position Choosen //Computer's move if (uc_chessboard.GameMode == eGameMode.VsComputer) { uc_chessboard.IsThinking = true; if (uc_chessboard.GameStatus == eGameStatus.Playing) { uc_chessboard.Computer_Move(); } } return; } } //If this Point wasn't eated => reset PositionChoosen and Unhighlight uc_chessboard.UnHighLightMoves(); uc_chessboard.PositionChoosen = new Point(0, 0); //Make this Point -> Position Choosen uc_chessboard.arrCanMove.Clear(); //if this point = position choosen -> return if ((uc_chessboard.OwnSide == this._side || uc_chessboard.GameMode == eGameMode.VsHuman) && uc_chessboard.GameStatus == eGameStatus.Playing) { if ((uc_chessboard.WhoTurn == eChessSide.White && this._side == eChessSide.White) || (uc_chessboard.WhoTurn == eChessSide.Black && this._side == eChessSide.Black)) { uc_chessboard.arrCanMove = Chess_Engine.FindAllPosibleMoves(uc_chessboard.arrState, this._position, uc_chessboard.OwnSide); uc_chessboard.PositionChoosen = new Point(_position.X, _position.Y); uc_chessboard.UnHighLightWhoCheckAndKingChecked(); uc_chessboard.UnHighLightLastMove(); uc_chessboard.HighLightAllPosibleMoves(); } } } else { //If this Point isn't highlighted //Make this Point -> Position Choosen uc_chessboard.arrCanMove.Clear(); //if this point = position choosen -> return if ((uc_chessboard.OwnSide == this._side || uc_chessboard.GameMode == eGameMode.VsHuman) && uc_chessboard.GameStatus == eGameStatus.Playing) { if ((uc_chessboard.WhoTurn == eChessSide.White && this._side == eChessSide.White) || (uc_chessboard.WhoTurn == eChessSide.Black && this._side == eChessSide.Black)) { uc_chessboard.arrCanMove = Chess_Engine.FindAllPosibleMoves(uc_chessboard.arrState, this._position, uc_chessboard.OwnSide); uc_chessboard.PositionChoosen = new Point(_position.X, _position.Y); uc_chessboard.UnHighLightWhoCheckAndKingChecked(); uc_chessboard.UnHighLightLastMove(); uc_chessboard.HighLightAllPosibleMoves(); } } } }