def test_load(self): b = Board(2, 2) b.load([[10, 20], ['a', False]]) self.assertEqual(b[0][0], 10) self.assertEqual(b[0][1], 20) self.assertEqual(b[1][0], 'a') self.assertEqual(b[1][1], False)
def size_test(): b = Board(3, 5) assert b.nrows == 3 assert b.ncols == 5 assert b.size == 15 b._setupUI() assert b.nrows == 3 assert b.ncols == 5 assert b.size == 15
def __init__(self, t_map, state, path: list): self.t_map = t_map self.state = state self.path = path[1:] self.start = False self.depth = len(path) self.cost = 0 self.board = Board(len(t_map), len(t_map[0])) self.board.on_timer = self.ontimer self.board.on_key_press = self.onkey self.board.start_timer(630)
def test_copy(self): b = Board(2, 3) for i in range(b.nrows): for j in range(b.ncols): b[i][j] = (i + 1) * (j + 5) bc = b.copy() self.assertEqual(len(bc), b.nrows) for i in range(len(bc)): self.assertEqual(len(bc[i]), b.ncols) for j in range(len(bc[i])): self.assertEqual(bc[i][j], b[i][j])
def __init__(self, simulation_mode=False): """:cvar """ env = get_environment("blobwar")() env.seed(10) self.images = {1: "blob_blue", 2: "blob_orange"} self.images_selected = { 1: "blob_blue_selected", 2: "blob_orange_selected" } self.images_candidates = { 1: "blob_blue_candidate", 2: "blob_orange_candidate" } self.env = env self.human_player = Human() try: self.ai_player_1 = PPO(self.env, size=self.env.core.board.shape[0]) except Exception as e: print("PPO not yet trained for : ", self.env.core.board.shape[0]) self.ai_player_1 = GreedyPlayer() self.ai_player_2 = GreedyPlayer() self.player1 = self.player2 = None if simulation_mode: self.state = GameState.WAITING_FIRST_CLICK self.player1 = self.ai_player_1 self.player2 = self.player1 return self.game = Board(self.env.core.board.shape[0], self.env.core.board.shape[1]) self.game.cell_size = 70 self.game.cell_spacing = 1 self.game.fill(None) self.game.margin_color = self.game.grid_color = "wheat4" self.game.cell_color = "white" self.game.title = "Blobwar" self.game.create_output(font_size=11) self.state = GameState.GAME_OVER self.move_start_case = None self.move_candidates_case = []
def test_fill(self): b = Board(3, 3) b.fill('ZZZ', row=1) self.assertEqual(b[1].count('ZZZ'), 3) b.fill(1.5, col=2) self.assertEqual(b[0][2], 1.5) self.assertEqual(b[1][2], 1.5) self.assertEqual(b[2][2], 1.5) b.fill(-3) for i in range(b.nrows): self.assertTrue(all(v == -3 for v in b[i]))
class Visualize(): def __init__(self, t_map, state, path: list): self.t_map = t_map self.state = state self.path = path[1:] self.start = False self.depth = len(path) self.cost = 0 self.board = Board(len(t_map), len(t_map[0])) self.board.on_timer = self.ontimer self.board.on_key_press = self.onkey self.board.start_timer(630) def ontimer(self): if len(self.path) == 0: print(f"Answer's Depth: {self.depth}") print(f"Answer's Cost: {self.cost}") exit(0) if not self.start: return self.board.clear() self.action = self.path[0] del self.path[0] self.state = act(self.t_map, self.state, self.action) # Update Path Cost! self.cost += eval( self.t_map[self.state['bot'][1]][self.state['bot'][0]]) for y, row in enumerate(self.t_map): for x, cell in enumerate(row): if 'x' in cell: self.board[y][x] = 'x' self.board[self.state['bot'][1]][self.state['bot'][0]] = 'r' for b_x, b_y in self.state['butters']: self.board[b_y][b_x] = 'b' for g_x, g_y in self.state['goals']: self.board[g_y][g_x] = 'g' def onkey(self, key): if key == "Return": self.start = True def render(self): self.board.show()
def title_test(): b = Board(2, 2) _value = "qwertyu 213213 dsdadasd~...." b.title = _value assert b.title == _value b._setupUI() assert b.title == _value assert b._root.title() == _value b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() assert b.title == _value assert b._root.title() == _value
def test_cell_size(self): b = Board(4, 9) _value = (34, 55) b.cell_size = _value self.assertEqual(b.cell_size[0], _value[0]) self.assertEqual(b.cell_size[1], _value[1]) b._setupUI() self.assertEqual(b.cell_size[0], _value[0]) self.assertEqual(b.cell_size[1], _value[1]) b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() self.assertEqual(b.cell_size[0], _value[0]) self.assertEqual(b.cell_size[1], _value[1]) b2 = Board(42, 99) _value = 1 b2.cell_size = _value self.assertEqual(b2.cell_size[0], _value) self.assertEqual(b2.cell_size[1], _value) b2._setupUI() self.assertEqual(b2.cell_size[0], _value) self.assertEqual(b2.cell_size[1], _value) b2.on_timer = lambda: b.close() b2.start_timer(TIME_TO_CLOSE) b2.show() self.assertEqual(b2.cell_size[0], _value) self.assertEqual(b2.cell_size[1], _value)
c = random.randint(0, FIELD_WIDTH - 1) # Random collumn if field[r][c] is None: # It must be an empty place field[r][c] = "fruit" break def setup(): global snake, lastkey w2 = FIELD_WIDTH // 2 # field center h2 = FIELD_HEIGHT // 2 field.fill(None) snake = [(h2, w2), (h2, w2 + 1)] # Initial snake position for pos in snake: field[pos[0]][pos[1]] = 'body' # Draw the snake fruit_random_position() lastkey = "Left" # starts moving to the left field.start_timer(300) # 300 ms field = Board(FIELD_HEIGHT, FIELD_WIDTH) field.cell_size = BLOCK_SIZE field.title = "Snake game" field.cursor = None # Hide the cursor field.margin = field.cell_spacing = 1 field.grid_color = field.margin_color = "dark sea green" field.cell_color = "PaleGreen4" field.on_key_press = kbd_fn field.on_timer = timer_fn setup() field.show()
def cell_spacing_test(): b = Board(1, 1) _value = 7 b.cell_spacing = _value assert b.cell_spacing == _value b._setupUI() assert b.cell_spacing == _value b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() assert b.cell_spacing == _value
class BlobwarGui: def __init__(self, simulation_mode=False): """:cvar """ env = get_environment("blobwar")() env.seed(10) self.images = {1: "blob_blue", 2: "blob_orange"} self.images_selected = { 1: "blob_blue_selected", 2: "blob_orange_selected" } self.images_candidates = { 1: "blob_blue_candidate", 2: "blob_orange_candidate" } self.env = env self.human_player = Human() try: self.ai_player_1 = PPO(self.env, size=self.env.core.board.shape[0]) except Exception as e: print("PPO not yet trained for : ", self.env.core.board.shape[0]) self.ai_player_1 = GreedyPlayer() self.ai_player_2 = GreedyPlayer() self.player1 = self.player2 = None if simulation_mode: self.state = GameState.WAITING_FIRST_CLICK self.player1 = self.ai_player_1 self.player2 = self.player1 return self.game = Board(self.env.core.board.shape[0], self.env.core.board.shape[1]) self.game.cell_size = 70 self.game.cell_spacing = 1 self.game.fill(None) self.game.margin_color = self.game.grid_color = "wheat4" self.game.cell_color = "white" self.game.title = "Blobwar" self.game.create_output(font_size=11) self.state = GameState.GAME_OVER self.move_start_case = None self.move_candidates_case = [] def __player_num__(self, num): return min(2 - num, 2) # 1=>1, -1 =>2 def initialize(self, game_type=GameType.HUMAN_AI): """:cvar Initiliaze game and set automove if current player is not human """ if game_type == GameType.HUMAN_AI: self.player1 = self.human_player self.player2 = self.ai_player_1 elif game_type == GameType.HUMAN_HUMAN: self.player1 = self.human_player self.player2 = self.human_player else: self.player1 = self.ai_player_1 self.player2 = self.ai_player_2 self.env.core.reset() self.state = GameState.WAITING_FIRST_CLICK self.move_start_case = None self.move_candidates_case = [] self.render() if not isinstance(self.player1, Human) and not isinstance( self.player2, Human): self.autoplay() else: self.state = GameState.WAITING_FIRST_CLICK self.print_status() if not isinstance(self.player1, Human): movement = self.player1.choose_action(self.env) self.env.core.apply_movement(movement) self.print_status() self.render() def render(self): for x in range(self.env.core.board.shape[0]): for y in range(self.env.core.board.shape[1]): if self.env.core.board[x][y] == 0: self.game[x][y] = None continue self.game[x][y] = self.images[self.__player_num__( self.env.core.board[x][y])] if self.move_start_case is not None: self.render_selected() def fnkbd(self, key): if key == "Escape": self.game.close() elif key == "F2": self.initialize(game_type=GameType.HUMAN_AI) elif key == "F3": self.initialize(game_type=GameType.HUMAN_HUMAN) elif key == "F4": self.initialize(game_type=GameType.AI_AI) def setup(self): self.game.on_mouse_click = self.on_click self.game.on_key_press = self.fnkbd self.game.on_start = lambda: self.initialize() self.game.show() def on_click(self, btn, x, y): current_player = self.player1 if self.__player_num__( self.env.core.current_player) == 1 else self.player2 if not isinstance(current_player, Human): return if self.state == GameState.GAME_OVER: return if self.state == GameState.WAITING_FIRST_CLICK: self.__on_first_click(btn, x, y) return if self.state == GameState.WAITING_SECOND_CLICK: self.__on_second_click(btn, x, y) return def __on_first_click(self, btn, x, y): if self.env.core.board[x][y] == 0: return current_player = self.__player_num__(self.env.core.current_player) if self.__player_num__(self.env.core.board[x][y]) != current_player: return else: self.move_start_case = (x, y) self.move_candidates_case = self.env.core.neighbours(x, y) self.render() self.state = GameState.WAITING_SECOND_CLICK def __on_second_click(self, btn, x, y): current_player = self.__player_num__(self.env.core.current_player) if (x, y) == self.move_start_case: self.move_start_case = None self.move_candidates_case = [] self.render() self.state = GameState.WAITING_FIRST_CLICK return movement = [self.move_start_case, (x, y)] if self.env.core.check_move(movement): self.env.core.apply_movement(movement) self.print_status() (x_start, y_start) = self.move_start_case self.game[x_start][y_start] = self.images[current_player] self.move_start_case = None self.move_candidates_case = [] self.state = GameState.WAITING_FIRST_CLICK self.render() ###Automatic play def after_move(): current_player = self.player1 if self.__player_num__( self.env.core.current_player) == 1 else self.player2 if self.env.core.game_over(): self.state = GameState.GAME_OVER winner_id = self.env.core.leading_player() self.render() self.print_status() return self.print_status() if not isinstance(current_player, Human): movement = current_player.choose_action(self.env) # self.pause(1000) self.env.core.apply_movement(movement) self.print_status() else: if len(self.env.core.movements()) == 0: self.env.core.apply_movement(None) self.print_status() after_move() self.render() else: x_old, y_old = self.move_start_case self.game[x_old][y_old] = self.images[current_player] self.move_candidates_case = [] if self.game[x][y] == self.images[current_player]: self.move_start_case = x, y self.move_candidates_case = self.env.core.neighbours(x, y) self.state = GameState.WAITING_SECOND_CLICK else: self.move_start_case = None self.move_candidates_case = [] self.state = GameState.WAITING_FIRST_CLICK self.render() return if self.env.core.game_over(): self.state = GameState.GAME_OVER self.print_status() def render_selected(self): if self.move_start_case is None: return current_player_num = self.__player_num__(self.env.core.current_player) x, y = self.move_start_case self.game[x][y] = self.images_selected[current_player_num] for (x_cand, y_cand) in self.move_candidates_case: self.game[x_cand][y_cand] = self.images_candidates[ current_player_num] def autoplay(self): """:cvar Game Autoplay between two IA players """ while self.state != GameState.GAME_OVER: current_player = self.player1 if self.__player_num__( self.env.core.current_player) == 1 else self.player2 movement = current_player.choose_action(self.env) self.env.core.apply_movement(movement) if self.env.core.game_over(): self.state = GameState.GAME_OVER self.print_status() self.pause(50) self.render() def simulate(self): """:cvar Game Autoplay between two IA players """ while self.state != GameState.GAME_OVER: current_player = self.player1 movement = current_player.choose_action(self.env) self.env.core.apply_movement(movement) if self.env.core.game_over(): self.state = GameState.GAME_OVER self.env.core.reset() def print_status(self): current_player, current_player_color = ( self.player1, PLAYER1_COLOR) if self.__player_num__( self.env.core.current_player) == 1 else (self.player2, PLAYER2_COLOR) player0_value = int(self.env.core.player_value(1)) # scores=f" {self.player1.name}({PLAYER1_COLOR}) : {player0_value} , {self.player2.name}({PLAYER2_COLOR}) : {-1*player0_value}" header = f"\t{self.player1.name}({PLAYER1_COLOR}): {player0_value} VS {self.player2.name}({PLAYER2_COLOR}): {-player0_value}" restart_message = "\n \tF2:Human vs AI F3:Human vs Human F4:AI vs AI " if self.env.core.game_over(): winner_id = self.env.core.leading_player() winner = self.__player_num__(winner_id) winner_player = self.player1 if winner == 1 else self.player2 winner_color = PLAYER1_COLOR if winner == 1 else PLAYER2_COLOR if player0_value == 0: current_message = f"\tDraw" else: current_message = f"\t{winner_player.name}({winner_color}) won!!" else: current_message = f"\t{current_player.name}({current_player_color}) playing ..." text = header + "\n" + current_message + "\n" + restart_message self.game.print(text) def pause(self, millis): self.game.pause(millis)
previous_row = r # Save last position previous_col = c def newgame(): global previous_row, previous_col, reversed_cards, match_count, attempts_count previous_row = previous_col = reversed_cards = match_count = attempts_count = 0 game[0][0] = game[0][1] = 1 game[0][2] = game[0][3] = 2 game[1][0] = game[1][1] = 3 game[1][2] = game[1][3] = 4 game[2][0] = game[2][1] = 5 game[2][2] = game[2][3] = 6 game[3][0] = game[3][1] = 7 game[3][2] = game[3][3] = 8 game.shuffle() game.print(MSG) game = Board(4, 4) game.cell_size = 130 game.margin_color = game.grid_color = "wheat1" game.cell_color = "wheat3" game.cell_spacing = 2 game.title = "Memory Game" game.create_output(background_color="wheat4", color="white") game.on_mouse_click = fnmouse game.on_key_press = fnkbd game.on_start = newgame game.show()
from game2dboard import Board b = Board(8, 8) b.title = "Playground!" b.margin = 21 b.cell_size = 74 b.cell_spacing = 1 b.background_image = "chess" b.create_output(color='gray20', background_color='AntiqueWhite3', font_size=10) b.on_mouse_click = lambda btn, r, c : b.print("Clicked on [{}][{}]".format(r, c)) b.print("Click me") b.show()
def margin_test(): b = Board(7, 1) _value = 19 b.margin = _value assert b.margin == _value b._setupUI() assert b.margin == _value b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() assert b.margin == _value
def margin_color_test(): b = Board(10, 10) _value = "dark gray" b.margin_color = _value assert b.margin_color == _value b._setupUI() assert b.margin_color == _value b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() assert b.margin_color == _value
from game2dboard import Board b = Board(2, 2) b[0][0] = 5 b[1][1] = 10 print(b) a = b.copy() print(a) b.title = "Hi!" b.grid_color = None b.on_mouse_click = lambda b, r, c: print(type(b), type(r), type(c)) b.show()
from game2dboard import Board def fntimer(): b.shuffle() b.start_timer(500) b = Board(3, 12) b[1][0] = 'H' b[1][1] = 'e' b[1][2] = 'l' b[1][3] = 'l' b[1][4] = 'o' b[1][5] = 'comma' b[1][6] = 'W' b[1][7] = 'o' b[1][8] = 'r' b[1][9] = 'l' b[1][10] = 'd' b[1][11] = 'excl' b.cell_size = (80, 100) b.title = "WTF????" b.margin = 10 b.cell_spacing = 3 b.grid_color = b.margin_color = "AntiqueWhite3" b.cell_color = "gray20" b.start_timer(2000) b.on_timer = fntimer b.show()
b[row][col] = IMGID if not b[row][col] else None elif btn == 3: b[row][col] = "fruit.png" def timerfn(): b.print(datetime.datetime.now().strftime("[H]: Help %H:%M:%S")) def startfn(): b[0][0] = 12 b[0][1] = "Hello" b[0][2] = b[4][1] = IMGID b[3][6] = "fruit.png" b = Board(5, 15) b.title = "Hello, World!" b.margin = 10 b.cell_spacing = 6 b.cell_size = (50, 40) b.margin_color = b.grid_color = "AntiqueWhite4" b.cell_color = "AntiqueWhite1" b.on_start = startfn b.on_key_press = kbdfn b.on_mouse_click = mousefn b.on_timer = timerfn b.start_timer(1000) b.create_output(color='gray20', background_color='AntiqueWhite3', font_size=10) b.show()
from game2dboard import Board b = Board(8, 8) b.title = "Playground!" b.margin = 0 b.cell_spacing = 1 b.cell_size = 80 b.margin_color = None b.grid_color = "red" b.cell_color = "green" # b.background_image = "chess" b.create_output(color='gray20', background_color='AntiqueWhite3', font_size=10) b.show()
from game2dboard import Board def mouse_fn(btn, row, col): # mouse calback function b[row][col] = 1 if not b[row][col] else 0 b = Board(3, 4) # 3 rows, 4 columns, filled w/ None b[0][0] = 0 b.title = "Click me!" b.cell_size = 120 b.cell_color = "honeydew" b.on_mouse_click = mouse_fn b.show()
def cursor_test(): b = Board(1, 20) _value = "watch" b.cursor = _value assert b.cursor == _value b._setupUI() assert b.cursor == _value b.on_timer = lambda: b.close() b.start_timer(TIME_TO_CLOSE) b.show() assert b.cursor == _value