Rework board display and human interaction

This commit is contained in:
Yorick Barbanneau 2023-12-21 23:09:04 +01:00
parent d846549bb7
commit 507964cd6b
2 changed files with 27 additions and 15 deletions

View file

@ -103,7 +103,7 @@ class HumanPlayerEngine(PlayerEngine):
move = None move = None
while move is None: while move is None:
user_input = input("Please enter player {} move, `print` to display board and `help` possible moves : ".format( user_input = input("Please enter player {} move, `print` to display board and `help` possible moves : ".format(
self.get_player_name(self.player) self._get_player_name(self.player)
)) ))
move = self.validate_input(user_input, board) move = self.validate_input(user_input, board)
return move return move
@ -114,23 +114,33 @@ class HumanPlayerEngine(PlayerEngine):
@param input: string @param input: string
@return: array @return: array
""" """
@staticmethod def validate_input(self, input, board):
def validate_input(input, board):
if input == 'print': if input == 'print':
print(board.show_board()) print("\n{}".format(board.show_board()))
return None return None
if input == 'help': if input == 'help':
print('{}'.format(board.legal_moves())) text = "Possible move:"
for m in board.legal_moves():
text += " {}{}".format(chr(65+m[1]), m[2])
print(text)
return None return None
if len(input) != 2: if len(input) != 2:
self.logger.error("Input coordinate (A1 for example), help or print")
return None return None
x = int(input[0]) x = ord(input[0]) - 65
y = int(input[1]) y = int(input[1])
try:
if not board.is_valid_move(board._nextPlayer, x, y): if not board.is_valid_move(board._nextPlayer, x, y):
self.logger.error("Move is not possible at this place")
return None return None
except IndexError:
self.logger.error("Invalid input must be [A-J][0-9] (was {})".format(input))
return None
return [board._nextPlayer, x, y] return [board._nextPlayer, x, y]

View file

@ -218,15 +218,17 @@ class Board:
def show_board(self): def show_board(self):
display = " |" display = " |"
sep = "----"
for x in range(self.get_board_size()): for x in range(self.get_board_size()):
display += "{}|".format(str(x)) display += " {} |".format(chr(65+x))
display += "\n" sep += '----'
display += "\n" + sep + "\n"
for x in range(self.get_board_size()): for x in range(self.get_board_size()):
display += "{}|".format(str(x)) display += " {} |".format(str(x))
for y in range(self.get_board_size()): for y in range(self.get_board_size()):
display += "{}|".format(self._piece2str(self._board[x][y])) display += " {} |".format(self._piece2str(self._board[x][y]))
display += "\n" display += "\n"#+sep+"\n"
return display return display + sep + '\n'
def __str__(self): def __str__(self):
toreturn="" toreturn=""