Esempio n. 1
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"
Esempio n. 2
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
Esempio n. 3
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()
Esempio n. 4
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
Esempio n. 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
Esempio n. 6
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]]
Esempio n. 7
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,
        )
Esempio n. 8
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
Esempio n. 9
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]]
Esempio n. 10
0
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
Esempio n. 11
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)
Esempio n. 12
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"]]
Esempio n. 13
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 #
Esempio n. 14
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)
Esempio n. 15
0
    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,
        )
Esempio n. 17
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,
Esempio n. 18
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
Esempio n. 19
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}
Esempio n. 20
0
 def __init__(self):
     self.game = Board()