示例#1
0
        public void ClearPossibleMoves(Piece piece, params Predicate <IMove>[] predicates)
        {
            var illegalMoves = new List <IMove>();

            var moves = piece.GetMoves();

            foreach (var move in moves)
            {
                var illegal = false;

                foreach (var predicate in predicates)
                {
                    if (!predicate(move))
                    {
                        continue;
                    }

                    illegal = true;
                    break;
                }

                if (illegal)
                {
                    illegalMoves.Add(move);
                }
            }

            piece.ClearMoves(illegalMoves);
        }
示例#2
0
        public void PrintBoard(Piece selectedPiece = null)
        {
            Console.Clear();
            var roundFlag = true;

            for (int i = _board.GetLength(0) - 1; i >= 0; i--)
            {
                Console.WriteLine("---------------------------------------------");
                Console.Write($"  {i + 1} |");
                for (int j = 0; j < _board.GetLength(1); j++)
                {
                    var piece = _board[i, j].OccupyingPiece;

                    var pos = new PiecePosition(i, j);

                    if (roundFlag)
                    {
                        Console.BackgroundColor = ConsoleColor.Gray;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.DarkGray;
                    }

                    roundFlag = !roundFlag;

                    if (selectedPiece != null && selectedPiece.GetMoves().Any(m => m.GetMovePos().Position == pos))
                    {
                        Console.BackgroundColor = ConsoleColor.Magenta;
                    }

                    if (piece == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.Write("     ");
                        continue;
                    }

                    Console.ForegroundColor = piece.PieceOwner.Side == "bottom" ? ConsoleColor.DarkBlue : ConsoleColor.DarkRed;
                    Console.Write($"  {piece.VisualID}  ");
                }

                // offset for checker board pattern.
                roundFlag = !roundFlag;

                Console.ResetColor();

                Console.WriteLine();
            }

            Console.WriteLine("---------------------------------------------");
            for (int i = 0; i < _board.GetLength(1) + 1; i++)
            {
                Console.Write($"| {i} |");
            }
            Console.WriteLine("\n---------------------------------------------");
        }
示例#3
0
        //Turn function
        public static bool turn()
        {
            //determine current player
            string player = PLAYERS[turns % 2];

            //Draw board and prompt
            Console.Clear();
            Console.WriteLine(board);
            Console.WriteLine(String.Format("Where would you like to move, {0}?", player));
            Console.WriteLine("\tType 'h' for help");

            //request input
            string input = Console.ReadLine();

            //Lambda for printing help menu;
            Action printH = () => {
                Console.WriteLine("Type [piece],[location] to move your piece.");
                Console.WriteLine("\tEx: 'wp1,a-3'");
                Console.WriteLine("Type [piece] to see a piece's moves.");
                Console.WriteLine("\tEx: 'wp1'");
                //After, printing instructions, request another input and
                //		return to original logic flow
                input = Console.ReadLine();
            };

            //Check for 'h' (or 'H')
            if (input.ToUpper().Equals("H"))
            {
                printH();
            }

            //loop variables
            bool   validMove = false;
            string message   = null;

            //Do, while the user has not entered a valid input, or moved
            do
            {
                while (!verify.Match(input).Success || message != null)
                {
                    if (message != null)
                    {
                        Console.WriteLine(message);
                    }
                    Console.WriteLine("Invalid input!  Try again!");
                    message = null;
                    input   = Console.ReadLine();
                    //Check for 'h' (or 'H')
                    if (input.ToUpper().Equals("H"))
                    {
                        printH();
                    }
                }
                //If input contains a "," and it passed the Regex match, then it must be a movement action
                if (input.Contains(","))
                {
                    string   piece;
                    string   move;
                    string[] splits = input.Split(",");
                    piece = splits[0];
                    move  = splits[1];

                    //Retrieving piece from board
                    Piece p = Chess.GetBoard().GetPiece(piece);
                    if (p != null)
                    {
                        if (p.GetColor() == PLAYTYPES[turns % 2])
                        {
                            //Parsing moveTo location
                            int   col    = ((int)move[0]) - 'a';
                            int   row    = 8 - (((int)move[2]) - '0');
                            Point target = new Point(row, col);

                            //Getting piece's moves
                            List <Point> moves = p.GetMoves();


                            //Searching through movelist for a point matching the moveTo coordinate
                            bool found = moves.Exists((point) => point.Equals(target));

                            //movepiece
                            if (found)
                            {
                                bool moved;
                                (moved, message) = MovePiece(p, target);

                                //If piece was able to move
                                if (moved)
                                {
                                    if (p.GetType() == typeof(Pawn))
                                    {
                                        ((Pawn)p).SetMoved(moved);

                                        //A pawn cannot move backwards, so the only time
                                        //		any pawn can reach the edge of the field is when it
                                        //		hits the opposite edge, turn the piece into a queen
                                        if (p.GetLoc().X == 7 || p.GetLoc().X == 0)
                                        {
                                            Chess.GetBoard().SetPiece(new Queen(p.GetColor(), p.GetLoc()), p.GetLoc());
                                        }
                                    }

                                    if (message != null && message.Equals("Check mate!"))
                                    {
                                        return(false);
                                    }
                                    validMove = true;
                                }
                                //Else if piece couldn't move (checking self)!
                                //		move was not valid, try again
                                else
                                {
                                }
                            }
                            else
                            {
                                message = "Cannot move there!";
                                //return false;
                            }
                        }
                        else
                        {
                            message = "Not your piece";
                            //return false;
                        }
                    }
                    //Invalid move location
                    else
                    {
                        message = String.Format("Cannot move there!\n\t" +
                                                "Type '{0}' to view the moves of piece {0}", piece);
                    }
                }
                //Else, printing moves of piece
                else
                {
                    //Getting piece
                    Piece piece = Chess.GetBoard().GetPiece(input);

                    //Getting piece's moves
                    List <Point> moves = piece.GetMoves();


                    string str = Chess.GetBoard().ToString(moves);


                    //Prining new board
                    Console.Clear();
                    Console.WriteLine(Chess.GetBoard().ToString(moves));
                    Console.WriteLine("Type 'x' to remove x's!");
                    input = Console.ReadLine();
                    //Check for 'h' (or 'H')
                    if (input.ToUpper().Equals("H"))
                    {
                        printH();
                    }
                    else if (input.ToUpper().Equals("X"))
                    {
                        //Printing cleared board
                        Console.Clear();
                        Console.WriteLine(Chess.GetBoard());
                        Console.WriteLine("Continue!");
                        input = Console.ReadLine();
                    }
                }
            }while (!validMove);


            turns++;
            return(true);
        }