def doaction(action, piece): k = s = old = 0 goal = piece[2] + action[0] r_p = numpy.rot90(piece[1],action[1]) p = pt.getPiece() # if goal < 1: goal = 1 # elif goal > 9 and piece[0] == 1 and action[1] % 2: goal = 9 # elif goal > 8 and piece[0] == 1 and action[1] % 2 == 0: goal = 6 # elif goal > 8 and piece[0] != 1: goal = 8 if goal == p[2]: k = 1 if action[0] > 0 and goal != p[2]: pt.move('right') elif action[0] < 0 and goal != p[2]: pt.move('left') old = p[2] for i in range(len(p[1])): for j in range(len(p[1][i])): if r_p[i][j] != p[1][i][j]: s = 2 break if s == 2: break if s == 2: break if s == 2: pt.rotate('ccw') s = 0 else: s = 1 if k == 1 and s == 1: return 2 else: return 1
def play_game(state_list, action_list, Qtable): step = 0 # 合計ループ数 old_state = new_state = [] #- state flag = 0 reset() pt.init(0.5) # メインループ while(pt.alive()): ######################### # ループごとの解析、操作をここに書く if step % 180 == 0 and flag == 0: new_board = pt.getBoard() for i in range(len(new_board)): for j in range(len(new_board[i])): if new_board[i][j] > 0 and new_board[i][j] < 8: new_board[i][j] = 1 piece = pt.getPiece() new_state = [new_board, piece] if new_state != old_state and flag == 0: action, value = egreedy.search_action(new_state) if action == []: action = egreedy.eGreedy(new_state) old_state = new_state flag = 1 if step % 10 == 0 and flag == 1: flag = doaction(action, piece) if step % 100 == 0: flag = 2 # print(new_state) ######################### # 次のループへ pt.loop() if flag == 2: print pt.param.flag['update'] pt.move('down') if pt.param.flag['update']: print "update" pt.loop() flag = 0 if pt.param.flag['gameover']: alive = flag = 0 step += 1 # 終了 pt.quit()