|
|
@ -32,13 +32,14 @@ def attempt_rotate(piece, board, i, j): |
|
|
|
return piece |
|
|
|
|
|
|
|
|
|
|
|
def clear_rows(board): |
|
|
|
def clear_rows(board, score): |
|
|
|
clear_indices = np.where(np.all(board == 1, axis=1))[0] |
|
|
|
if clear_indices.size > 0: |
|
|
|
board = np.delete(board, clear_indices, axis=0) |
|
|
|
new_rows = np.zeros((len(clear_indices), board.shape[1])) |
|
|
|
board = np.vstack([new_rows, board]) |
|
|
|
return board |
|
|
|
score = update_score(score) |
|
|
|
return board, score |
|
|
|
|
|
|
|
|
|
|
|
def calculate_shadow(piece, board, i, j): |
|
|
@ -48,7 +49,7 @@ def calculate_shadow(piece, board, i, j): |
|
|
|
return i |
|
|
|
|
|
|
|
|
|
|
|
def display_board(board, piece, i, j): |
|
|
|
def display_board(board, piece, i, j, score): |
|
|
|
display = board.copy() |
|
|
|
shadow_i = calculate_shadow(piece, board, i, j) |
|
|
|
n, m = piece.shape |
|
|
@ -69,7 +70,7 @@ def display_board(board, piece, i, j): |
|
|
|
for row in display |
|
|
|
) |
|
|
|
) |
|
|
|
print() |
|
|
|
print(f"Score: {score}\n") |
|
|
|
|
|
|
|
|
|
|
|
def game_over(board): |
|
|
@ -78,9 +79,13 @@ def game_over(board): |
|
|
|
return np.zeros_like( |
|
|
|
board |
|
|
|
) # Clear the board and continue or call sys.exit() to end |
|
|
|
|
|
|
|
def update_score(score): |
|
|
|
return score + 1 |
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
|
|
score = 0 |
|
|
|
board = np.zeros((20, 10), dtype=int) |
|
|
|
piece_types = list(PIECES.keys()) |
|
|
|
current_piece = PIECES[np.random.choice(piece_types)] |
|
|
@ -89,7 +94,7 @@ def main(): |
|
|
|
auto_gravity = True # Auto-gravity enabled by default |
|
|
|
|
|
|
|
while True: |
|
|
|
display_board(board, current_piece, i, j) |
|
|
|
display_board(board, current_piece, i, j, score) |
|
|
|
if not check_placement(current_piece, board, *START_POS): |
|
|
|
board = game_over(board) |
|
|
|
continue |
|
|
@ -117,13 +122,13 @@ def main(): |
|
|
|
i += 1 |
|
|
|
else: |
|
|
|
board = place_piece(current_piece, board, i, j) |
|
|
|
board = clear_rows(board) |
|
|
|
board, score = clear_rows(board, score) |
|
|
|
current_piece = PIECES[np.random.choice(piece_types)] |
|
|
|
i, j = 0, 4 |
|
|
|
elif command == "S": |
|
|
|
i = calculate_shadow(current_piece, board, i, j) |
|
|
|
board = place_piece(current_piece, board, i, j) |
|
|
|
board = clear_rows(board) |
|
|
|
board, score = clear_rows(board, score) |
|
|
|
current_piece = PIECES[np.random.choice(piece_types)] |
|
|
|
i, j = START_POS |
|
|
|
elif command == "G": |
|
|
@ -134,7 +139,7 @@ def main(): |
|
|
|
i += 1 |
|
|
|
else: |
|
|
|
board = place_piece(current_piece, board, i, j) |
|
|
|
board = clear_rows(board) |
|
|
|
board, score = clear_rows(board, score) |
|
|
|
current_piece = PIECES[np.random.choice(piece_types)] |
|
|
|
i, j = START_POS |
|
|
|
|
|
|
|