Beispiel #1
0
 def take_action(self, state):
     if state.last_move == 0:
         empty = [
             pos for pos, tile in [(i, state.state[i])
                                   for i in [12, 13, 14, 15]] if not tile
         ]
     elif state.last_move == 1:
         empty = [
             pos for pos, tile in [(i, state.state[i])
                                   for i in [0, 4, 8, 12]] if not tile
         ]
     elif state.last_move == 2:
         empty = [
             pos for pos, tile in [(i, state.state[i])
                                   for i in [0, 1, 2, 3]] if not tile
         ]
     elif state.last_move == 3:
         empty = [
             pos for pos, tile in [(i, state.state[i])
                                   for i in [3, 7, 11, 15]] if not tile
         ]
     else:
         empty = [pos for pos, tile in enumerate(state.state) if not tile]
     if empty:
         pos = self.choice(empty)
         if len(self.tile_bag) == 0:
             self.init_tile_bag()
         tile = self.choice(self.tile_bag)
         self.tile_bag.remove(tile)
         return action.place(pos, tile)
     else:
         return action()
Beispiel #2
0
 def take_action(self, state,a,b,c,slideway):
     if slideway==10:
         empty = [pos for pos, tile in enumerate(state.state) if not tile]
         #print (state.state)
     elif slideway==0:
         empty = [pos for pos, tile in enumerate(state.state[12:16]) if not tile]
         empty=[i+12 for i in empty]
         #print (state.state[12:16])
     elif slideway==1:
         empty = [pos for pos, tile in enumerate(state.state[0:4]) if not tile]
         #print (state.state[0:4])
     elif slideway==2:
         empty = [pos for pos, tile in enumerate(state.state[3::4]) if not tile]
         empty=[i*4+3 for i in empty]  
         #print (state.state[3::4]) 
     elif slideway==3:
         empty = [pos for pos, tile in enumerate(state.state[0::4]) if not tile]
         empty=[i*4 for i in empty]
         #print (state.state[0::4])            
     
     if empty:
         pos = self.choice(empty)
         tile = self.choice(a*[1]+b*[2]+c*[3])
         return action.place(pos, tile)
     else:
         return action()    
Beispiel #3
0
 def take_action(self, state):
     empty = [pos for pos, tile in enumerate(state.state) if not tile]
     if empty:
         pos = self.choice(empty)
         tile = self.choice([1] * 9 + [2])
         return False, action.place(pos, tile)
     else:
         return True, action()
Beispiel #4
0
 def take_action(self, state):
     empty = [pos for pos, tile in enumerate(state.state) if not tile]
     fil = [[12, 13, 14, 15], [0, 4, 8, 12], [0, 1, 2, 3], [3, 7, 11, 15]]
     if state.op is not None:
         empty = list(filter(lambda x: x in fil[state.op], empty))
     if empty:
         pos = self.choice(empty)
         if self.bag == []:
             self.bag = [1, 2, 3]
         tile = self.choice(self.bag)
         self.bag.remove(tile)
         return action.place(pos, tile)
     else:
         return action()
Beispiel #5
0
        self.ep_open = "N/A", 0 # flag, time usage
        self.ep_close = "N/A", 0 # flag, time usage
        return
    
    def initial_state(self):
        return board()
    
    def millisec(self):
        return int(round(time.time() * 1000))
        
    
if __name__ == '__main__':
    print('2048 Demo: episode.py\n')
    # action, reward, time usage
    moves = []
    moves += [(action.place(0,1), 0, 1)]
    moves += [(action.place(1,1), 0, 1)]
    moves += [(action.slide(3), 2, 1)]
    for mv in moves:
        print(str(mv[0]) + str(mv[1]) + str(mv[2]))
    print("".join([str(move[0]) + ("[" + str(move[1]) + "]" if move[1] else "") + ("(" + str(move[2]) + ")" if move[2] else "") for move in moves]))
    
    sio = io.StringIO("0123")
    print(sio.read(1))
    print(sio.read(1))
    print(sio.read(1))
    print(sio.read(1))
    print(sio.read(1) == "")
    
    line = "".join([str(move[0]) + ("[" + str(move[1]) + "]" if move[1] else "") + ("(" + str(move[2]) + ")" if move[2] else "") for move in moves])
    print(line)