def game_loop(screen: Surface, board: BoardState, ai: AI): grid_size = screen.get_size()[0] // 8 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN: if event.key == pygame.K_s: with open('save', 'wb') as f: f.write(pickle.dumps(board)) if event.key == pygame.K_l: with open('save', 'rb') as f: board = pickle.loads(f.read()) if board.ended(): continue if board.current_player == -1: pygame.display.set_caption(CAPTION + ' [Computing...]') start_time = time.time() new_board = ai.next_move(board) finish_time = time.time() elp_time = finish_time - start_time sleep_time = MOVE_TIME - elp_time if sleep_time > 0: time.sleep(sleep_time) pygame.display.set_caption(CAPTION) if new_board is not None: board = new_board if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mouse_click_position = event.pos if event.type == pygame.MOUSEBUTTONUP and event.button == 1: new_x, new_y = [p // grid_size for p in event.pos] old_x, old_y = [p // grid_size for p in mouse_click_position] new_board = board.do_move(old_x, old_y, new_x, new_y) if new_board is not None: board = new_board draw_board(screen, 0, 0, grid_size, board) pygame.display.flip()
def game_loop(screen: Surface, board: BoardState, ai: AI): grid_size = screen.get_size()[0] // game_set.board_size while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.MOUSEBUTTONUP: x, y = [p // grid_size for p in event.pos] if event.button == 1: # do move board.notification = None board = board.do_move(y, x) elif event.button == 3: # change figure board.board[y, x] = ((board.board[y, x] + 2) % 3) - 1 if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: board = BoardState.initial_state() if event.key == pygame.K_s: board.save() if event.key == pygame.K_l: board = BoardState.load() if event.key == pygame.K_z: pass if (game_set.is_ai_enabled and event.key == pygame.K_SPACE and game_set.first_player_is_ai == board.is_first_player_turn): new_board = ai.next_move(board, None)[0] if new_board is not None: board = new_board if board.is_game_finished: board.notification = "Second" if board.is_first_player_turn \ else "First" board.notification += " player won! Press R to restart" draw_board(screen, 0, 0, grid_size, board) pygame.display.flip()
def game_loop(screen: Surface, board: BoardState, ai: AI): grid_size = screen.get_size()[0] // 8 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mouse_click_position = event.pos if event.type == pygame.MOUSEBUTTONUP and event.button == 1: new_x, new_y = [p // grid_size for p in event.pos] old_x, old_y = [p // grid_size for p in mouse_click_position] new_board = board.do_move(old_x, old_y, new_x, new_y) if new_board is not None: board = new_board if event.type == pygame.MOUSEBUTTONUP and event.button == 3: x, y = [p // grid_size for p in event.pos] board.board[y, x] = (board.board[y, x] + 1 + 2) % 5 - 2 # change figure if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: board = board.inverted() if event.key == pygame.K_s: board.save_board("checkers.txt") if event.key == pygame.K_l: board.open_saved_board("checkers.txt") if event.key == pygame.K_SPACE: new_board = board.inverted() while True: new_board = ai.next_move(new_board) if new_board is None or new_board.in_the_process_of_taking == False: break if new_board is not None: board = new_board.inverted() draw_board(screen, 0, 0, grid_size, board) pygame.display.flip()
def next_move(self, board: BoardState, move, current_depth=0) -> Optional[BoardState]: maximize = True if current_depth % 2 == 0 else False if maximize: player = board.current_player limit_score = -100000 elif not maximize: player = board.current_player * (-1) limit_score = 100000 if board == None: if maximize: return 1000000 elif not maximize: return -1000000 # new_board = board.copy() if current_depth == self.search_depth: return self.position_evaluation(board, move, player) if current_depth == 0: highY, leftX, lowY, rightX = Restriction_first(board) else: highY, leftX, lowY, rightX = Restriction_first(board) moves = [(y, x) for y in range(highY, lowY + 1) for x in range(leftX, rightX + 1) if board.board[y, x] == 0] if len(moves) == 0: return -1000000 evolution_tree = [] for move in moves: try: new_board = board.do_move(move[1], move[0]) current_score = self.position_evaluation( new_board, move, player) # player = кто только что сделала ход move if (maximize and current_score >= limit_score) or ( not maximize and current_score <= limit_score ): # todo : may be to fix coeficient limit_score = current_score if current_depth == 0: evolution_tree.append( (self.next_move(new_board, move, current_depth + 1), move)) else: evolution_tree.append( self.next_move(new_board, move, current_depth + 1)) except EndGame as EG: if current_depth == 0: return self.position_evaluation(EG.board, move, player), move else: return self.position_evaluation(EG.board, move, player) if maximize: board.board[move[0], move[1]] = 0 return max(evolution_tree) elif not maximize: board.board[move[0], move[1]] = 0 return min(evolution_tree)
def game_loop(screen: Surface, board: BoardState, ai: AI): grid_size = screen.get_size()[0] // 8 previous_board = board visible_board = board step = 0 flag = True while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mouse_click_position = event.pos if event.type == pygame.MOUSEBUTTONUP and event.button == 1: new_x, new_y = [p // grid_size for p in event.pos] old_x, old_y = [p // grid_size for p in mouse_click_position] new_board = board.do_move(old_x, old_y, new_x, new_y) if new_board is not None: previous_board = board board = new_board.inverted() if step % 2 == 0: visible_board = new_board else: visible_board = board step += 1 if board.get_winner != 0: flag = False #if event.type == pygame.MOUSEBUTTONUP and event.button == 3: #x, y = [p // grid_size for p in event.pos] #board.board[y, x] = (board.board[y, x] + 1 + 2) % 5 - 2 # change figure if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: visible_board = visible_board.inverted() elif event.key == pygame.K_n: board = board.initial_state() visible_board = board step = 0 elif event.key == pygame.K_s: save_board(board, './src/save_board.bin') save_board(visible_board, './src/save_visible.bin') elif event.key == pygame.K_l: board = load_board('./src/save_board.bin') visible_board = load_board('./src/save_visible.bin') flag = True if board.current_player == visible_board.current_player: step = 0 else: step = 1 elif event.key == pygame.K_1: board = load_board( './src/test_boards/test_board_1_visible') visible_board = load_board( './src/test_boards/test_board_1_board') flag = True if board.current_player == visible_board.current_player: step = 0 else: step = 1 elif event.key == pygame.K_2: board = load_board( './src/test_boards/test_board_2_visible') visible_board = load_board( './src/test_boards/test_board_2_board') flag = True if board.current_player == visible_board.current_player: step = 0 else: step = 1 elif event.key == pygame.K_z: board = previous_board elif event.key == pygame.K_SPACE: new_board = ai.next_move(board) if new_board is not None: previous_board = board board = new_board.inverted() if step % 2 == 0: visible_board = new_board else: visible_board = board step += 1 if board.get_winner != 0: flag = False draw_board(screen, 0, 0, grid_size, visible_board) if not flag: draw_final_message(screen, board.get_winner) pygame.display.flip()
def game_loop(screen: Surface, board: BoardState, ai: AI): max_y, max_x = screen.get_size()[1], screen.get_size()[0] elem_size = max_y // 16 # размер одной клетки save_box = InputBox('SAVE', max_y + (max_x - max_y) * 625 // 10000, max_y * 4 // 5, 250, 30) load_box = InputBox('LOAD', max_y + (max_x - max_y) * 625 // 10000, max_y * 4 // 5 + 50, 250, 30) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: return save_name = save_box.handle_event(event) load_name = load_box.handle_event(event) if save_name: db = shelve.open(save_name) db[save_name] = board db.close() elif load_name: if os.path.exists(os.path.join(os.getcwd(), load_name + '.db')): db = shelve.open(load_name) board = db[load_name] db.close() else: print('No such file:(') if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mouse_click_position = event.pos to_x, to_y = [(p - elem_size // 2) // elem_size for p in mouse_click_position] try: new_board = board.do_move(to_x, to_y) except EndGame as EG: print(EG.winner, 'bruh') board = EG.board board.current_player *= 2 new_board = None # draw_board(screen, elem_size, elem_size, elem_size, board) # screen.fill((23, 123, 234)) # pygame.display.flip() # print('--------------') # print(new_board) # pygame.time.wait(1000) # return # screen.fill(color) # screen.blit(InputBox.FONT.render(('black' if EG.winner == 1 else 'white') + 'player won!!!', True, (0, 0, 0)), (0.7 * screen.get_size()[0], 0.2 * screen.get_size()[1])) # time.sleep(3) # return if new_board is not None: board = new_board if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: print('kek') # print(ai.next_move(board)) again = True while again: try: position_evalution, *move = ai.next_move(board, (0, 0)) again = False except TypeError: pass # print(position_evalution) # print(move) # print((ai.next_move(board))) print( '+=+=+=++=++++++++=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=' ) # break move = move[0] print(f'my move = {move}') try: new_board = board.do_move(move[1], move[0]) except EndGame as EG: print(EG.winner, 'bruh') board = EG.board board.current_player *= 2 new_board = None if new_board is not None: board = new_board save_box.update() load_box.update() draw_board(screen, elem_size, elem_size, elem_size, board) save_box.draw(screen) load_box.draw(screen) pygame.display.flip()
def next_move(self, board: BoardState, move, current_depth=0) -> Optional[BoardState]: maximize = True if current_depth % 2 == 0 else False if maximize: player = board.current_player limit_score = -100000 elif not maximize: player = board.current_player * (-1) limit_score = 100000 if board == None: if maximize: return 1000000 elif not maximize: return -1000000 # new_board = board.copy() if current_depth == self.search_depth: # print(f'depth = {current_depth}') return self.position_evaluation(board, move, player) if current_depth == 0: highY, leftX, lowY, rightX = Restriction_first(board) # # print('rest_arr first = \n', rest_arr) # # print(f'delta_y = {delta_y}, delta_x = {delta_x}') else: highY, leftX, lowY, rightX = Restriction_first(board) # delta_y, delta_x, rest_arr = Restriction_first(new_board) # # print('rest_ arr = \n', rest_arr) # # print(f'delta_y = {delta_y}, delta_x = {delta_x}') moves = [(y, x) for y in range(highY, lowY+1) for x in range(leftX, rightX+1) if board.board[y, x] == 0] if len(moves) == 0: # if maximize: # return 1000000 # elif not maximize: return -1000000 # print(current_depth) # print("moves", moves) evolution_tree = [] for move in moves: try: # print('move', move) # # print(f'current depth = {current_depth}, self.depth = {self.search_depth}') new_board = board.do_move(move[1], move[0]) # if type(new_board) == 'NoneType': # # print(new_board) # if new_board == None: # if maximize: # return 10000 # elif not maximize: # return -10000 current_score = self.position_evaluation(new_board, move, player) # player = кто только что сделала ход move # print('move = ', move, 'score = ', current_score) if (maximize and current_score >= limit_score) or (not maximize and current_score <= limit_score): # todo : may be to fix coeficient # if (current_score >= limit_score): # todo : may be to fix coeficient limit_score = current_score if current_depth == 0: # # print('move = ', move, ) evolution_tree.append((self.next_move(new_board, move, current_depth + 1), move)) else: # # print(f'type = {type(board.do_move(move[0], move[1]))}') evolution_tree.append(self.next_move(new_board, move, current_depth + 1)) except EndGame as EG: if current_depth == 0: return self.position_evaluation(EG.board, move, player), move else: return self.position_evaluation(EG.board, move, player) if maximize: # print(f'current depth = {current_depth}, evoltion = {max(evolution_tree)}') # print(f'max = {max(evolution_tree)}') board.board[move[0], move[1]] = 0 return max(evolution_tree) elif not maximize: # print(f'current depth = {current_depth}, evoltion = {min(evolution_tree)}') # print(f'min = {min(evolution_tree)}') board.board[move[0], move[1]] = 0 return min(evolution_tree)
def game_loop(screen: Surface, board: BoardState, ai: AI): grid_size = screen.get_size()[0] // 8 # размер одной клетки count = -1 while True: # print(pygame.event.get()) for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: mouse_click_position = event.pos # print(f'event.pos = {event.pos}') if event.type == pygame.MOUSEBUTTONUP and event.button == 1: new_x, new_y = [p // grid_size for p in event.pos] old_x, old_y = [p // grid_size for p in mouse_click_position] try: new_board = board.do_move(old_x, old_y, new_x, new_y) except EndGame: return if new_board is not None: board = new_board if event.type == pygame.MOUSEBUTTONUP and event.button == 3: # ???????????????????????? x, y = [p // grid_size for p in event.pos] board.board[ y, x] = (board.board[y, x] + 1 + 2) % 5 - 2 # change figure if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: board = board.inverted() elif event.key == pygame.K_SPACE: new_board = ai.next_move(board) if new_board is not None: board = new_board elif event.key == pygame.K_s: count += 1 count %= 5 name = f'game{count}' # name = input_name() db = shelve.open(name) db[name] = board db.close() # todo elif event.key == pygame.K_l: load = True elif event.key in (pygame.K_0, pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4) and load: name = 'game' + (chr(event.key)) if os.path.exists(os.path.join(os.getcwd(), name + '.db')): db = shelve.open(name) board = db[name] db.close() load = False else: print('No such file:(') draw_board(screen, 0, 0, grid_size, board) pygame.display.flip()