示例#1
0
        public static List<Node> GenerateSuccessors(Board board, bool isBlack)
        {
            //Validate moves based on current state to generate new states

            var nodes = new List<Node>();


            //Generate # of valid moves
            var ValidMoves = GetNumberOfMoves(board);
            //Foreach valid move, generate a new node (evaluated in minimax)
            for (var i = 0; i < ValidMoves.Length; i++)
            {
                if (ValidMoves[i])
                {
                    var tempBoard = new Board(board.State);
                    //Make the move
                    tempBoard.AddToBoard(i, isBlack);
                    //Create a Node
                    var node = new Node();
                    node.Board = tempBoard;
                    nodes.Add(node);
                }
            }

            return nodes;
        }
示例#2
0
        //This method will search all the columns and return the best move for the AI to make.
        //Will run AlphaBeta or regular Minimax depending on user selection.
        public static int AiHelperMove(Board board, bool alphabeta, int depth)
        {
            var rows = board.State.GetLength(1) - 1;
            int bestValue = Int32.MinValue;
            int bestColumn = -1;

           
            if (alphabeta)
            {
                MiniMaxAb miniMaxAb = new MiniMaxAb();
                for (var i = 0; i <= rows; i++)
                {
                    var node = new Node();
                    var tempBoard = new Board(board.State);
                    if (tempBoard.ValidateMove(i))
                    {
                        tempBoard.AddToBoard(i, true);
                        node.Board = tempBoard;
                        var currentValue = miniMaxAb.Search(node, depth, Int32.MinValue, Int32.MaxValue, false);
                        if (currentValue >= bestValue)
                        {
                            bestValue = currentValue;
                            bestColumn = i;
                        }
                    }
                }
            }
            else
            {
                MiniMax miniMax = new MiniMax();
                for (var i = 0; i <= rows; i++)
                {
                    var node = new Node();
                    var tempBoard = new Board(board.State);
                    if (tempBoard.ValidateMove(i))
                    {
                        tempBoard.AddToBoard(i, true);
                        node.Board = tempBoard;
                        var currentValue = miniMax.Search(node, depth, false);
                        if (currentValue >= bestValue)
                        {
                            bestValue = currentValue;
                            bestColumn = i;
                        }
                    }
                }
            }

            
            return bestColumn;
        }
示例#3
0
        public static bool[] GetNumberOfMoves(Board board)
        {// Check if each column is full
            var col = board.State.GetLength(1);
            var validColumnsToMoveTo = new bool[col];
            //Initialize boolean array, each cell represents a column 
            for (var i = 0; i < validColumnsToMoveTo.Length; i++)
            {
                validColumnsToMoveTo[i] = false;
            }

            for (var i = 0; i < col; i++)
            {
                if (board.State[0, i] == null)
                {
                    validColumnsToMoveTo[i] = true;
                }
            }


            return validColumnsToMoveTo;
        }
示例#4
0
        //Returns all adjacent pieces to a given piece, hardcoded the search, could have been better.
        public static List<Tuple<bool, Piece>> GetAdjacentPieces(Piece piece, Board board)
        {
            var row = board.State.GetLength(0) - 1;
            var col = board.State.GetLength(1) - 1;
            var listOfAdjacentPieces = new List<Tuple<bool, Piece>>();
            if (piece.Position.Row > 0) //Top Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row - 1, piece.Position.Col]));
            }
            if (piece.Position.Col < col && piece.Position.Row > 0) //TopRight Boundary Test
            {
                var adjPiece = board.State[piece.Position.Row - 1, piece.Position.Col + 1];
                listOfAdjacentPieces.Add(Tuple.Create(true, adjPiece));
            }
            if (piece.Position.Col < col) //Right Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row, piece.Position.Col + 1]));
            }
            if (piece.Position.Row < row && piece.Position.Col < col) //BottomRight Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row + 1, piece.Position.Col + 1]));
            }
            if (piece.Position.Row < row)//Bottom Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row + 1, piece.Position.Col]));
            }
            if (piece.Position.Row < row && piece.Position.Col > 0) //BottomLeft Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row + 1, piece.Position.Col - 1]));
            }
            if (piece.Position.Col > 0) //Left Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row, piece.Position.Col - 1]));
            }
            if (piece.Position.Row > 0 && piece.Position.Col > 0)//TopLeft Boundary Test
            {
                listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row - 1, piece.Position.Col - 1]));
            }

            return listOfAdjacentPieces;
        }
示例#5
0
        static void Main(string[] args)
        {
            //Following is setup for AI and board
            Console.WriteLine("Welcome to Simacogo! You are Player 1, Color white.");
            Console.WriteLine("At any time, type 'exit' to leave the game!");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("\n");
            Console.WriteLine("Enter # of plies for AI to search to? (numeric only)");
            var depth = Int32.Parse(Console.ReadLine()); //AI lookahead value

            Console.WriteLine("\n");
            Console.WriteLine("Enable Alpha Beta Pruning? (True or False)");
            var alphabeta = Boolean.Parse(Console.ReadLine()); //AI lookahead value

            Console.WriteLine("\n");
            Console.WriteLine("Enter board width? (numeric only)");
            var width = Int32.Parse(Console.ReadLine()); //Board width

            Console.WriteLine("Enter board height? (numeric only)");
            var height = Int32.Parse(Console.ReadLine()); //Board height

            Console.WriteLine("\n");
            Board board = new Board(width, height);
            bool humanTurn = true;
            bool runProgram = true;
            while (!board.IsFull() && runProgram)
            {
                if (humanTurn)
                {
                    Console.WriteLine("--------------------Begin Human Turn-----------------------------------");
                    Console.WriteLine("Please type the column you want to drop a piece into and press enter. (1-9)");
                    string enteredValue = Console.ReadLine();
                    if (enteredValue.Equals("exit"))
                    {
                        runProgram = false;
                        break;
                    }
                    int colValue = Int32.MinValue;
                    Int32.TryParse(enteredValue, out colValue);
                    if (colValue > 0 && colValue < 10 && board.ValidateMove(colValue-1))
                    {
                        //Get column from User && Create new piece and enter it in the board at deepest possible position in column.
                        board.AddToBoard(colValue-1, false);
                        board.PrintState();

                        var node = new Node();
                        node.Board = board;
                        var score = Utility.FinalScore(node);
                        Console.WriteLine("\n");
                        Console.WriteLine("Current Score: \n");
                        Console.WriteLine("Human: " + score.Item1);
                        Console.WriteLine("AI " + score.Item2);
                        Console.WriteLine("--------------------End Human Turn----------------------");
                        Console.WriteLine("\n");
                        humanTurn = !humanTurn;
                    }
                    else
                    {
                        Console.WriteLine("Invalid selection, please try again.");
                    }

                    
                }
                else
                {
                    Console.WriteLine("--------------------Begin AI Turn--------------------");
                    //Let AI player pick position
                    //Update Board
                    //AI Runs either Miniax or MinimaxAB depending on user selection.
                    var bestMove = Utility.AiHelperMove(board, alphabeta, depth);

                    board.AddToBoard(bestMove, true);
                    board.PrintState();
                    var node = new Node();
                    node.Board = board;
                    var score = Utility.FinalScore(node);
                    Console.WriteLine("\n");
                    Console.WriteLine("Current Score: \n");
                    Console.WriteLine("Human: " + score.Item1);
                    Console.WriteLine("AI " + score.Item2);
                    Console.WriteLine("\n");
                    Console.WriteLine("--------------------End AI Turn----------------------");
                    humanTurn = !humanTurn;
                }
            }

            Console.WriteLine("\n");
            Console.WriteLine("Gameboard is Full!");
            Console.WriteLine("\n");
            var finalNode = new Node();
            var tempBoard = new Board(board.State);
            finalNode.Board = tempBoard;
            var finalScore = Utility.FinalScore(finalNode);
            Console.WriteLine("Final Board");
            Console.WriteLine("\n");
            board.PrintState();
            Console.WriteLine("\n");
            Console.WriteLine("Final Score Is: ");
            Console.WriteLine("Human: " + finalScore.Item1);
            Console.WriteLine("AI: " + finalScore.Item2);

            Console.WriteLine("Thank you for playing Simacogo! Application will close in 5 seconds.");

            
            System.Threading.Thread.Sleep(5000);



        }