mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-19 00:34:30 +01:00
feat: add board rotating
This commit is contained in:
parent
010ed1e7f5
commit
c8e0b10590
43
lab2/main.py
43
lab2/main.py
@ -155,11 +155,20 @@ class Game:
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def print_board(self):
|
def print_board(self, rotate=False):
|
||||||
"""Print the board in the console"""
|
"""Print the board in the console"""
|
||||||
|
|
||||||
def get_piece_code(pos, background):
|
def print_letters():
|
||||||
"""Return code piece"""
|
"""Print the letters above or under the board"""
|
||||||
|
print(" ", end="")
|
||||||
|
for col in range(self.board_size):
|
||||||
|
if rotate:
|
||||||
|
print(f" {chr(ord('a')+self.board_size-1-col)}", end=" ")
|
||||||
|
else:
|
||||||
|
print(f" {chr(ord('a')+col)}", end=" ")
|
||||||
|
|
||||||
|
def get_square_code(pos, background):
|
||||||
|
"""Return the code of a given square on the board"""
|
||||||
if (*pos, False) in self.white_positions:
|
if (*pos, False) in self.white_positions:
|
||||||
return "w"
|
return "w"
|
||||||
if (*pos, True) in self.white_positions:
|
if (*pos, True) in self.white_positions:
|
||||||
@ -170,15 +179,18 @@ class Game:
|
|||||||
return "B"
|
return "B"
|
||||||
return background
|
return background
|
||||||
|
|
||||||
print(" ", end="")
|
print_letters()
|
||||||
for col in range(self.board_size):
|
|
||||||
print(f" {chr(ord('a')+col)} ", end="")
|
|
||||||
print(" ")
|
print(" ")
|
||||||
|
|
||||||
|
row_range = range(
|
||||||
|
self.board_size*4) if not rotate else reversed(range(self.board_size*4))
|
||||||
line = 0
|
line = 0
|
||||||
for row in range(self.board_size*4):
|
for row in row_range:
|
||||||
for col in range(self.board_size):
|
for col in range(self.board_size):
|
||||||
background = "#" if col % 2 == (row//4) % 2 else " "
|
background = "#" if (col % 2 == (row//4) % 2) != rotate\
|
||||||
checker = get_piece_code((col, row//4), background)
|
else " "
|
||||||
|
checker = get_square_code((col, row//4), background)\
|
||||||
|
if not rotate else get_square_code((self.board_size-1-col, row//4), background)
|
||||||
|
|
||||||
if col == 0:
|
if col == 0:
|
||||||
if line % 4 == 2:
|
if line % 4 == 2:
|
||||||
@ -201,9 +213,8 @@ class Game:
|
|||||||
print(" ", end="")
|
print(" ", end="")
|
||||||
for col in range(self.board_size):
|
for col in range(self.board_size):
|
||||||
print("+---", end="")
|
print("+---", end="")
|
||||||
print("+\n ", end="")
|
print("+")
|
||||||
for col in range(self.board_size):
|
print_letters()
|
||||||
print(f" {chr(ord('a')+col)}", end=" ")
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# Ran first in the code
|
# Ran first in the code
|
||||||
@ -368,7 +379,7 @@ class Game:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
has_moved = self.make_move(from_coordinates, to_coordinates, color)
|
has_moved = self.make_move(from_coordinates, to_coordinates, color)
|
||||||
self.print_board()
|
self.print_board(color == 'white')
|
||||||
|
|
||||||
def start_game(self, player_color='black'):
|
def start_game(self, player_color='black'):
|
||||||
"""Start the main loop of the game"""
|
"""Start the main loop of the game"""
|
||||||
@ -377,7 +388,7 @@ class Game:
|
|||||||
return
|
return
|
||||||
ai_color = 'black' if player_color == 'white' else 'white'
|
ai_color = 'black' if player_color == 'white' else 'white'
|
||||||
|
|
||||||
game.print_board()
|
game.print_board(player_color == 'white')
|
||||||
if player_color == 'white':
|
if player_color == 'white':
|
||||||
game.handle_player_move('white')
|
game.handle_player_move('white')
|
||||||
|
|
||||||
@ -392,7 +403,7 @@ class Game:
|
|||||||
"AI's move: "
|
"AI's move: "
|
||||||
f"{chr(ord('a')+ai_move[0][0])}{ai_move[0][1]} "
|
f"{chr(ord('a')+ai_move[0][0])}{ai_move[0][1]} "
|
||||||
f"{chr(ord('a')+ai_move[1][0])}{ai_move[1][1]}")
|
f"{chr(ord('a')+ai_move[1][0])}{ai_move[1][1]}")
|
||||||
game.print_board()
|
game.print_board(player_color == 'white')
|
||||||
|
|
||||||
possible_moves_player = game.get_possible_moves(player_color)
|
possible_moves_player = game.get_possible_moves(player_color)
|
||||||
if len(possible_moves_player) == 0:
|
if len(possible_moves_player) == 0:
|
||||||
@ -404,4 +415,4 @@ class Game:
|
|||||||
# Ran first in the code
|
# Ran first in the code
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
game = Game(8)
|
game = Game(8)
|
||||||
game.start_game('white')
|
game.start_game('black')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user