Exemplo n.º 1
0
        /// <summary>
        /// Attack the piece at the target cell using the SelectedPiece.
        /// </summary>
        /// <returns>true if the attack success and the SelectedPiece moves to occupy the target cell,
        /// false if the attack success and the SelectedPiece stays at its cell.</returns>
        private bool AttackPieceAt(int _row, int _col)
        {
            int pcsRow = _row;

            if (IsFlagSetAtCell(_row, _col, CellStatus.EnPassant))
            {
                if (SelectedPiece.Color == PieceColor.Black)
                {
                    ++pcsRow;
                }
                else
                {
                    --pcsRow;
                }
            }
            bool dead = SelectedPiece.Attack(Table[GetIndex(pcsRow, _col)]);

            if (dead)
            {
                MovePieceTo(_row, _col);
            }
            else
            {
                if (LastMovedPawn != null)
                {
                    LastMovedPawn.EnPassantEligible = false;
                }
                // pieces stay still!
                LastMovement = Tuple.Create(GetIndex(SelectedPiece.Row, SelectedPiece.Column), GetIndex(_row, _col));
                //TableStatus[GetIndex(SelectedPiece.Row, SelectedPiece.Column)] |= (int)CellStatus.LastMove;
                //TableStatus[GetIndex(_row, _col)] |= (int)CellStatus.LastMove;
                Switch();
            }
            return(dead);
        }
Exemplo n.º 2
0
 public void EndTurn()
 {
     TurnEnding?.Invoke(this, new TurnEndingEventArgs(CurrentTurn));
     CurrentTurn = CurrentTurn.Opposite();
     TurnNumber++;
     SelectedPiece.OnDeselected();
     SelectedPiece = null;
     TurnEnded?.Invoke(this, new TurnEndedEventArgs());
 }
Exemplo n.º 3
0
 void TileClickedToMove(CheckerTile _tile)
 {
     foreach (var dg in DiagonalMoves)
     {
         dg.Highlight(false);
     }
     StartCoroutine(SelectedPiece.MoveTo(_tile, () => {
         Debug.Log("Piece Reached Destination");
         _tile.Piece = SelectedPiece;
         RoundEnd();
     }));
 }
Exemplo n.º 4
0
        public void OnPrimaryMouseUp(MouseEventArgs mouseEventArgs)
        {
            var boardCell = GetCellFromWorldPoint(mouseEventArgs.Point);

            if (SelectedPiece == null)
            {
                // We know we're going to try to make a selection
                // Is the cell empty or an enemy piece?
                if (!boardCell.IsOccupied || boardCell.Piece.Side != CurrentTurn)
                {
                    return;
                }

                // It must be our piece!
                SelectedPiece = boardCell.Piece;
                SelectedPiece.OnSelected();
            }
            else
            {
                // Here we have a piece selected so the player is trying to do something with that piece or change the selection.
                if (SelectedPiece.Equals(boardCell.Piece))
                {
                    SelectedPiece.OnDeselected();
                    SelectedPiece = null;
                    return;
                }

                var possibleMovesForSelectedPiece = PossibleMoves[SelectedPiece.Side][SelectedPiece];

                var movementToCell = possibleMovesForSelectedPiece.SingleOrDefault(x => x.EndingCell.Equals(boardCell));

                if (movementToCell == null || !TryMovement(movementToCell))
                {
                    SelectedPiece.OnDeselected();
                    SelectedPiece = null;
                    return;
                }

                foreach (var piece in movementToCell.FinalPositions().Select(x => x.Item1))
                {
                    piece.HasMoved = true;
                }

                EndTurn();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Move the SelectedPiece to the target cell.
        /// </summary>
        private bool MovePieceTo(int _row, int _col)
        {
            if (LastMovedPawn != null)
            {
                LastMovedPawn.EnPassantEligible = false;
            }
            if (SelectedPiece.Type == PieceType.Pawn)
            {
                LastMovedPawn = SelectedPiece as PawnPiece;
            }
            else
            {
                LastMovedPawn = null;
            }
            // the SelectedPiece is being moved so there is no need to get the piece;
            // the move is always a valid move so there is no need to validate it;
            LastMovement = Tuple.Create(GetIndex(SelectedPiece.Row, SelectedPiece.Column), GetIndex(_row, _col));
            //TableStatus[GetIndex(SelectedPiece.Row, SelectedPiece.Column)] |= (int)CellStatus.LastMove;
            //TableStatus[GetIndex(_row, _col)] |= (int)CellStatus.LastMove;

            Array.Copy(Table, GetIndex(SelectedPiece.Row, SelectedPiece.Column), Table, GetIndex(_row, _col), 1);
            Table[GetIndex(SelectedPiece.Row, SelectedPiece.Column)] = null;
            int pcsRow = _row;

            if (IsFlagSetAtCell(_row, _col, CellStatus.EnPassant))
            {
                if (SelectedPiece.Color == PieceColor.Black)
                {
                    Table[GetIndex(pcsRow + 1, _col)] = null;
                }
                else
                {
                    Table[GetIndex(pcsRow - 1, _col)] = null;
                }
            }
            SelectedPiece.Move(_row, _col);

            UpdateCheckStatus();


            Switch();
            return(true);
        }
Exemplo n.º 6
0
        public void SelectTile(Tile selectedtile)
        {
            if (selectedtile != null)
            {
                if (!selectedtile.IsVacant && selectedtile.Piece.Alliance == Player.Alliance)
                {
                    if (SelectedPiece != null)
                    {
                        BoardState[SelectedPiece.Row, SelectedPiece.Column].SetTint(Color.White);
                    }
                    SelectedPiece = selectedtile.Piece;
                    selectedtile.SetTint(Color.LightBlue);
                }
                else if (SelectedPiece != null)
                {
                    ReadOnlyCollection <Move> CandidateMoves = SelectedPiece.GetCandidateMoves(BoardState);
                    BoardState CopiedBoard         = BoardState.GetCopy();
                    ChessPiece CopiedSelectedPiece = CopiedBoard[SelectedPiece.Row, SelectedPiece.Column].Piece;
                    ReadOnlyCollection <Move> CopiedCandidateMoves = CopiedSelectedPiece.GetCandidateMoves(CopiedBoard);

                    List <Move> CopiedNonLegalMoves = new List <Move>();
                    foreach (Move CopiedCandidateMove in CopiedCandidateMoves)
                    {
                        CopiedCandidateMove.Execute();
                        bool LegalMove = true;
                        List <ChessPiece> OpponentPieces = new List <ChessPiece>();
                        foreach (Tile tile in CopiedBoard.Tiles)
                        {
                            if (!tile.IsVacant)
                            {
                                if (tile.Piece.Alliance != CopiedSelectedPiece.Alliance)
                                {
                                    OpponentPieces.Add(tile.Piece);
                                }
                            }
                        }
                        foreach (ChessPiece OpponentPiece in OpponentPieces)
                        {
                            ReadOnlyCollection <Move> OpponentCandidateMoves = OpponentPiece.GetCandidateMoves(CopiedBoard);
                            foreach (Move OpponentCandidateMove in OpponentCandidateMoves)
                            {
                                if (OpponentCandidateMove is BasicCaptureMove)
                                {
                                    BasicCaptureMove captureMove = OpponentCandidateMove as BasicCaptureMove;
                                    if (captureMove.CapturedPiece is King)
                                    {
                                        LegalMove = false;
                                    }
                                }
                                if (!LegalMove)
                                {
                                    break;
                                }
                            }
                            if (!LegalMove)
                            {
                                break;
                            }
                        }
                        if (!LegalMove)
                        {
                            CopiedNonLegalMoves.Add(CopiedCandidateMove);
                        }
                        CopiedCandidateMove.Undo();
                    }
                    List <Move> LegalMoves = CandidateMoves.ToList();
                    foreach (Move CopiedNonLegalMove in CopiedNonLegalMoves)
                    {
                        Move NonLegalMove = CandidateMoves.Where(m => m.TargetTile.Row == CopiedNonLegalMove.TargetTile.Row && m.TargetTile.Column == CopiedNonLegalMove.TargetTile.Column).FirstOrDefault();
                        LegalMoves.Remove(NonLegalMove);
                    }

                    Move SelectedMove = LegalMoves.Where(m => m.Piece == SelectedPiece && m.TargetTile == selectedtile).FirstOrDefault();
                    if (SelectedMove != null)
                    {
                        BoardState[SelectedPiece.Row, SelectedPiece.Column].SetTint(Color.White);
                        SelectedMove.Execute();
                        PartialTurn = new PartialTurn(Player, SelectedMove, StartTime, Clock.Time);
                    }
                    else
                    {
                        BoardState[SelectedPiece.Row, SelectedPiece.Column].SetTint(Color.White);
                        SelectedPiece = null;
                    }
                }
            }
        }