Ejemplo n.º 1
0
        public Gameboard Clone()
        {
            Gameboard copy = new Gameboard();

            foreach (Tile t in tiles)
            {
                Tile newTile = new Tile(t.NumOfRows, t.NumOfCols);
                newTile.Points                = t.Points;
                newTile.NumOfRedMarbles       = t.NumOfRedMarbles;
                newTile.NumOfBlueMarbles      = t.NumOfBlueMarbles;
                newTile.LastPlayedOnByPlayer1 = t.LastPlayedOnByPlayer1;
                newTile.LastPlayedOnByPlayer2 = t.LastPlayedOnByPlayer2;

                for (int r = 0; r < t.NumOfRows; r++)
                {
                    for (int c = 0; c < t.NumOfCols; c++)
                    {
                        Hole newHole = new Hole(t.Holes[r, c].Coord.Row, t.Holes[r, c].Coord.Col);
                        newHole.IsFilled     = t.Holes[r, c].IsFilled;
                        newHole.CanBePlayed  = t.Holes[r, c].CanBePlayed;
                        newHole.MarbleInHole = t.Holes[r, c].MarbleInHole;
                        newTile.Holes[r, c]  = newHole;
                    }
                }
                copy.Tiles.Add(newTile);
            }
            return(copy);
        }
Ejemplo n.º 2
0
        public async Task<string> GetMove()
        {
            DateTime beginFunction = DateTime.Now;
            gameboard = game.GetCopyOfGameBoard();
            GameTreeNode testroot = new GameTreeNode(null, gameboard);
            DateTime start = DateTime.Now;
            int levelsTraveled = 0;
            String move = "";

            game.Board = gameboard;

            for (int i = 3; (DateTime.Now - start).TotalSeconds < 5.6; i++)
            {
                testroot = new GameTreeNode(null, gameboard);
                game.Board = gameboard;
                finishedTree = true;
                await expandTree(testroot, i + 1, 1, start, DateTime.Now);
                if (finishedTree)
                {
                    move = chosenMove;
                    levelsTraveled = i;
                }
            }
            game.Board = gameboard;
            testroot = new GameTreeNode(null, gameboard);
            return move;
        }
Ejemplo n.º 3
0
        public async Task <string> GetMove()
        {
            DateTime beginFunction = DateTime.Now;

            gameboard = game.GetCopyOfGameBoard();
            GameTreeNode testroot       = new GameTreeNode(null, gameboard);
            DateTime     start          = DateTime.Now;
            int          levelsTraveled = 0;
            String       move           = "";

            game.Board = gameboard;

            for (int i = 3; (DateTime.Now - start).TotalSeconds < 5.6; i++)
            {
                testroot     = new GameTreeNode(null, gameboard);
                game.Board   = gameboard;
                finishedTree = true;
                await expandTree(testroot, i + 1, 1, start, DateTime.Now);

                if (finishedTree)
                {
                    move           = chosenMove;
                    levelsTraveled = i;
                }
            }
            game.Board = gameboard;
            testroot   = new GameTreeNode(null, gameboard);
            return(move);
        }
Ejemplo n.º 4
0
 public GameTreeNode(GameTreeNode p, Gameboard board)
 {
     parent             = p;
     currentBoardConfig = board;
     children           = new List <GameTreeNode>();
     heuristicValue     = 0;
     Alpha = -100000;
     Beta  = 100000;
 }
Ejemplo n.º 5
0
 public GameTreeNode(GameTreeNode p, Gameboard board)
 {
     parent = p;
     currentBoardConfig = board;
     children = new List<GameTreeNode>();
     heuristicValue = 0;
     Alpha = -100000;
     Beta = 100000;
 }
Ejemplo n.º 6
0
 public Game(GameType gt)
 {
     player1Points = 0;
     player2Points = 0;
     board = new Gameboard();
     gameType = gt;
     if (gameType == GameType.LocalComputer)
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.ComputerOpponent);
     }
     else if (gameType == GameType.LocalMultiplayer)
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.HumanPlayer);
     }
     else
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.HumanOpponent);
     }
     gameTimeStopWatch = new Stopwatch();
     gameTimeStopWatch.Start();
 }
Ejemplo n.º 7
0
 public Game(GameType gt)
 {
     player1Points = 0;
     player2Points = 0;
     board         = new Gameboard();
     gameType      = gt;
     if (gameType == GameType.LocalComputer)
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.ComputerOpponent);
     }
     else if (gameType == GameType.LocalMultiplayer)
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.HumanPlayer);
     }
     else
     {
         player1 = new Player(PlayerType.HumanPlayer);
         player2 = new Player(PlayerType.HumanOpponent);
     }
     gameTimeStopWatch = new Stopwatch();
     gameTimeStopWatch.Start();
 }
Ejemplo n.º 8
0
 public HardAI(Game g)
 {
     gameboard = g.GetCopyOfGameBoard();
     game = g;
 }
Ejemplo n.º 9
0
        public Gameboard Clone()
        {
            Gameboard copy = new Gameboard();
            foreach (Tile t in tiles)
            {
                Tile newTile = new Tile(t.NumOfRows, t.NumOfCols);
                newTile.Points = t.Points;
                newTile.NumOfRedMarbles = t.NumOfRedMarbles;
                newTile.NumOfBlueMarbles = t.NumOfBlueMarbles;
                newTile.LastPlayedOnByPlayer1 = t.LastPlayedOnByPlayer1;
                newTile.LastPlayedOnByPlayer2 = t.LastPlayedOnByPlayer2;

                for (int r = 0; r < t.NumOfRows; r++)
                {
                    for (int c = 0; c < t.NumOfCols; c++)
                    {
                        Hole newHole = new Hole(t.Holes[r, c].Coord.Row, t.Holes[r, c].Coord.Col);
                        newHole.IsFilled = t.Holes[r, c].IsFilled;
                        newHole.CanBePlayed = t.Holes[r, c].CanBePlayed;
                        newHole.MarbleInHole = t.Holes[r, c].MarbleInHole;
                        newTile.Holes[r, c] = newHole;
                    }
                }
                copy.Tiles.Add(newTile);
            }
            return copy;
        }
Ejemplo n.º 10
0
        private async Task expandTree(GameTreeNode node, int level, int currentLevel, DateTime start, DateTime current)
        {
            List <Coordinate> moves = node.CurrentBoardConfig.GetAllAvailableMoves();

            if (node.Parent == null)
            {
                node.Alpha = -10000;
                node.Beta  = 10000;
            }
            else
            {
                node.Alpha = node.Parent.Alpha;
                node.Beta  = node.Parent.Beta;
            }

            if (moves.Count == 0 || level == currentLevel)
            {
                game.Board = node.CurrentBoardConfig.Clone();

                game.getPlayer1Points();
                game.getPlayer2Points();
                node.HeuristicValue = game.Player2Points - game.Player1Points;
                if (currentLevel % 2 == 0)
                {
                    if (node.HeuristicValue > node.Parent.Alpha)
                    {
                        node.Parent.Alpha = node.HeuristicValue;
                        if (currentLevel == 2)
                        {
                            chosenMove = node.Move;
                        }
                    }
                }
                else
                {
                    if (node.HeuristicValue < node.Parent.Beta)
                    {
                        node.Parent.Beta = node.HeuristicValue;
                    }
                }
            }
            else
            {
                foreach (Coordinate c in moves)
                {
                    Gameboard newConfig = node.CurrentBoardConfig.Clone();
                    if (node.Alpha >= node.Beta)
                    {
                        break;
                    }
                    string move;
                    if (currentLevel % 2 == 0)
                    {
                        move = "R" + c.Row + c.Col;
                    }
                    else
                    {
                        move = "B" + c.Row + c.Col;
                    }
                    newConfig.MakeMoveOnBoard(move);
                    GameTreeNode child = new GameTreeNode(node, newConfig);
                    child.Parent         = node;
                    child.Move           = move;
                    child.HeuristicValue = -100000;
                    node.Children.Add(child);
                    if ((current - start).TotalSeconds < 5.6)
                    {
                        await expandTree(child, level, currentLevel + 1, start, DateTime.Now);
                    }
                    else
                    {
                        finishedTree = false;
                        break;
                    }
                }

                if (currentLevel != 1) //something wrong here maybe with logic
                {
                    if ((current - start).TotalSeconds < 5.6)
                    {
                        if (currentLevel % 2 == 0)
                        {
                            if (node.Beta > node.Parent.Alpha)
                            {
                                node.Parent.Alpha = node.Beta;
                                if (currentLevel == 2)
                                {
                                    chosenMove = node.Move;
                                }
                            }
                        }
                        else
                        {
                            if (node.Alpha < node.Parent.Beta)
                            {
                                node.Parent.Beta = node.Alpha;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 11
0
 public HardAI(Game g)
 {
     gameboard = g.GetCopyOfGameBoard();
     game      = g;
 }
Ejemplo n.º 12
0
        public string GetMove()
        {
            gameboard = game.GetCopyOfGameBoard();
            string results  = "B";
            bool   madeMove = false;

            foreach (Tile t in gameboard.Tiles)
            {
                if (t.Points == 6 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                {
                    foreach (Hole h in t.Holes)
                    {
                        if (h.CanBePlayed)
                        {
                            results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                            madeMove = true;
                            break;
                        }
                    }
                }
                if (madeMove)
                {
                    break;
                }
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 4 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                    {
                        break;
                    }
                }
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 3 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                    {
                        break;
                    }
                }
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 2 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                    {
                        break;
                    }
                }
            }

            if (!madeMove)
            {
                int currentRow        = 0;
                int currentCol        = 0;
                int currentPointValue = 0;

                foreach (Tile t in gameboard.Tiles)
                {
                    foreach (Hole h in t.Holes)
                    {
                        if (h.CanBePlayed && (t.Points > currentPointValue))
                        {
                            currentRow        = h.Coord.Row;
                            currentCol        = h.Coord.Col;
                            currentPointValue = t.Points;
                        }
                    }
                }
                results += currentRow.ToString() + currentCol.ToString();
            }

            return(results);
        }
Ejemplo n.º 13
0
        public string GetMove()
        {
            gameboard = game.GetCopyOfGameBoard();
            string results = "B";
            bool madeMove = false;
            foreach (Tile t in gameboard.Tiles) 
            {
                if (t.Points == 6 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                {
                    foreach (Hole h in t.Holes)
                    {
                        if (h.CanBePlayed)
                        {
                            results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                            madeMove = true;
                            break;
                        }
                    }
                }
                if (madeMove)
                    break;
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 4 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                        break;
                }
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 3 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                        break;
                }
            }

            if (!madeMove)
            {
                foreach (Tile t in gameboard.Tiles)
                {
                    if (t.Points == 2 && (t.NumOfRedMarbles > t.NumOfBlueMarbles))
                    {
                        foreach (Hole h in t.Holes)
                        {
                            if (h.CanBePlayed)
                            {
                                results += h.Coord.Row.ToString() + h.Coord.Col.ToString();
                                madeMove = true;
                                break;
                            }
                        }
                    }
                    if (madeMove)
                        break;
                }
            }

            if (!madeMove)
            {
                int currentRow = 0;
                int currentCol = 0;
                int currentPointValue = 0;

                foreach (Tile t in gameboard.Tiles)
                {
                    foreach (Hole h in t.Holes)
                    {
                        if (h.CanBePlayed && (t.Points > currentPointValue) )
                        {
                            currentRow = h.Coord.Row;
                            currentCol = h.Coord.Col;
                            currentPointValue = t.Points;
                        }
                    }
                }
                results += currentRow.ToString() + currentCol.ToString();
            }

            return results;
        }