Решение на Морски шах от Свилен Андонов

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

Към профила на Свилен Андонов

Резултати

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

Код

import re
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.count = 0
self.board = {}
self.last = ''
def __getitem__(self, position):
return self.board.get(position, None)
def game_status(self):
if self['A1'] is not None and self['A1'] == self['A2'] \
and self['A2'] is self['A3']:
return self['A1'] + ' wins!'
if self['B1'] is not None and self['B1'] == self['B2'] \
and self['B2'] is self['B3']:
return self['B1'] + ' wins!'
if self['C1'] is not None and self['C1'] == self['C2'] \
and self['C2'] is self['C3']:
return self['C1'] + ' wins!'
if self['A1'] is not None and self['A1'] == self['B1'] \
and self['B1'] is self['C1']:
return self['A1'] + ' wins!'
if self['A2'] is not None and self['A2'] == self['B2'] \
and self['B2'] is self['C2']:
return self['A2'] + ' wins!'
if self['A3'] is not None and self['A3'] == self['B3'] \
and self['B3'] is self['C3']:
return self['A3'] + ' wins!'
if self['A1'] is not None and self['A1'] == self['B2'] \
and self['B2'] is self['C3']:
return self['A1'] + ' wins!'
if self['A3'] is not None and self['A3'] == self['B2'] \
and self['B2'] is self['C1']:
return self['A3'] + ' wins!'
if self.count is not 9:
return 'Game in progress.'
return 'Draw!'
def __setitem__(self, position, player):
match = re.search('^[ABC][123]$', position)
if match is None:
raise InvalidKey
if player != 'X' and player != 'O':
raise InvalidValue
if self.last == player:
raise NotYourTurn
if position in self.board:
raise InvalidMove
self.board[position] = player
self.last = player
self.count += 1
def __str__(self):
result = '\n -------------\n'
for row in ['3', '2', '1']:
result += row + ' |'
for col in ['A', 'B', 'C']:
if self[col+row] is not None:
result += ' ' + self[col+row] + ' |'
else:
result += ' |'
result += '\n -------------\n'
result += ' A B C \n'
return result

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.033s

OK

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

Свилен обнови решението на 13.04.2013 23:54 (преди около 11 години)

+import re
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class TicTacToeBoard:
+
+ def __init__(self):
+ self.count = 0
+ self.board = {}
+ self.last = ''
+
+ def __getitem__(self, position):
+ return self.board.get(position, None)
+
+ def game_status(self):
+ if self['A1'] is not None and self['A1'] == self['A2'] \
+ and self['A2'] is self['A3']:
+ return self['A1'] + ' wins!'
+ if self['B1'] is not None and self['B1'] == self['B2'] \
+ and self['B2'] is self['B3']:
+ return self['B1'] + ' wins!'
+ if self['C1'] is not None and self['C1'] == self['C2'] \
+ and self['C2'] is self['C3']:
+ return self['C1'] + ' wins!'
+
+ if self['A1'] is not None and self['A1'] == self['B1'] \
+ and self['B1'] is self['C1']:
+ return self['A1'] + ' wins!'
+ if self['A2'] is not None and self['A2'] == self['B2'] \
+ and self['B2'] is self['C2']:
+ return self['A2'] + ' wins!'
+ if self['A3'] is not None and self['A3'] == self['B3'] \
+ and self['B3'] is self['C3']:
+ return self['A3'] + ' wins!'
+
+ if self['A1'] is not None and self['A1'] == self['B2'] \
+ and self['B2'] is self['C3']:
+ return self['A1'] + ' wins!'
+ if self['A3'] is not None and self['A3'] == self['B2'] \
+ and self['B2'] is self['C1']:
+ return self['A3'] + ' wins!'
+
+ if self.count is not 9:
+ return 'Game in progress.'
+
+ return 'Draw!'
+
+ def __setitem__(self, position, player):
+ match = re.search('^[ABC][123]$', position)
+
+ if match is None:
+ raise InvalidKey
+
+ if player != 'X' and player != 'O':
+ raise InvalidValue
+
+ if self.last == player:
+ raise NotYourTurn
+
+ if position in self.board:
+ raise InvalidMove
+
+ self.board[position] = player
+ self.last_player = player
+ self.count += 1
+
+ def __str__(self):
+ result = '\n -------------\n'
+ for row in ['3', '2', '1']:
+ result += row + ' |'
+ for col in ['A', 'B', 'C']:
+ if self[col+row] is not None:
+ result += ' ' + self[col+row] + ' |'
+ else:
+ result += ' |'
+ result += '\n -------------\n'
+ result += ' A B C \n'
+ return result

Свилен обнови решението на 14.04.2013 10:29 (преди около 11 години)

import re
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.count = 0
self.board = {}
self.last = ''
def __getitem__(self, position):
return self.board.get(position, None)
def game_status(self):
if self['A1'] is not None and self['A1'] == self['A2'] \
and self['A2'] is self['A3']:
return self['A1'] + ' wins!'
if self['B1'] is not None and self['B1'] == self['B2'] \
and self['B2'] is self['B3']:
return self['B1'] + ' wins!'
if self['C1'] is not None and self['C1'] == self['C2'] \
and self['C2'] is self['C3']:
return self['C1'] + ' wins!'
if self['A1'] is not None and self['A1'] == self['B1'] \
and self['B1'] is self['C1']:
return self['A1'] + ' wins!'
if self['A2'] is not None and self['A2'] == self['B2'] \
and self['B2'] is self['C2']:
return self['A2'] + ' wins!'
if self['A3'] is not None and self['A3'] == self['B3'] \
and self['B3'] is self['C3']:
return self['A3'] + ' wins!'
if self['A1'] is not None and self['A1'] == self['B2'] \
and self['B2'] is self['C3']:
return self['A1'] + ' wins!'
if self['A3'] is not None and self['A3'] == self['B2'] \
and self['B2'] is self['C1']:
return self['A3'] + ' wins!'
if self.count is not 9:
return 'Game in progress.'
return 'Draw!'
def __setitem__(self, position, player):
match = re.search('^[ABC][123]$', position)
if match is None:
raise InvalidKey
if player != 'X' and player != 'O':
raise InvalidValue
if self.last == player:
raise NotYourTurn
if position in self.board:
raise InvalidMove
self.board[position] = player
- self.last_player = player
+ self.last = player
self.count += 1
def __str__(self):
result = '\n -------------\n'
for row in ['3', '2', '1']:
result += row + ' |'
for col in ['A', 'B', 'C']:
if self[col+row] is not None:
result += ' ' + self[col+row] + ' |'
else:
result += ' |'
result += '\n -------------\n'
result += ' A B C \n'
return result