コード例 #1
0
        public Move GenerateMove(Board board, Player player)
        {
            Random rand = new Random();

            ConnectFourBoard gameBoard = board as ConnectFourBoard;

            int    bestCount    = 1;
            string selectedMove = "0";

            foreach (int possibleMove in gameBoard.OpenColumns)
            {
                int[,] imaginaryMatrix = Utils.CopyBoardMatrix(gameBoard.MatrixFormat());
                int result = TryMove(imaginaryMatrix, possibleMove, player.Id);
                if (result >= 4)
                {
                    selectedMove = possibleMove.ToString();
                    break;
                }
                else if (result > bestCount)
                {
                    bestCount    = result;
                    selectedMove = possibleMove.ToString();
                }
                else if (result == bestCount)
                {
                    if (rand.NextDouble() < 0.1)
                    {
                        selectedMove = possibleMove.ToString();
                    }
                }
                ;
            }
            return(new ConnectFourMove(selectedMove, player.Id));
        }
コード例 #2
0
        private int currentPlayer; //either 1 or 2



        public void startGame()
        {
            turnStackProp = new Stack <TurnData>();
            string type = "";

            player1.type = Player.playerType.Human;
            //player1.colour = "Blue";
            //player2.colour = "DarkYellow";
            currentPlayer = 1;
            while (!(type.Equals("s") | type.Equals("m")))
            {
                Console.WriteLine("Press (s) for single player or (m) for multi-player ");
                type = Console.ReadLine();
                if (type == "s")
                {
                    player2.type = Player.playerType.Computer;
                    player2.name = "Roboto-san";
                    Console.WriteLine("Please enter your name:");
                    player1.name = Console.ReadLine();
                }

                else if (type.Equals("m"))
                {
                    player2.type = Player.playerType.Human;
                    Console.WriteLine("Player 1: Please enter your name:");
                    player1.name = Console.ReadLine();
                    Console.WriteLine("Player 2: Please enter your name:");
                    player2.name = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Invalid character. Press s or m.");
                }
            }


            Console.WriteLine("Please choose the size of the board, starting with the length.\n Please enter an integer value:");
            Length = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Now enter the desired height of the board:");
            Height  = Convert.ToInt32(Console.ReadLine());
            newGame = new ConnectFourBoard(Length, Height);
            newGame.display();

            while (newGame.FreeSpaces() == true && newGame.AndTheWinnerIs() == null)
            {
                round();
            }

            //player2 won
            if (newGame.AndTheWinnerIs() == "player2")
            {
                announceWinner();
            }
            return;
        }
コード例 #3
0
        public override Board ReconstructBoard(int moveNumber)
        {
            ConnectFourBoard ConstructedBoard = new ConnectFourBoard(7, 6);
            int moveCount = 0;
            int playerId  = 1;

            foreach (ConnectFourMove move in MoveList.GetRange(0, moveNumber))
            {
                moveCount++;
                playerId = (moveCount % 2 == 1) ? 1 : 2;
                ConstructedBoard.ExecuteMove(move, Utils.GetPlayerById(Connect4Game.Instance.Players, playerId));
            }

            return(ConstructedBoard);
        }
コード例 #4
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);
        }