Esempio n. 1
0
        public static string TakeStringInput(bool allowCommand)
        {
            string str = Console.ReadLine();
            string s   = str.ToUpper();

            if (s.StartsWith("/"))
            {
                if (s == "/" || s == "/HELP")
                {
                    Help();
                    return(null);
                }
                else if (s.StartsWith("/SAVE"))
                {
                    C4TextSaveRepository sr = new C4TextSaveRepository();
                    string fileName         = "";
                    try
                    {
                        fileName = s.Split(" ")[1];
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        fileName = DateTime.Now.ToString("MMdd-HHmm");
                    }
                    sr.Save(fileName);
                }
                else if (s.StartsWith("/LOAD"))
                {
                    C4TextSaveRepository sr = new C4TextSaveRepository();
                    try
                    {
                        string      fileName      = s.Split(" ")[1];
                        MoveHistory loadedHistory = sr.Load(fileName);
                        if (loadedHistory != null)
                        {
                            ConnectFourBoard newBoard = loadedHistory.ReconstructBoard(loadedHistory.MoveList.Count) as ConnectFourBoard;
                            newBoard.Render();
                            Console.WriteLine(">> Warning: Loading a position will terminate the current round. ");
                            Console.WriteLine(">> You may want to save your position with /save <filename> before continuing. ");
                            Console.WriteLine(">> Press [Enter] to continue, or [Esc] to cancel. ");
                            if (Console.ReadKey().Key == ConsoleKey.Enter)
                            {
                                Connect4Game.Instance.GameBoard       = newBoard;
                                Connect4Game.Instance.GameMoveHistory = loadedHistory;
                                Connect4Game.Instance.ContinueFromMove(loadedHistory.MoveList.Count,
                                                                       Connect4Game.Instance.GameBoard); // set the new board to live, and continue with the correct ActivePlayer.
                            }
                        }
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        Console.WriteLine(">> Found the following save files: ");
                        foreach (var fileName in sr.FetchSaves())
                        {
                            Console.WriteLine(fileName);
                        }
                        Console.WriteLine(">> (End of list) ");
                    }
                }

                // Traverse the move list...
                else if (s.StartsWith("/TRAVERSE"))
                {
                    if (Connect4Game.Instance.GameMoveHistory == null || Connect4Game.Instance.GameMoveHistory.MoveList.Count == 0)
                    {
                        Console.WriteLine(">> No move history to traverse. Use this feature once you've made some moves!");
                        return(null);
                    }
                    Console.WriteLine("********************************************************************************");
                    Console.WriteLine(">> You are now in traverse mode. ");
                    Console.WriteLine(">> Use /moves to see a list of past moves. ");
                    Console.WriteLine(">> Use /tb and /rp to traverse the move list; ");
                    Console.WriteLine(">> Use /goto <#> (i.e. \"/goto 5\") to go to a position directly;");
                    Console.WriteLine(">> use /select to continue from the chosen position.");
                    Console.WriteLine(">> Press [Enter] with no input to exit traverse mode. ");
                    Console.WriteLine("********************************************************************************");
                    int  current = Connect4Game.Instance.GameMoveHistory.MoveList.Count;
                    bool quit    = false;
                    while (!quit)
                    {
                        string input = Console.ReadLine().ToUpper();
                        if (input.StartsWith("/TAKEBACK") || input.StartsWith("/TB"))
                        {
                            if (current > 0)
                            {
                                current--;
                            }
                            else
                            {
                                Console.WriteLine("No more move to take back.");
                            }
                            Connect4Game.Instance.GoToMove(current).Render();
                        }
                        else if (input.StartsWith("/REPLAY") || input.StartsWith("/RP"))
                        {
                            if (current < Connect4Game.Instance.GameMoveHistory.MoveList.Count)
                            {
                                current++;
                            }
                            else
                            {
                                Console.WriteLine("No more move to replay.");
                            }
                            Connect4Game.Instance.GoToMove(current).Render();
                        }
                        else if (input.StartsWith("/SELECT"))
                        {
                            quit = true;
                            Connect4Game.Instance.ContinueFromMove(current, Connect4Game.Instance.GoToMove(current));
                            Connect4Game.Instance.GameBoard.Render();
                        }
                        else if (input.StartsWith("/MOVES"))
                        {
                            Console.WriteLine(Connect4Game.Instance.GameMoveHistory);
                        }
                        else if (input.StartsWith("/GOTO "))
                        {
                            if (int.TryParse(input.Substring(6), out current))
                            {
                                Connect4Game.Instance.GoToMove(current).Render();
                            }
                            ;
                        }
                        else if (input == "")
                        {
                            // Re-renders the board and quit.
                            Connect4Game.Instance.GameBoard.Render();
                            quit = true;
                        }
                    }
                }
                return(null);
            }
            return(str);
        }