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

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

Към профила на Марио Даскалов

Резултати

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

Код

HORIZONTAL_VALUES = ('A', 'B', 'C')
VERTICAL_VALUES = (1, 2, 3)
LEFT_DIAGONAL = ('A1', 'B2', 'C3')
RIGHT_DIAGONAL = ('A3', 'B2', 'C1')
TOP_LINE = '\n -------------\n'
BOTTOM_LINE = ' A B C \n'
MIDDLE_LINE = ' -------------\n'
class InvalidMove(Exception):
pass
class InvalidKey(Exception):
pass
class InvalidValue(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.board = dict()
for h in HORIZONTAL_VALUES:
for v in VERTICAL_VALUES:
self.board[h + str(v)] = None
self.last_move = None
self.winner = None
self.moves_made = 0
def game_status(self):
if self.winner is not None:
return self.winner + ' wins!'
elif self.moves_made == len(self.board):
return 'Draw!'
else:
return 'Game in progress.'
def check_for_winner(self, index, value):
h = index[0]
v = index[1]
is_winner = True
for tv in VERTICAL_VALUES:
if self.board[h + str(tv)] != value:
is_winner = False
break
if is_winner is True:
self.winner = value
return
is_winner = True
for th in HORIZONTAL_VALUES:
if self.board[th + v] != value:
is_winner = False
break
if is_winner is True:
self.winner = value
return
if index in LEFT_DIAGONAL:
is_winner = True
for i in LEFT_DIAGONAL:
if self.board[i] != value:
is_winner = False
break
if is_winner is True:
self.winner = value
return
if index in RIGHT_DIAGONAL:
is_winner = True
for i in RIGHT_DIAGONAL:
if self.board[i] != value:
is_winner = False
break
if is_winner is True:
self.winner = value
return
def __getitem__(self, i):
if i in self.board:
return self.board[i]
else:
raise InvalidKey()
def __setitem__(self, i, v):
if i not in self.board:
raise InvalidKey()
if v not in ('X', 'O'):
raise InvalidValue()
if self.last_move == v:
raise NotYourTurn()
if self.board[i] is None:
self.board[i] = v
self.last_move = v
self.moves_made = self.moves_made + 1
if self.winner is None:
self.check_for_winner(i, v)
else:
raise InvalidMove()
def __str__(self):
board_draw = TOP_LINE
for v in reversed(VERTICAL_VALUES):
board_draw += str(v) + ' |'
for h in HORIZONTAL_VALUES:
value = ' ' if self.board[h + str(v)] is None \
else self.board[h + str(v)]
board_draw += ' ' + value + ' |'
board_draw += '\n'
board_draw += MIDDLE_LINE
board_draw += BOTTOM_LINE
return board_draw

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.143s

OK

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

Марио обнови решението на 15.04.2013 16:43 (преди над 11 години)

+HORIZONTAL_VALUES = ('A', 'B', 'C')
+VERTICAL_VALUES = (1, 2, 3)
+LEFT_DIAGONAL = ('A1', 'B2', 'C3')
+RIGHT_DIAGONAL = ('A3', 'B2', 'C1')
+
+TOP_LINE = '\n -------------\n'
+BOTTOM_LINE = ' A B C \n'
+MIDDLE_LINE = ' -------------\n'
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class TicTacToeBoard:
+
+ def __init__(self):
+ self.board = dict()
+ for h in HORIZONTAL_VALUES:
+ for v in VERTICAL_VALUES:
+ self.board[h + str(v)] = None
+ self.last_move = None
+ self.winner = None
+ self.moves_made = 0
+
+ def game_status(self):
+ if self.winner is not None:
+ return self.winner + ' wins!'
+ elif self.moves_made == len(self.board):
+ return 'Draw!'
+ else:
+ return 'Game in progress.'
+
+ def check_for_winner(self, index, value):
+ h = index[0]
+ v = index[1]
+
+ is_winner = True
+ for tv in VERTICAL_VALUES:
+ if self.board[h + str(tv)] != value:
+ is_winner = False
+ break
+ if is_winner is True:
+ self.winner = value
+ return
+
+ is_winner = True
+ for th in HORIZONTAL_VALUES:
+ if self.board[th + v] != value:
+ is_winner = False
+ break
+ if is_winner is True:
+ self.winner = value
+ return
+
+ if index in LEFT_DIAGONAL:
+ is_winner = True
+ for i in LEFT_DIAGONAL:
+ if self.board[i] != value:
+ is_winner = False
+ break
+ if is_winner is True:
+ self.winner = value
+ return
+
+ if index in RIGHT_DIAGONAL:
+ is_winner = True
+ for i in RIGHT_DIAGONAL:
+ if self.board[i] != value:
+ is_winner = False
+ break
+ if is_winner is True:
+ self.winner = value
+ return
+
+ def __getitem__(self, i):
+ if i in self.board:
+ return self.board[i]
+ else:
+ raise InvalidKey()
+
+ def __setitem__(self, i, v):
+ if i not in self.board:
+ raise InvalidKey()
+ if v not in ('X', 'O'):
+ raise InvalidValue()
+ if self.last_move == v:
+ raise NotYourTurn()
+ if self.board[i] is None:
+ self.board[i] = v
+ self.last_move = v
+ self.moves_made = self.moves_made + 1
+ if self.winner is None:
+ self.check_for_winner(i, v)
+ else:
+ raise InvalidMove()
+
+ def __str__(self):
+ board_draw = TOP_LINE
+ for v in reversed(VERTICAL_VALUES):
+ board_draw += str(v) + ' |'
+ for h in HORIZONTAL_VALUES:
+ value = ' ' if self.board[h + str(v)] is None \
+ else self.board[h + str(v)]
+ board_draw += ' ' + value + ' |'
+ board_draw += '\n'
+ board_draw += MIDDLE_LINE
+ board_draw += BOTTOM_LINE
+ return board_draw