Решение на Морски шах от Христо Хърсев

Обратно към всички решения

Към профила на Христо Хърсев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class InvalidValue(Exception):
pass
class InvalidMove(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __str__(self):
separator = '\n' + 2 * ' ' + 13 * '-' + '\n'
last_row = ' ' + 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + '\n'
field = []
for i in range(3, 0, -1):
field.append(str(i) + " | {} | {} | {} |".format
(self.board['A' + str(i)], self.board['B' + str(i)],
self.board['C' + str(i)]))
field.insert(0, '')
field.append(last_row)
return separator.join(field)
def __init__(self):
self.winner = ' '
self.last_played = ' '
self.board = {}
for letter in range(ord('A'), ord('C') + 1):
for digit in range(1, 4):
self.board[chr(letter) + str(digit)] = ' '
def __setitem__(self, key, value):
self.exception_handler(key, value)
self.board[key] = value
if self.winner == ' ':
self.check_for_winner()
self.last_played = value
def exception_handler(self, key, value):
if key not in self.board:
raise InvalidKey
if value not in ['X', 'O']:
raise InvalidValue
if self.board[key] != ' ':
raise InvalidMove
if value not in self.on_move_is():
raise NotYourTurn
def empty_board(self):
return len(set(self.board.values())) == 1
def is_game_in_progress(self):
return self.winner == ' ' and ' ' in (set(self.board.values()))
def game_status(self):
if self.winner != ' ':
return "{} wins!".format(self.winner)
elif self.is_game_in_progress():
return "Game in progress."
else:
return "Draw!"
def on_move_is(self):
return list('XO'.strip(self.last_played))
def return_verticals(self):
return [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']]
def return_horizontals(self):
return [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']]
def return_diagonals(self):
return [['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
def all_possible_winning_combinations(self):
return (self.return_verticals() + self.return_diagonals() +
self.return_horizontals())
def check_for_winner(self):
for combination in self.all_possible_winning_combinations():
values = set()
for key in combination:
values.add(self.board[key])
if len(values) == 1 and values != {' '}:
self.winner = values.pop()
break

Лог от изпълнението

........
----------------------------------------------------------------------
Ran 8 tests in 0.120s

OK

История (4 версии и 3 коментара)

Христо обнови решението на 09.04.2013 23:42 (преди над 11 години)

+import sys
+
+
+class TicTacToeBoard:
+
+ def __str__(self):
+ return '\n' + 2 * ' ' + 13 * '-' + '\n' +\
+ '3 ' + '|' + ' ' + str(self.board["A3"]) + ' | ' +\
+ str(self.board["B3"]) +\
+ ' | ' + str(self.board["C3"]) + ' |' +\
+ '\n' + 2 * ' ' + 13 * '-' + '\n' +\
+ '2 ' + '|' + ' ' + str(self.board["A2"]) + ' | ' +\
+ str(self.board["B2"]) +\
+ ' | ' + str(self.board["C2"]) + ' |' +\
+ '\n' + 2 * ' ' + 13 * '-' + '\n' +\
+ '1 ' + '|' + ' ' + str(self.board["A1"]) + ' | ' +\
+ str(self.board["B1"]) +\
+ ' | ' + str(self.board["C1"]) + ' |' +\
+ '\n' + 2 * ' ' + 13 * '-' + '\n' +\
+ 4 * ' ' + 'A' + 3 * ' ' + 'B' + 3 * ' ' + 'C' + 2 * ' ' + '\n'
+
+ def __setitem__(self, key, value):
+ if self.game_status() == "Game in progress.":
+ self.exception_handler(key, value)
+ self.board[key] = value
+ if self.first_played == ' ':
+ self.first_played = value
+ self.check_for_winner()
+
+ def __init__(self):
+ self.board = {
+ "A1": " ", "A2": " ", "A3": " ", "B1": " ", "B2": " ",
+ "B3": " ", "C1": " ", "C2": " ", "C3": " "
+ }
+ self.first_played = " "
+ self.winner = " "
+
+ def exception_handler(self, key, value):
+ if key not in self.board:
+ raise InvalidKey(key)
+ if value not in ['X', 'O']:
+ raise InvalidValue(value)
+ if self.board[key] != ' ':
+ raise InvalidMove(key)
+ if value not in self.on_move_is():
+ raise NotYourTurn(value)
+
+ def on_move_is(self):
+ if self.empty_board():
+ return 'XO'
+ elif list(self.board.values()).count('O') ==\
+ list(self.board.values()).count('X'):
+ return self.first_played
+ else:
+ return 'O' if self.first_played == 'X' else 'X'
+
+ def empty_board(self):
+ return len(set(self.board.values())) == 1
+
+ def is_game_in_progress(self):
+ return ' ' in list(self.board.values())
+
+ def check_for_winner(self):
+ if self.winner != ' ':
+ return self.winner
+
+ alphas = ['A', 'B', 'C']
+ check_result = set()
+
+ for alpha in range(0, 3):
+ for digit in range(1, 4):
+ check_result.add(self.board[alphas[alpha] + str(digit)])
+ if len(check_result) == 1 and check_result != {' '}:
+ self.winner = check_result.pop()
+ check_result = set()
+
+ for digit in range(1, 4):
+ for alpha in range(0, 3):
+ check_result.add(self.board[alphas[alpha] + str(digit)])
+ if len(check_result) == 1 and check_result != {' '}:
+ self.winner = check_result.pop()
+ check_result = set()
+
+ check_result.add(self.board['A1'])
+ check_result.add(self.board['B2'])
+ check_result.add(self.board['C3'])
+ if len(check_result) == 1 and check_result != {' '}:
+ self.winner = check_result.pop()
+ check_result = set()
+
+ check_result.add(self.board['C1'])
+ check_result.add(self.board['B2'])
+ check_result.add(self.board['A3'])
+ if len(check_result) == 1 and check_result != {' '}:
+ self.winner = check_result.pop()
+ check_result = set()
+
+ def game_status(self):
+ if self.winner != ' ':
+ return "{} wins!".format(self.winner)
+ elif self.is_game_in_progress():
+ return "Game in progress."
+ else:
+ return "Draw!"
+
+
+class InvalidValue(Exception):
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return "'{}' must be 'X' or 'O'".format(self.value)
+
+
+class InvalidMove(Exception):
+ def __init__(self, key):
+ self.key = key
+
+ def __str__(self):
+ return "The {} area is already taken".format(self.key)
+
+
+class InvalidKey(Exception):
+ def __init__(self, key):
+ self.key = key
+
+ def __str__(self):
+ return "'{}' happens to be non-existent key".format(self.key)
+
+
+class NotYourTurn(Exception):
+ def __init__(self, player):
+ self.player = player
+
+ def __str__(self):
+ return "Hey, player '{}', it is not your turn".format(self.player)

По-добре си направи изключенията така: MyException(Exception): pass и ги възбуждай така, ако настояваш за съобщение: raise MyException('my exceptional message') поведението, което им даваш го има в Exception по подразбиране.

Помисли дали не можеш да преработиш __str__, така че да е по-лесен за редактиране при промяна на дъската. Hints: multiline string, str.join, str.format

Опитай да преработиш check_for_winner. Повтаряш голямо количество еднотипни операции. Опитай предварително да опишеш кои са победните тройки полета, така че с едно обхождане да направиш всички проверки.

Не import-вай модули, които не ползваш :)

Христо обнови решението на 11.04.2013 16:55 (преди над 11 години)

-import sys
-
-
class TicTacToeBoard:
def __str__(self):
- return '\n' + 2 * ' ' + 13 * '-' + '\n' +\
- '3 ' + '|' + ' ' + str(self.board["A3"]) + ' | ' +\
- str(self.board["B3"]) +\
- ' | ' + str(self.board["C3"]) + ' |' +\
- '\n' + 2 * ' ' + 13 * '-' + '\n' +\
- '2 ' + '|' + ' ' + str(self.board["A2"]) + ' | ' +\
- str(self.board["B2"]) +\
- ' | ' + str(self.board["C2"]) + ' |' +\
- '\n' + 2 * ' ' + 13 * '-' + '\n' +\
- '1 ' + '|' + ' ' + str(self.board["A1"]) + ' | ' +\
- str(self.board["B1"]) +\
- ' | ' + str(self.board["C1"]) + ' |' +\
- '\n' + 2 * ' ' + 13 * '-' + '\n' +\
- 4 * ' ' + 'A' + 3 * ' ' + 'B' + 3 * ' ' + 'C' + 2 * ' ' + '\n'
+ separator = '\n' + 2 * ' ' + 13 * '-' + '\n'
+ last_row = (' ' + 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + '\n')
+ field = []
+ for i in range(3, 0, -1):
+ field.append(str(i) + " | {} | {} | {} |".format
+ (self.board['A' + str(i)], self.board['B' + str(i)],
+ self.board['C' + str(i)]))
+ field.insert(0, '')
+ field.append(last_row)
+ return separator.join(field)
+ def __init__(self):
+ self.winner = ' '
+ self.first_played = ' '
+ self.board = {}
+ for letter in range(ord('A'), ord('C') + 1):
+ for digit in range(1, 4):
+ self.board[chr(letter) + str(digit)] = ' '
+
def __setitem__(self, key, value):
- if self.game_status() == "Game in progress.":
- self.exception_handler(key, value)
- self.board[key] = value
- if self.first_played == ' ':
- self.first_played = value
+ self.exception_handler(key, value)
+ self.board[key] = value
+ if self.first_played == ' ':
+ self.first_played = value
+ if self.winner == ' ':
self.check_for_winner()
- def __init__(self):
- self.board = {
- "A1": " ", "A2": " ", "A3": " ", "B1": " ", "B2": " ",
- "B3": " ", "C1": " ", "C2": " ", "C3": " "
- }
- self.first_played = " "
- self.winner = " "
+ def empty_board(self):
+ return len(set(self.board.values())) == 1
- def exception_handler(self, key, value):
- if key not in self.board:
- raise InvalidKey(key)
- if value not in ['X', 'O']:
- raise InvalidValue(value)
- if self.board[key] != ' ':
- raise InvalidMove(key)
- if value not in self.on_move_is():
- raise NotYourTurn(value)
+ def is_game_in_progress(self):
+ return ' ' in (set(self.board.values()))
+ def game_status(self):
+ if self.winner != ' ':
+ return "{} wins!".format(self.winner)
+ elif self.is_game_in_progress():
+ return "Game in progress."
+ else:
+ return "Draw!"
+
def on_move_is(self):
if self.empty_board():
return 'XO'
- elif list(self.board.values()).count('O') ==\
- list(self.board.values()).count('X'):
+ elif (list(self.board.values()).count('O') ==
+ list(self.board.values()).count('X')):
return self.first_played
else:
return 'O' if self.first_played == 'X' else 'X'
- def empty_board(self):
- return len(set(self.board.values())) == 1
+ def exception_handler(self, key, value):
+ if key not in self.board:
+ raise InvalidKey
+ if value not in ['X', 'O']:
+ raise InvalidValue
+ if self.board[key] != ' ':
+ raise InvalidMove
+ if value not in self.on_move_is():
+ raise NotYourTurn
- def is_game_in_progress(self):
- return ' ' in list(self.board.values())
+ def return_verticals(self):
+ return [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']]
- def check_for_winner(self):
- if self.winner != ' ':
- return self.winner
+ def return_horizontals(self):
+ return [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']]
- alphas = ['A', 'B', 'C']
- check_result = set()
+ def return_diagonals(self):
+ return [['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
- for alpha in range(0, 3):
- for digit in range(1, 4):
- check_result.add(self.board[alphas[alpha] + str(digit)])
- if len(check_result) == 1 and check_result != {' '}:
- self.winner = check_result.pop()
- check_result = set()
+ def all_possible_winning_combinations(self):
+ return (self.return_verticals() + self.return_diagonals() +
+ self.return_horizontals())
- for digit in range(1, 4):
- for alpha in range(0, 3):
- check_result.add(self.board[alphas[alpha] + str(digit)])
- if len(check_result) == 1 and check_result != {' '}:
- self.winner = check_result.pop()
- check_result = set()
+ def check_for_winner(self):
+ for combination in self.all_possible_winning_combinations():
+ values = set()
+ for key in combination:
+ values.add(self.board[key])
+ if len(values) == 1 and values != {' '}:
+ self.winner = values.pop()
+ break
- check_result.add(self.board['A1'])
- check_result.add(self.board['B2'])
- check_result.add(self.board['C3'])
- if len(check_result) == 1 and check_result != {' '}:
- self.winner = check_result.pop()
- check_result = set()
- check_result.add(self.board['C1'])
- check_result.add(self.board['B2'])
- check_result.add(self.board['A3'])
- if len(check_result) == 1 and check_result != {' '}:
- self.winner = check_result.pop()
- check_result = set()
-
- def game_status(self):
- if self.winner != ' ':
- return "{} wins!".format(self.winner)
- elif self.is_game_in_progress():
- return "Game in progress."
- else:
- return "Draw!"
-
-
class InvalidValue(Exception):
- def __init__(self, value):
- self.value = value
+ pass
- def __str__(self):
- return "'{}' must be 'X' or 'O'".format(self.value)
-
class InvalidMove(Exception):
- def __init__(self, key):
- self.key = key
+ pass
- def __str__(self):
- return "The {} area is already taken".format(self.key)
-
class InvalidKey(Exception):
- def __init__(self, key):
- self.key = key
+ pass
- def __str__(self):
- return "'{}' happens to be non-existent key".format(self.key)
-
class NotYourTurn(Exception):
- def __init__(self, player):
- self.player = player
+ pass
-
- def __str__(self):
- return "Hey, player '{}', it is not your turn".format(self.player)

Христо обнови решението на 14.04.2013 20:54 (преди над 11 години)

+class InvalidValue(Exception):
+ pass
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
class TicTacToeBoard:
def __str__(self):
separator = '\n' + 2 * ' ' + 13 * '-' + '\n'
- last_row = (' ' + 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + '\n')
+ last_row = ' ' + 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + '\n'
field = []
for i in range(3, 0, -1):
field.append(str(i) + " | {} | {} | {} |".format
(self.board['A' + str(i)], self.board['B' + str(i)],
self.board['C' + str(i)]))
field.insert(0, '')
field.append(last_row)
return separator.join(field)
def __init__(self):
self.winner = ' '
self.first_played = ' '
self.board = {}
for letter in range(ord('A'), ord('C') + 1):
for digit in range(1, 4):
self.board[chr(letter) + str(digit)] = ' '
def __setitem__(self, key, value):
self.exception_handler(key, value)
self.board[key] = value
if self.first_played == ' ':
self.first_played = value
if self.winner == ' ':
self.check_for_winner()
+ def exception_handler(self, key, value):
+ if key not in self.board:
+ raise InvalidKey
+ if value not in ['X', 'O']:
+ raise InvalidValue
+ if self.board[key] != ' ':
+ raise InvalidMove
+ if value not in self.on_move_is():
+ raise NotYourTurn
+
def empty_board(self):
return len(set(self.board.values())) == 1
def is_game_in_progress(self):
- return ' ' in (set(self.board.values()))
+ return self.winner == ' ' and ' ' in (set(self.board.values()))
def game_status(self):
if self.winner != ' ':
return "{} wins!".format(self.winner)
elif self.is_game_in_progress():
return "Game in progress."
else:
return "Draw!"
def on_move_is(self):
if self.empty_board():
- return 'XO'
+ return ['X', 'O']
elif (list(self.board.values()).count('O') ==
list(self.board.values()).count('X')):
return self.first_played
else:
return 'O' if self.first_played == 'X' else 'X'
- def exception_handler(self, key, value):
- if key not in self.board:
- raise InvalidKey
- if value not in ['X', 'O']:
- raise InvalidValue
- if self.board[key] != ' ':
- raise InvalidMove
- if value not in self.on_move_is():
- raise NotYourTurn
-
def return_verticals(self):
return [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']]
def return_horizontals(self):
return [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']]
def return_diagonals(self):
return [['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
def all_possible_winning_combinations(self):
return (self.return_verticals() + self.return_diagonals() +
self.return_horizontals())
def check_for_winner(self):
for combination in self.all_possible_winning_combinations():
values = set()
for key in combination:
values.add(self.board[key])
if len(values) == 1 and values != {' '}:
self.winner = values.pop()
- break
-
+ break
-
-class InvalidValue(Exception):
- pass
-
-
-class InvalidMove(Exception):
- pass
-
-
-class InvalidKey(Exception):
- pass
-
-
-class NotYourTurn(Exception):
- pass

Христо обнови решението на 14.04.2013 21:32 (преди над 11 години)

class InvalidValue(Exception):
pass
class InvalidMove(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __str__(self):
separator = '\n' + 2 * ' ' + 13 * '-' + '\n'
last_row = ' ' + 'A' + ' ' + 'B' + ' ' + 'C' + ' ' + '\n'
field = []
for i in range(3, 0, -1):
field.append(str(i) + " | {} | {} | {} |".format
(self.board['A' + str(i)], self.board['B' + str(i)],
self.board['C' + str(i)]))
field.insert(0, '')
field.append(last_row)
return separator.join(field)
def __init__(self):
self.winner = ' '
- self.first_played = ' '
+ self.last_played = ' '
self.board = {}
for letter in range(ord('A'), ord('C') + 1):
for digit in range(1, 4):
self.board[chr(letter) + str(digit)] = ' '
def __setitem__(self, key, value):
self.exception_handler(key, value)
self.board[key] = value
- if self.first_played == ' ':
- self.first_played = value
if self.winner == ' ':
self.check_for_winner()
+ self.last_played = value
def exception_handler(self, key, value):
if key not in self.board:
raise InvalidKey
if value not in ['X', 'O']:
raise InvalidValue
if self.board[key] != ' ':
raise InvalidMove
if value not in self.on_move_is():
raise NotYourTurn
def empty_board(self):
return len(set(self.board.values())) == 1
def is_game_in_progress(self):
return self.winner == ' ' and ' ' in (set(self.board.values()))
def game_status(self):
if self.winner != ' ':
return "{} wins!".format(self.winner)
elif self.is_game_in_progress():
return "Game in progress."
else:
return "Draw!"
def on_move_is(self):
- if self.empty_board():
- return ['X', 'O']
- elif (list(self.board.values()).count('O') ==
- list(self.board.values()).count('X')):
- return self.first_played
- else:
- return 'O' if self.first_played == 'X' else 'X'
+ return list('XO'.strip(self.last_played))
def return_verticals(self):
return [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']]
def return_horizontals(self):
return [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']]
def return_diagonals(self):
return [['A1', 'B2', 'C3'], ['A3', 'B2', 'C1']]
def all_possible_winning_combinations(self):
return (self.return_verticals() + self.return_diagonals() +
self.return_horizontals())
def check_for_winner(self):
for combination in self.all_possible_winning_combinations():
values = set()
for key in combination:
values.add(self.board[key])
if len(values) == 1 and values != {' '}:
self.winner = values.pop()
- break
+ break