import random
import nxt.locator
from nxt.sensor import *
from nxt.motor import *

# states: 40/20 or 120/70 (rounded)
# 
# actions: UP_UP, UP_DOWN, ...
# move by 10 degrees
# 
# reward: distance moved

class Environment:
    running = True

    SPEED = 50
    STEP_SIZE = 10

    NUM_ACTIONS = 4

    def __init__(self):
        self.state = [0,0]
        self.completion = self.RUNNING
        self.nxt = nxt.locator.find_one_brick(name = 'NXT')
        self.ultrasonic = Ultrasonic(nxt, PORT_1)

        self.motor_top = Motor(nxt, PORT_A)
        self.motor_bottom = Motor(nxt, PORT_B)

    def move(self, action):
        start_distance = ultrasonic.get_sample()
        print 'start distance: ' + start_distance
        
        if action == Action.UP_UP:
            motor_top.turn(SPEED, STEP_SIZE)
            motor_bottom.turn(SPEED, STEP_SIZE)
        else if action == Action.UP_DOWN:
            motor_top.turn(SPEED, STEP_SIZE)
            motor_bottom.turn(SPEED, 0 - STEP_SIZE)
        else if action == Action.DOWN_UP:
            motor_top.turn(SPEED, 0 - STEP_SIZE)
            motor_bottom.turn(SPEED, STEP_SIZE)
        else if action == Action.DOWN_DOWN:
            motor_top.turn(SPEED, 0 - STEP_SIZE)
            motor_bottom.turn(SPEED, 0 - STEP_SIZE)

        reward = ultrasonic.get_sample() - start_distance
        self.state = motor_top.

        print 'reward: ' + reward
        return reward

class Action:
    UP_UP = 0x1
    UP_DOWN = 0x2
    DOWN_UP = 0x3
    DOWN_DOWN = 0x4
@@ -235,16 +235,39 @@
 = True%0A%0A
+    REWARD_BAD = -100%0A%0A
     SPEE
@@ -314,16 +314,134 @@
 NS = 4%0A%0A
+    top_current = 0%0A    TOP_MIN = 0%0A    TOP_MAX = 84%0A%0A    bottom_current = 0%0A    BOTTOM_MIN = 0%0A    BOTTOM_MAX = 270%0A%0A
     def 
@@ -480,16 +480,17 @@
 te = %5B0,
+ 
 0%5D%0A     
@@ -501,33 +501,22 @@
 elf.
-completion = self.RUNNING
+running = True
 %0A   
@@ -562,20 +562,8 @@
 ick(
-name = 'NXT'
 )%0A  
@@ -886,37 +886,39 @@
         
-motor_top.turn(SPEED,
+top_new = top_current +
  STEP_SI
@@ -911,33 +911,32 @@
 rent + STEP_SIZE
-)
 %0A            mot
@@ -936,32 +936,37 @@
     
-motor_bottom.turn(SPEED,
+bottom_new = bottom_current +
  STE
@@ -963,33 +963,32 @@
 rent + STEP_SIZE
-)
 %0A        else if
@@ -1026,37 +1026,39 @@
         
-motor_top.turn(SPEED,
+top_new = top_current +
  STEP_SI
@@ -1051,33 +1051,32 @@
 rent + STEP_SIZE
-)
 %0A            mot
@@ -1076,34 +1076,35 @@
     
-motor_bottom.turn(SPEED, 0
+bottom_new = bottom_current
  - S
@@ -1103,33 +1103,32 @@
 rent - STEP_SIZE
-)
 %0A        else if
@@ -1166,39 +1166,37 @@
         
-motor_top.turn(SPEED, 0
+top_new = top_current
  - STEP_
@@ -1191,33 +1191,32 @@
 rent - STEP_SIZE
-)
 %0A            mot
@@ -1216,32 +1216,37 @@
     
-motor_bottom.turn(SPEED,
+bottom_new = bottom_current +
  STE
@@ -1243,33 +1243,32 @@
 rent + STEP_SIZE
-)
 %0A        else if
@@ -1312,43 +1312,182 @@
     
-motor_top.turn(SPEED, 0 - STEP_SIZE
+top_new = top_current - STEP_SIZE%0A            bottom_new = bottom_current - STEP_SIZE%0A%0A        if check_bounds(top_new, bottom_new):%0A            motor_top.turn(SPEED, top_new
 )%0A  
@@ -1525,24 +1525,24 @@
 ED, 
-0 - STEP_SIZE)%0A%0A
+bottom_new)%0A    
     
@@ -1607,88 +1607,427 @@
     
-self.state = motor_top.%0A%0A        print 'reward: ' + reward%0A        return reward
+else:%0A            print 'out of bounds'%0A            reward = REWARD_BAD%0A%0A        self.state = round(top_current / 10) + %22/%22 + round(bottom_current / 10)%0A%0A        print 'reward: ' + reward%0A        return reward%0A%0A    def check_bounds(self, top_new, bottom_new):%0A        if top_new %3C TOP_MIN or top_new %3E TOP_MAX: return False%0A        if bottom_new %3C BOTTOM_MIN or bottom_new %3E BOTTOM_MAX: return False%0A        return True
 %0A%0Acl

Add bounds