Exemplo n.º 1
0
        private List<Tuple< int, int>> getCanMove(
            IEnumerable<Tuple<MoveType, int, int>> locateInfos,
            KomaBase koma)
        {
            var inBoardLocates = this.getInBoardLocate(locateInfos);
            var canMoveLocates = new List<Tuple< int, int>>();

            canMoveLocates.AddRange(this.getNotExistsAny(inBoardLocates));
            canMoveLocates.AddRange(this.getExistsEnemy(inBoardLocates));
            canMoveLocates.AddRange(this.getDirection(inBoardLocates, koma));
            canMoveLocates.AddRange(this.getNormal(inBoardLocates));

            return canMoveLocates;

        }
Exemplo n.º 2
0
        //create new board
        public Board()
        {
            //Initiate Pawns
            for (int i = 0; i < 8; i++)
            {
                WhitePieces.Add(new Pawn(true, this));
                BlackPieces.Add(new Pawn(false, this));
            }

            //Initiate Knights/Bishops/Rooks
            for (int i = 0; i < 2; i++)
            {
                WhitePieces.Add(new Knight(true, this));
                BlackPieces.Add(new Knight(false, this));
                WhitePieces.Add(new Bishop(true, this));
                BlackPieces.Add(new Bishop(false, this));
                WhitePieces.Add(new Rook(true, this));
                BlackPieces.Add(new Rook(false, this));
            }
            //Intiate Kings/Queens
            WhitePieces.Add(new King(true, this));
            WhitePieces.Add(new Queen(true, this));

            BlackPieces.Add(new Queen(false, this));
            BlackPieces.Add(new King(false, this));

            AllPieces = WhitePieces;
            AllPieces.AddRange(BlackPieces);
        }
Exemplo n.º 3
0
        public List<KomaBase> KomaCreate()
        {
            if (komaList != null)
            {
                return komaList;
            }
            komaList = new List<KomaBase>();
            foreach (KomaKind komaKind in Enum.GetValues(typeof(KomaKind)))
            {
                int index = 0;
                while(true)
                {
                    var komaKuro = createKoma(komaKind, PlayerNo.Two, index);
                    komaKuro.SetDefaultPoint();
                    var komaShiro = createKoma(komaKind, PlayerNo.One, index);
                    komaShiro.SetDefaultPoint();
                    komaList.AddRange(new[] { komaKuro, komaShiro });

                    if (komaShiro.GetMaxCount() == index + 1)
                        break;
                    ++index;

                   
                }
            }


            return komaList;

        }
Exemplo n.º 4
0
        private void RookMoves(ref Piece rook)
        {
            var moves = new List<Move>();

            moves.AddRange(NextLegalSquares(rook.Square, Direction.Up, rook.Color));
            moves.AddRange(NextLegalSquares(rook.Square, Direction.Down, rook.Color));
            moves.AddRange(NextLegalSquares(rook.Square, Direction.Left, rook.Color));
            moves.AddRange(NextLegalSquares(rook.Square, Direction.Right, rook.Color));

            rook.Moves = moves;
        }
Exemplo n.º 5
0
        private void LegalQueenMoves(ref Piece queen)
        {
            var moves = new List<Move>();

            foreach (Direction direction in Enum.GetValues(typeof(Direction)))
            {
                moves.AddRange(NextLegalSquares(queen.Square, direction, queen.Color));
            }

            queen.Moves = moves;
        }
Exemplo n.º 6
0
        private void BishopMoves(ref Piece bishop)
        {
            var moves = new List<Move>();

            moves.AddRange(NextLegalSquares(bishop.Square, Direction.UpperLeft, bishop.Color));
            moves.AddRange(NextLegalSquares(bishop.Square, Direction.UpperRight, bishop.Color));
            moves.AddRange(NextLegalSquares(bishop.Square, Direction.LowerLeft, bishop.Color));
            moves.AddRange(NextLegalSquares(bishop.Square, Direction.LowerRight, bishop.Color));

            bishop.Moves = moves;
        }
Exemplo n.º 7
0
        public IList<Piece> GetAttackers(Square square, PlayerColor attacker)
        {
            var ret = new List<Piece>();

            //Find adjacent king
            if (this[attacker].First(i => i is King).Square.IsAdjacent(square))
                ret.Add(this[attacker].First(i => i is King));

            //Find attacking Pawn
            foreach (var dir in Pawn.GetAttackingDirection(attacker).GetDiagonals())
            {
                var pawn = this[square.Move(dir)] as Pawn;
                if (pawn != null && pawn.Player == attacker)
                    ret.Add(pawn);
            }

            var opponents = this[attacker].Where(i => !(i is King));
            var moves = opponents.Where(i => !(i is King || i is Pawn)).SelectMany(o => o.GetValidMoves().Where(i => i.Target == square && i.CanCapture));

            ret.AddRange(moves.Select(i => i.Piece));
            return ret;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Checks the endgame.
        /// </summary>
        /// <param name="oppositePlayer">The opposite player.</param>
        /// <returns>
        ///     <c>GameEventType.None</c> if there were no threats from either side.
        ///     <c>GameEventType.Check</c> if there was a check from the opposing side.
        ///     <c>GameEventType.Checkmate</c> if there was a checkmate from the opposing side.
        ///     <c>GameEventType.Stalemate</c> if there is a stalemate.
        /// </returns>
        public override GameEventType CheckEndgame(PlayerBase oppositePlayer)
        {
            List<Position> oppositeKingMoves;
            List<Position> attacks;
            GameEventType endgame = GameEventType.None;

            if (oppositePlayer == null || !(oppositePlayer is PlayerChess))
            {
                throw new Exception("oppositePlayer is not of PlayerChess type");
            }

            oppositeKingMoves = ((PlayerChess)oppositePlayer).King.AllMoveOptions;
            foreach (IPiece piece in this.PieceList)
            {
                attacks = piece.AttackOptions;
                foreach (Position target in attacks)
                {
                    if (this.gameboard.Figures[target] != null && this.gameboard.Figures[target] is King)
                    {
                        endgame = GameEventType.Check;
                        break;
                    }
                }
            }

            // If there is no Check and no Checkmate, check for Stalemate.
            List<Position> posList = new List<Position>();
            bool isStalemate = false;

            // Aggregate all the possible moves from this player's pieces
            foreach (IPiece piece in this.PieceList)
            {
                posList.AddRange(piece.AllMoveOptions.ToList());
                posList.Add(piece.Position);
            }

            // Check each possible move of the opposite king's moves
            foreach (Position position in oppositeKingMoves)
            {
                isStalemate = true;
                if (!posList.Contains(position))
                {
                    isStalemate = false;
                    break;
                }
            }

            if (isStalemate)
            {
                if (endgame == GameEventType.Check)
                {
                    return GameEventType.Checkmate;
                }

                // else
                // {
                //    return GameEventType.Stalemate;
                // }
            }

            return endgame;
        }
Exemplo n.º 9
0
        private static List<position_t> Rook(ChessBoard board, position_t pos, bool verify_check = true)
        {
            List<position_t> moves = new List<position_t>();

            piece_t p = board.Grid[pos.number][pos.letter];
            if (p.piece == Piece.NONE) return moves;

            // slide along vert/hor for possible moves
            moves.AddRange(Slide(board, p.player, pos, new position_t(1, 0)));
            moves.AddRange(Slide(board, p.player, pos, new position_t(-1, 0)));
            moves.AddRange(Slide(board, p.player, pos, new position_t(0, 1)));
            moves.AddRange(Slide(board, p.player, pos, new position_t(0, -1)));

            if (verify_check)// make sure each move doesn't put us in check
            {
                for (int i = moves.Count - 1; i >= 0; i--)
                {
                    ChessBoard b2 = LegalMoveSet.move(board, new move_t(pos, moves[i]));
                    if (isCheck(b2, p.player))
                    {
                        moves.RemoveAt(i);
                    }
                }
            }
            return moves;
        }