Пример #1
0
        static void Main()
        {
            //console settings
            Console.Title        = "Just Star Chess";
            Console.BufferHeight = 50;
            Console.BufferWidth  = 102;
            Console.SetWindowSize(102, 50);

            string menu      = Menu.Initialise();
            bool   hasLoaded = false;

            Board board = new Board();

            InitialBoard();

            WhitePlayer = new Player("None", Colors.White);
            BlackPlayer = new Player("None", Colors.Black);

            switch (menu)
            {
            case "LOAD":
                try
                {
                    Menu.Load();
                    hasLoaded = true;
                }
                catch (Exception)
                {
                    System.Console.SetCursorPosition(33, 27);
                    System.Console.Write("No file to load! Starting a new game...");
                    System.Threading.Thread.Sleep(2500);
                    hasLoaded = false;
                }
                break;

            case "HELP":
                Menu.Help();
                break;

            case "EXIT":
                Console.SetCursorPosition(0, 0);
                return;
            }

            bool hasCheck = false;

            //check for loading
            if (!hasLoaded)
            {
                PlayerInitialisation();
            }

            while (true)
            {
                whiteTimer.StartCounting = DateTime.Now;
                blackTimer.StartCounting = DateTime.Now;

                Console.SetCursorPosition(37, 2);

                Console.BackgroundColor = ConsoleColor.Black;

                board.PrintBoard(BoardFigures);

                Console.BackgroundColor = ConsoleColor.Black;

                //hasCheck = ChessChecker.ChessValidator(BoardFigures); (for check checking but it is buggy)

                //if check
                if (hasCheck)
                {
                    Console.SetCursorPosition(29, 2);
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine("CHECK!");
                }
                else
                {
                    Console.SetCursorPosition(29, 2);
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine("      ");
                }

                ShowHistory();

                ShowTakenFigures();

                Console.SetCursorPosition(36, 2);

                Console.Write("                                                  "); //clears old symbols on console

                Console.SetCursorPosition(36, 2);

                if (CurrentPlayer == "white")
                {
                    Console.Write("{0}{1}: ", WhitePlayer.Name[0].ToString().ToUpper(), WhitePlayer.Name.Substring(1));
                }
                else
                {
                    Console.Write("{0}{1}: ", BlackPlayer.Name[0].ToString().ToUpper(), BlackPlayer.Name.Substring(1));
                }

                string input = Console.ReadLine();

                if (input.ToUpper() == "EXIT")
                {
                    Console.SetCursorPosition(0, 0);
                    return;
                }
                else if (input.ToUpper() == "LOAD")
                {
                    try
                    {
                        Menu.Load();
                        Console.SetCursorPosition(34, 2);
                        Console.Write("   Game loaded");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }
                    catch (Exception)
                    {
                        System.Console.SetCursorPosition(35, 2);
                        System.Console.Write("No file to load!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(25, 2);
                        Console.Write("                                        ");
                        continue;
                    }
                }
                else if (input.ToUpper() == "SAVE")
                {
                    Menu.Save();
                    Console.SetCursorPosition(34, 2);
                    Console.Write("   Game saved");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                         ");
                    continue;
                }

                Input currentInput = new Input(input);

                try
                {
                    currentInput.ValidateInput();
                }
                catch
                {
                    Console.SetCursorPosition(31, 2);
                    Console.Write("   Invalid command!");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                          ");
                    continue;
                }

                {
                    string forHistory = input;

                    input = currentInput.GetFieldCoordinates();

                    string[] coordinateSplit = input.Split('-');

                    int lastRow = int.Parse(coordinateSplit[0]);
                    int lastCol = int.Parse(coordinateSplit[1]);

                    //for check and mate
                    int nextCol = int.Parse(coordinateSplit[2]);
                    int nextRow = int.Parse(coordinateSplit[3]);

                    FigureProperties figure = BoardFigures.Position[lastCol, lastRow];

                    //check player color
                    if (figure.Name != Names.None && figure.Color != CurrentColor)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("Figure is not your color!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }

                    //create figures
                    Pawn   pawn   = new Pawn(figure.Color, figure.Name);
                    Knight knight = new Knight(figure.Color, figure.Name);
                    Bishop bishop = new Bishop(figure.Color, figure.Name);
                    Rook   rook   = new Rook(figure.Color, figure.Name);
                    Queen  queen  = new Queen(figure.Color, figure.Name);
                    King   king   = new King(figure.Color, figure.Name);

                    if (figure.Name == Names.None)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("     Empty field!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }
                    else if (figure.Name == Names.Pawn)
                    {
                        if (!pawn.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Pawn cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Knight)
                    {
                        if (!knight.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Knight cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Bishop)
                    {
                        if (!bishop.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Bishop cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Rook)
                    {
                        if (!rook.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Rook cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Queen)
                    {
                        if (!queen.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Queen cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.King)
                    {
                        if (!king.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("King cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }

                    //add move into history
                    if (CurrentPlayer == "white")
                    {
                        WhiteHistory.Manage(forHistory.ToUpper());
                    }
                    else
                    {
                        BlackHistory.Manage(forHistory.ToUpper());
                    }

                    //change players
                    if (CurrentPlayer == "white")
                    {
                        CurrentPlayer = "black";
                        CurrentColor  = Colors.Black;
                    }
                    else
                    {
                        CurrentPlayer = "white";
                        CurrentColor  = Colors.White;
                    }
                }
            }
        }
Пример #2
0
        static void Main()
        {
            //console settings
            Console.Title = "Just Star Chess";
            Console.BufferHeight = 50;
            Console.BufferWidth = 102;
            Console.SetWindowSize(102, 50);

            string menu = Menu.Initialise();
            bool hasLoaded = false;

            Board board = new Board();

            InitialBoard();

            WhitePlayer = new Player("None", Colors.White);
            BlackPlayer = new Player("None", Colors.Black);

            switch (menu)
            {
                case "LOAD":
                    try
                    {
                        Menu.Load();
                        hasLoaded = true;
                    }
                    catch (Exception)
                    {
                        System.Console.SetCursorPosition(33, 27);
                        System.Console.Write("No file to load! Starting a new game...");
                        System.Threading.Thread.Sleep(2500);
                        hasLoaded = false;
                    }
                    break;
                case "HELP":
                    Menu.Help();
                    break;
                case "EXIT":
                    Console.SetCursorPosition(0, 0);
                    return;
            }

            bool hasCheck = false;

            //check for loading
            if (!hasLoaded)
            {
                PlayerInitialisation();
            }

            /*
            //TEST EVENTS
            //Publisher pub = new Publisher();
            Publisher pub = new Publisher();
            Subscriber sub1 = new Subscriber("Game Over, Player 1 Wins", pub);
            Subscriber sub2 = new Subscriber("sub2", pub);

            // Call the method that raises the event.
            pub.DoSomething();
            */

            /*
            Console.Write("Enter Player 1 name: ");
            string name = Console.ReadLine();
            */

            while (true)
            {
                whiteTimer.StartCounting = DateTime.Now;
                blackTimer.StartCounting = DateTime.Now;

                Console.SetCursorPosition(37, 2);

                Console.BackgroundColor = ConsoleColor.Black;

                board.PrintBoard(BoardFigures);

                Console.BackgroundColor = ConsoleColor.Black;

                //if check
                if (hasCheck)
                {
                    Console.SetCursorPosition(0, 0);
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine("CHECK!!!");
                }

                ShowHistory();

                ShowTakenFigures();

                Console.SetCursorPosition(36, 2);

                Console.Write("                                                  "); //clears old symbols on console

                Console.SetCursorPosition(36, 2);

                if (CurrentPlayer == "white")
                {
                    Console.Write("{0}{1}: ", WhitePlayer.Name[0].ToString().ToUpper(), WhitePlayer.Name.Substring(1));
                }
                else
                {
                    Console.Write("{0}{1}: ", BlackPlayer.Name[0].ToString().ToUpper(), BlackPlayer.Name.Substring(1));
                }

                string input = Console.ReadLine();

                if (input.ToUpper() == "EXIT")
                {
                    Console.SetCursorPosition(0, 0);
                    return;
                }
                else if (input.ToUpper() == "LOAD")
                {
                    Menu.Load();
                    Console.SetCursorPosition(34, 2);
                    Console.Write("   Game loaded");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                         ");
                    continue;
                }
                else if (input.ToUpper() == "SAVE")
                {
                    Menu.Save();
                    Console.SetCursorPosition(34, 2);
                    Console.Write("   Game saved");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                         ");
                    continue;
                }

                Input currentInput = new Input(input);

                try
                {
                    currentInput.ValidateInput();
                }
                catch
                {
                    Console.SetCursorPosition(31, 2);
                    Console.Write("   Invalid command!");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                          ");
                    continue;
                }

                {
                    string forHistory = input;

                    input = currentInput.GetFieldCoordinates();

                    string[] coordinateSplit = input.Split('-');

                    int lastRow = int.Parse(coordinateSplit[0]);
                    int lastCol = int.Parse(coordinateSplit[1]);

                    //for check and mate
                    int nextCol = int.Parse(coordinateSplit[2]);
                    int nextRow = int.Parse(coordinateSplit[3]);

                    FigureProperties figure = BoardFigures.Position[lastCol, lastRow];

                    //check player color
                    if (figure.Name != Names.None && figure.Color != CurrentColor)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("Figure is not your color!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }

                    //create figures
                    Pawn pawn = new Pawn(figure.Color, figure.Name);
                    Knight knight = new Knight(figure.Color, figure.Name);
                    Bishop bishop = new Bishop(figure.Color, figure.Name);
                    Rook rook = new Rook(figure.Color, figure.Name);
                    Queen queen = new Queen(figure.Color, figure.Name);
                    King king = new King(figure.Color, figure.Name);

                    if (figure.Name == Names.None)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("     Empty field!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }
                    else if (figure.Name == Names.Pawn)
                    {
                        if (!pawn.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Pawn cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Knight)
                    {
                        if (!knight.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Knight cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Bishop)
                    {
                        if (!bishop.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Bishop cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Rook)
                    {
                        if (!rook.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Rook cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Queen)
                    {
                        if (!queen.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Queen cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.King)
                    {
                        if (!king.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("King cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }

                    //add move into history
                    if (CurrentPlayer == "white")
                    {
                        WhiteHistory.Manage(forHistory.ToUpper());
                    }
                    else
                    {
                        BlackHistory.Manage(forHistory.ToUpper());
                    }

                    //change players
                    if (CurrentPlayer == "white")
                    {
                        CurrentPlayer = "black";
                        CurrentColor = Colors.Black;
                    }
                    else
                    {
                        CurrentPlayer = "white";
                        CurrentColor = Colors.White;
                    }
                }
            }
        }