Пример #1
0
def main():
    gomoku = Gomoku()
    while True:
        ret = gomoku.catch_input()
        if (ret != 0):
            del gomoku
            return (0)
Пример #2
0
def calculator(mat):
    #input: (2d array) boardstate
    #output: (tuple,2 elements) position
    gomoku = Gomoku()
    for i in range(N):
        for j in range(N):
            gomoku.set_chessboard_state(i, j, mat[i][j])

    ai = gomokuAI(gomoku, BoardState.BLACK, 1)
    #render = GameRender(gomoku)
    result = BoardState.EMPTY
    boolean, position = ai.one_step()
    return position
Пример #3
0
    def __init__(self, game=Gomoku(), playerNum=1):
        super().__init__(game, playerNum)
        self.name = "MINIMAX"
        self.move_num = 1
        self.expandedNodes = 0

        self.treeDepth = 1
def train():
    game_episode_num = 3000
    selfplay_batch_size = 1
    data_buffer_size = 10000
    check_step = 10
    train_batch_size = 512

    data_buffer = deque(maxlen=data_buffer_size)

    game = Gomoku(game_board_width)
    policy = policy_network(input_dim=game.nn_input.shape,
                            output_dim=game.w**2)
    mcts_player = MCTS(policy, mcts_playout_itermax_train)
    winner_num = [0] * 3

    print('[*] Start self play')
    # game episode
    for i in range(game_episode_num):

        # get train data
        start_time = time.time()
        for _ in range(selfplay_batch_size):
            play_data, winner, starting_player = self_play(game, mcts_player)
            episode_len = len(play_data)
            extend_data = augment_data(play_data)
            data_num = len(extend_data)
            data_buffer.extend(extend_data)
            winner_num[winner] += 1
        end_time = time.time()

        print(
            '[*] Episode: {}, length: {}, start: {}, winner: {}, data: {}, time: {}s, win ratio: X {:.1f}%, O {:.1f}%, - {:.1f}%'
            .format(
                i + 1,
                episode_len,
                ['-', 'X', 'O'][starting_player],
                ['-', 'X', 'O'][winner],
                data_num,
                int(end_time - start_time),
                winner_num[1] / (i + 1) * selfplay_batch_size * 100,
                winner_num[2] / (i + 1) * selfplay_batch_size * 100,
                winner_num[0] / (i + 1) * selfplay_batch_size * 100,
            ))

        # train
        if len(data_buffer) > train_batch_size:
            mini_batch = random.sample(data_buffer, train_batch_size)
            state_batch = np.array([d[0] for d in mini_batch])
            pi_batch = np.array([d[1] for d in mini_batch])
            z_batch = np.array([d[2] for d in mini_batch])

            policy.train(state_batch, [z_batch, pi_batch])

        # check current policy model and save the params
        if (i + 1) % check_step == 0:
            policy.loss_history.plot_loss('loss.png')
            print('[*] Save current policy model')
            policy.save(model_file)
            print('[*] done')
Пример #5
0
    def __init__(self, game=Gomoku(), playerNum=1):
        super().__init__(game, playerNum)
        self.name = "ALPHA_BETA"
        self.move_num = 1
        self.expandedNodes = 0

        # Max of 3
        self.treeDepth = 1
def play_game():
    game = Gomoku(game_board_width)
    policy = policy_network(input_dim=game.nn_input.shape,
                            output_dim=game.w**2)
    policy.load(model_file)
    mcts_player = MCTS(policy, mcts_playout_itermax_play)

    starting_player = random.choice([1, 2])
    game.reset(starting_player)
    mcts_player.set_rootnode(starting_player)
    while not game.is_end:
        print(game)
        # print(game.nn_input)

        if game.current_player == 1:  # Player X
            action, _ = mcts_player.get_move(game)
        else:  # Player O
            action = human_play()

        game.move(action)
        mcts_player.update_with_move(action, game)

        print("[*] Player %s move: %s\n" %
              (['X', 'O'][game.player_just_moved - 1], action))

    print(game)
    if game.winner > 0:
        print("[*] Player %s win" % ['X', 'O'][game.winner - 1])
    else:
        print("[*] Player draw")
Пример #7
0
import pygame

from gomoku import Gomoku
from gomoku.palette import COLOR_BLACK, COLOR_RED, COLOR_GRAY, \
    COLOR_GREEN, COLOR_WHITE


if __name__ == "__main__":
    pygame.init()
    pygame.font.init()

    stone = {}
    stone["white"], stone["black"] = [], []
    player1_score, player2_score = 0, 0
    game = Gomoku()
    game.draw_main()
    game.draw_score(player1_score, player2_score)

    play_order = None

    while True:
        event = pygame.event.poll()
        if event.type == pygame.MOUSEBUTTONDOWN:

            x_stone, y_stone = game.play_get_pos()

            # New game.
            if (125 + 45 * 16) > x_stone > 45 * 16 and 90 > y_stone > 45:
                stone["white"], stone["black"] = [], []
                player1_score, player2_score = 0, 0
                game = Gomoku()
Пример #8
0
 def __init__(self, game=Gomoku(), playerNum=1):
     # super(self).__init__(game, playerNum)
     super().__init__(game, playerNum)
     self.name = "HUMAN"
Пример #9
0
          : "human" or "h" for a human agent (for extra credit)
-------------------------------------------------------------------------------
"""

from gomoku import Gomoku

import time, sys
from Agents.agent import Agent
from Agents.alphabeta import AlphaBeta
from Agents.minimax import MiniMax
from Agents.reflex import Reflex
# from Agents.human import Human
import helper
# import gui

game = Gomoku()

if __name__ == "__main__":

    incorrectUsageError = "Incorrect Usage: Expected " \
                        + "\"python %s <agent1> <agent2>\"" % sys.argv[0]

    assert len(sys.argv) == 3, incorrectUsageError

    # Agent 1
    if sys.argv[1] == "alpha-beta" or sys.argv[1] == "ab":
        Jon = AlphaBeta(game, 1)
    elif sys.argv[1] == "minimax" or sys.argv[1] == "mm":
        Jon = MiniMax(game, 1)
    elif sys.argv[1] == "reflex" or sys.argv[1] == "r":
        Jon = Reflex(game, 1)
Пример #10
0
    for l in range(len(ascii_arts[0])):
        for i in range(len(ascii_arts)):
            display += ascii_arts[i][l] + '   '
        display += "\n"

    print(display)


iterations = 0

while True:
    histories = [[] for _ in range(batch_size)]
    results = []

    for r in range(round_batch_size):
        games = [Gomoku(shape) for _ in range(batch_size)]
        lmodel = LearnedGomokuModel(model)
        tree_search = ParallelMonteCarloTreeSearch(LearnedGomokuModel(model),
                                                   4, 4)

        os.system('clear')
        print(iterations, r)
        display_games(games[0:4])
        display_games(games[4:8])
        while not games_over(games):
            outcomes, actions = tree_search.search(games)
            results.append({'outcome': outcomes[0], 'action': actions[0]})
            for i in range(len(games)):
                if not games[i].game_over():
                    games[i].take_action(actions[i])
                    histories[i].append({
Пример #11
0
import pygame
from pygame.locals import *
from sys import exit

import boardstate
from boardstate import *
from gomoku import Gomoku
from render import GameRender
#from gomoku_ai import *

if __name__ == '_main_':
    gomoku = Gomoku()
    render = GameRender(gomoku)
    #ai =  GomokuAI(gomoku, board.BLACK, 2)
    #ai2 = GomokuAI(gomoku, board.WHITE, 1)

    result = BoardState.EMPTY

    enable_ai = False
    """
    enable_ai = False
    ai.first_step()
    result = gomoku.get_chess_result()
    render.change_state()
    """
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exec()

            if event.type == MOUSEBUTTONDOWN:
Пример #12
0
 def __init__(self, game=Gomoku(), playerNum=1):
     self.name = "AGENT"
     self.game = game
     self.playerNum = playerNum
     self.player = Gomoku.players[(playerNum - 1) % 2]
Пример #13
0
model = load_model('models/20210320_172325.h5')

app = Ursina()

window.borderless = False
window.color = color._50

w, h = 20, 20
camera.orthographic = True
camera.fov = 23
camera.position = (w // 2, h // 2)

board = Board(w=w, h=h)
board_buttons = [[None for x in range(w)] for y in range(h)]
game = Gomoku(board=board)

Entity(model=Grid(w + 1, h + 1),
       scale=w + 1,
       color=color.black,
       x=w // 2 - 0.5,
       y=h // 2 - 0.5,
       z=0.1)

for y in range(h):
    for x in range(w):
        b = Button(parent=scene,
                   position=(x, y),
                   color=color.clear,
                   model='circle',
                   scale=0.9)  # start from bottom left
Пример #14
0
        bestval = MinimaxTree.alphabeta(self, sample_space, affinity, depth_limit, -10000, 10001, True)
        if bestval[0] is None: bestval = ((0,6),'x', 0)

        # print the number of nodes expanded 
        print(self.nodes_expanded)

        # make the move found by the search 
        self.get_game_space().set_tile(bestval[0][0], bestval[0][1], affinity)
    
    def play_full_game(self): pass

""" Code below here is to be exclusively used for 
testing the Agent class and its subclasses.
""" 
if __name__ == '__main__': 
    new_game = Gomoku(7,7)

    #blue_reflex = Reflex(BLUE_TILE(), Gomoku, new_game)
    #red_reflex = Reflex(RED_TILE(), Gomoku, new_game, blue_reflex)
    
    blue_alphabeta = AlphaBeta(BLUE_TILE(), Gomoku, new_game, 3)
    #blue_minimax = MiniMax(BLUE_TILE(), Gomoku, new_game, 3)
    red_reflex =  Reflex(RED_TILE(), Gomoku, new_game, blue_alphabeta)

    # make the first two assigned moves
    #new_game.set_tile(1,5,RED_TILE())
    #new_game.print_board()
    #print('\n')
    #new_game.set_tile(5,1,BLUE_TILE())
    #new_game.print_board()
    #print('\n')
Пример #15
0
 def setUp(self):
     self.game = Gomoku()
Пример #16
0
def main():
	# initialize OpenAI Gym env and dqn agent
	print (EPISODE)
	env = Gomoku()
	agent = DQN(env)
	SIZE = env.SIZE
 
	agent.copyWeightsToTarget()
 
	for episode in range(EPISODE):
		# initialize task
		state = env.reset()
 
		camp = -1
		state = np.reshape(state,[-1])
		state = np.append(state,camp)
 
		#print('episode ',episode)
 
		# Train
		for step in range(STEP):
			#take one step
			action = agent.egreedy_action(state) # e-greedy action for train
			
			action = [math.floor(action/SIZE),action%SIZE,camp]
			#if env.env.is_valid_set_coord(action[0],action[1]):
			next_state,reward,done,_ = env.step(action)
			next_state = np.reshape(next_state,[-1])
			if step%2 == 0:
				camp = 1
			else:
				camp = -1
			next_state = np.append(next_state,camp)
			# Define reward for agent
			reward_agent = reward
			agent.perceive(state,action,reward,next_state,done)
			state = next_state
			if done:
				#print('done step ',step)
				break
		# Test every 100 episodes
		if episode % 100 == 99:
			total_reward = 0
			for i in range(TEST):
				state = env.reset()
				state = np.reshape(state,[-1])
				camp = -1
				state = np.append(state,camp)
 
				for j in range(STEP):
					#env.render()
					action = agent.action(state) # direct action for test
 
					action = [math.floor(action/SIZE),action%SIZE,camp]
					state,reward,done,_ = env.step(action)
					state = np.reshape(state,[-1])
					if j%2 == 0:
						camp = 1
					else:
						camp = -1
					state = np.append(state,camp)
 
					total_reward += reward
					time.sleep(0.5)
					if done:
						print('done')
						time.sleep(3)
						break
			ave_reward = total_reward/TEST
			print('episode: ',episode,  'Evaluation Average Reward:',ave_reward)
			epi.append(episode)
			reward_epi.append(ave_reward)
			if episode % 1000 == 99:
				np.save('epi_ddqn.npy', np.array(epi))
				np.save('reward_epi_ddqn.npy', np.array(reward_epi))

	np.save('epi_ddqn.npy', np.array(epi))
	np.save('reward_epi_ddqn.npy', np.array(reward_epi))
Пример #17
0
from datetime import datetime

from control import Control
from gomoku import Gomoku
from player_policy_gradient import PlayerPolicyGradient

size_board = (9, 9)
name = 'pattern_1'
path = 'weights'

environment = Gomoku(size_board)

player_black = PlayerPolicyGradient(True,
                                    size_board,
                                    path,
                                    name,
                                    mode='random')
player_white = PlayerPolicyGradient(False,
                                    size_board,
                                    path,
                                    name,
                                    mode='random2')

players = [player_black, player_white]

control = Control(environment, players)

for _ in range(1):
    experience = control.play(100)

    for i in range(len(players)):
Пример #18
0
from gomoku import Gomoku

""" 
This module is the main module of part 2.2
"""

__author__ = 'Griffin A. Tucker / Michael Racine'
__version__ = '1.0'
__date__ = '2_19_18'

# only run the following code if we are at top level
if __name__ == "__main__":

    # get dimensions of Gomoku board and instantiate
    while True: 
        w = input("Please enter a value for the width of the Gomoku board: ")
        while:
            w = input("Invalid value: Please enter an integer: ")
        h = input("Please enter a value for the height of the Gomoku board: ")
        while:
            h = input("Invalid value: Please enter an integer: ")
        got_dimensions = input("Is " + str(w) + "x" + str(h) + " your desired dimensions? Enter 1 for yes; 0 for no: ")
        while got_dimensions != 0 and got_dimensions != 1:
            got_dimensions = input("Invalid input. Enter 1 for yes; 0 for no: ")
        if got_dimensions == 1: break;

    cur_board = Gomoku(w, h) 
Пример #19
0
from gomoku import Gomoku

if __name__ == '__main__':
    game = Gomoku()
    game.run()
Пример #20
0
def play():
    mes("This is a Gomoku-game. Type your deserved option: ")
    gomoku_game = Gomoku(SIZE, '+')
    option = start_menu()
    if option == 0:
        mes("bye!!")
        return
    elif option > 3:
        mes("Sorry, not avaliable :(")
        return
    elif option < 0:
        mes("Not valid option!")
        return
    rm = gomoku_game.copy_matrix()
    mes("Let's play!")
    playing = True
    valid = False
    i = 0
    mes(gomoku_game.show_matrix())
    while playing:
        if option == 1: # human vs human
            rm = turn(players[0], gomoku_game)
            if gomoku_game.is_over():
                break
            mes(gomoku_game.show_matrix())
            valid = False
            rm = turn(players[1], gomoku_game)
            if gomoku_game.is_over():
                break
            mes(gomoku_game.show_matrix())
            valid = False
        elif option == 2: # human vs computer
            rm = turn(players[0], gomoku_game)
            if gomoku_game.is_over():
                break
            mes(gomoku_game.show_matrix())
            valid = False
            rm = gomoku_game.computer_turn()
            mes("Computer turn:")
            mes(rm)
            if gomoku_game.is_over():
                break
        else: # computer vs human
            rm = gomoku_game.computer_turn()
            mes("Computer turn:")
            mes(rm)
            if gomoku_game.is_over():
                break
            rm = turn(players[0], gomoku_game)
            if gomoku_game.is_over():
                break
            mes(gomoku_game.show_matrix())
            valid = False
        i = i + 1
        if i > 30:
            playing = False
    mes("bye!! :D")
Пример #21
0
 def __init__(self, game=Gomoku(), playerNum=1):
     super().__init__(game, playerNum)
     self.name = "REFLEX"
     self.firstMove = True
Пример #22
0
# colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)

coordinateList = [50, 160, 270, 380, 490, 600, 710]

boardInitialized = False
gameDisplay = None
keepPlaying = True
gameExit = False
win = False

game = Gomoku()
# Jon = Human(game, 1)
# Jess = Human(game, 2)
# check if the board has been initalized
if boardInitialized == False:
    pygame.init()
    pygame.display.init()
    gameDisplay = pygame.display.set_mode((870, 940))
    pygame.display.set_caption('Gomoku')
    # draw board
    gameDisplay.fill(white)
    for x in range(0, 8):
        pygame.draw.rect(gameDisplay, black, [110 * x + 50, 50, 10, 780])
    for y in range(0, 8):
        pygame.draw.rect(gameDisplay, black, [50, 110 * y + 50, 780, 10])
    pygame.display.update()