コード例 #1
0
    def restart(self):
        self.color = 1
        self.gamealive = True
        self.board = HexBoard(RADIUS, (SIZE, SIZE))
        self.board.padding = PAD
        self.draw_board()
        pg.display.update()
        if len(sys.argv) == 2:
            self.filename = sys.argv[1]
        else:
            self.filename = 'map' + str(random.randrange(9999))
        while self.gamealive:
            deltaTime = self.clock.tick(FPS)
            for event in pg.event.get():
                if event.type == QUIT:
                    pg.quit()
                    sys.exit()
                elif event.type == MOUSEBUTTONDOWN:
                    cpoint = self.board.centerPoint((3, 3))
                    for loc in self.board.alllocs:
                        if vect.dist(self.board.centerPoint(loc),
                                     event.pos) < RADIUS:
                            self.board.map[loc] = self.color
                elif event.type == KEYDOWN:
                    if event.key == K_s:
                        np.save('assets/maps/' + self.filename, self.board.map)

                        print "Map Saved as", self.filename
                    elif event.key == K_1:
                        self.color = 0
                    elif event.key == K_2:
                        self.color = 1

            self.draw_board()
            pg.display.update()
コード例 #2
0
def main():
    # find a board configuration from input
    board_config = parseboardinput()

    # initialise board
    board = HexBoard(board_config)

    # used for debug purposes
    if PRINT_HEURISTICS:
        board.print_board_heuristics()

    # run search algorithm
    dest_node = A_star_search(board)

    if dest_node == None:
        print("# can't do it sorry :(")
        return

    # prints every board state along the path (run with "| more -16" to animate with spacebar)
    if PRINT_BOARD_PATH:
        board.print_path(dest_node)

    # prints the required output
    dest_node.print_instructions()

    # Billy insisting on doing a weird output for some reason
    print(f"# yeehaw {dest_node.cost} moves")
コード例 #3
0
ファイル: nextki.py プロジェクト: ewuerger/Hex
 def swapp(self):
     curBoard = copy(self.startState)
     self.startState = HexBoard(self.height, self.width)
     self.move = MCTS(self)[0]
     i, j = self.move
     if curBoard.board[i][j] != 0:
         self.swap = True
     self.startState = curBoard
コード例 #4
0
def copy(Board):
    m = len(Board.board[0])
    a, b = Board.zug, Board.no_filled
    newBoard = HexBoard(m, m)
    newBoard.board = deepcopy(Board.board)
    newBoard.starter = Board.starter
    newBoard.win = Board.win
    newBoard.zug = a
    newBoard.lastmove = Board.lastmove
    newBoard.no_filled = b
    newBoard.swap = Board.swap
    return newBoard
コード例 #5
0
    def __init__(self, colour):
        """
        This method is called once at the beginning of the game to initialise
        your player. You should use this opportunity to set up your own internal
        representation of the game state, and any other information about the 
        game state you would like to maintain for the duration of the game.

        The parameter colour will be a string representing the player your 
        program will play as (Red, Green or Blue). The value will be one of the 
        strings "red", "green", or "blue" correspondingly.
        """
        self.board = HexBoard()
        self.player = colour
コード例 #6
0
    def run_match(self, blue, red):
        b = self.players[blue](self.size, HexBoard.BLUE, self.seed)
        r = self.players[red](self.size, HexBoard.RED, self.seed)
        if self.names[blue] is None:
            self.names[blue] = b.name()
        if self.names[red] is None:
            self.names[red] = r.name()
        board = HexBoard(self.size)

        blue_turn = True
        while not board.is_game_over():
            if blue_turn:
                b.make_move(board)
            else:
                r.make_move(board)
            blue_turn = not blue_turn

        return not blue_turn, blue, red
コード例 #7
0
 def restart(self):
     self.turn = 1
     self.gamealive = True
     self.board = HexBoard(settings.radius,(settings.map_size,settings.map_size))
     self.board.padding = settings.padding
     self.view = View(self.board.size,settings.view_height,settings.view_width)
     self.draw_board()
     self.bots = []
     size = self.board.size[0]-1
     for i in range(150):
         x = int(floor(max(0,min(size,gauss(size/2,10)))))
         y = int(floor(max(0,min(size,gauss(size/2,10)))))
         self.bots.append((x,y))
     pg.display.update()
     while self.gamealive:
         deltaTime = self.clock.tick(settings.fps)
         pg.display.set_caption('BotSurvival! '+str(round(1.0/deltaTime*1000)))
         self.dirty_rects = []
         for event in pg.event.get():
             if event.type == QUIT:
                 close()
             elif event.type == MOUSEBUTTONDOWN: 
                 self.run_turn()
                 self.turn += 1
             elif event.type == KEYDOWN:
                 if event.key == K_r:
                     self.restart()
                     self.gamealive = False
                 elif event.key == K_ESCAPE:
                     close()
                 elif event.key == K_RIGHT:
                     self.view.move((0,2))
                 elif event.key == K_LEFT:
                     self.view.move((0,-2))
                 elif event.key == K_DOWN:
                     self.view.move((2,0))
                 elif event.key == K_UP:
                     self.view.move((-2,0))
                 self.draw_board()
                 self.draw_bots()
         pg.display.update()
     self.restart()
     close()
コード例 #8
0
    def __init__(self, n):
        self.root = tk.Tk()
        self.root.title('Hex')

        self.n = n

        self.data = pd.DataFrame()

        self.current_player = 1
        self.current_player_color = 'blue'

        self.current_move = 1

        self.player_1_selection = tk.StringVar()
        self.player_1_selection.set('Human Player')
        self.player_2_selection = tk.StringVar()
        self.player_2_selection.set('Human Player')

        self.board = HexBoard(n)

        self.window = HexGui(n, self)

        self.root.mainloop()
コード例 #9
0
ファイル: task4.py プロジェクト: JasperO98/rl-assignment1
from hexgame import HexGame
from hexplayer import HexPlayerHuman, HexPlayerDijkstra
from hexboard import HexBoard
from hexcolour import HexColour

if __name__ == '__main__':
    # test functionality
    board = HexBoard(3)
    board.set_colour([(0, 1), (1, 1), (2, 1)], HexColour.RED)
    board.dijkstra(HexColour.RED, True)
    board.dijkstra(HexColour.BLUE, True)

    board = HexBoard(5)
    board.set_colour([(0, 3), (2, 2), (4, 1)], HexColour.RED)
    board.dijkstra(HexColour.RED, True)

    # play a game
    game = HexGame(5, HexPlayerDijkstra(4), HexPlayerHuman())
    game.play()
コード例 #10
0
from hexgame import HexGame
from hexplayer import HexPlayerHuman, HexPlayerEnhanced
from hexboard import HexBoard

if __name__ == '__main__':
    # test functionality
    board = HexBoard(2)
    for child, _ in board.children():
        print(child.board, hash(child))

    game = HexGame(2, HexPlayerEnhanced(10, True), None)
    game.step(['tree'])
    game = HexGame(2, HexPlayerEnhanced(10, False), None)
    game.step(['tree'])

    # play a game
    game = HexGame(5, HexPlayerEnhanced(10, True), HexPlayerHuman())
    game.play()
コード例 #11
0
from hexboard import HexBoard

parser = argparse.ArgumentParser(description="Hex interactive player")
parser.add_argument("BOARD_SIZE", type=int)
parser.add_argument("-r", help="play as red", action="store_true")
parser.add_argument("PLAYER", type=str, nargs='?', default="player")
parser.add_argument("SEED", type=int, nargs='?', default=0)
args = parser.parse_args()

opp_color = HexBoard.RED
play_color = HexBoard.BLUE
if args.r:
    opp_color = HexBoard.BLUE
    play_color = HexBoard.RED

board = HexBoard(args.BOARD_SIZE)
board.print()
opponent_class = importlib.import_module("players." + args.PLAYER).export
opponent = opponent_class(args.BOARD_SIZE, opp_color, args.SEED)

print("Playing vs: " + opponent.name())

is_player_turn = not args.r
while not board.is_game_over():
    if is_player_turn:
        try:
            text_in = input("move?> ")
            x, y = text_in.split(',')
            x = int(x)
            y = ord(y) - ord('a')
            if x >= 0 and x < board.size and y >= 0 and y < board.size and board.is_empty(
コード例 #12
0
ファイル: hexgame.py プロジェクト: JasperO98/rl-assignment1
 def __init__(self, size, player1, player2):
     self.board = HexBoard(size)
     self.player1 = player1
     self.player2 = player2
     self.win = ""
     self.lose = ""