Example #1
0
    public GameBoardState Clone()
    {
        GameBoardState newGbs = new GameBoardState(this.levels, this.rows, this.cols);

        IGameBoardState igbs = this;

        for (int lvl = 0; lvl < (igbs.GetNumLevels()); lvl++)
        {
            for (int row = 0; row < (igbs.GetNumRows()); row++)
            {
                for (int col = 0; col < (igbs.GetNumCols()); col++)
                {
                    ISpaceState iss = igbs.GetSpaceState(lvl, row, col);
                    newGbs.spaces[lvl][row][col].occupied = iss.IsOccupied();
                    IPieceState ips = iss.Occupier();

                    if (ips != null)
                    {
                        PieceState ps = ips.CreatePieceState();
                        newGbs.spaces[lvl][row][col].occupier = ps;
                        newGbs.AlivePieces.Add(ps);
                        ps.space = newGbs.spaces[lvl][row][col];
                    }
                }
            }
        }

        foreach (IPieceState piece in DeadPieces)
        {
            newGbs.DeadPieces.Add(piece.CreatePieceState());
        }

        return(newGbs);
    }
Example #2
0
    public Piece GetPiece(IPieceState pieceState)
    {
        ISpaceState iss_source = pieceState.GetSpaceState();

        Space space = this.GetSpace(iss_source.GetLevel(), iss_source.GetRow(), iss_source.GetCol());

        return(space.occupier);
    }
Example #3
0
    private static List <Move> GetPawnMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves         = new List <Move>();
        int         moveDirection = GetPawnMoveDirection(piece);
        int         pawnStartRow  = GetPawnStartRow(gameBoard, piece);
        int         possibleDistance;


        //check one space ahead, two spaces ahead if on starting row
        if (piece.GetSpaceState().GetRow() == pawnStartRow)
        {
            possibleDistance = 2;
        }
        else
        {
            possibleDistance = 1;
        }

        int[] vOffsets = { -1, 0, 1 };
        foreach (int vOffset in vOffsets)
        {
            for (int i = 1; i <= possibleDistance; i++)
            {
                ISpaceState cSpace = gameBoard.GetSpaceState(piece.GetSpaceState().GetLevel() + vOffset, piece.GetSpaceState().GetRow() + moveDirection * i, piece.GetSpaceState().GetCol());

                if (cSpace != null && cSpace.IsOccupied())
                {
                    break;
                }
                else if (cSpace != null)
                {
                    moves.Add(new Move(piece, cSpace, Move.MoveType.nocap));
                }
            }
        }

        //check diagonals for possible capture
        int[] cOffsets = { -1, 1 };

        foreach (int vOffset in vOffsets)
        {
            foreach (int cOffset in cOffsets)
            {
                ISpaceState cSpace = gameBoard.GetSpaceState(piece.GetSpaceState().GetLevel(), piece.GetSpaceState().GetRow() + moveDirection, piece.GetSpaceState().GetCol() + cOffset);

                if (cSpace != null && cSpace.IsOccupied() && cSpace.Occupier().GetPlayer() != piece.GetPlayer())
                {
                    moves.Add(new Move(piece, cSpace, Move.MoveType.cap));
                }
            }
        }

        //TODO: check for en passant

        return(moves);
    }
Example #4
0
    public void Move(Move move)
    {
        //Move objects can be passed between games so we
        //have to look up the piece and space owned by this game
        //and check to ensure the move is applicable to this game

        IPieceState ips        = move.piece;
        ISpaceState iss_dest   = move.space;
        ISpaceState iss_source = ips.GetSpaceState();

        ISpaceState this_iss_dest   = this.GetSpaceState(iss_dest.GetLevel(), iss_dest.GetRow(), iss_dest.GetCol());
        ISpaceState this_iss_source = this.GetSpaceState(iss_source.GetLevel(), iss_source.GetRow(), iss_source.GetCol());

        if (!this_iss_source.IsOccupied() || this_iss_source.Occupier().GetPieceType() != ips.GetPieceType() || this_iss_source.Occupier().GetPlayer() != ips.GetPlayer())
        {
            throw new Exception("Invalid move for this simulated game board state.");
        }

        Move((PieceState)this_iss_source.Occupier(), (SpaceState)this_iss_dest);
    }
Example #5
0
    private static List <Move> GetMovesInDirection(IGameBoardState gameBoard, IPieceState piece, int levelDirection, int rowDirection, int colDirection, int max = 99)
    {
        List <Move> moves = new List <Move>();

        int levelChange = levelDirection;
        int rowChange   = rowDirection;
        int colChange   = colDirection;

        for (int i = 0; i < max; i++)
        {
            ISpaceState cSpace = gameBoard.GetSpaceState(piece.GetSpaceState().GetLevel() + levelChange, piece.GetSpaceState().GetRow() + rowChange, piece.GetSpaceState().GetCol() + colChange);

            if (cSpace == null)
            {
                break;
            }

            if (cSpace.IsOccupied() && cSpace.Occupier().GetPlayer() != piece.GetPlayer())
            {
                //last move in the sequence is a cap move
                moves.Add(new Move(piece, cSpace, Move.MoveType.cap));
                break;
            }

            if (cSpace.IsOccupied() && cSpace.Occupier().GetPlayer() == piece.GetPlayer())
            {
                break;
            }

            if (!cSpace.IsOccupied())
            {
                moves.Add(new Move(piece, cSpace, Move.MoveType.nocap));
                //amplify move for next iteration
                levelChange += levelDirection;
                rowChange   += rowDirection;
                colChange   += colDirection;
            }
        }

        return(moves);
    }
Example #6
0
    public bool Move(Move move)
    {
        //Move objects can be passed between games so we
        //have to look up the piece and space owned by this game
        //and check to ensure the move is applicable to this game

        IPieceState ips        = move.piece;
        ISpaceState iss_dest   = move.space;
        ISpaceState iss_source = ips.GetSpaceState();

        ISpaceState this_iss_dest   = this.GetSpace(iss_dest.GetLevel(), iss_dest.GetRow(), iss_dest.GetCol());
        ISpaceState this_iss_source = this.GetSpace(iss_source.GetLevel(), iss_source.GetRow(), iss_source.GetCol());

        if (!this_iss_source.IsOccupied() || this_iss_source.Occupier().GetPieceType() != ips.GetPieceType() || this_iss_source.Occupier().GetPlayer() != ips.GetPlayer())
        {
            Debug.Log("Illegal move attempted. Skipping turn." + move.ToString());

            if (!this_iss_source.IsOccupied())
            {
                Debug.Log("Source is not occupied in real game board.");
            }
            else if (this_iss_source.Occupier().GetPieceType() != ips.GetPieceType())
            {
                Debug.Log("A different piece type is at the source location in real game board.");
            }
            else if (this_iss_source.Occupier().GetPlayer() != ips.GetPlayer())
            {
                Debug.Log("A different player owns the piece in the real game board.");
            }

            return(false);
            //throw new Exception("Invalid move for this real game board.");
        }

        Move((Piece)this_iss_source.Occupier(), (Space)this_iss_dest);
        return(true);
    }
Example #7
0
    public void SquareClickEvent(int level, int row, int col)
    {
        if (game.currentPlayer.playerType == Player.PlayerType.Human)
        {
            ISpaceState s     = game.gameBoard.GetSpace(level, row, col);
            bool        moved = false;
            if (moveTargets != null)
            {
                foreach (Move m in moveTargets)
                {
                    if (m.space == s)
                    {
                        game.currentPlayer.selectedMove = m;
                        game.currentPlayer.playerState  = Player.PlayerState.Moving;

                        moved = true;

                        break;
                    }
                }
                moveTargets = null;
            }

            ResetButtonHighlights();

            if (s.IsOccupied() && !moved && s.Occupier().GetPlayer() == game.currentPlayer.playerNumber)
            {
                game.gameBoard.GetPiece(s.Occupier()).ZoomToPiece();

                moveTargets = MoveGenerator.GetMoves(game.gameBoard, s.Occupier());
                foreach (Move m in moveTargets)
                {
                    squareLookup[m.space.GetLevel()][m.space.GetRow()][m.space.GetCol()].color = Color.cyan;
                }
            }
        }
    }
Example #8
0
    void IGameBoardState.Move(IPieceState piece, ISpaceState space)
    {
        Move m = new Move(piece, space, global::Move.MoveType.none);

        this.Move(m);
    }
Example #9
0
 public Move(IPieceState piece, ISpaceState space, MoveType moveType)
 {
     this.piece    = piece;
     this.space    = space;
     this.moveType = moveType;
 }
Example #10
0
 public void ChangeState(ISpaceState state)
 {
     this.state = state;
 }