Alexey Dorokhov пре 3 година
родитељ
комит
ae8845d318
5 измењених фајлова са 71 додато и 9 уклоњено
  1. 23 2
      bot_example.py
  2. 20 2
      competitive_tetris.py
  3. 22 1
      game_state.py
  4. 3 2
      wire_protocol.fbs
  5. 3 2
      wire_protocol/Command.py

+ 23 - 2
bot_example.py

@@ -52,6 +52,26 @@ class ShiftAndRotate:
         self.state = state
 
 
+def shift_state(state, shift):
+    if shift == 0:
+        return state
+
+    if shift < 0:
+        for _ in range(abs(shift)):
+            next_state = state.shift_left()
+            if next_state is None:
+                return None
+            state = next_state
+    else:
+        for _ in range(shift):
+            next_state = state.shift_right()
+            if next_state is None:
+                return None
+            state = next_state
+
+    return state
+
+
 def create_shifts_and_rotates(state):
     shift_and_rotates = []
 
@@ -65,8 +85,9 @@ def create_shifts_and_rotates(state):
         if rotate_state is None:
             continue
 
-        for col_offset in range(-game_state.FIELD_WIDTH, game_state.FIELD_HEIGHT+1):
-            next_state = rotate_state.shift(col_offset)
+        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()
                 if is_running:

+ 20 - 2
competitive_tetris.py

@@ -107,7 +107,7 @@ def draw_game_state_to_window(surface, state):
 
 def handle_shift_left(state):
     error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
-    next_state = state.shift(-1)
+    next_state = state.shift_left()
     if next_state is None:
         error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
     else:
@@ -123,7 +123,7 @@ def handle_shift_left(state):
 
 def handle_shift_right(state):
     error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
-    next_state = state.shift(1)
+    next_state = state.shift_right()
     if next_state is None:
         error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
     else:
@@ -153,6 +153,22 @@ def handle_rotate(state):
     return state, builder
 
 
+def handle_down(state):
+    error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
+    next_state = state.down()
+    if next_state is None:
+        error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
+    else:
+        state = next_state
+
+    builder = flatbuffers.Builder()
+    wire_protocol.CommandResponse.Start(builder)
+    wire_protocol.CommandResponse.AddError(builder, error_code)
+    rsp = wire_protocol.CommandResponse.End(builder)
+    builder.Finish(rsp)
+    return state, builder
+
+
 def handle_land(state):
     error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
     next_state = state.land()
@@ -246,6 +262,8 @@ def main(win):
                 state, builder = handle_shift_right(state)
             elif req.Command() == wire_protocol.Command.Command.Rotate:
                 state, builder = handle_rotate(state)
+            elif req.Command() == wire_protocol.Command.Command.Down:
+                state, builder = handle_down(state)
             elif req.Command() == wire_protocol.Command.Command.Land:
                 state, builder = handle_land(state)
             elif req.Command() == wire_protocol.Command.Command.GetGameState:

+ 22 - 1
game_state.py

@@ -67,7 +67,7 @@ class GameState:
         total_field = np.add(total_field, tetromino_array)
         return total_field
 
-    def shift(self, col_offset):
+    def _shift(self, col_offset):
         next_game_state = GameState()
         next_game_state.next_piece_func = self.next_piece_func
         next_game_state.data = GameStateData(
@@ -82,6 +82,12 @@ class GameState:
             return next_game_state
         return None
 
+    def shift_left(self):
+        return self._shift(-1)
+
+    def shift_right(self):
+        return self._shift(1)
+
     def rotate(self):
         next_game_state = GameState()
         next_game_state.next_piece_func = self.next_piece_func
@@ -135,6 +141,21 @@ class GameState:
                 score=next_game_state.data.score + drop_count*2)
         return next_game_state
 
+    def down(self):
+        next_game_state = GameState()
+        next_game_state.next_piece_func = self.next_piece_func
+        next_game_state.data = GameStateData(
+                grid=self.data.grid,
+                current_piece=self.data.current_piece,
+                next_piece=self.data.next_piece,
+                rotation=self.data.rotation,
+                row=self.data.row + 1,
+                col=self.data.col,
+                score=self.data.score + 1)
+        if next_game_state.is_valid():
+            return next_game_state
+        return None
+
     def _advance_down(self):
         next_game_state = GameState()
         next_game_state.next_piece_func = self.next_piece_func

+ 3 - 2
wire_protocol.fbs

@@ -4,8 +4,9 @@ enum Command : short {
   ShiftLeft = 0,
   ShiftRight = 1,
   Rotate = 2,
-  Land = 3,
-  GetGameState = 4,
+  Down = 3,
+  Land = 4,
+  GetGameState = 5,
 }
 
 enum CommandResponseError : short {

+ 3 - 2
wire_protocol/Command.py

@@ -6,5 +6,6 @@ class Command(object):
     ShiftLeft = 0
     ShiftRight = 1
     Rotate = 2
-    Land = 3
-    GetGameState = 4
+    Down = 3
+    Land = 4
+    GetGameState = 5