示例#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);
    }
示例#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);
    }
示例#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);
    }
示例#4
0
    private static List <Move> GetBishopMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves = new List <Move>();

        //bishop can move diagonally foward-left, foward-right, back-left, back-right, forward-left-up, forward-left-down, forward-right-up, forward-right-down, back-left-up, back-left-down, back-right-up, back-right-down
        int[,] triplets = { { 0, 1, 1 }, { 0, 1, -1 }, { 0, -1, 1 }, { 0, -1, -1 }, { 1, 1, 1 }, { -1, 1, 1 }, { 1, 1, -1 }, { -1, 1, -1 }, { 1, -1, 1 }, { -1, -1, 1 }, { 1, -1, -1 }, { -1, -1, -1 } };

        for (int i = 0; i < triplets.GetLength(0); i++)
        {
            moves.AddRange(GetMovesInDirection(gameBoard, piece, triplets[i, 0], triplets[i, 1], triplets[i, 2]).ToArray());
        }

        return(moves);
    }
示例#5
0
    private static List <Move> GetRookMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves = new List <Move>();

        //rook can move straight forward, back, left, right, up, or down
        int[,] triplets = { { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 }, { 1, 0, 0 }, { -1, 0, 0 } };

        for (int i = 0; i < triplets.GetLength(0); i++)
        {
            moves.AddRange(GetMovesInDirection(gameBoard, piece, triplets[i, 0], triplets[i, 1], triplets[i, 2]).ToArray());
        }

        return(moves);
    }
示例#6
0
    private static int GetPawnStartRow(IGameBoardState gameBoard, IPieceState piece)
    {
        int pawnStartRow;

        if (piece.GetPlayer() == Player.PlayerNumber.Player1)
        {
            pawnStartRow = 1;
        }
        else
        {
            pawnStartRow = gameBoard.GetNumRows() - 2;
        }

        return(pawnStartRow);
    }
示例#7
0
    public SpaceState CreateSpaceState()
    {
        SpaceState ss = new SpaceState();

        ss.occupied = ((ISpaceState)this).IsOccupied();

        IPieceState ps = ((ISpaceState)this).Occupier();


        if (ps != null)
        {
            ss.occupier = ps.CreatePieceState();
        }

        return(ss);
    }
示例#8
0
    SpaceState ISpaceState.CreateSpaceState()
    {
        SpaceState ss = new SpaceState();

        IPieceState ps = ((ISpaceState)this).Occupier();

        if (ps != null)
        {
            ss.occupier = ps.CreatePieceState();
        }
        ss.occupied = ((ISpaceState)this).IsOccupied();
        ss.level    = this.level;
        ss.row      = this.row;
        ss.col      = this.col;

        return(ss);
    }
示例#9
0
    private static List <Move> GetKnightMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves = new List <Move>();

        //knight moves 2 forward 1 left, 2 forward 1 right, 2 right 1 foward, 2 right 1 back, 2 left 1 forward, 2 left 1 back, 2 back 1 left, 2 back 1 right
        //these moves can happen on the same level, 2 levels up, or 2 levels down only.
        int[,] doublets = { { 2, 1 }, { 2, -1 }, { 1, 2 }, { 1, -2 }, { -1, 2 }, { -1, -2 }, { -2, 1 }, { -2, -1 } };
        int[] levelDirections = { -2, 0, 2 };

        foreach (int levelDirection in levelDirections)
        {
            for (int i = 0; i < doublets.GetLength(0); i++)
            {
                moves.AddRange(GetMovesInDirection(gameBoard, piece, levelDirection, doublets[i, 0], doublets[i, 1], 1).ToArray());
            }
        }

        return(moves);
    }
示例#10
0
    private static int GetPawnMoveDirection(IPieceState piece)
    {
        int moveDirection;

        if (piece.GetPlayer() == Player.PlayerNumber.Player1)
        {
            moveDirection = 1;
        }
        else if (piece.GetPlayer() == Player.PlayerNumber.Player2)
        {
            moveDirection = -1;
        }
        else
        {
            throw new Exception("Player not set on piece.");
        }

        return(moveDirection);
    }
示例#11
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);
    }
示例#12
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);
    }
示例#13
0
    private static List <Move> GetQueenMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves = new List <Move>();

        //queen can move one space in any direction, so generate cartesian product of all possible directions
        int[] rowDirections = { -1, 0, 1 };
        int[] colDirections = { -1, 0, 1 };
        int[] lvlDirections = { -1, 0, 1 };

        foreach (int rowDirection in rowDirections)
        {
            foreach (int colDirection in colDirections)
            {
                foreach (int lvlDirection in lvlDirections)
                {
                    moves.AddRange(GetMovesInDirection(gameBoard, piece, lvlDirection, rowDirection, colDirection).ToArray());
                }
            }
        }

        return(moves);
    }
示例#14
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);
    }
示例#15
0
    public static List <Move> GetMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves;

        switch (piece.GetPieceType())
        {
        case Piece.PieceType.pawn:
            moves = GetPawnMoves(gameBoard, piece);
            break;

        case Piece.PieceType.bishop:
            moves = GetBishopMoves(gameBoard, piece);
            break;

        case Piece.PieceType.king:
            moves = GetKingMoves(gameBoard, piece);
            break;

        case Piece.PieceType.knight:
            moves = GetKnightMoves(gameBoard, piece);
            break;

        case Piece.PieceType.queen:
            moves = GetQueenMoves(gameBoard, piece);
            break;

        case Piece.PieceType.rook:
            moves = GetRookMoves(gameBoard, piece);
            break;

        default:
            moves = new List <Move>();
            break;
        }

        return(moves);
    }
示例#16
0
    public static List <Move> GetCaptureMoves(IGameBoardState gameBoard, IPieceState piece)
    {
        List <Move> moves;

        switch (piece.GetPieceType())
        {
        case Piece.PieceType.pawn:
            moves = GetPawnMoves(gameBoard, piece);
            break;

        case Piece.PieceType.bishop:
            moves = GetBishopMoves(gameBoard, piece);
            break;

        case Piece.PieceType.king:
            moves = GetKingMoves(gameBoard, piece);
            break;

        case Piece.PieceType.knight:
            moves = GetKnightMoves(gameBoard, piece);
            break;

        case Piece.PieceType.queen:
            moves = GetQueenMoves(gameBoard, piece);
            break;

        case Piece.PieceType.rook:
            moves = GetRookMoves(gameBoard, piece);
            break;

        default:
            moves = new List <Move>();
            break;
        }

        return(moves.Where(s => s.moveType == Move.MoveType.cap).Select(s => s).ToList());
    }
示例#17
0
    void IGameBoardState.Move(IPieceState piece, ISpaceState space)
    {
        Move m = new Move(piece, space, global::Move.MoveType.none);

        this.Move(m);
    }
示例#18
0
文件: Move.cs 项目: kalomj/edchess
 public Move(IPieceState piece, ISpaceState space, MoveType moveType)
 {
     this.piece    = piece;
     this.space    = space;
     this.moveType = moveType;
 }