Rework board display and human interaction
This commit is contained in:
parent
d846549bb7
commit
507964cd6b
2 changed files with 27 additions and 15 deletions
|
@ -103,7 +103,7 @@ class HumanPlayerEngine(PlayerEngine):
|
|||
move = None
|
||||
while move is None:
|
||||
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)
|
||||
return move
|
||||
|
@ -114,23 +114,33 @@ class HumanPlayerEngine(PlayerEngine):
|
|||
@param input: string
|
||||
@return: array
|
||||
"""
|
||||
@staticmethod
|
||||
def validate_input(input, board):
|
||||
def validate_input(self, input, board):
|
||||
if input == 'print':
|
||||
print(board.show_board())
|
||||
print("\n{}".format(board.show_board()))
|
||||
return None
|
||||
|
||||
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
|
||||
|
||||
if len(input) != 2:
|
||||
self.logger.error("Input coordinate (A1 for example), help or print")
|
||||
return None
|
||||
|
||||
x = int(input[0])
|
||||
x = ord(input[0]) - 65
|
||||
y = int(input[1])
|
||||
try:
|
||||
if not board.is_valid_move(board._nextPlayer, x, y):
|
||||
self.logger.error("Move is not possible at this place")
|
||||
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]
|
||||
|
||||
|
|
|
@ -218,15 +218,17 @@ class Board:
|
|||
|
||||
def show_board(self):
|
||||
display = " |"
|
||||
sep = "----"
|
||||
for x in range(self.get_board_size()):
|
||||
display += "{}|".format(str(x))
|
||||
display += "\n"
|
||||
display += " {} |".format(chr(65+x))
|
||||
sep += '----'
|
||||
display += "\n" + sep + "\n"
|
||||
for x in range(self.get_board_size()):
|
||||
display += " {} |".format(str(x))
|
||||
for y in range(self.get_board_size()):
|
||||
display += " {} |".format(self._piece2str(self._board[x][y]))
|
||||
display += "\n"
|
||||
return display
|
||||
display += "\n"#+sep+"\n"
|
||||
return display + sep + '\n'
|
||||
|
||||
def __str__(self):
|
||||
toreturn=""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue