Forráskód Böngészése

bot is a bit smarter now

Alexey Dorokhov 3 éve
szülő
commit
12ff7d55cd
2 módosított fájl, 42 hozzáadás és 14 törlés
  1. 41 14
      bot_example.py
  2. 1 0
      competitive_tetris.py

+ 41 - 14
bot_example.py

@@ -20,6 +20,18 @@ import game_state
 def dummy_next_piece_func():
     return 0
 
+def row_strength(field, row):
+    strength = 0
+    bonus = 1
+    for col in range(field.shape[1]):
+        if field[row][col] > 0:
+            bonus = bonus * 2
+            strength += bonus
+        else:
+            bonus = 1
+    return strength
+
+
 def cell_strength(field, row, col):
     neighbour_count = 0
     for r in range(row-1, row+2):
@@ -36,11 +48,25 @@ def cell_strength(field, row, col):
         return -neighbour_count * row
 
 
+def has_top_neighbour(field, row, col):
+    for r in range(0,row):
+        if field[r][col] > 0:
+            return True
+    return False
+
+
 def field_heuristic(field):
     result = 0.0
     for r in range(field.shape[0]):
+        result += row_strength(field, r)
         for c in range(field.shape[1]):
             result += cell_strength(field, r, c)
+
+    for r in range(field.shape[0]):
+        for c in range(field.shape[1]):
+            if has_top_neighbour(field, r, c):
+                result -= 1000.0
+
     return result
 
 
@@ -86,7 +112,6 @@ def create_shifts_and_rotates(state):
             continue
 
         for col_offset in range(-game_state.FIELD_WIDTH, game_state.FIELD_WIDTH+1):
-
             next_state = shift_state(rotate_state, col_offset)
             if next_state is not None:
                 final_state, is_running = next_state.land().advance()
@@ -131,7 +156,7 @@ def main():
         rsp_buf = socket.recv()
         rsp = wire_protocol.CommandResponse.CommandResponse.GetRootAs(rsp_buf)
         if rsp.Error() != wire_protocol.CommandResponseError.CommandResponseError.OK:
-           return 
+           return
 
         grid = rsp.GameState().GridAsNumpy()
         grid = grid.reshape((rsp.GameState().Rows(), rsp.GameState().Cols()))
@@ -147,7 +172,6 @@ def main():
                 score = rsp.GameState().Score())
         state.next_piece_func = dummy_next_piece_func
 
-        millis_before_down = rsp.GameState().MillisBeforeDown()
 
         next_moves = create_shifts_and_rotates(state)
         if len(next_moves) > 0:
@@ -182,17 +206,20 @@ def main():
                 if rsp.Error() != wire_protocol.CommandResponseError.CommandResponseError.OK:
                     return
 
-            builder = flatbuffers.Builder()
-            wire_protocol.CommandRequest.Start(builder)
-            wire_protocol.CommandRequest.AddCommand(
-                    builder, wire_protocol.Command.Command.Land)
-            req = wire_protocol.CommandRequest.End(builder)
-            builder.Finish(req)
-            socket.send(builder.Output())
-            rsp_buf = socket.recv()
-            rsp = wire_protocol.CommandResponse.CommandResponse.GetRootAs(rsp_buf)
-            if rsp.Error() != wire_protocol.CommandResponseError.CommandResponseError.OK:
-                return
+
+            if best_move.col_offset == 0:
+                builder = flatbuffers.Builder()
+                wire_protocol.CommandRequest.Start(builder)
+                wire_protocol.CommandRequest.AddCommand(
+                        builder, wire_protocol.Command.Command.Down)
+                req = wire_protocol.CommandRequest.End(builder)
+                builder.Finish(req)
+                socket.send(builder.Output())
+                rsp_buf = socket.recv()
+                rsp = wire_protocol.CommandResponse.CommandResponse.GetRootAs(rsp_buf)
+                if (rsp.Error() != wire_protocol.CommandResponseError.CommandResponseError.OK and
+                    rsp.Error() != wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND):
+                    return
 
 if __name__ == "__main__":
     main()

+ 1 - 0
competitive_tetris.py

@@ -355,6 +355,7 @@ def server_loop(win):
 
 
 if __name__ == "__main__":
+    #random.seed(0)
     win = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
     pygame.display.set_caption("Tetris")
     server_loop(win)