コード例 #1
0
ファイル: Program.cs プロジェクト: caleefulling/chess
        public static void Main(string[] args)
        {
            Board board = new Board(args[0]);

            PieceColorEnum?winner      = null;
            bool           isCheckMate = false;
            bool           isDraw      = false;
            bool           validMove   = false;

            while (!isCheckMate && !isDraw)
            {
                Console.WriteLine($"\n{board.ColorToMove}'s move");
                board.PrintBoardWithTakenPieces();
                validMove = false;

                Console.Write(" > ");
                var command = Console.ReadLine().Trim();
                if (command == "stats")
                {
                    Console.WriteLine($"black score: {board.BlackScore}, white score: {board.WhiteScore}");
                }
                if (command == "flip")
                {
                    board = board.FlipBoard();
                }
                else if (command.Contains("0-0"))
                {
                    // castle
                    CastleTypeEnum castleType = CastleTypeEnum.Queenside;
                    var            king       = board.ColorToMove == PieceColorEnum.Black ? board.BlackKing : board.WhiteKing;
                    if (command == "0-0")
                    {
                        // castle kingside
                        castleType = CastleTypeEnum.Kingside;
                    }
                    else if (command == "0-0-0")
                    {
                        // castle queenside
                        castleType = CastleTypeEnum.Queenside;
                    }

                    var canCastleResult = king.CanCastle(castleType, board);
                    if (canCastleResult.CanCastle)
                    {
                        var castleResult = king.Castle(castleType, board);
                        if (castleResult.Castled)
                        {
                            validMove = true;
                        }
                        else
                        {
                            Console.WriteLine($"can't castle {castleType}, {castleResult.Error}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"can't castle {castleType}, {canCastleResult.Error}");
                    }
                }
                else
                {
                    try
                    {
                        var commands = command.Split(' ');
                        var startPos = board.ConvertNotationForMove(commands[0]);
                        var endPos   = board.ConvertNotationForMove(commands[1]);

                        var piece = board.Instance[startPos.Key, startPos.Value];
                        if (piece != null && piece.Color == board.ColorToMove)
                        {
                            var moves = piece.AvailableMoves(board);
                            if (moves.Contains(new KeyValuePair <int, int>(endPos.Key, endPos.Value)))
                            {
                                // valid move
                                board.MovePiece(piece, endPos.Key, endPos.Value);
                                validMove = true;
                            }
                            else
                            {
                                Console.WriteLine($"piece at {commands[0]} can't move to {commands[1]}");
                            }
                        }
                        else if (piece == null)
                        {
                            Console.WriteLine($"piece at {commands[0]} not found");
                        }
                        else
                        {
                            Console.WriteLine($"piece at {commands[0]} isn't yours to move");
                        }
                    }
                    catch
                    {
                        Console.WriteLine($"invalid command");
                    }
                }

                if (validMove)
                {
                    // see if there's a resulting check or mate
                    King king          = board.ColorToMove == PieceColorEnum.Black ? board.WhiteKing : board.BlackKing;
                    var  isCheckOrMate = board.IsCheckOrMate(king);
                    if (isCheckOrMate.IsMate && isCheckOrMate.IsCheck)
                    {
                        // check & mate
                        isCheckMate = true;
                        winner      = board.ColorToMove;
                    }
                    else if (isCheckOrMate.IsCheck)
                    {
                        // check
                        Console.WriteLine("check");
                    }

                    // check if it's stalemate
                    if (board.IsStaleMate(board.ColorToMove == PieceColorEnum.Black ? PieceColorEnum.White : PieceColorEnum.Black))
                    {
                        isDraw = true;
                    }

                    board.ColorToMove = board.ColorToMove == PieceColorEnum.Black ? PieceColorEnum.White : PieceColorEnum.Black;
                }
            }

            if (winner != null)
            {
                Console.WriteLine($"check mate - game over. {winner} wins!");
            }
            else
            {
                Console.WriteLine($"draw - game over.");
            }

            Console.WriteLine($"black score: {board.BlackScore}, white score: {board.WhiteScore}");
            Console.ReadLine();
        }