コード例 #1
0
ファイル: CCGame.cs プロジェクト: diodel-agr/MCTS-Othello
        /* methods. */
        public void Start()
        {
            InitBoard();
            /* init score. */
            board.SetScore(1, 0);
            board.SetScore(2, 0);
            /* init player tiles. */
            Piece p1 = new Piece(4, 3, player1);
            Piece p2 = new Piece(3, 4, player1);
            Piece p3 = new Piece(3, 3, player2);
            Piece p4 = new Piece(4, 4, player2);

            board.AddPiece(p1);
            board.AddPiece(p2);
            board.AddPiece(p3);
            board.AddPiece(p4);
            state         = GameState.waitPlayer;
            currentPlayer = player1;
            /* set bots boards. */
            player1.SetBoard(board);
            player2.SetBoard(board);
            /* launch the thread that will let the 2 bots to play. */
            botThread = new Thread(new ParameterizedThreadStart(BotPlay));
            botThread.Start(cancelToken);
        }
コード例 #2
0
ファイル: CCGame.cs プロジェクト: diodel-agr/MCTS-Othello
 /* constructors. */
 public CCGame(string bot1, string bot2)
 {
     cancelToken = new CancellationTokenSource();
     player1     = PlayerFactory.Create(bot1, Color.black, "simple_selection", "simple_expansion", "random_simulation", "simple_bp");
     player2     = PlayerFactory.Create(bot2, Color.white, "simple_selection", "simple_expansion", "random_simulation", "simple_bp");
     pieceMutex  = new Mutex();
 }
コード例 #3
0
 public HHGame()
 {
     board      = null;
     player1    = player2 = currentPlayer = null;
     optionList = null;
     state      = GameState.stopped;
 }
コード例 #4
0
ファイル: HCGame.cs プロジェクト: diodel-agr/MCTS-Othello
        /// <summary>
        /// Function executed by the bot thread.
        /// </summary>
        private void BotThread()
        {
            /* do bot things. */
            Thread.Sleep(BOT_WAIT_TIMEOUT);
            /* get move from bot. */
            Piece piece = bot.MakeMove();

            if (piece == null)
            {
                return;
            }
            Console.WriteLine("Bot chose piece: " + piece.X + ": " + piece.Y);
            board.AddPiece(piece);
            List <Piece> neigh = board.GetPieceNeighbors(piece);

            foreach (Piece n in neigh)
            {
                board.AddPiece(piece, n);
            }
            //board.PrintBoard();
            bot.SetBoard(board);
            // find a way to get rid of this ugly call to garbage collector.
            GC.Collect();
            /* set next player. */
            currentPlayer = player1;
            /* set game state. */
            state = GameState.waitPlayer;
            /* update ui. */
            T x = default(T);

            Refresh(x);
        }
コード例 #5
0
ファイル: HCGame.cs プロジェクト: diodel-agr/MCTS-Othello
 /* constructors. */
 public HCGame()
 {
     board       = null;
     player1     = bot = currentPlayer = null;
     clickedTile = null;
     optionList  = null;
     state       = GameState.stopped;
     observers   = new List <IObserver <T> >();
 }
コード例 #6
0
        public bool PlayerClicked(int x, int y)
        {
            /* see if the tile corresponds to the current player. */
            Piece p = board.pieces[x, y];

            if (p != null)
            {
                if (p.owner == currentPlayer && (state == GameState.waitPlayer || state == GameState.tileClicked))
                {
                    /* construct and show possible moves. */
                    clickedTile = p;
                    optionList  = board.GetValidMoves(p);
                    state       = GameState.tileClicked;
                }
            }
            else if (state == GameState.tileClicked)
            {
                /* check if tile is in the option list. */
                bool isInList = false;
                foreach (Piece opt in optionList)
                {
                    if (opt.X == x && opt.Y == y)
                    {
                        isInList = true;
                    }
                }
                if (isInList == true)
                {
                    /* add tile to the board. */
                    Piece newPiece = new ui.Piece(x, y, currentPlayer);
                    board.AddPiece(newPiece);
                    List <Piece> neigh = board.GetPieceNeighbors(newPiece);
                    foreach (Piece n in neigh)
                    {
                        board.AddPiece(newPiece, n);
                    }
                    optionList = null;
                    /* set next player. */
                    if (currentPlayer == player1)
                    {
                        currentPlayer = player2;
                    }
                    else
                    {
                        currentPlayer = player1;
                    }
                    /* set game state. */
                    state = GameState.waitPlayer;
                }
            }
            // WARNING! Check this function and decide which return parameter it should have.
            return(true);
        }
コード例 #7
0
        public static ISimulation Create(string simulationType, IMCTSPlayer player,
                                         IMCTSPlayer opponent, Board board, string expansionType, string backPropagationType)
        {
            switch (simulationType)
            {
            case "random_simulation":
                return(new RandomSimulation(player, opponent, board, expansionType, backPropagationType));

            default:
                throw new MCTSException("[SimulationFactory::Create] Unknown simulation type '" + simulationType + "'");
            }
        }
コード例 #8
0
ファイル: CCGame.cs プロジェクト: diodel-agr/MCTS-Othello
        public IMCTSPlayer GetWinner()
        {
            IMCTSPlayer winner = null;

            if (board.GetScore(1) > board.GetScore(2))
            {
                winner = player1;
            }
            else
            {
                winner = player2;
            }
            return(winner);
        }
コード例 #9
0
 /* constructors. */
 public RandomSimulation(IMCTSPlayer thisPlayer, IMCTSPlayer opponent, Board b, string expansion, string backPropagation)
 {
     board           = new Board(b);
     this.thisPlayer = thisPlayer;
     this.opponent   = opponent;
     wins            = visits = 0;
     exp             = ExpansionFactory.Create(expansion);
     bp           = BackPropagationFactory.Create(backPropagation);
     rand         = new Random();
     workers      = new List <Thread>(threadNo);
     mutex        = new Mutex();
     isSimulating = false;
     root         = null;
     treeRoot     = null;
 }
コード例 #10
0
ファイル: Board.cs プロジェクト: diodel-agr/MCTS-Othello
 /**
  * IncPlayerScore - increments player score.
  *
  * @p: the player whoose score needs to be incremented.
  */
 private void IncPlayerScore(IMCTSPlayer p)
 {
     if (p.GetColor() == player.Color.black)
     {   /* player 1. */
         playerOneScore++;
     }
     else
     {   /* player 2. */
         playerTwoScore++;
     }
     if (playerOneScore + playerTwoScore > 64)
     {
         throw new MCTSException("[Board/IncPlayerScore()] - players scores are invalid ( > 64).");
     }
 }
コード例 #11
0
ファイル: HCGame.cs プロジェクト: diodel-agr/MCTS-Othello
        public bool PlayerClicked(int x, int y)
        {
            /* see if the tile corresponds to the current player. */
            Piece p = board.pieces[x, y];

            if (p != null)
            {
                if (p.owner == player1 && (state == GameState.waitPlayer || state == GameState.tileClicked))
                {
                    /* construct and show possible moves. */
                    clickedTile = p;
                    optionList  = board.GetValidMoves(p);
                    state       = GameState.tileClicked;
                }
            }
            else if (state == GameState.tileClicked)
            {
                /* check if tile is in the option list. */
                bool isInList = false;
                foreach (Piece opt in optionList)
                {
                    if (opt.X == x && opt.Y == y)
                    {
                        isInList = true;
                        break;
                    }
                }
                if (isInList == true)
                {
                    /* add tile to the board. */
                    Piece newPiece = new Piece(x, y, player1);
                    board.AddPiece(newPiece);
                    foreach (Piece n in board.GetPieceNeighbors(newPiece))
                    {
                        board.AddPiece(newPiece, n);
                    }
                    optionList = null;
                    //board.PrintBoard();
                    /* let bot play. */
                    currentPlayer = bot;
                    bot.SetBoard(board);
                    // tell the form to register to the observable and call the delegate.
                    return(true);
                }
            }
            return(false);
        }
コード例 #12
0
ファイル: CCGame.cs プロジェクト: diodel-agr/MCTS-Othello
        private void BotPlay(object token)
        {
            Console.WriteLine("[CCGame] worker thread started!");
            CancellationTokenSource cancelToken = (CancellationTokenSource)token;

            lastMove = null;
            while (board.IsFinished(currentPlayer) == false && state != GameState.stopped &&
                   cancelToken.IsCancellationRequested != true)
            {
                /* let the current player play. */
                currentPlayer.SetBoard(board);
                Thread.Sleep(1000);
                /* obtain move. */
                Piece move = currentPlayer.MakeMove();
                /* print new move. */
                //Console.WriteLine("Player:" + currentPlayer.GetColor().ToString() + " moved " + move.X + ":" + move.Y);
                /* modify board. */
                pieceMutex.WaitOne();
                board.AddPiece(move);
                List <Piece> neigh = board.GetPieceNeighbors(move);
                foreach (Piece n in neigh)
                {
                    board.AddPiece(move, n);
                }
                lastMove = move;
                /* release board mutex. */
                pieceMutex.ReleaseMutex();
                /**/
                if (currentPlayer.GetType() == typeof(MCTSPlayer))
                {
                    currentPlayer.SetBoard(board);
                }
                /* set next player. */
                if (currentPlayer == player1)
                {
                    currentPlayer = player2;
                }
                else
                {
                    currentPlayer = player1;
                }
                // Hopefully, one day, you will get rid of this call.
                GC.Collect();
            }
            state = GameState.stopped;
            Console.WriteLine("[CCGame] Game thread exit!");
        }
コード例 #13
0
ファイル: Board.cs プロジェクト: diodel-agr/MCTS-Othello
        /**
         * PlayerHasPossibleMoves - returns true if the current player has possible moves
         * and false otherwise.
         *
         * @player: the current player.
         * @return: true / false.
         */
        public bool PlayerHasPossibleMoves(IMCTSPlayer player)
        {
            bool result = false;

            foreach (Piece p in pieces)
            {
                if (p != null && p.owner == player)
                {
                    if (GetValidMoves(p).Count > 0)
                    {
                        result = true;
                        break;
                    }
                }
            }
            return(result);
        }
コード例 #14
0
ファイル: Board.cs プロジェクト: diodel-agr/MCTS-Othello
        /**
         * IsFinished - returns true if the game is finished and false otherwise.
         *
         * @return: true / false.
         */
        public bool IsFinished(IMCTSPlayer player)
        {
            bool result = false;

            if (playerOneScore + playerTwoScore > 64)
            {
                throw new MCTSException("[Board/IsFinished()] - players score exceed maximum value.");
            }
            else if (playerOneScore + playerTwoScore == 64)
            {
                result = true;
            }
            else if (PlayerHasPossibleMoves(player) == false)
            {
                result = true;
            }
            return(result);
        }
コード例 #15
0
        /* methods implemetation. */
        public void Start()
        {
            InitBoard();
            /* init score. */
            board.SetScore(1, 0);
            board.SetScore(2, 0);
            /* init player tiles. */
            Piece p1 = new Piece(4, 3, player1);
            Piece p2 = new Piece(3, 4, player1);
            Piece p3 = new Piece(3, 3, player2);
            Piece p4 = new Piece(4, 4, player2);

            board.AddPiece(p1);
            board.AddPiece(p2);
            board.AddPiece(p3);
            board.AddPiece(p4);
            state         = GameState.waitPlayer;
            currentPlayer = player1;
        }
コード例 #16
0
ファイル: Board.cs プロジェクト: diodel-agr/MCTS-Othello
 /**
  * DecPlayerScore - decrements player score.
  *
  * @p: current player.
  */
 private void DecPlayerScore(IMCTSPlayer p)
 {
     if (p.GetColor() == player.Color.black)
     {
         playerOneScore--;
         if (playerOneScore < 0)
         {
             throw new MCTSException("[Board/DecPlayerScore()] - player one score is negative!");
         }
     }
     else
     {
         playerTwoScore--;
         if (playerTwoScore < 0)
         {
             throw new MCTSException("[Board/DecPlayerScore()] - player two score is negative!");
         }
     }
 }
コード例 #17
0
        private IMCTSPlayer GetOwner(Board b, Node n)
        {
            IMCTSPlayer result = null;

            if (n.X == -1)
            {
                result = opponent;
                if (thisPlayer.GetColor() == Color.black)
                {
                    result = thisPlayer;
                }
            }
            else if (b.pieces[n.X, n.Y].owner == thisPlayer)
            {
                result = opponent;
            }
            else
            {
                result = thisPlayer;
            }
            return(result);
        }
コード例 #18
0
 public void InitBoard()
 {
     player1 = PlayerFactory.Create("Human", Color.black, null, null, null, null);
     player2 = PlayerFactory.Create("Human", Color.white, null, null, null, null);
     board   = new Board(8);
 }
コード例 #19
0
ファイル: HCGame.cs プロジェクト: diodel-agr/MCTS-Othello
 public HCGame(string comp) : this()
 {
     player1 = PlayerFactory.Create("Human", Color.black, null, null, null, null);
     bot     = PlayerFactory.Create(comp, Color.white, "simple_selection", "simple_expansion", "random_simulation", "simple_bp");
 }
コード例 #20
0
 public Piece(Piece p)
 {
     X     = p.X;
     Y     = p.Y;
     owner = p.owner;
 }
コード例 #21
0
 public Piece(int _x, int _y, IMCTSPlayer _owner)
 {
     X     = _x;
     Y     = _y;
     owner = _owner;
 }
コード例 #22
0
 /* constructors. */
 public Piece()
 {
     X     = 0;
     Y     = 0;
     owner = null;
 }