now can rotate the pieces

master
kirbylife 2019-11-06 18:46:00 -06:00
parent 3db11fe531
commit 9ab42452da
1 changed files with 38 additions and 8 deletions

View File

@ -34,6 +34,13 @@ class Afutc:
self.board_center = WIDTH // 2
self.current_piece = None
self.char = ""
self.debug = ""
def is_valid(self, board):
for field in (board + self.board).flat:
if field == 2:
return True
return False
def get_size(self):
return self.stdscr.getmaxyx()
@ -48,11 +55,11 @@ class Afutc:
print(f"{row}")
for n, row in enumerate(self.board + self.current_move):
# row = "".join("" if field else " " for field in row)
row = "".join("" if field else " " for field in row)
row = "".join(self.char if field else " " for field in row)
print(f"{row}")
row = "" * (self.board.shape[1])
# print(f"╚{row}╝")
print(str(self.pivot))
print(str(self.pivot) + "\t" + self.debug)
except CursesException:
self.pause()
@ -80,7 +87,7 @@ class Afutc:
temp_board = temp_board[:-1, :]
new_row = np.zeros(shape=(1, WIDTH))
temp_board = np.concatenate((new_row, temp_board))
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
if self.is_valid(temp_board):
self.board = self.board + self.current_move
self.new_random_piece()
else:
@ -91,7 +98,7 @@ class Afutc:
temp_board = temp_board[:, 1:]
new_column = np.zeros(shape=(HEIGHT, 1))
temp_board = np.concatenate((temp_board, new_column), axis=1)
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
if self.is_valid(temp_board):
return
self.current_move = temp_board
self.pivot[1] -= 1
@ -101,11 +108,36 @@ class Afutc:
temp_board = temp_board[:, :-1]
new_column = np.zeros(shape=(HEIGHT, 1))
temp_board = np.concatenate((new_column, temp_board), axis=1)
if any(filter(lambda x: x == 2, (temp_board + self.board).flat)):
if self.is_valid(temp_board):
return
self.current_move = temp_board
self.pivot[1] += 1
def rotate(self):
if not self.debug:
self.debug = "-1"
self.debug = str(int(self.debug) + 1)
temp_board = self.current_move.copy()
temp_board.fill(0.)
temp_piece = self.current_piece.copy()
pivot = self.pivot[:]
# Magic to rotate a matrix !!!!!!
temp_piece = np.array(list(zip(*reversed(temp_piece))))
try:
for i, row in enumerate(temp_piece):
for j, value in enumerate(row):
temp_board[i + pivot[0], j + pivot[1]] = value
except IndexError:
if pivot[1] > 0:
self.pivot[1] -= 1
self.rotate()
return
if not self.is_valid(temp_board):
self.current_move = temp_board.copy()
self.current_piece = temp_piece.copy()
def start(self):
scr = self.stdscr
input = self.input
@ -123,15 +155,13 @@ class Afutc:
elif char == "q":
self.running = False
elif char == "C": # Right
self.char = char
self.move("right")
elif char == "D": # Left
self.char = char
self.move("left")
elif char == "B": # Down
self.move("down")
elif char == "A": # Rotate
pass
self.rotate()
elif char == " ": # Rotate
self.pause()