Exemplo n.º 1
0
        public void GetValidPawnMoves(EChessTeam team, int pos, List <Move> moves)
        {
            int FrontPos = -1, LeftFrontPos = -1, RightFrontPos = -1;

            if (team == EChessTeam.White)
            {
                FrontPos = new BoardPos(pos).GetTop();
                if (FrontPos > 0)
                {
                    LeftFrontPos  = new BoardPos(FrontPos).GetLeft();
                    RightFrontPos = new BoardPos(FrontPos).GetRight();
                }
                if (new BoardPos(pos).Y == 1)
                {
                    AddMoveIfValidSquare(team, pos, new BoardPos(FrontPos).GetTop(), moves, (int)ETeamFlag.None);
                }
            }
            else
            {
                FrontPos = new BoardPos(pos).GetBottom();
                if (FrontPos > 0)
                {
                    LeftFrontPos  = new BoardPos(FrontPos).GetRight();
                    RightFrontPos = new BoardPos(FrontPos).GetLeft();
                }
                if (new BoardPos(pos).Y == 6)
                {
                    AddMoveIfValidSquare(team, pos, new BoardPos(FrontPos).GetBottom(), moves, (int)ETeamFlag.None);
                }
            }

            AddMoveIfValidSquare(team, pos, FrontPos, moves, (int)ETeamFlag.None);
            AddMoveIfValidSquare(team, pos, LeftFrontPos, moves, (int)ETeamFlag.Enemy);
            AddMoveIfValidSquare(team, pos, RightFrontPos, moves, (int)ETeamFlag.Enemy);
        }
Exemplo n.º 2
0
        public void GetValidMoves(EChessTeam team, List <Move> moves)
        {
            for (int i = 0; i < BOARD_SIZE * BOARD_SIZE; ++i)
            {
                if (Squares[i].Team == team)
                {
                    switch (Squares[i].Piece)
                    {
                    case EPieceType.King: GetValidKingMoves(team, i, moves); break;

                    case EPieceType.Queen: GetValidQueenMoves(team, i, moves); break;

                    case EPieceType.Pawn: GetValidPawnMoves(team, i, moves); break;

                    case EPieceType.Rook: GetValidRookMoves(team, i, moves); break;

                    case EPieceType.Bishop: GetValidBishopMoves(team, i, moves); break;

                    case EPieceType.Knight: GetValidKnightMoves(team, i, moves); break;

                    default: break;
                    }
                }
            }
        }
Exemplo n.º 3
0
    public void PlayTurn(Move move)
    {
        if (boardState.IsValidMove(teamTurn, move))
        {
            boardState.PlayUnsafeMove(move);

            EChessTeam otherTeam = (teamTurn == EChessTeam.White) ? EChessTeam.Black : EChessTeam.White;
            if (boardState.DoesTeamLose(otherTeam))
            {
                scores[(int)teamTurn]++;
                if (OnScoreUpdated != null)
                {
                    OnScoreUpdated(scores[0], scores[1]);
                }

                PrepareGame(false);
            }
            else
            {
                teamTurn = otherTeam;
            }
            // raise event
            if (OnPlayerTurn != null)
            {
                OnPlayerTurn(teamTurn == EChessTeam.White);
            }
        }
    }
Exemplo n.º 4
0
        public bool IsValidMove(EChessTeam team, Move move)
        {
            List <Move> validMoves = new List <Move>();

            GetValidMoves(team, validMoves);

            return(validMoves.Contains(move));
        }
Exemplo n.º 5
0
        public void PromoteTo(int pos, EPieceType piece)
        {
            EChessTeam color = GetColorFromPos(pos);
            PieceData  data  = GetPieceFromPos(pos, color);

            SetBitValue(EPieceTypeToBitBoardIndex(data.piece, color), pos, false);
            SetBitValue(EPieceTypeToBitBoardIndex(piece, color), pos, true);
        }
Exemplo n.º 6
0
        public BitBoardIndex GetBitBoardIndex(EChessTeam team, EPieceType piece)
        {
            if (team == EChessTeam.None)
            {
                return(BitBoardIndex.NONE);
            }

            return(EPieceTypeToBitBoardIndex(piece, team));
        }
Exemplo n.º 7
0
 public void AddMoveIfValidSquare(EChessTeam team, int from, int to, List <Move> moves, int teamFlag = (int)ETeamFlag.Enemy | (int)ETeamFlag.None)
 {
     if (IsValidSquare(to, team, teamFlag))
     {
         Move move;
         move.From = from;
         move.To   = to;
         moves.Add(move);
     }
 }
Exemplo n.º 8
0
 public void SetPieceAtSquare(int index, EChessTeam team, EPieceType piece)
 {
     BitBoard.BitBoardIndex tmpIndex = bitBoard.GetBitBoardIndex(team, piece);
     if (tmpIndex != BitBoard.BitBoardIndex.NONE)
     {
         bitBoard.SetBitValue(tmpIndex, index, true);
     }
     bitBoard.SetBitValue(BitBoard.BitBoardIndex.FREE_CASE, index, false);
     bitBoard.SetBitValue(BitBoard.BitBoardIndex.OCCUPED_CASE, index, true);
 }
Exemplo n.º 9
0
 public void GetValidKingMoves(EChessTeam team, int pos, List <Move> moves)
 {
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(1, 0)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(1, 1)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(0, 1)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(-1, 1)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(-1, 0)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(-1, -1)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(0, -1)), moves);
     AddMoveIfValidSquare(team, pos, (new BoardPos(pos) + new BoardPos(1, -1)), moves);
 }
Exemplo n.º 10
0
 // approximation : opponent king must be "eaten" to win instead of detecting checkmate state
 public bool DoesTeamLose(EChessTeam team)
 {
     for (int i = 0; i < Squares.Count; ++i)
     {
         if (Squares[i].Team == team && Squares[i].Piece == EPieceType.King)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 11
0
        public List <PieceData> GetAllPiecesListOfColor(EChessTeam team)
        {
            switch (team)
            {
            case EChessTeam.White: return(GetAllWhitePieceslist());

            case EChessTeam.Black: return(GetAllBlackPiecesList());

            default: throw new ArgumentException("team: ", team.ToString(), null);
            }
        }
Exemplo n.º 12
0
        public BitBoardIndex EPieceTypeToBitBoardIndex(EPieceType piece, EChessTeam team)
        {
            if (team == EChessTeam.None)
            {
                throw new ArgumentException("team", team.ToString());
            }
            string s_team = (team == EChessTeam.White) ? "WHITE_" : "BLACK_";

            s_team += piece.ToString();
            return((BitBoardIndex)Enum.Parse(typeof(BitBoardIndex), s_team.ToUpper(), true));
        }
Exemplo n.º 13
0
        public bool IsValidSquare(int pos, EChessTeam team, int teamFlag)
        {
            if (pos < 0)
            {
                return(false);
            }
            PieceData data        = bitBoard.GetPieceFromPos(pos);
            bool      isTeamValid = (data.team == EChessTeam.None && ((teamFlag & (int)ETeamFlag.None) > 0)) ||
                                    ((data.team != team && data.team != EChessTeam.None) && ((teamFlag & (int)ETeamFlag.Enemy) > 0));

            return(isTeamValid);
        }
Exemplo n.º 14
0
        public bool IsValidSquare(int pos, EChessTeam team, int teamFlag)
        {
            if (pos < 0)
            {
                return(false);
            }

            bool isTeamValid = ((Squares[pos].Team == EChessTeam.None) && ((teamFlag & (int)ETeamFlag.None) > 0)) ||
                               ((Squares[pos].Team != team && Squares[pos].Team != EChessTeam.None) && ((teamFlag & (int)ETeamFlag.Enemy) > 0));

            return(isTeamValid);
        }
Exemplo n.º 15
0
        private void SetPieceAtSquare(int index, EChessTeam team, EPieceType piece)
        {
            if (index > Squares.Count)
            {
                return;
            }
            BoardSquare square = Squares[index];

            square.Piece   = piece;
            square.Team    = team;
            Squares[index] = square;
        }
Exemplo n.º 16
0
        public bool DoesTeamLoseForCustomBoard(EChessTeam team, BoardState tmpBoard)
        {
            BitBoard.BitBoardIndex index = tmpBoard.bitBoard.EPieceTypeToBitBoardIndex(EPieceType.King, team);
            int idx      = 0;
            int iterator = 0;

            tmpBoard.bitBoard.FindFirstSetBit(tmpBoard.bitBoard.GetBitBoard(index), ref idx, ref iterator);
            if (idx != -1)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 17
0
        public PieceData GetPieceFromPos(int pos, EChessTeam team)
        {
            switch (team)
            {
            case EChessTeam.White: return(GetWhitePieceFromPos(pos));

            case EChessTeam.Black: return(GetBlackPieceFromPos(pos));

            case EChessTeam.None: return(new PieceData(EPieceType.None, EChessTeam.None, pos));

            default: throw new ArgumentException("team", team.ToString(), null);
            }
        }
Exemplo n.º 18
0
        // approximation : opponent king must be "eaten" to win instead of detecting checkmate state
        public bool DoesTeamLose(EChessTeam team)
        {
            BitBoard.BitBoardIndex index = bitBoard.EPieceTypeToBitBoardIndex(EPieceType.King, team);
            int idx      = 0;
            int iterator = 0;

            bitBoard.FindFirstSetBit(bitBoard.GetBitBoard(index), ref idx, ref iterator);

            if (idx != -1)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 19
0
        public void GetValidKnightMoves(EChessTeam team, int pos, List <Move> moves)
        {
            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(1, 2), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(2, 1), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(-1, 2), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(-2, 1), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(1, -2), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(2, -1), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(-1, -2), moves);

            AddMoveIfValidSquare(team, pos, new BoardPos(pos) + new BoardPos(-2, -1), moves);
        }
Exemplo n.º 20
0
        public void GetValidRookMoves(EChessTeam team, int pos, List <Move> moves)
        {
            bool bBreak = false;
            int  TopPos = new BoardPos(pos).GetTop();

            while (!bBreak && TopPos >= 0 && bitBoard.GetPieceFromPos(TopPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, TopPos, moves);
                bBreak = bitBoard.GetPieceFromPos(TopPos).team != EChessTeam.None;
                TopPos = new BoardPos(TopPos).GetTop();
            }

            bBreak = false;
            int BottomPos = new BoardPos(pos).GetBottom();

            while (!bBreak && BottomPos >= 0 && bitBoard.GetPieceFromPos(BottomPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, BottomPos, moves);
                bBreak    = bitBoard.GetPieceFromPos(BottomPos).team != EChessTeam.None;
                BottomPos = new BoardPos(BottomPos).GetBottom();
            }

            bBreak = false;
            int LeftPos = new BoardPos(pos).GetLeft();

            while (!bBreak && LeftPos >= 0 && bitBoard.GetPieceFromPos(LeftPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, LeftPos, moves);
                bBreak  = bitBoard.GetPieceFromPos(LeftPos).team != EChessTeam.None;
                LeftPos = new BoardPos(LeftPos).GetLeft();
            }

            bBreak = false;
            int RightPos = new BoardPos(pos).GetRight();

            while (!bBreak && RightPos >= 0 && bitBoard.GetPieceFromPos(RightPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, RightPos, moves);
                bBreak   = bitBoard.GetPieceFromPos(RightPos).team != EChessTeam.None;
                RightPos = new BoardPos(RightPos).GetRight();
            }
        }
Exemplo n.º 21
0
        public void GetValidBishopMoves(EChessTeam team, int pos, List <Move> moves)
        {
            bool bBreak      = false;
            int  TopRightPos = new BoardPos(pos) + new BoardPos(1, 1);

            while (!bBreak && TopRightPos >= 0 && bitBoard.GetPieceFromPos(TopRightPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, TopRightPos, moves);
                bBreak      = bitBoard.GetPieceFromPos(TopRightPos).team != EChessTeam.None;
                TopRightPos = new BoardPos(TopRightPos) + new BoardPos(1, 1);
            }

            bBreak = false;
            int TopLeftPos = new BoardPos(pos) + new BoardPos(-1, 1);

            while (!bBreak && TopLeftPos >= 0 && bitBoard.GetPieceFromPos(TopLeftPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, TopLeftPos, moves);
                bBreak     = bitBoard.GetPieceFromPos(TopLeftPos).team != EChessTeam.None;
                TopLeftPos = new BoardPos(TopLeftPos) + new BoardPos(-1, 1);
            }

            bBreak = false;
            int BottomRightPos = new BoardPos(pos) + new BoardPos(1, -1);

            while (!bBreak && BottomRightPos >= 0 && bitBoard.GetPieceFromPos(BottomRightPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, BottomRightPos, moves);
                bBreak         = bitBoard.GetPieceFromPos(BottomRightPos).team != EChessTeam.None;
                BottomRightPos = new BoardPos(BottomRightPos) + new BoardPos(1, -1);
            }

            bBreak = false;
            int BottomLeftPos = new BoardPos(pos) + new BoardPos(-1, -1);

            while (!bBreak && BottomLeftPos >= 0 && bitBoard.GetPieceFromPos(BottomLeftPos).team != team)
            {
                AddMoveIfValidSquare(team, pos, BottomLeftPos, moves);
                bBreak        = bitBoard.GetPieceFromPos(BottomLeftPos).team != EChessTeam.None;
                BottomLeftPos = new BoardPos(BottomLeftPos) + new BoardPos(-1, -1);
            }
        }
Exemplo n.º 22
0
    public void PrepareGame(bool resetScore = true)
    {
        chessAI = ChessAI.Instance;

        // Start game
        boardState.Reset();
        teamTurn = EChessTeam.White;
        if (scores == null)
        {
            scores = new List <uint>();
            scores.Add(0);
            scores.Add(0);
        }
        if (resetScore)
        {
            scores.Clear();
            scores.Add(0);
            scores.Add(0);
        }
    }
Exemplo n.º 23
0
        public void MovePiece(Move move)
        {
            EChessTeam color     = GetColorFromPos(move.From);
            PieceData  pieceFrom = GetPieceFromPos(move.From, color);

            color = GetColorFromPos(move.To);
            PieceData pieceTo = GetPieceFromPos(move.To, color);

            SetBitValue(EPieceTypeToBitBoardIndex(pieceFrom.piece, pieceFrom.team), pieceTo.pos, true);
            SetBitValue(EPieceTypeToBitBoardIndex(pieceFrom.piece, pieceFrom.team), pieceFrom.pos, false);

            if (pieceFrom.team == EChessTeam.White)
            {
                SetBitValue(FullColorBitBoard.WHITE_PIECES, pieceFrom.pos, false);
                SetBitValue(FullColorBitBoard.WHITE_PIECES, pieceTo.pos, true);
            }
            else if (pieceFrom.team == EChessTeam.Black)
            {
                SetBitValue(FullColorBitBoard.BLACK_PIECES, pieceFrom.pos, false);
                SetBitValue(FullColorBitBoard.BLACK_PIECES, pieceTo.pos, true);
            }

            if (color != EChessTeam.None)
            {
                SetBitValue(EPieceTypeToBitBoardIndex(pieceTo.piece, pieceTo.team), pieceTo.pos, false);
                if (pieceTo.team == EChessTeam.White)
                {
                    SetBitValue(FullColorBitBoard.WHITE_PIECES, pieceTo.pos, false);
                }
                else if (pieceFrom.team == EChessTeam.Black)
                {
                    SetBitValue(FullColorBitBoard.BLACK_PIECES, pieceTo.pos, false);
                }
            }

            SetBitValue(BitBoardIndex.FREE_CASE, pieceFrom.pos, true);
            SetBitValue(BitBoardIndex.FREE_CASE, pieceTo.pos, false);
            SetBitValue(BitBoardIndex.OCCUPED_CASE, pieceFrom.pos, false);
            SetBitValue(BitBoardIndex.OCCUPED_CASE, pieceTo.pos, true);
        }
Exemplo n.º 24
0
        public int Evaluate(EChessTeam team)
        {
            if (team == EChessTeam.None)
            {
                throw new ArgumentException("team: ", team.ToString(), null);
            }
            int score;
            int otherTeamScore;

            List <PieceData> currentTeamData = GetAllPiecesListOfColor(team);

            EvaluateTeam(currentTeamData, out score);

            EChessTeam       otherTeam     = (team == EChessTeam.White) ? EChessTeam.Black : EChessTeam.White;
            List <PieceData> otherTeamData = GetAllPiecesListOfColor(otherTeam);

            EvaluateTeam(otherTeamData, out otherTeamScore);

            int tmpScore = 189 - otherTeamScore;

            return(score + Mathf.Abs(tmpScore));
        }
Exemplo n.º 25
0
    public void PlayTurn(Move move)
    {
        if (boardState.IsValidMove(teamTurn, move))
        {
            if (boardState.PlayUnsafeMove(move))
            {
                // promote pawn to queen
                AddQueenAtPos(move.To);
            }

            EChessTeam otherTeam = (teamTurn == EChessTeam.White) ? EChessTeam.Black : EChessTeam.White;
            if (boardState.DoesTeamLose(otherTeam))
            {
                // increase score and reset board
                scores[(int)teamTurn]++;
                if (OnScoreUpdated != null)
                {
                    OnScoreUpdated(scores[0], scores[1]);
                }

                PrepareGame(false);
                // remove extra piece instances if pawn promotions occured
                teamPiecesArray[0].ClearPromotedPieces();
                teamPiecesArray[1].ClearPromotedPieces();
            }
            else
            {
                teamTurn = otherTeam;
            }
            // raise event
            if (OnPlayerTurn != null)
            {
                OnPlayerTurn(teamTurn == EChessTeam.White);
            }
        }
    }
Exemplo n.º 26
0
        public void GetValidPawnMoves(EChessTeam team, int pos, List <Move> moves)
        {
            int FrontPos = -1, LeftFrontPos = -1, RightFrontPos = -1;

            if (team == EChessTeam.White)
            {
                FrontPos = new BoardPos(pos).GetTop();
                if (FrontPos != -1)
                {
                    LeftFrontPos  = new BoardPos(FrontPos).GetLeft();
                    RightFrontPos = new BoardPos(FrontPos).GetRight();
                }
                if (new BoardPos(pos).Y == 1 && bitBoard.GetPieceFromPos(pos + BOARD_SIZE).piece == EPieceType.None)
                {
                    AddMoveIfValidSquare(team, pos, new BoardPos(FrontPos).GetTop(), moves, (int)ETeamFlag.None);
                }
            }
            else
            {
                FrontPos = new BoardPos(pos).GetBottom();
                if (FrontPos != -1)
                {
                    RightFrontPos = new BoardPos(FrontPos).GetLeft();
                    LeftFrontPos  = new BoardPos(FrontPos).GetRight();
                }

                if (new BoardPos(pos).Y == 6 && bitBoard.GetPieceFromPos(pos - BOARD_SIZE).piece == EPieceType.None)
                {
                    AddMoveIfValidSquare(team, pos, new BoardPos(FrontPos).GetBottom(), moves, (int)ETeamFlag.None);
                }
            }

            AddMoveIfValidSquare(team, pos, FrontPos, moves, (int)ETeamFlag.None);
            AddMoveIfValidSquare(team, pos, LeftFrontPos, moves, (int)ETeamFlag.Enemy);
            AddMoveIfValidSquare(team, pos, RightFrontPos, moves, (int)ETeamFlag.Enemy);
        }
Exemplo n.º 27
0
        public void GetValidMoves(EChessTeam team, List <Move> moves)
        {
            List <PieceData> pieceDatas = bitBoard.GetAllPiecesListOfColor(team);

            foreach (PieceData data in pieceDatas)
            {
                switch (data.piece)
                {
                case EPieceType.King: GetValidKingMoves(team, data.pos, moves); break;

                case EPieceType.Queen: GetValidQueenMoves(team, data.pos, moves); break;

                case EPieceType.Pawn: GetValidPawnMoves(team, data.pos, moves); break;

                case EPieceType.Rook: GetValidRookMoves(team, data.pos, moves); break;

                case EPieceType.Bishop: GetValidBishopMoves(team, data.pos, moves); break;

                case EPieceType.Knight: GetValidKnightMoves(team, data.pos, moves); break;

                default: break;
                }
            }
        }
Exemplo n.º 28
0
 public uint GetScore(EChessTeam team)
 {
     return(scores[(int)team]);
 }
Exemplo n.º 29
0
 public void MakeMove(EChessTeam currentTeam, Move move, BoardState currentBoard)
 {
     currentBoard.bitBoard.MovePiece(move);
 }
Exemplo n.º 30
0
 public int Evaluate(EChessTeam team, BoardState currentBoard)
 {
     return(currentBoard.bitBoard.Evaluate(team));
 }