Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gym
- import os
- import json
- import gym_checkers
- state_array = []
- action_array = []
- f = open('data.txt')
- line = f.readline()
- #define a conversion array used for the conversion from Directions to moves
- DIRECTION_MAP = [[-1,-1], [1,-1], [-1,1], [1,1]]
- # Load the reward config
- with open('config/rewards.json') as fp:
- reward_spec = json.load(fp=fp)
- # Create an environment to train on
- env = gym.make("Checkers-v0")
- env.set_reward_spec(reward_spec)
- counter = 0
- while line and counter < 16:
- # in python 2+
- # print line
- # in python 3 print is a builtin function, so
- print(line)
- buffer = ""
- select_action_buffer = None
- move_action_buffer = []
- while (line[0] == "[" or line[0] == ' '):
- line = f.readline()
- for index, char in enumerate(line):
- if index == 0 and (char == "[" or char == ' '):
- env.reset()
- if str.isdigit(char) and char != "-":
- buffer += char
- # New Move
- if char == ' ' and select_action_buffer != None:
- # Buffer verarbeiten
- env.step_select(select_action_buffer)
- print("Lenlen",len(move_action_buffer))
- for index_a, move_action in enumerate(move_action_buffer):
- if index_a == 0:
- sel_x, sel_y = select_action_to_coords(select_action_buffer)
- mov_x, mov_y = select_action_to_coords(move_action)
- action = move_to_direction(sel_x, sel_y, mov_x, mov_y)
- else:
- sel_x, sel_y = select_action_to_coords(move_action_buffer[index_a-1])
- mov_x, mov_y = select_action_to_coords(move_action)
- action = move_to_direction(sel_x, sel_y, mov_x, mov_y)
- print("Move action", action)
- env.step_move(action)
- env.render()
- # Empty the buffer
- buffer = ""
- select_action_buffer = None
- move_action_buffer = []
- elif char == '.':
- buffer = ""
- elif char == 'x' or char == "-":
- if select_action_buffer == None:
- select_action_buffer = 32 - int(buffer)
- buffer = ""
- else:
- move_action_buffer.append(32 - int(buffer))
- buffer = ""
- # use realine() to read next line
- line = f.readline()
- counter += 1
- f.close()
- def move_to_direction(selected_x,selected_y,move_x,move_y):
- """
- convert a Move to a field to a direction relationally to the piece
- Args:
- selected_x(int): x position of the selected field
- selected_y(int): y position of the selected field
- move_x(int): x position of the desired field
- move_y(int): y position of the desired field
- Return:
- direction(int): used in the board class
- """
- x = 0
- y = 0
- if (move_x-selected_x)>0:
- x=1
- elif (move_x-selected_x)<0:
- x=-1
- if (move_y-selected_y)>0 :
- y=1
- elif (move_y-selected_y)<0:
- y=-1
- dir= [x,y]
- for index, direction in enumerate(DIRECTION_MAP):
- if dir == direction:
- return index
- return -1
- def select_action_to_coords(action : int):
- """
- Converts a select action to coordinates.
- Args:
- action (int) : Select action
- Returns:
- (int) (int) : corresponding coordinates within the board
- """
- # Convert number to coordinates
- pos_x = action % 4
- pos_y = int((action-pos_x)/4)
- # Return the coordinates to the full field
- if pos_y % 2 == 1:
- pos_x *= 2
- else:
- pos_x *= 2
- pos_x += 1
- return pos_x, pos_y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement