now can rotate the pieces
parent
3db11fe531
commit
9ab42452da
|
@ -34,6 +34,13 @@ class Afutc:
|
||||||
self.board_center = WIDTH // 2
|
self.board_center = WIDTH // 2
|
||||||
self.current_piece = None
|
self.current_piece = None
|
||||||
self.char = "▣"
|
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):
|
def get_size(self):
|
||||||
return self.stdscr.getmaxyx()
|
return self.stdscr.getmaxyx()
|
||||||
|
@ -48,11 +55,11 @@ class Afutc:
|
||||||
print(f"╔{row}╗")
|
print(f"╔{row}╗")
|
||||||
for n, row in enumerate(self.board + self.current_move):
|
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("▢" if field else " " for field in row)
|
row = "".join(self.char if field else " " for field in row)
|
||||||
print(f"║{row}║")
|
print(f"║{row}║")
|
||||||
row = "═" * (self.board.shape[1])
|
row = "═" * (self.board.shape[1])
|
||||||
# print(f"╚{row}╝")
|
# print(f"╚{row}╝")
|
||||||
print(str(self.pivot))
|
print(str(self.pivot) + "\t" + self.debug)
|
||||||
except CursesException:
|
except CursesException:
|
||||||
self.pause()
|
self.pause()
|
||||||
|
|
||||||
|
@ -80,7 +87,7 @@ class Afutc:
|
||||||
temp_board = temp_board[:-1, :]
|
temp_board = temp_board[:-1, :]
|
||||||
new_row = np.zeros(shape=(1, WIDTH))
|
new_row = np.zeros(shape=(1, WIDTH))
|
||||||
temp_board = np.concatenate((new_row, temp_board))
|
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.board = self.board + self.current_move
|
||||||
self.new_random_piece()
|
self.new_random_piece()
|
||||||
else:
|
else:
|
||||||
|
@ -91,7 +98,7 @@ class Afutc:
|
||||||
temp_board = temp_board[:, 1:]
|
temp_board = temp_board[:, 1:]
|
||||||
new_column = np.zeros(shape=(HEIGHT, 1))
|
new_column = np.zeros(shape=(HEIGHT, 1))
|
||||||
temp_board = np.concatenate((temp_board, new_column), axis=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
|
return
|
||||||
self.current_move = temp_board
|
self.current_move = temp_board
|
||||||
self.pivot[1] -= 1
|
self.pivot[1] -= 1
|
||||||
|
@ -101,11 +108,36 @@ class Afutc:
|
||||||
temp_board = temp_board[:, :-1]
|
temp_board = temp_board[:, :-1]
|
||||||
new_column = np.zeros(shape=(HEIGHT, 1))
|
new_column = np.zeros(shape=(HEIGHT, 1))
|
||||||
temp_board = np.concatenate((new_column, temp_board), axis=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
|
return
|
||||||
self.current_move = temp_board
|
self.current_move = temp_board
|
||||||
self.pivot[1] += 1
|
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):
|
def start(self):
|
||||||
scr = self.stdscr
|
scr = self.stdscr
|
||||||
input = self.input
|
input = self.input
|
||||||
|
@ -123,15 +155,13 @@ class Afutc:
|
||||||
elif char == "q":
|
elif char == "q":
|
||||||
self.running = False
|
self.running = False
|
||||||
elif char == "C": # Right
|
elif char == "C": # Right
|
||||||
self.char = char
|
|
||||||
self.move("right")
|
self.move("right")
|
||||||
elif char == "D": # Left
|
elif char == "D": # Left
|
||||||
self.char = char
|
|
||||||
self.move("left")
|
self.move("left")
|
||||||
elif char == "B": # Down
|
elif char == "B": # Down
|
||||||
self.move("down")
|
self.move("down")
|
||||||
elif char == "A": # Rotate
|
elif char == "A": # Rotate
|
||||||
pass
|
self.rotate()
|
||||||
elif char == " ": # Rotate
|
elif char == " ": # Rotate
|
||||||
self.pause()
|
self.pause()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue