Решение на Морски шах от Данаил Димитров

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

Към профила на Данаил Димитров

Резултати

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

Код

import re
class TicTacToeBoard:
def __init__(self):
self.player_passed_turn = ''
self.Board_progress = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ',
'B2': ' ', 'B3': ' ', 'C1': ' ', 'C2': ' ',
'C3': ' '}
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self.Board_progress['A3'] +\
' | ' + self.Board_progress['B3'] +\
' | ' + self.Board_progress['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self.Board_progress['A2'] +\
' | ' + self.Board_progress['B2'] +\
' | ' + self.Board_progress['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self.Board_progress['A1'] +\
' | ' + self.Board_progress['B1'] +\
' | ' + self.Board_progress['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def __setitem__(self, key, item):
if self.game_status() != "Game in progress.":
print(self.game_status())
else:
try:
self[key]
except KeyError:
if not item in {'O', 'X'}:
raise InvalidValue
elif not bool(re.search(r'[A-C][1-3]', key)):
raise InvalidKey
elif self.player_passed_turn == item:
raise NotYourTurn
self.Board_progress[key] = item
self.player_passed_turn = item
else:
raise InvalidMove
def __getitem__(self, key):
if self.Board_progress[key] == " ":
raise KeyError
return self.Board_progress[key]
def game_status(self):
for a in ['1', '2', '3']:
if (self.Board_progress[str('A'+a)] ==
self.Board_progress[str('B'+a)] ==
self.Board_progress[str('C'+a)] != " "):
if self.Board_progress[str('A'+a)] == 'O':
return "O wins!"
else:
return "X wins!"
elif (self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '1')] ==
self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '2')] ==
self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '3')] != " "):
if self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '1')] == 'O':
return "O wins!"
else:
return "X wins!"
if (self.Board_progress['A1'] ==
self.Board_progress['B2'] ==
self.Board_progress['C3'] != " " or
self.Board_progress['A3'] ==
self.Board_progress['B2'] ==
self.Board_progress['C1'] != " "):
if self.Board_progress['B2'] == 'O':
return "O wins!"
else:
return "X wins!"
elif not " " in self.Board_progress.values():
return "Draw!"
else:
return "Game in progress."
class InvalidMove(Exception):
exeption = "Tried to put in existing place"
message = "This move is invalid ," +\
"you've already placed there !"
def __str__(self):
return self.message
class InvalidValue(Exception):
exeption = "Tried to put improper value"
message = "This value is improper ," +\
"you can only place \"X\" or \"O\"!"
def __str__(self):
return self.message
class InvalidKey(Exception):
exeption = "Tried to put improper place"
message = "This position is improper," +\
"positions are A,B,C from 1 to 3 !"
def __str__(self):
return self.message
class NotYourTurn(Exception):
exeption = "Tried to place twice"
message = "You are not allowed to place twice in a row !"
def __str__(self):
return self.message

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.171s

OK

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

Данаил обнови решението на 13.04.2013 17:59 (преди над 11 години)

+import re
+
+
+class TicTacToeBoard:
+
+ def __init__(self):
+ self.player_passed_turn = ''
+ self.Board_progress = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ',
+ 'B2': ' ', 'B3': ' ', 'C1': ' ', 'C2': ' ',
+ 'C3': ' '}
+
+ def __str__(self):
+ return '\n -------------\n' +\
+ '3 | ' + self.Board_progress['A3'] +\
+ ' | ' + self.Board_progress['B3'] +\
+ ' | ' + self.Board_progress['C3'] + ' |\n' +\
+ ' -------------\n' +\
+ '2 | ' + self.Board_progress['A2'] +\
+ ' | ' + self.Board_progress['B2'] +\
+ ' | ' + self.Board_progress['C2'] + ' |\n' +\
+ ' -------------\n' +\
+ '1 | ' + self.Board_progress['A1'] +\
+ ' | ' + self.Board_progress['B1'] +\
+ ' | ' + self.Board_progress['C1'] + ' |\n' +\
+ ' -------------\n' +\
+ ' A B C \n'
+
+ def __setitem__(self, key, item):
+ if self.game_status() != "Game in progress.":
+ print(self.game_status())
+ else:
+ try:
+ self[key]
+ except KeyError:
+ if not item in {'O', 'X'}:
+ raise InvalidValue
+ elif not bool(re.search(r'[A-C][1-3]', key)):
+ raise InvalidKey
+ elif self.player_passed_turn == item:
+ raise NotYourTurn
+ self.Board_progress[key] = item
+ self.player_passed_turn = item
+ else:
+ raise InvalidMove
+
+ def __getitem__(self, key):
+ if self.Board_progress[key] == " ":
+ raise KeyError
+ return self.Board_progress[key]
+
+ def game_status(self):
+ for a in ['1', '2', '3']:
+ if (self.Board_progress[str('A'+a)] ==
+ self.Board_progress[str('B'+a)] ==
+ self.Board_progress[str('C'+a)] != " "):
+ if self.Board_progress[str('A'+a)] == 'O':
+ return "O wins!"
+ else:
+ return "X wins!"
+ elif (self.Board_progress[str(chr(ord('A') +
+ int(a) - 1) + '1')] ==
+ self.Board_progress[str(chr(ord('A') +
+ int(a) - 1) + '2')] ==
+ self.Board_progress[str(chr(ord('A') +
+ int(a) - 1) + '3')] != " "):
+ if self.Board_progress[str(chr(ord('A') +
+ int(a) - 1) + '1')] == 'O':
+ return "O wins!"
+ else:
+ return "X wins"
+ if (self.Board_progress['A1'] ==
+ self.Board_progress['B2'] ==
+ self.Board_progress['C3'] != " " or
+ self.Board_progress['A3'] ==
+ self.Board_progress['B2'] ==
+ self.Board_progress['C1'] != " "):
+ if self.Board_progress['B2'] == 'O':
+ return "O wins!"
+ else:
+ return "X wins!"
+ elif not " " in self.Board_progress.values():
+ return "Draw!"
+ else:
+ return "Game in progress."
+
+
+class InvalidMove(Exception):
+ exeption = "Tried to put in existing place"
+ message = "This move is invalid ," +\
+ "you've already placed there !"
+
+ def __str__(self):
+ return self.message
+
+
+class InvalidValue(Exception):
+ exeption = "Tried to put improper value"
+ message = "This value is improper ," +\
+ "you can only place \"X\" or \"O\"!"
+
+ def __str__(self):
+ return self.message
+
+
+class InvalidKey(Exception):
+ exeption = "Tried to put improper place"
+ message = "This position is improper," +\
+ "positions are A,B,C from 1 to 3 !"
+
+ def __str__(self):
+ return self.message
+
+
+class NotYourTurn(Exception):
+ exeption = "Tried to place twice"
+ message = "You are not allowed to place twice in a row !"
+
+ def __str__(self):
+ return self.message

По-добрата практика е съобщението при изключение е да се подава като аргумент:

raise NotYourTurn("It's not your turn!")

Ако наследяваш Exception не е нужно да добавяш message атрибут. Неговия конструктор има атрибут args и str(exception) връща първият аргумент подаден на конструктора.

Данаил обнови решението на 15.04.2013 16:53 (преди над 11 години)

import re
class TicTacToeBoard:
def __init__(self):
self.player_passed_turn = ''
self.Board_progress = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ',
'B2': ' ', 'B3': ' ', 'C1': ' ', 'C2': ' ',
'C3': ' '}
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self.Board_progress['A3'] +\
' | ' + self.Board_progress['B3'] +\
' | ' + self.Board_progress['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self.Board_progress['A2'] +\
' | ' + self.Board_progress['B2'] +\
' | ' + self.Board_progress['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self.Board_progress['A1'] +\
' | ' + self.Board_progress['B1'] +\
' | ' + self.Board_progress['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def __setitem__(self, key, item):
if self.game_status() != "Game in progress.":
print(self.game_status())
else:
try:
self[key]
except KeyError:
if not item in {'O', 'X'}:
raise InvalidValue
elif not bool(re.search(r'[A-C][1-3]', key)):
raise InvalidKey
elif self.player_passed_turn == item:
raise NotYourTurn
self.Board_progress[key] = item
self.player_passed_turn = item
else:
raise InvalidMove
def __getitem__(self, key):
if self.Board_progress[key] == " ":
raise KeyError
return self.Board_progress[key]
def game_status(self):
for a in ['1', '2', '3']:
if (self.Board_progress[str('A'+a)] ==
self.Board_progress[str('B'+a)] ==
self.Board_progress[str('C'+a)] != " "):
if self.Board_progress[str('A'+a)] == 'O':
return "O wins!"
else:
return "X wins!"
elif (self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '1')] ==
self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '2')] ==
self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '3')] != " "):
if self.Board_progress[str(chr(ord('A') +
int(a) - 1) + '1')] == 'O':
return "O wins!"
else:
- return "X wins"
+ return "X wins!"
if (self.Board_progress['A1'] ==
self.Board_progress['B2'] ==
self.Board_progress['C3'] != " " or
self.Board_progress['A3'] ==
self.Board_progress['B2'] ==
self.Board_progress['C1'] != " "):
if self.Board_progress['B2'] == 'O':
return "O wins!"
else:
return "X wins!"
elif not " " in self.Board_progress.values():
return "Draw!"
else:
return "Game in progress."
class InvalidMove(Exception):
exeption = "Tried to put in existing place"
message = "This move is invalid ," +\
"you've already placed there !"
def __str__(self):
return self.message
class InvalidValue(Exception):
exeption = "Tried to put improper value"
message = "This value is improper ," +\
"you can only place \"X\" or \"O\"!"
def __str__(self):
return self.message
class InvalidKey(Exception):
exeption = "Tried to put improper place"
message = "This position is improper," +\
"positions are A,B,C from 1 to 3 !"
def __str__(self):
return self.message
class NotYourTurn(Exception):
exeption = "Tried to place twice"
message = "You are not allowed to place twice in a row !"
def __str__(self):
return self.message