Exemple #1
0
 public CommandManager(LatrunculiApp app, HistoryPrinter historyPrinter, DeskPrinter deskPrinter)
 {
     this.app            = app;
     this.historyPrinter = historyPrinter;
     this.deskPrinter    = deskPrinter;
     this.playerTypes    = new Dictionary <string, Func <IPlayer> >()
     {
         ["human"]    = () => createHumanPlayer(),
         ["random"]   = () => createRandomPlayer(),
         ["minimax2"] = () => createMiniMaxPlayer(2),
         ["minimax4"] = () => createMiniMaxPlayer(4),
         ["minimax3"] = () => createMiniMaxPlayer(3)
     };
     this.helpPlayer = createMiniMaxPlayer(3);
 }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("========================================");
            Console.WriteLine("Latrunculi - @author: František Pospíšil");
            Console.WriteLine("========================================");
            Console.WriteLine();

            var            latrunculi     = new LatrunculiApp();
            HistoryPrinter historyPrinter = new HistoryPrinter(latrunculi.HistoryManager);
            DeskPrinter    deskPrinter    = new DeskPrinter(latrunculi.Desk, latrunculi.HistoryManager);
            var            commandManager = new CommandManager(latrunculi, historyPrinter, deskPrinter);

            latrunculi.WhitePlayer =
                commandManager.createPlayerByType(args.FirstOrDefault()) ??
                commandManager.GetPlayerType(ChessBoxState.White);

            latrunculi.BlackPlayer =
                commandManager.createPlayerByType(args.Skip(1).FirstOrDefault()) ??
                commandManager.GetPlayerType(ChessBoxState.Black);

            while (true)
            {
                try
                {
                    while (true)
                    {
                        try
                        {
                            if (!latrunculi.IsEnded)
                            {
                                Console.WriteLine();
                                deskPrinter.PrintDesk();
                                string actualPlayer = latrunculi.HistoryManager.ActualPlayer == ChessBoxState.Black ? "černý" : "bílý";
                                Console.WriteLine();
                                WriteColoredMulti(
                                    TextSegment($"Tah "),
                                    TextSegment($"@{latrunculi.HistoryManager.ActualRound}", ConsoleColor.Yellow),
                                    TextSegment($", hraje "),
                                    TextSegment($"{actualPlayer} ", ConsoleColor.Yellow),
                                    TextSegment($"hráč ({latrunculi.ActualPlayer.ToString()}). "));
                                var move = latrunculi.Turn();
                                if (move != null)
                                {
                                    WriteColoredMulti(TextSegment("Zahrán tah "), TextSegment($"{move}", ConsoleColor.Green), TextSegment("."));
                                }
                                latrunculi.Rules.CheckEndOfGame();
                            }
                            else
                            {
                                Console.Write("Zadejte příkaz: ");
                                var line = Console.ReadLine();
                                commandManager.CheckCommand(line);
                            }
                        }
                        catch (EndOfGameException e)
                        {
                            Console.WriteLine();
                            deskPrinter.PrintDesk();
                            WriteColoredLine(e.Message, ConsoleColor.Green);
                            WriteColoredLine("Engine: " + (e.Winner == ChessBoxState.Black ? latrunculi.BlackPlayer : latrunculi.WhitePlayer).ToString(), ConsoleColor.Gray);
                            break;
                        }
                        catch (AbortGameException)
                        {
                            return;
                        }
                        catch (Exception e)
                        {
                            WriteColoredLine(e.Message, ConsoleColor.Red);
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
        }