Example #1
0
def randomize_board():
    global board
    board.clear()
    board = Board(0, window_width, window_height, board_style=None)
    if pause:
        display_pause_message()
    board._is_user_interaction_enabled = pause
Example #2
0
 def new_game(self):
     """Initializes a new game."""
     self.last_tick = None
     self.board = Board()
     self.board_drawer = BoardDrawer()
     self.board_drawer.clear_score()
     self.board.start_game()
Example #3
0
class Sudoku:
    def __init__(self):
        self.game = Board()

    def init_game(self):
        self.game.draw_board()
        self.game.load_puzzle()
        self.game.window.mainloop()
Example #4
0
def test_flip():
    gc = GameController(40, 40)
    new_board = Board(40, 40, 10, gc)
    new_board.table = [[1, 2, "B", 4], [1, "W", "W", 4], [1, "B", "B", 4],
                       [1, 2, 3, 4]]
    new_board.flip(0, 3, "B")
    assert new_board.table == [[1, 2, "B", 4], [1, "W", "B", 4],
                               [1, "B", "B", 4], [1, 2, 3, 4]]
Example #5
0
 def __init__(self, channel: discord.channel, user: discord.User):
     self.__channel = channel
     self.__owner = user
     self.__state = None
     self.__board = Board()
     self.__players = Players()
     self.__currentPower = None
     self.__dangerZone = False
Example #6
0
	def initPlayers(self):
		self.width = 9
		self.height = 9
		self.board = Board(width=self.width, height=self.height, n_in_row=5)
		self.mcts_player = MCTSPlayer(c_puct=5, n_playout=1000)
		self.human_player = HumanPlayer()

		self.start_player = 0	# 0 - human, 1 - mcts_player

		self.board.init_board(self.start_player)
		p1, p2 = self.board.players
		self.human_player.set_player_id(p1)
		self.mcts_player.set_player_id(p2)
		self.players = {p2: self.mcts_player, p1: self.human_player}
		self.board.show(self.human_player.playerId, self.mcts_player.playerId)
Example #7
0
 def new_game(self):
     """Initializes a new game."""
     self.last_tick = None
     self.board = Board()
     self.board_drawer = BoardDrawer()
     self.board_drawer.clear_score()
     self.board.start_game()
Example #8
0
    def __init__(self, init_model=None, transfer_model=None):
        self.resnet_block = 19  # num of block structures in resnet
        # params of the board and the game
        self.board_width = 11
        self.board_height = 11
        self.n_in_row = 5
        self.board = Board(width=self.board_width,
                           height=self.board_height,
                           n_in_row=self.n_in_row)
        self.game = Game(self.board)
        # training params
        self.learn_rate = 1e-3
        self.n_playout = 400  # num of simulations for each move
        self.c_puct = 5
        self.buffer_size = 500000  # memory size
        self.batch_size = 512  # mini-batch size for training
        self.data_buffer = deque(maxlen=self.buffer_size)
        self.play_batch_size = 1  # play n games for each network training
        self.check_freq = 50
        self.game_batch_num = 50000000  # total game to train
        self.best_win_ratio = 0.0
        # num of simulations used for the pure mcts, which is used as
        # the opponent to evaluate the trained policy
        self.pure_mcts_playout_num = 200
        if (init_model is not None) and os.path.exists(init_model + ".index"):
            # start training from an initial policy-value net
            self.policy_value_net = PolicyValueNet(
                self.board_width,
                self.board_height,
                block=self.resnet_block,
                init_model=init_model,
                cuda=True,
            )
        elif (transfer_model
              is not None) and os.path.exists(transfer_model + ".index"):
            # start training from a pre-trained policy-value net
            self.policy_value_net = PolicyValueNet(
                self.board_width,
                self.board_height,
                block=self.resnet_block,
                transfer_model=transfer_model,
                cuda=True,
            )
        else:
            # start training from a new policy-value net
            self.policy_value_net = PolicyValueNet(self.board_width,
                                                   self.board_height,
                                                   block=self.resnet_block,
                                                   cuda=True)

        self.mcts_player = MCTSPlayer(
            policy_value_function=self.policy_value_net.policy_value_fn_random,
            action_fc=self.policy_value_net.action_fc_test,
            evaluation_fc=self.policy_value_net.evaluation_fc2_test,
            c_puct=self.c_puct,
            n_playout=self.n_playout,
            is_selfplay=True,
        )
Example #9
0
def run(start_player=0, is_shown=1):
    # run a gomoku game with AI
    # you can set
    # human vs AI or AI vs AI
    n = 5
    width, height = 15, 15
    # model_file = 'model_15_15_5/best_policy.model'
    # width, height = 6, 6
    # model_file = 'model/best_policy.model'
    # width, height = 11, 11
    # model_file = 'model/best_policy.model'
    model_file = 'training/model_best/policy.model'
    # model_file = 'training/best_policy.model'
    p = os.getcwd()
    model_file = path.join(p, model_file)

    board = Board(width=width, height=height, n_in_row=n)
    game = Game(board)

    mcts_player = MCTS_pure(5, 8000)

    best_policy = PolicyValueNet(board_width=width,
                                 board_height=height,
                                 block=19,
                                 init_model=model_file,
                                 cuda=True)

    # alpha_zero vs alpha_zero

    # best_policy.save_numpy(best_policy.network_all_params)
    # best_policy.load_numpy(best_policy.network_oppo_all_params)
    alpha_zero_player = MCTSPlayer(
        best_policy,
        model_file,
        policy_value_function=best_policy.policy_value_fn_random,
        action_fc=best_policy.action_fc_test,
        evaluation_fc=best_policy.evaluation_fc2_test,
        c_puct=5,
        n_playout=400,
        is_selfplay=False)

    # alpha_zero_player_oppo = MCTSPlayer(policy_value_function=best_policy.policy_value_fn_random,
    #                                     action_fc=best_policy.action_fc_test_oppo,
    #                                     evaluation_fc=best_policy.evaluation_fc2_test_oppo,
    #                                     c_puct=5,
    #                                     n_playout=400,
    #                                     is_selfplay=False)

    # human player, input your move in the format: 2,3
    # set start_player=0 for human first
    # play in termianl without GUI

    # human = Human()
    # win = game.start_play(human, alpha_zero_player, start_player=start_player, is_shown=is_shown,print_prob=True)
    # return win

    # play in GUI
    game.start_play_with_UI(alpha_zero_player)  # Play with alpha zero
Example #10
0
def test_tile_numbers():
    gc = GameController(400, 400)
    new_board = Board(400, 400, 100, gc)
    new_board.begin()
    new_board.table[3][2] = "B"
    new_board.tile_numbers()
    assert new_board.black == 3
    assert new_board.white == 2
Example #11
0
def test_constructor():
    gc = GameController(400, 400)
    new_board = Board(400, 400, 100, gc)
    assert new_board.WIDTH == 400
    assert new_board.HEIGHT == 400
    assert new_board.GRID == 100
    assert new_board.whiteTurn is False
    assert new_board.available == [1, 2, 3, 4]
    assert new_board.table == [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4],
                               [1, 2, 3, 4]]
Example #12
0
def test_begin():
    gc = GameController(800, 800)
    new_board = Board(800, 800, 100, gc)
    new_board.begin()
    assert new_board.table[3][3] == "W"
    assert new_board.table[4][4] == "W"
    gc_mini = GameController(200, 200)
    mini_board = Board(200, 200, 100, gc_mini)
    mini_board.begin()
    assert mini_board.table[0][0] == "W"
    assert mini_board.table[1][1] == "W"
    assert mini_board.table[0][1] == "B"
    assert mini_board.table[1][0] == "B"
Example #13
0
def test_playerMove_pcMove():
    gc = GameController(40, 40)
    new_board = Board(40, 40, 10, gc)
    new_board.begin()
    new_board.playerMove(22, 35)
    assert new_board.table == [[1, 2, 3, 4], [1, "W", "B", 4],
                               [1, "B", "B", 4], [1, 2, "B", 4]]
    new_board.pcMove()
    assert new_board.table == [[1, 2, 3, 4],
                               [1, "W", "W", "W"],
                               [1, "B", "B", 4],
                               [1, 2, "B", 4]] or\
                              [[1, 2, 3, 4],
                               [1, "W", "B", 4],
                               [1, "W", "B", 4],
                               [1, "W", "B", 4]] or\
                              [[1, 2, 3, 4],
                               [1, "W", "B", 4],
                               [1, "B", "W", 4],
                               [1, 2, "B", "W"]]
def connect_four(file):
    with open(file, 'r') as f:
        moves = [int(line.strip()) for line in f.readlines()]
    b = Board(6, 7)
    for move in moves:
        if not moves.index(move) % 2:
            b.drop_token(move, 'Y')
            print(b)
            print('')
        else:
            b.drop_token(move, 'R')
            print(b)
            print('')
    return b
Example #15
0
from game_board import Board
from functions import *
import pygame as pg
import sys
from time import *

pg.init()

#constants
DISPLAY_SIZE = 800
BOARD_SIZE = 8

#initisalise the game board
board = Board(DISPLAY_SIZE, BOARD_SIZE)
board.initial_display()
board.display()

playersTurn = True  #human player goes first
gameFinished = False

while not gameFinished:

    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
            pg.quit()
            sys.exit()

    if playersTurn:

        # PLAYER'S TURN #
Example #16
0
 def __init__(self):
     self.game = Board()
Example #17
0
class Game(object):
    """Manages the game and updating the screen."""

    def __init__(self, player=None):
        self.last_tick = None
        self.board = None
        self.board_drawer = None
        self.player = player or Human()

    def new_game(self):
        """Initializes a new game."""
        self.last_tick = None
        self.board = Board()
        self.board_drawer = BoardDrawer()
        self.board_drawer.clear_score()
        self.board.start_game()

    def pause_game(self):
        """Pauses or unpauses the game."""
        if self.last_tick:
            self.stop_ticking()
        else:
            self.start_ticking()

    def run_game(self):
        self.start_ticking()
        self._tick()
        while True:
            try:
                if isinstance(self.player, Human):
                    self.process_user_input()
                elif isinstance(self.player, AI):
                    self.process_ai_input()
                self.update()
            except GameOverError:
                self.end_game()
                return self.board.score

    def save_game(self):
        """Writes the state of the game to a file."""
        pass

    def load_game(self):
        """Loads a game from a file."""
        pass

    def end_game(self):
        """Ends the current game."""
        pass

    def exit(self):
        """Exits the program."""
        self.end_game()
        self.board_drawer.return_screen_to_normal()
        print('Game Over! Final Score: {}'.format(int(self.board.score)))
        sys.exit(0)

    def start_ticking(self):
        self.last_tick = datetime.datetime.now()

    def stop_ticking(self):
        self.last_tick = None

    def update(self):
        current_time = datetime.datetime.now()
        tick_multiplier = math.pow(0.9, self.board.level-1)
        tick_threshold = datetime.timedelta(milliseconds=LEVEL_ONE_TICK_LENGTHMS*tick_multiplier)
        if self.last_tick and current_time - self.last_tick > tick_threshold:
            self.last_tick = current_time
            self._tick()

    def _tick(self):
        self.board.let_shape_fall()
        self.board_drawer.update(self.board)

    def process_ai_input(self):
        move = self.player.get_moves(self.board, self.board_drawer)
        if move:
            self.board.falling_shape.orientation = move.orientation
            self.board.falling_shape.move_to(move.column_position, move.row_position)
            self.board_drawer.update(self.board)
        else:
            self.end_game()

    def process_user_input(self):
        user_input = self.board_drawer.stdscr.getch()
        moves = {
            curses.KEY_RIGHT: self.board.move_shape_right,
            curses.KEY_LEFT: self.board.move_shape_left,
            curses.KEY_UP: self.board.rotate_shape,
            curses.KEY_DOWN: self.board.let_shape_fall,
            curses.KEY_ENTER: self.board.drop_shape,
            10: self.board.drop_shape,
            13: self.board.drop_shape,
            112: self.pause_game,
            113: self.exit,
        }
        move_fn = moves.get(user_input)
        if move_fn:
            piece_moved = move_fn()
            if piece_moved:
                self.board_drawer.update(self.board)
Example #18
0
class Game(object):
    """Manages the game and updating the screen."""
    def __init__(self, player=None):
        self.last_tick = None
        self.board = None
        self.board_drawer = None
        self.player = player or Human()

    def new_game(self):
        """Initializes a new game."""
        self.last_tick = None
        self.board = Board()
        self.board_drawer = BoardDrawer()
        self.board_drawer.clear_score()
        self.board.start_game()

    def pause_game(self):
        """Pauses or unpauses the game."""
        if self.last_tick:
            self.stop_ticking()
        else:
            self.start_ticking()

    def run_game(self):
        self.start_ticking()
        self._tick()
        while True:
            try:
                if isinstance(self.player, Human):
                    self.process_user_input()
                elif isinstance(self.player, AI):
                    self.process_ai_input()
                self.update()
            except GameOverError:
                self.end_game()
                return self.board.score

    def save_game(self):
        """Writes the state of the game to a file."""
        pass

    def load_game(self):
        """Loads a game from a file."""
        pass

    def end_game(self):
        """Ends the current game."""
        pass

    def exit(self):
        """Exits the program."""
        self.end_game()
        self.board_drawer.return_screen_to_normal()
        print('Game Over! Final Score: {}'.format(int(self.board.score)))
        sys.exit(0)

    def start_ticking(self):
        self.last_tick = datetime.datetime.now()

    def stop_ticking(self):
        self.last_tick = None

    def update(self):
        current_time = datetime.datetime.now()
        tick_multiplier = math.pow(0.9, self.board.level - 1)
        tick_threshold = datetime.timedelta(
            milliseconds=LEVEL_ONE_TICK_LENGTHMS * tick_multiplier)
        if self.last_tick and current_time - self.last_tick > tick_threshold:
            self.last_tick = current_time
            self._tick()

    def _tick(self):
        self.board.let_shape_fall()
        self.board_drawer.update(self.board)

    def process_ai_input(self):
        move = self.player.get_moves(self.board, self.board_drawer)
        if move:
            self.board.falling_shape.orientation = move.orientation
            self.board.falling_shape.move_to(move.column_position,
                                             move.row_position)
            self.board_drawer.update(self.board)
        else:
            self.end_game()

    def process_user_input(self):
        user_input = self.board_drawer.stdscr.getch()
        moves = {
            curses.KEY_RIGHT: self.board.move_shape_right,
            curses.KEY_LEFT: self.board.move_shape_left,
            curses.KEY_UP: self.board.rotate_shape,
            curses.KEY_DOWN: self.board.let_shape_fall,
            curses.KEY_ENTER: self.board.drop_shape,
            10: self.board.drop_shape,
            13: self.board.drop_shape,
            112: self.pause_game,
            113: self.exit,
        }
        move_fn = moves.get(user_input)
        if move_fn:
            piece_moved = move_fn()
            if piece_moved:
                self.board_drawer.update(self.board)
Example #19
0
class Game:
    def __init__(self, channel: discord.channel, user: discord.User):
        self.__channel = channel
        self.__owner = user
        self.__state = None
        self.__board = Board()
        self.__players = Players()
        self.__currentPower = None
        self.__dangerZone = False

    async def __electionDone(self):
        self.__board.clearEdit()
        flag = self.__board.electionResult(self.__channel, self.__players)
        if flag is None:
            self.__players.freezePrevious()
            if self.__dangerZone and await self.__checkWin():
                self.__state = State.GameOver
            else:
                self.__state = State.Legislation
                await self.__board.showBoard(self.__channel, self.__state,
                                             self.__players,
                                             self.__currentPower)
                await self.__board.policyPile.presidentTurn(
                    self.__channel, self.__players.president)
        else:
            if flag:
                (
                    fascistCardCount,
                    liberalCardCount,
                ) = await self.__board.placeRandomPolicy(self.__channel)
                if await self.__checkWin(fascistCardCount, liberalCardCount):
                    self.__state = State.GameOver
                    return
                elif fascistCardCount > 3:
                    self.__dangerZone = True
            self.__state = State.Nomination
            self.__players.nextPresident()
            await self.__board.showBoard(self.__channel, self.__state,
                                         self.__players, self.__currentPower)

    async def launch(self):
        self.__state = State.launch(self.__state)
        await self.__board.openBoard(self.__channel, self.__owner)

    async def join(self, user: discord.User) -> bool:
        if await self.__state.check(State.Inactive, self.__channel,
                                    user) and await self.__players.addPlayer(
                                        self.__channel, user):
            await self.__board.joinBoard(self.__channel, user.name,
                                         self.__players.count)
            return True
        return False

    async def begin(self, user: discord.User):
        if user.id != self.__owner.id:
            await self.__channel.send(
                f"Sorry {user.name}, only the game owner can begin the game")
            return
        if await self.__players.beginGame(
                self.__channel, user) and await self.__board.beginBoard(
                    self.__channel):
            self.__state = State.Nomination
            await self.__players.generateRoles()
            self.__board.setType(self.__players.count)
            await self.__board.showBoard(self.__channel, self.__state,
                                         self.__players, self.__currentPower)

    async def pick(self, ctx: Context, arg: str):
        if self.__state == State.Election:
            await ctx.send(
                f"Sorry {ctx.author.name}, thats an invalid command at the moment"
            )
            return
        elif self.__state == State.Nomination and await self.__players.pickChancellor(
                ctx, arg):
            self.__state = State.Election
            self.__board.clearEdit()
        elif self.__state == State.Legislation:
            flag = await self.__board.pickPolicy(self.__channel, ctx, arg,
                                                 self.__players)
            if flag == True:
                if self.__currentPower == Power.killVeto:
                    self.__currentPower = None
            elif flag == False:
                return
            else:
                self.__board.clearEdit()
                fascistCardCount, liberalCardCount = self.__board.getCardCount(
                )
                if await self.__checkWin(fascistCardCount, liberalCardCount):
                    self.__state = State.GameOver
                    return
                else:
                    if fascistCardCount > 3:
                        self.__dangerZone = True
                    if flag:
                        self.__currentPower = flag
                        self.__state = State.Execution
                    else:
                        self.__players.nextPresident()
                        self.__state = State.Nomination
        elif self.__state == State.Execution:
            if ctx.author.id != self.__players.president.id:
                await ctx.send(
                    f"Sorry {ctx.author.name}, only the President can execute Presidential Powers"
                )
                return
            elif (arg[:3] != "<@!"
                  or not self.__players.checkPlayerID(int(arg[3:-1]))
                  or arg[3:-1] == self.__players.president.id):
                await ctx.send(
                    f"Sorry {ctx.author.name}, that's an invalid selection, please retry!"
                )
                return
            else:
                candidateID = arg[3:-1]
                if self.__currentPower in (Power.kill, Power.killVeto):
                    await self.__players.assassinate(self.__channel,
                                                     candidateID)
                    self.__players.nextPresident()
                elif self.__currentPower == Power.getParty:
                    await self.__players.inspect(self.__channel, candidateID)
                    self.__players.nextPresident()
                elif self.__currentPower == Power.nextPresident:
                    self.__players.chooseSuccessor(self.__channel, candidateID)
                else:
                    raise Exception("Unkown Power")
                self.__board.clearEdit()
                self.__state = State.Nomination
                if self.__currentPower != Power.killVeto:
                    self.__currentPower = None
        else:
            return
        await self.__board.showBoard(self.__channel, self.__state,
                                     self.__players, self.__currentPower)

    async def vote(self, ctx: Context, arg: str):
        if await self.__state.check(
                State.Election, ctx.channel,
                ctx.author) and await self.__players.markVote(ctx, arg):
            await self.__board.showBoard(self.__channel, self.__state,
                                         self.__players, self.__currentPower)
            if self.__players.votingComplete():
                await self.__electionDone()

    async def see(self, ctx: Context):
        if (self.__state != State.Execution
                or self.__currentPower != Power.peekTop3
                or self.__players.president.id != ctx.author.id):
            await ctx.send(
                f"Sorry {ctx.author.name}, this seems to be an invalid command"
            )
        else:
            await self.__board.policyPile.executeTop3(self.__players.president)
            self.__currentPower = None
            self.__board.clearEdit()
            self.__players.nextPresident()
            self.__state = State.Nomination
            await self.__channel.send(
                f"President {ctx.author.name} has peeked the next set of Policies"
            )
            await self.__board.showBoard(self.__channel, self.__state,
                                         self.__players, self.__currentPower)

    async def veto(self, ctx: Context):
        if (self.__currentPower != Power.killVeto
                or self.__players.president.id != ctx.author.id
                or self.__state != State.Legislation):
            await self.__channel.send(
                f"Sorry {ctx.author.name}, you don't have the power to veto right now!"
            )
        else:
            self.__currentPower = None
            self.__board.clearEdit()
            self.__players.nextPresident()
            self.__state = State.Nomination
            await self.__board.showBoard(self.__channel, self.__state,
                                         self.__players, self.__currentPower)

    async def __checkWin(self, fascistCardCount=0, liberalCardCount=0):
        if (not (fascistCardCount or liberalCardCount) and self.__dangerZone
                and self.__players.hitler.id == self.__players.chancellor.id):
            await self.__channel.send(
                "Facists win! Hitler has been made Chancellor")
            return True
        elif fascistCardCount == 6:
            await self.__channel.send(
                "Facists win! 6 Fascist policies have been passed")
            return True
        elif liberalCardCount == 5:
            await self.__channel.send(
                "Liberals win! 5 Liberal policies have been passed")
            return True
        else:
            for player in self.__players.getPlayers():
                if player.id == self.__players.hitler.id and player.isDead:
                    await self.__channel.send(
                        "Liberals win! Hitler has been assassinated")
                    return True
        return False
    def __init__(self):
        # def run(start_player=0,is_shown=1):
        self.start_player = 0
        self.is_shown = 1
        menu = Gui_menu()

        # run a gomoku game with AI
        # you can set
        # human vs AI or AI vs AI
        self.n = 5  # Rule 5 목

        if menu.rule == 11:
            width, height = 11, 11  # 바둑판의 폭과 높이
            model_file = 'model_11_11_5/best_policy.model'
        elif menu.rule == 15:
            width, height = 15, 15  # 바둑판의 폭과 높이
            model_file = 'model_15_15_5/best_policy.model'

        p = os.getcwd()  # 현재 작업 경로를 얻음
        model_file = path.join(p, model_file)  # 파일 경로 + model file name

        board = Board(width=width, height=height, n_in_row=self.n)  # 게임 판
        game = Game(board)

        mcts_player = MCTS_pure(5, 400)

        best_policy = PolicyValueNet(board_width=width,
                                     board_height=height,
                                     block=19,
                                     init_model=model_file,
                                     cuda=True)

        # alpha_zero vs alpha_zero

        # best_policy.save_numpy(best_policy.network_all_params)
        # best_policy.load_numpy(best_policy.network_oppo_all_params)
        alpha_zero_player = MCTSPlayer(
            policy_value_function=best_policy.policy_value_fn_random,
            action_fc=best_policy.action_fc_test,
            evaluation_fc=best_policy.evaluation_fc2_test,
            c_puct=5,
            n_playout=400,
            is_selfplay=False)

        # alpha_zero_player_oppo = MCTSPlayer(policy_value_function=best_policy.policy_value_fn_random,
        #                                     action_fc=best_policy.action_fc_test_oppo,
        #                                     evaluation_fc=best_policy.evaluation_fc2_test_oppo,
        #                                     c_puct=5,
        #                                     n_playout=400,
        #                                     is_selfplay=False)

        # human player, input your move in the format: 2,3
        # set start_player=0 for human first
        # play in termianl without GUI

        # human = Human()
        # win = game.start_play(human, alpha_zero_player, start_player=start_player, is_shown=is_shown,print_prob=True)
        # return win

        # play in GUI
        game.start_play_with_UI(alpha_zero_player)
    def __init__(self, init_model=None, transfer_model=None):
        self.game_count = 0  # count total game have played
        self.resnet_block = 19  # num of block structures in resnet
        # params of the board and the game
        self.board_width = 11
        self.board_height = 11
        self.n_in_row = 5
        self.board = Board(
            width=self.board_width, height=self.board_height, n_in_row=self.n_in_row
        )
        self.game = Game(self.board)
        # training params
        self.learn_rate = 1e-3
        self.n_playout = 400  # num of simulations for each move
        self.c_puct = 5
        self.buffer_size = 500000
        # memory size, should be larger with bigger board
        # in paper it can stores 500,000 games, here with 11x11 board can store only around 2000 games
        self.batch_size = 512  # mini-batch size for training
        self.data_buffer = deque(maxlen=self.buffer_size)
        self.play_batch_size = 1
        self.game_batch_num = 10000000  # total game to train

        # num of simulations used for the pure mcts, which is used as
        # the opponent to evaluate the trained policy
        # only for monitoring the progress of training
        self.pure_mcts_playout_num = 200
        # record the win rate against pure mcts
        # once the win ratio risen to 1,
        # pure mcts playout num will plus 100 and win ratio reset to 0
        self.best_win_ratio = 0.0

        # GPU setting
        # be careful to set your GPU using depends on GPUs' and CPUs' memory
        if rank in {0, 1, 2}:
            cuda = True
        elif rank in range(10, 30):
            cuda = True
            os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
            os.environ["CUDA_VISIBLE_DEVICES"] = "1"
        else:
            cuda = False

        # cuda = True
        if (init_model is not None) and os.path.exists(init_model + ".index"):
            # start training from an initial policy-value net
            self.policy_value_net = PolicyValueNet(
                self.board_width,
                self.board_height,
                block=self.resnet_block,
                init_model=init_model,
                cuda=cuda,
            )
        elif (transfer_model is not None) and os.path.exists(transfer_model + ".index"):
            # start training from a pre-trained policy-value net
            self.policy_value_net = PolicyValueNet(
                self.board_width,
                self.board_height,
                block=self.resnet_block,
                transfer_model=transfer_model,
                cuda=cuda,
            )
        else:
            # start training from a new policy-value net
            self.policy_value_net = PolicyValueNet(
                self.board_width, self.board_height, block=self.resnet_block, cuda=cuda
            )

        self.mcts_player = MCTSPlayer(
            policy_value_function=self.policy_value_net.policy_value_fn_random,
            action_fc=self.policy_value_net.action_fc_test,
            evaluation_fc=self.policy_value_net.evaluation_fc2_test,
            c_puct=self.c_puct,
            n_playout=self.n_playout,
            is_selfplay=True,
        )
Example #22
0
    for i in range(height - 1, -1, -1):
        print("{0:4d}".format(i), end="")
        for j in range(width):
            loc = i * width + j
            p = board.states.get(loc, -1)
            if p == player1:
                print("X".center(4), end="")
            elif p == player2:
                print("O".center(4), end="")
            else:
                print("-".center(4), end="")
        # print('\r\n') # new line
        print("\r")


board = Board(width=width, height=height, n_in_row=n_in_row)

# init model here
# if you want to load different models in each rank,
# you can assign it here ,like
# if rank == 0 : model_file = '...'
# if rank == 1 : model_file = '...'

model_file = "model_11_11_5/best_policy.model"
best_policy = PolicyValueNet(board_width=width,
                             board_height=height,
                             block=19,
                             init_model=model_file,
                             cuda=True)
alpha_zero_player = MCTSPlayer(
    policy_value_function=best_policy.policy_value_fn_random,
Example #23
0
import pygame
import sys
from game_board import Board
import message
from message import message_display
from public_UI import *
import webbrowser

pygame.init()

board = Board(0, window_width, window_height, 1, 100)

# MARK: Run Loop
i = 0


def run():
    global i
    while True:
        # TODO: use dt to control movement speed of objects? (if implementing particles)
        if pause:
            if i == 0:
                i += 1
                paused()
        board.increase_generation()
        screen.fill(fill_color)
        board.draw_grid(board.active_grid)
        handle_events()
        pygame.display.flip()
        clock.tick(fps)
Example #24
0
def test_on_board():
    gc = GameController(400, 400)
    new_board = Board(400, 400, 100, gc)
    assert new_board.on_board(5, 2) is False
    assert new_board.on_board(1, 3) is True
Example #25
0
class BoardCanvas(tk.Canvas):
	"""Apply the tkinter Canvas Widget to plot the game board and stones."""
	
	def __init__(self, master=None, height=0, width=0):
		
		tk.Canvas.__init__(self, master, height=height, width=width)
		self.draw_gameBoard()
		self.turn = BLACK
		self.undo = False
		self.depth = 2
		self.prev_exist = False
		self.prev_row = 0
		self.prev_col = 0

		self.initPlayers()
		

	def initPlayers(self):
		self.width = 9
		self.height = 9
		self.board = Board(width=self.width, height=self.height, n_in_row=5)
		self.mcts_player = MCTSPlayer(c_puct=5, n_playout=1000)
		self.human_player = HumanPlayer()

		self.start_player = 0	# 0 - human, 1 - mcts_player

		self.board.init_board(self.start_player)
		p1, p2 = self.board.players
		self.human_player.set_player_id(p1)
		self.mcts_player.set_player_id(p2)
		self.players = {p2: self.mcts_player, p1: self.human_player}
		self.board.show(self.human_player.playerId, self.mcts_player.playerId)


	def draw_gameBoard(self):
		"""Plot the game board."""

		# 9 horizontal lines
		for i in range(9):
			start_pixel_x = (i + 1) * 30
			start_pixel_y = (0 + 1) * 30
			end_pixel_x = (i + 1) * 30
			end_pixel_y = (8 + 1) * 30
			self.create_line(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y)

		# 9 vertical lines
		for j in range(9):
			start_pixel_x = (0 + 1) * 30
			start_pixel_y = (j + 1) * 30
			end_pixel_x = (8 + 1) * 30
			end_pixel_y = (j + 1) * 30
			self.create_line(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y)

		# place a "star" to particular intersections 
		self.draw_star(2, 2)
		self.draw_star(6, 2)
		self.draw_star(4, 4)
		self.draw_star(2, 6)
		self.draw_star(6, 6)


	def draw_star(self, row, col):
		"""Draw a "star" on a given intersection
		
		Args:
			row, col (i.e. coord of an intersection)
		"""
		start_pixel_x = (row + 1) * 30 - 2
		start_pixel_y = (col + 1) * 30 - 2
		end_pixel_x = (row + 1) * 30 + 2
		end_pixel_y = (col + 1) * 30 + 2
		
		self.create_oval(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y, fill=GoBoardUtil.color_string(BLACK))


	def draw_stone(self, row, col):
		"""Draw a stone (with a circle on it to denote latest move) on a given intersection.
		
		Specify the color of the stone depending on the turn.
		
		Args:
			row, col (i.e. coord of an intersection)
		"""

		inner_start_x = (row + 1) * 30 - 4
		inner_start_y = (col + 1) * 30 - 4
		inner_end_x = (row + 1) * 30 + 4
		inner_end_y = (col + 1) * 30 + 4

		outer_start_x = (row + 1) * 30 - 6
		outer_start_y = (col + 1) * 30 - 6
		outer_end_x = (row + 1) * 30 + 6
		outer_end_y = (col + 1) * 30 + 6

		start_pixel_x = (row + 1) * 30 - 10
		start_pixel_y = (col + 1) * 30 - 10
		end_pixel_x = (row + 1) * 30 + 10
		end_pixel_y = (col + 1) * 30 + 10

		self.create_oval(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y, fill=GoBoardUtil.color_string(self.turn))
		self.create_oval(outer_start_x, outer_start_y, outer_end_x, outer_end_y, fill=GoBoardUtil.color_string(GoBoardUtil.opponent(self.turn)))
		self.create_oval(inner_start_x, inner_start_y, inner_end_x, inner_end_y, fill=GoBoardUtil.color_string(self.turn))


	def draw_plain_stone(self, row, col):
		"""Draw the plain stone with single color.
		Specify the color of the stone depending on the turn.
		Args:
			row, col (i.e. coord of an intersection)
		"""
		start_pixel_x = (row + 1) * 30 - 10
		start_pixel_y = (col + 1) * 30 - 10
		end_pixel_x = (row + 1) * 30 + 10
		end_pixel_y = (col + 1) * 30 + 10
		self.create_oval(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y, fill=GoBoardUtil.color_string(self.turn))


	def draw_prev_stone(self, row, col):
		"""Draw the previous stone with single color.
		
		Specify the color of the stone depending on the turn.
		
		Args:
			row, col (i.e. coord of an intersection)
		"""
		
		start_pixel_x = (row + 1) * 30 - 10
		start_pixel_y = (col + 1) * 30 - 10
		end_pixel_x = (row + 1) * 30 + 10
		end_pixel_y = (col + 1) * 30 + 10

		self.create_oval(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y, fill=GoBoardUtil.color_string(GoBoardUtil.opponent(self.turn)))


	def isValidClickPos(self, event, row, col):
		"""Since there is only one intersection such that the distance between it 
		and where the user clicks is less than 9, it is not necessary to find 
		the actual least distance
		"""
		pixel_x = (row + 1) * 30
		pixel_y = (col + 1) * 30
		square_x = math.pow((event.x - pixel_x), 2)
		square_y = math.pow((event.y - pixel_y), 2)
		move = self.board.location_to_move([row, col])
		return math.sqrt(square_x + square_y) < 9 and self.board and self.board.valid_move(move)


	def check_win(self):
		"""If the user wins the game, end the game and unbind."""
		end, winner = self.board.game_end()
		if end:
			if winner != -1:
				message = GoBoardUtil.color_string(self.turn).upper() + " WINS"
				print("{} WINS".format(self.players[winner]))
				self.create_text(150, 320, text=message)
			else:
				print("DRAW")
				self.create_text(150, 320, text='DRAW')
			self.unbind(LEFTBUTTON)
		return end, winner


	def gameLoop_human(self, event, turn=False):
		"""The main loop of the game. 
		Note: The game is played on a tkinter window. However, there is some quite useful information 
			printed onto the terminal such as the simple visualizaiton of the board after each turn,
			messages indicating which step the user reaches at, and the game over message. The user
			does not need to look at what shows up on the terminal. 
		
		self.gameBoard.board()[row][col] == 1(black stone) / 2(white stone)
		self.gameBoard.check() == 1(black wins) / 2(white wins)
		
		Args:
			event (the position the user clicks on using a mouse)
		"""
		if turn:
			self.turn = WHITE
		else:
			self.turn = BLACK

		print('Your turn now...\n')
		invalid_pos = True
		for i in range(self.height):
			for j in range(self.width):
				if self.isValidClickPos(event, i, j):
					invalid_pos = False
					row = i
					col = j
					break
		
		if invalid_pos:
			print('Invalid position.\n')
			print('Please re-select a point\n')
			self.bind(LEFTBUTTON, lambda event, arg = turn:self.gameLoop_human(event,arg))	
		else:
			self.draw_plain_stone(row, col)
			#if self.prev_exist == False:
			#	self.prev_exist = True
			#else:
				#self.draw_prev_stone(self.prev_row, self.prev_col)
			#	self.prev_row, self.prev_col = row, col
						# unbind to ensure the user cannot click anywhere until the program
						# has placed a white stone already
			self.unbind(LEFTBUTTON)

			# Place a black stone after determining the position
			move = self.board.location_to_move([row, col])
			self.board.do_move(move)
			self.board.show(self.human_player.playerId, self.mcts_player.playerId)
			print('\n')

			end, winner = self.check_win()
			if end:
				return winner
			self.unbind(LEFTBUTTON)
			self.update()
			self.gameLoop_robot(turn)
	
	
	def gameLoop_robot(self, turn=False, start=False):
		if turn:
			self.turn = BLACK
		else:
			self.turn = WHITE	
		print('Program is thinking now...')	
		move = self.mcts_player.get_action(self.board,start)
		self.board.do_move(move)
		row, col = self.board.move_to_location(move)
		coord = '%s%s'%(chr(ord('A') + row), col + 1)
		print('Program has moved to {}\n'.format(coord))
		self.draw_plain_stone(row,col)
		#if self.prev_exist == False:
		#	self.prev_exist = True
		#else:
			#self.draw_prev_stone(self.prev_row, self.prev_col)
		#self.prev_row, self.prev_col = row, col
		self.board.show(self.human_player.playerId, self.mcts_player.playerId)
		print('\n')
		# bind after the program makes its move so that the user can continue to play
		self.bind(LEFTBUTTON, lambda event, arg = turn:self.gameLoop_human(event,arg))

		end, winner = self.check_win()
		if end:
			return winner
Example #26
0
def test_legalMoves():
    gc = GameController(400, 400)
    new_board = Board(400, 400, 100, gc)
    new_board.begin()
    legal_move = new_board.legalMoves("B", "W")
    assert legal_move == {(0, 1): 1, (1, 0): 1, (2, 3): 1, (3, 2): 1}
Example #27
0
from debug_ai import debug_ai
import sys, argparse, time

# constants
DEBUG_AI = False

# get board
args = argparse.ArgumentParser()
args.add_argument('-b', '--board', help='specify a custom board', type=str)
args = args.parse_args()

board_file = 'boards/Punxsutawney.txt'
if args.board:
    board_file = args.board

board = Board(board_file)
print(board)

if DEBUG_AI:
    # debug minimax
    debug_ai(board, calculate_minimax)

    # debug abpruning
    debug_ai(board, calculate_abprune)
#data constants
num_moves = {'green' : 0, 'blue' : 0}
total_nodes = {'green' : 0, 'blue' : 0}
total_time = 0

while board.open:
    current_turn = board.turn