Skip to content

Commit

Permalink
lock template
Browse files Browse the repository at this point in the history
  • Loading branch information
aeblyve committed Dec 3, 2021
1 parent 61b05f3 commit fd57ee8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Binary file modified code/bot/__pycache__/game.cpython-39.pyc
Binary file not shown.
33 changes: 25 additions & 8 deletions code/bot/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,42 @@ def move_anchor(self, x_shift, y_shift):

def is_legal(self):
"""Illegal if oob or intersecting"""
for i in range(len(self.piece)):
x, y, label = self.piece[i]
for x, y, label in self.piece:
if x < 0 or x >= len(self.grid[0]) or y < 0 or grid[y][x] != BLANK_LABEL:
return False
return True

def lock_piece(self, randomizer):
"""Fix the current piece to the board if possible, and get a new one"""
# TODO suboptimal

can_lock = False
for x, y, label in self.piece:
if y == len(self.grid) - 1 or self.grid[y + 1][x] != BLANK_LABEL:
can_lock = True
break
if can_lock and self.is_legal():
new_grid = []
for row in self.grid:
new_grid.append(list(row))
for x, y, label in self.piece:
new_grid[y][x] = label
for i in range(len(new_grid)):
new_grid[i] = tuple(new_grid[i])
new_grid = tuple(new_grid)
new_piece = randomizer.choice()
mapped_piece = shift_piece(new_piece, self.spawn[0], self.spawn[1])
return CheeseState(self.spawn, self.spawn, mapped_piece, new_grid)
else:
return CheeseState(self.spawn, self.anchor, self.piece, self.grid)

def __repr__(self):
rep = []
st = ""

for row in self.grid:
rep.append(list(row))
for i in range(len(self.piece)):
x, y, label = self.piece[i]
for x, y, label in self.piece:
if x in range(len(self.grid[0])) and y in range(len(self.grid)):
rep[y][x] = label
count = 0
Expand All @@ -131,8 +150,7 @@ def __repr__(self):
def rotate_left(self):
new_piece = []
anchor_x, anchor_y = self.anchor
for i in range(len(self.piece)):
x, y, label = self.piece[i]
for x, y, label in self.piece:
offset_x, offset_y = (x - anchor_x, y - anchor_y)
new_offset_x, new_offset_y = (offset_y, -1 * offset_x)
new_x, new_y = (new_offset_x + anchor_x, new_offset_y + anchor_y)
Expand All @@ -143,8 +161,7 @@ def rotate_left(self):
def rotate_right(self):
new_piece = []
anchor_x, anchor_y = self.anchor
for i in range(len(self.piece)):
x, y, label = self.piece[i]
for x, y, label in self.piece:
offset_x, offset_y = (x - anchor_x, y - anchor_y)
new_offset_x, new_offset_y = (-1 * offset_y, offset_x)
new_x, new_y = (new_offset_x + anchor_x, new_offset_y + anchor_y)
Expand Down

0 comments on commit fd57ee8

Please sign in to comment.