コード例 #1
0
        public static double BestMoveDepthLimited(Connect6State state, int depth)
        {
            if (state.IsFinal())
            {
                if (state.IsSix(state.currentPlayer))
                {
                    return(double.PositiveInfinity);
                }
                else
                {
                    return(state.IsTied() ? 0 : double.NegativeInfinity);
                }
            }
            if (depth == 0)
            {
                return(Evaluation(state));
            }

            double bestscore = double.NegativeInfinity;

            foreach (Connect6Move move in state.AllPossibleMoves())
            {
                double score = -BestMoveDepthLimited(state.Apply(move), depth - 1);
                if (score > bestscore)
                {
                    bestscore = score;
                }
            }
            return(bestscore);
        }
コード例 #2
0
        public static Tuple <Connect6Move, double> GetBestMove(Connect6State state)
        {
            if (state.IsFinal())
            {
                if (state.IsSix(state.currentPlayer))
                {
                    return(new Tuple <Connect6Move, double>(null, double.PositiveInfinity));
                }
                if (state.IsTied())
                {
                    return(new Tuple <Connect6Move, double>(null, 0));
                }
                else
                {
                    return(new Tuple <Connect6Move, double>(null, double.NegativeInfinity));
                }
            }

            double       bestscore = double.NegativeInfinity;
            Connect6Move bestmove  = new Connect6Move(null, null);

            foreach (Connect6Move move in state.AllPossibleMoves())
            {
                double score = -GetBestMove(state.Apply(move)).Item2;
                if (score > bestscore)
                {
                    bestscore = score;
                    bestmove  = move;
                }
            }
            return(new Tuple <Connect6Move, double>(bestmove, bestscore));
        }
コード例 #3
0
        public static double BestMove(Connect6State state)
        {
            if (state.IsFinal())
            {
                if (state.IsSix(state.currentPlayer))
                {
                    return(double.PositiveInfinity);
                }
                else
                {
                    return(state.IsTied() ? 0 : double.NegativeInfinity);
                }
            }

            double bestscore = double.NegativeInfinity;

            foreach (Connect6Move move in state.AllPossibleMoves())
            {
                double score = -BestMove(state.Apply(move));

                if (score > bestscore)
                {
                    bestscore = score;
                }
            }
            return(bestscore);
        }
コード例 #4
0
        public static void PlayGame()
        {
            Player[,] board = new Player[, ] {
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty }
            };

            Connect6State state = new Connect6State(Player.White, board);

            Console.WriteLine("The Computer is White, You Are Black");
            while (!state.IsFinal())
            {
                Console.WriteLine("Current Player: " + state.currentPlayer);
                PrintBoard(board);
                if (state.currentPlayer == Player.White)
                {
                    Console.WriteLine("Calculating Move...");
                    Connect6Move move = GetBestMoveDepthLimited(state, 2).Item1;
                    state = state.Apply(move);
                }
                else
                {
                    Console.WriteLine("Choose A Row And A Column for position 1:");
                    int           row  = int.Parse(Console.ReadLine());
                    int           col  = int.Parse(Console.ReadLine());
                    BoardPosition pos1 = new BoardPosition(row, col);
                    Console.WriteLine("Choose A Row And A Column for position 2:");
                    int           row2 = int.Parse(Console.ReadLine());
                    int           col2 = int.Parse(Console.ReadLine());
                    BoardPosition pos2 = new BoardPosition(row2, col2);
                    Connect6Move  move = new Connect6Move(pos1, pos2);
                    state = state.Apply(move);
                }

                board = state.board;
            }

            PrintBoard(board);

            if (state.IsTied())
            {
                Console.WriteLine("Tied!");
            }
            else
            {
                Console.WriteLine("{0} Won!", state.currentPlayer.Next());
            }
        }
コード例 #5
0
        ///////////Need to check

        public Connect6State Apply(Connect6Move move)
        {
            Player[,] newboard = CloneBoard();

            if (IsValid(move.Pos1) && IsValid(move.Pos2))
            {
                newboard[move.Pos1.Row, move.Pos1.Column] = currentPlayer;
                newboard[move.Pos2.Row, move.Pos2.Column] = currentPlayer;
            }

            Connect6State newstate = new Connect6State(currentPlayer.Next(), newboard);

            return(newstate);
        }
コード例 #6
0
        public static void PlayGameComputer()
        {
            Player[,] board = new Player[, ] {
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty }
            };

            Connect6State state = new Connect6State(Player.White, board);

            while (!state.IsFinal())
            {
                Console.WriteLine("Current Player: " + state.currentPlayer);
                Console.WriteLine();
                PrintBoard(board);
                if (state.currentPlayer == Player.White)
                {
                    Console.WriteLine("Calculating Move...");
                    Console.WriteLine();
                    Connect6Move move = GetBestMoveDepthLimited(state, 2).Item1;
                    state = state.Apply(move);
                }
                else
                {
                    Console.WriteLine("Calculating Move...");
                    Console.WriteLine();
                    Connect6Move move = GetBestMoveDepthLimited(state, 2).Item1;
                    state = state.Apply(move);
                }

                board = state.board;
            }

            PrintBoard(board);
            if (state.IsTied())
            {
                Console.WriteLine("Tied!");
            }
            else
            {
                Console.WriteLine("{0} Won!", state.currentPlayer.Next());
            }
        }
コード例 #7
0
        private static double Evaluation(Connect6State state)
        {
            Connect6State secondstate = new Connect6State(state.currentPlayer.Next(), state.board);

            return(state.TotalScore() - secondstate.TotalScore());
        }