def genmove(self, player): if winner(self.board) or winner(flip(self.board)): raise GtpException('Game is over') self.history.append(self.board) if player == 'black': # TODO: reuse previous calculations from the MCTS predictor = TreeSearchPredictor(self.config.search_config, self.model, self.board, not self.history) predictor.run(self.config.iterations) value, probabilities = predictor.predict() move = best_move(probabilities) self.board = flip(make_move(self.board, move)) elif player == 'white': predictor = TreeSearchPredictor(self.config.search_config, self.model, flip(self.board), not self.history) predictor.run(self.config.iterations) value, probabilities = predictor.predict() move = best_move(probabilities) self.board = make_move(flip(self.board), move) move = flip_move(move) else: self.history.pop() raise GtpException('Player is invalid') print('Estimated value: %.2f' % value, file=sys.stderr) return write_move(move)
def play(self, player, move): move = read_move(move) self.history.append(numpy.copy(self.board)) if player == 'black': self.board = flip(make_move(self.board, move)) elif player == 'white': self.board = make_move(flip(self.board), flip_move(move)) else: self.history.pop() raise GtpException('Player is invalid') return ''
def main(playmode): view = GameView() board, score = new_game(4) view.draw(board, score) clock = pygame.time.Clock() while True: clock.tick(20) # limit fps redraw = False # get move from player for event in pygame.event.get(): if event.type == QUIT: print("Exiting...") return if event.type == KEYDOWN and playmode: if event.key == K_LEFT: score += make_move(board, 'left') redraw = True if event.key == K_RIGHT: score += make_move(board, 'right') redraw = True if event.key == K_UP: score += make_move(board, 'up') redraw = True if event.key == K_DOWN: score += make_move(board, 'down') redraw = True if redraw: view.draw(board, score) # get move from ai if not playmode: best_move = game_ai.expectimax_move((board, score), 'gradient') # make the move and redraw score += make_move(board, best_move) view.draw(board, score) pygame.time.wait(0) # check if it is game over if len(possible_moves(board)) == 0: print("Game over! Score: " + str(score) + " Max square: " + str(max_square(board))) print("Starting new game...\n") pygame.time.wait(3000) pygame.event.clear() board, score = new_game(4) view.draw(board, score)
def main(playmode): view = GameView() board, score = new_game(4) view.draw(board,score) clock = pygame.time.Clock() while True: clock.tick(20) # limit fps redraw = False # get move from player for event in pygame.event.get(): if event.type == QUIT: print("Exiting...") return if event.type == KEYDOWN and playmode: if event.key == K_LEFT: score += make_move(board, 'left') redraw = True if event.key == K_RIGHT: score += make_move(board, 'right') redraw = True if event.key == K_UP: score += make_move(board, 'up') redraw = True if event.key == K_DOWN: score += make_move(board, 'down') redraw = True if redraw: view.draw(board, score) # get move from ai if not playmode: best_move = game_ai.expectimax_move((board, score), 'gradient') # make the move and redraw score += make_move(board, best_move) view.draw(board, score) pygame.time.wait(0) # check if it is game over if len(possible_moves(board)) == 0: print("Game over! Score: " + str(score) + " Max square: " + str(max_square(board))) print("Starting new game...\n") pygame.time.wait(3000) pygame.event.clear() board, score = new_game(4) view.draw(board, score)
def generate(config, model_file, output_file): model = load_model(model_file) with open(output_file, 'ab') as fout: file_pos = fout.tell() # Truncate any partially written record fout.seek(file_pos - file_pos % record_size(config.size)) samples = 0 start_time = time.time() game_boards = numpy.array( [new_board(config.size) for i in range(config.batch_size)]) game_moves = [[] for i in range(config.batch_size)] while True: _values, priors = model.predict(game_boards) priors = numpy.reshape(priors, (-1, config.size, config.size)) for i in range(config.batch_size): probs = fix_probabilities(game_boards[i], priors[i]) move = sample_move(probs) game_moves[i].append(move) game_boards[i] = make_move(game_boards[i], move) if winner(game_boards[i]): samples += 1 board, won, visits = game_result(config, model, game_moves[i]) write_record(fout, board, won, visits) fout.flush() print_board(board, file=sys.stderr) print('Games: %d, Time per game: %.2fs' % (samples, (time.time() - start_time) / samples), file=sys.stderr) game_boards[i] = new_board(config.size) game_moves[i] = []
def play(): cur_player = input('Would you like to be X or O: ').upper() main_board = game.new_board() while True: game.print_board(main_board) move_str = input( 'Where would you like to place an %s? (e.g. 12 for 1st row, 2nd column) ' % cur_player) move_row = int(move_str[0]) - 1 move_col = int(move_str[1]) - 1 main_board = game.make_move(main_board, move_row, move_col, cur_player) if game.any_win(main_board, cur_player): game.print_board(main_board) print( 'Congratulations Player %s! Better luck next time player %s' % (cur_player, game.other_player(cur_player))) break elif game.board_full(main_board): game.print_board(main_board) print('No more moves possible. The game ended in a draw.') break cur_player = game.other_player(cur_player) if input('Would you like to play again? [y/n]') == 'y': play() else: print('Thanks for playing.') return 0
def play_game(is_learning): game.setup(1) rAll = 0 d = False j = 0 race_timer = 0 is_race_over = False while j < 99: j += 1 # choose an action by greedily (with noise) picking from Q table game.players[0].racers[1].draw_hand() s = game.current_observation() a1 = RL.choose_action(s) game.players[0].racers[1].select_move(a1) if not is_learning: game.print_cards_for_racer(game.players[0].racers[1]) game.players[0].racers[0].draw_hand() s2 = game.current_observation() a2 = RL.choose_action(s2) game.players[0].racers[0].select_move(a2) if not is_learning: game.print_cards_for_racer(game.players[0].racers[0]) result = game.make_move() if not is_learning: racers = [] for player in game.players: racers = [*racers, *player.racers] racers.sort() racers.reverse() game.draw_course(racers) if result: print(j, 'turns taken') r = 0 if result: r = 25 - j s3 = game.current_observation() if is_learning: RL.store_transition(s, a1, r, s2) RL.store_transition(s2, a2, r, s3) if (counter > 200) and (counter % 10 == 0): RL.learn() rAll += r if result: break rList.append(rAll)
def game_result(config, model, moves): last_move_index = len(moves) - 1 end = random.randint(0, last_move_index) board = new_board(config.size) for move in moves[:end]: board = make_move(board, move) predictor = TreeSearchPredictor(config.search_config, model, board, end == 0) predictor.run(config.iterations) return board, last_move_index % 2 == end % 2, predictor.visits()
def make_move(self, move): self.is_first_move = False self.board = make_move(self.board, move) if self.root.edges is not None: for edge in self.root.edges: if edge.node is None: continue if edge.move == move: self.root = edge.node return self._create_root()
async def visit(self, config, predictor, board, is_first_move): if self.node is None: self.value_priority = -1e6 value, priors = await predictor.predict(make_move( board, self.move)) self.node = Node(value, priors) else: self.value_priority = -(self.node.value + config.virtual_loss) / ( self.node.visits + config.virtual_loss) value = await self.node.visit(config, predictor, make_move(board, self.move), False) board[0, self.move[0], self.move[1]] = 0 self.value_priority = -self.node.value / self.node.visits # We basically hack this for the swap move. Swap is not implemented, but the search behaves as if it does. if is_first_move: value = abs(value) self.value_priority = -abs(self.value_priority) self.uct_priority = config.uct_factor * self.prior / (1 + self.node.visits) return value
def play_game(is_learning): game.setup(1) rAll = 0 d = False j = 0 race_timer = 0 is_race_over = False game.players[0].racers[1].draw_hand() while j < 99: j += 1 # choose an action by greedily (with noise) picking from Q table s = game.current_observation() a1 = RL.choose_action(str(s)) game.players[0].racers[1].select_move(a1) game.players[0].racers[0].draw_hand() s2 = game.current_observation() a2 = RL.choose_action(str(s2)) game.players[0].racers[0].select_move(a2) result = game.make_move() if not is_learning: racers = [] for player in game.players: racers = [*racers, *player.racers] racers.sort() racers.reverse() game.print_cards() game.draw_course(racers) if result: print(j, 'turns taken') r = 0 if result: r = 1 s3 = 'terminal' else: game.players[0].racers[1].draw_hand() s3 = game.current_observation() if is_learning: RL.learn(str(s2), a2, r, str((s3))) RL.learn(str(s), a1, r, str((s2))) rAll += r if result: break rList.append(rAll)
def gen_move(b, p, d): "Generation based on player(minimize) and computer(maximize)" best = MIN if not p else MAX move = (-1, -1) if d == MAX_DEPTH or game.is_full(b): best = evaluate(b) else: for i in range(3): for j in range(3): if game.is_placed(b, i, j): continue cp = game.cp_board(b) game.make_move(cp, i, j, p) score = gen_move(cp, not p, d + 1)[0] if p: if score < best: best = score move = (i, j) else: if score > best: best = score move = (i, j) return best, move
def return_opponent_moves_count(self, move, board): ''' Returns count of opponent moves after we perform our move :param move: My move coordinates (row, col) :param board: Playfield state :return: Number of available moves ''' # copy board because we don't want to change anything in original copied_board = copy.deepcopy(board) copied_board = game.make_move(copied_board, self.my_color, move) valid_moves = self.return_valid_moves(copied_board, self.opponent_color, 1) return len(valid_moves)
def click_fire(x, y, board): height = 10 x_fixed = (x - 450) // 40 y_fixed = height - y // 40 - 1 response = game.make_move(board, x_fixed, y_fixed) if response == 'miss': state["state"] = 3 board[y_fixed][x_fixed] = '*' game.game["boards"]["boardC_show"][y_fixed][x_fixed] = '*' game.game["player_last"] = "Missed" elif response == 'hit': state["state"] = 3 if game.check_sunk(board, x_fixed, y_fixed, "computer_ships"): game.game["player_last"] = "Hit and sunk" else: game.game["player_last"] = "Hit" board[y_fixed][x_fixed] = '$' game.game["boards"]["boardC_show"][y_fixed][x_fixed] = '$'
def ai_mode(number_of_games=1): """ Just let the AI play a number of times without showing the UI. """ scores = [] for i in range(number_of_games): board, score = new_game(4) possible = possible_moves(board) while possible: move = game_ai.expectimax_move((board, score), 'gradient') score += make_move(board, move) possible = possible_moves(board) scores.append(score) print("Game {0}: Score = {1}".format(i + 1, score)) print() print("Mean score = " + str(sum(scores) / float(len(scores))))
def ai_mode(number_of_games=1): """ Just let the AI play a number of times without showing the UI. """ scores = [] for i in range(number_of_games): board, score = new_game(4) possible = possible_moves(board) while possible: move = game_ai.expectimax_move((board, score), 'gradient') score += make_move(board, move) possible = possible_moves(board) scores.append(score) print("Game {0}: Score = {1}".format(i+1, score)) print() print("Mean score = " + str(sum(scores)/float(len(scores))))
game.players[0].racers[1].draw_hand() while j < 99: j += 1 # choose an action by greedily (with noise) picking from Q table s = game.current_observation() a1 = np.argmax(Q[s, :] + np.random.randn(1, 4) * (1. / (i + 1))) game.players[0].racers[1].select_move(a1) game.players[0].racers[0].draw_hand() s2 = game.current_observation() a2 = np.argmax(Q[s2, :] + np.random.randn(1, 4) * (1. / (i + 1))) game.players[0].racers[0].select_move(a2) result = game.make_move() r = 0 if result: r = 1 game.players[0].racers[1].draw_hand() s3 = game.current_observation() Q[s,a1] = Q[s,a1] + lr*(r+y*np.max(Q[s3,:]) - Q[s,a1]) Q[s2,a2] = Q[s2,a2] + lr*(r+y*np.max(Q[s3,:]) - Q[s2,a2]) rAll += r if result: break rList.append(rAll) print ('Score over time: ' + str(sum(rList) / num_episodes))
def getMove(self, *args): global position global game_type global eat_move global gargabe_pos global number_of_beaten_pieces global current_garbage turn_ended = False while(turn_ended == False and not rules.black_win(position) and not rules.white_win(position)): if current_player == 2 and game_type == "ai": # player is AI movestring = rules.make_move(position) current_move = [["1" + movestring[0] + movestring[1]], ["2" + movestring[0] + movestring[1]], ["1" + movestring[3] + movestring[4]], ["3" + movestring[3] + movestring[4]]] else: foundPoint, img = camera.detectGesture(position, board_fields) camera.draw_positions( position, board_fields, img) # x = "0" # print("enter piece position: ") # x = raw_input() # foundPoint = board_fields[x] # print(str(board_fields)) dist_min = 10000.0 closest_point = () closest_point_2 = () eat_move = "00" for points in board_fields: print("checking " + str(points) + ": " + str(board_fields[points])) # loop over all points and find closest # print(str(camera.getDistance( # board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]))) if (camera.getDistance(board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]) < dist_min): closest_point = (points, board_fields[points]) print("found lower distance: " + str(closest_point) + " with distance: " + str( camera.getDistance(board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]))) dist_min = camera.getDistance( board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]) print("Found field is: " + str(closest_point) + " for piece.") print("Please point to the target now...") time.sleep(3) # raw_input() current_piece = closest_point movestring = str(current_piece[0]) + " => ..." # lbl_moves.set_text(movestring) print("Getting target.") dist_min = 100000 # x = "0" # print("enter target position: ") # x = raw_input() # foundPoint = board_fields[x] foundPoint, _ = camera.detectGesture(position, board_fields) for points in board_fields: # print(str(board_fields[points])) # loop over all points and find closest # print(str(camera.getDistance( # board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]))) if (camera.getDistance(board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]) < dist_min): closest_point_2 = (points, board_fields[points]) # print("found lower distance: " + str(closest_point) + " with distance: " + str( # camera.getDistance(board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]))) dist_min = camera.getDistance( board_fields[points][0], board_fields[points][1], foundPoint[0], foundPoint[1]) current_target = closest_point_2 print("Found field is: " + str(current_target) + " for target.") current_move = [["1" + str(current_piece[0])], ["2" + str(current_piece[0])], ["1" + str(current_target[0])], ["3" + str(current_target[0])]] print("Current move is: " + str(current_move)) # move_str = str(current_move[0]) + str(current_target[0]) # validation_move = str(current_piece + current_target) movestring = str(current_piece[0]) + \ " " + str(current_target[0]) # print(str(position)) # lbl_moves.set_text(movestring) print("\n *** TEST *** \n") if (rules.Valid_move(position, movestring) == False) or (rules.verif_collor(position, movestring, "white") == False): print( "Move is invalid, please try again! Starting new detection...") # raw_input() time.sleep(5) return print("Move is valid.") if (rules.food_pos(movestring) != "00"): print("Player has to beat a piece!") eat_move = rules.food_pos(movestring) print("Food position: " + str(rules.food_pos(movestring))) position = rules.new_position(position, movestring) print("New position: " + str(position)) turn_ended = rules.turn_endet(position) print("Player endet turn: " + str(rules.turn_endet(position))) self.performMove(current_move) print("Detection finished.") # position = rules.new_position(position, movestring) print("before garbage: " + str(position)) if (eat_move != "00"): print("a piece was beaten, performing removal...") if (current_player == 1): current_move = [["1" + eat_move], ["2" + eat_move], ["1" + gargabe_pos[0]], ["3" + gargabe_pos[0]]] else: current_move = [["1" + eat_move], ["2" + eat_move], ["1" + gargabe_pos[1]], ["3" + gargabe_pos[1]]] # current_move = [["1" + eat_move], ["2" + # eat_move], ["1" + letters[number_of_beaten_pieces] + garbage[current_garbage]], ["3" + letters[number_of_beaten_pieces] + garbage[current_garbage]]] # number_of_beaten_pieces += 1 # if (letters[number_of_beaten_pieces] == 'F'): # number_of_beaten_pieces = 0 # current_garbage += 1 self.performMove(current_move) eat_move = "00" rules.print_position(position) self.changePlayer()
import game print('Random selection, 2 player game') game.setup(2) counter = 0 race_timer = 0 race_over = False while not race_over: if counter == 5: counter = 0 game.print_cards() for player in game.players: for racer in player.racers: racer.draw_hand() racer.select_move(0) race_over = game.make_move() print('Next Round') counter += 1 race_timer += 1 # sleep(2) game.print_cards() print('Finished in', race_timer, 'turns')
titleRect.center = (width // 2, 50) screen.blit(title, titleRect) # Check for AI move if user != player and not game_over and ai: if ai_turn: pygame.draw.rect(screen, BLACK, (0, 0, width, square_size)) title = "AI thinking..." title = largeFont.render(title, True, ai_color) titleRect = title.get_rect() titleRect.center = (width // 2, 50) screen.blit(title, titleRect) pygame.display.flip() time.sleep(0.5) (row, col) = C4.AI(board) board = C4.make_move(board, row, col) pygame.draw.rect(screen, BLACK, (0, 0, width, square_size)) title = "Your Turn..." title = largeFont.render(title, True, user_color) titleRect = title.get_rect() titleRect.center = (width // 2, 50) screen.blit(title, titleRect) pygame.display.flip() if C4.terminal(board): pygame.draw.rect(screen, BLACK, (0, 0, width, square_size)) pygame.display.flip() ai_turn = False else: ai_turn = True
def play_game(is_learning): game.setup(2) if not is_learning: racers = [] for player in game.players: racers = [*racers, *player.racers] racers.sort() racers.reverse() game.draw_course(racers) rAll = 0 d = False j = 0 race_timer = 0 is_race_over = False while j < 99: j += 1 s, a1 = play_racer_RL(game.players[0].racers[1], is_learning) s2, a2 = play_racer_RL(game.players[0].racers[0], is_learning) play_racer_max(game.players[1].racers[0]) play_racer_max(game.players[1].racers[1]) if not is_learning: game.print_cards_for_racer(game.players[1].racers[1]) game.print_cards_for_racer(game.players[1].racers[0]) result = game.make_move() if not is_learning: racers = [] for player in game.players: racers = [*racers, *player.racers] racers.sort() racers.reverse() game.draw_course(racers) if result: print(result.name, ' won,', j, 'turns taken') r = 0 if result and (result == game.players[0].racers[0] or result == game.players[0].racers[1]): r = 1 elif result: r = -1 s3 = game.current_observation() if is_learning: RL.store_transition(s, a1, r, s2) RL.store_transition(s2, a2, r, s3) if (counter > 500) and (counter % 10 == 0): RL.learn() rAll += r if result: break if rAll == 0: racers = [] for player in game.players: racers = [*racers, *player.racers] racers.sort() racers.reverse() game.print_cards() game.draw_course(racers) print(j) print() pass rList.append(rAll)
def move(user_id, players, user_name, turn, possible_moves, player1, player2, channel, cell): """Returns a draw, winner, or prints the new board after a player makes a move""" if channel['accepted_invite'] is False: res = { "response_type": "ephemeral", "text": "To start a game type '/ttt challenge @username'" } if channel['accepted_invite'] is True: if user_id in players: if turn == user_name: if cell in possible_moves: game_moves = channel['possible_moves'] if cell not in game_moves: res = { "response_type": "ephemeral", "text": "Sorry, that is not a legitimate move." } if cell in game_moves: game_board = game.make_move(cell, turn, channel['board']) channel['board'] = game_board game_moves.remove(cell) if len(game_moves) > 0 and game.is_winner( game_board, cell) is True: channel['board'] = [' '] * 9 channel['ongoing_game'] = False channel['accepted_invite'] = False channel['possible_moves'] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] res = { "response_type": "in_channel", "text": turn + " has won the game!\n" + "```" + game.print_board(game_board) + "```" } if len(game_moves) > 0 and game.is_winner( game_board, cell) is False: if turn == player1: channel['turn'] = player2 else: channel['turn'] = player1 res = { "response_type": "in_channel", "text": "You selected cell " + str(cell) + "\nIt is now " + channel['turn'] + "'s turn.\n" + "Here is the current gameboard:\n" + "```" + game.print_board(game_board) + "```" } if len(game_moves) == 0: channel['board'] = [' '] * 9 channel['ongoing_game'] = False channel['accepted_invite'] = False channel['possible_moves'] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] res = { "response_type": "in_channel", "text": "DRAW!\n " + "```" + game.print_board(game_board) + "```" } if turn != user_name: res = { "response_type": "ephemeral", "text": "Sorry, it is not your turn." } if user_id not in players: player1 = channel['player1'] player2 = channel['player2'] res = { "response_type": "in_channel", "text": "Sorry, this is a game between %s and %s. To end their game type '/ttt end'" % (player1, player2) } # if channel['game_ended'] is True: # res = { # "response_type": "ephemeral", # "text": "To start a new game type '/ttt challenge @username'" # } if channel['accepted_invite'] is False and channel['ongoing_game'] is True: res = { "response_type": "in_channel", "text": "Waiting for %s to accept a game. To cancel invite type '/ttt end'" % channel['player2'] } return res
player_letter, computer_letter = input_letter() turn = '' if random.randint(0, 1) == 1: turn = 'computer' else: turn = 'player' print(turn + ' play first\n') game_in_progress = True while game_in_progress: if turn == 'player': print_board(board) move = get_move(board) make_move(board, player_letter, move) if is_winner(board, player_letter): print_board(board) print("You win!!") game_in_progress = False else: if is_board_full(board): print_board(board) print('Draw') break else: turn = 'computer' else: move = get_computer_move(board, player_letter, computer_letter)