コード例 #1
0
        public bool Play(bool twoPlayer)
        {
            GameLogEntry logEntry = NewLogEntry();

            DrawBoard();

            int column = -1;

            if (PlayerTurn == 1 || twoPlayer)
            {
                ConsoleKeyInfo key;
                string         keyString;
                Prompt(out key, out keyString);

                if (!int.TryParse(keyString, out column))
                {
                    return(HandleNonColumnInput(key));
                }

                column = column - 1;
            }
            else
            {
                column = strategy2.Run(board, numberToWin, PlayerTurn);
            }

            return(EndTurn(logEntry, column));
        }
コード例 #2
0
        private bool RunEntry(GameLogEntry entry, out bool startNewGame)
        {
            startNewGame = false;
            DrawBoard();
            Console.WriteLine();
            Console.ForegroundColor = PlayerTurn == 1 ? ConsoleColor.Blue : ConsoleColor.Red;
            Console.WriteLine($"Player {PlayerTurn} plays {entry.ColumnPlayed + 1}. (N)ext Move, (S)tart game here, (R)un stratgey?");
            Console.ResetColor();
            var key = Console.ReadKey();

            if (key.Key == ConsoleKey.N)
            {
                return(true);
            }
            else if (key.Key == ConsoleKey.S)
            {
                startNewGame = true;
                return(false);
            }
            else if (key.Key == ConsoleKey.R)
            {
                var strategy = new MinMaxStrategy();
                var column   = strategy.Run(board, numberToWin, PlayerTurn);
                Console.WriteLine();
                Console.WriteLine($"Strategy chose {column}.");
                Console.ReadLine();
                return(false);
            }

            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine();
            Console.WriteLine($"Invalid column. Press any key");
            Console.ReadLine();
            return(false);
        }
コード例 #3
0
        private GameLogEntry NewLogEntry()
        {
            var logEntry = new GameLogEntry();

            logEntry.BoardBeforeMove = (int[, ])board.Matrix.Clone();
            logEntry.PlayerNumber    = PlayerTurn;
            return(logEntry);
        }
コード例 #4
0
        public bool PlayAutomated(bool pauseOnTurn)
        {
            AutoTurns++;
            var random       = new Random();
            var randomChance = random.Next(10);

            GameLogEntry logEntry = NewLogEntry();

            int column = -1;

            var boardSearch = new BoardSearch(numberToWin);
            var legalMoves  = boardSearch.FindAllLegalMoves(board);

            if (legalMoves.Count == 0)
            {
                return(EndTurn(logEntry, -1, pauseOnTurn));
            }

            if (boardSearch.IsBoardEmpty(board))
            {
                //Console.WriteLine("Random move. ");
                column = random.Next(6);
            }
            else
            {
                var strategy = PlayerTurn == 1 ? strategy1 : strategy2;

                var depth = PlayerTurn == 1 ? Player1SearchDepth : GetSearchDepth(AutoTurns);


                column = strategy.Run(board, numberToWin, PlayerTurn, depth);
            }

            //Console.WriteLine();
            //Console.ForegroundColor = PlayerTurn == 1 ? ConsoleColor.Blue : ConsoleColor.Red;
            //Console.WriteLine($"Player {PlayerTurn} plays {column + 1}.");
            //if (pauseOnTurn)
            //{
            //    Console.ReadKey();
            //}

            //Console.ResetColor();

            return(EndTurn(logEntry, column, pauseOnTurn, true));
        }
コード例 #5
0
        private bool EndTurn(GameLogEntry logEntry, int column, bool pause = true, bool drawBoard = true)
        {
            if (column == -1)
            {
                if (drawBoard)
                {
                    DrawBoard();
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.WriteLine($"Draw!");
                }

                log.Winner = 0;
                if (pause)
                {
                    Console.ReadLine();
                }

                return(false);
            }

            if (rules.IsValidMove(column, board))
            {
                board.DropChecker(column, PlayerTurn);

                logEntry.ColumnPlayed = column;
                log.Add(logEntry);

                var winningGroups = rules.PlayerWins(PlayerTurn, board);
                if (winningGroups.Any())
                {
                    if (drawBoard)
                    {
                        DrawBoard();
                        Console.WriteLine();
                        Console.WriteLine();
                        // Console.BackgroundColor = PlayerTurn == 1 ? ConsoleColor.Blue : ConsoleColor.Red;
                        Console.WriteLine($"Player {PlayerTurn} wins!");
                    }
                    log.Winner = PlayerTurn;
                    if (pause)
                    {
                        Console.ReadLine();
                    }

                    return(false);
                }
                else
                {
                    DrawBoard();
                }
            }
            else
            {
                if (drawBoard)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine();
                    Console.WriteLine($"Invalid move. Press any key");
                }

                if (pause)
                {
                    Console.ReadLine();
                }

                return(true);
            }


            PlayerTurn = PlayerTurn == 1 ? 2 : 1;
            return(true);
        }
コード例 #6
0
ファイル: GameLog.cs プロジェクト: ryanbennett/Connect4AI
 public void Add(GameLogEntry entry)
 {
     Log.Add(entry);
 }