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

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

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

Резултати

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

Код

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard(dict):
def __init__(self):
self.winner = ''
dict.__init__(self)
self.update({
'A1': ' ',
'A2': ' ',
'A3': ' ',
'B1': ' ',
'B2': ' ',
'B3': ' ',
'C1': ' ',
'C2': ' ',
'C3': ' ', })
def __setitem__(self, key, item):
x_count = len([x for x in self.values() if x == 'X'])
o_count = len([o for o in self.values() if o == 'O'])
try:
if key not in self.keys():
raise InvalidKey
if self[key] == 'O' or self[key] == 'X':
raise InvalidMove
if item not in ['X', 'O']:
raise InvalidValue
if (x_count > o_count and item == 'X' or
x_count < o_count and item == 'O'):
raise NotYourTurn
super(TicTacToeBoard, self).__setitem__(key, item)
except InvalidKey:
raise InvalidKey
except InvalidMove:
raise InvalidMove
except InvalidValue:
raise InvalidValue
except NotYourTurn:
raise NotYourTurn
else:
first_winner = self.game_status()
if 'win' in first_winner and not self.winner:
self.winner = first_winner
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self['A3'] + ' | ' + self['B3'] + ' | ' \
+ self['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self['A2'] + ' | ' + self['B2'] + ' | ' \
+ self['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self['A1'] + ' | ' + self['B1'] + ' | ' \
+ self['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def game_status(self):
if self.winner:
return self.winner
winner_list = [[self['A1'], self['A2'], self['A3']],
[self['B1'], self['B2'], self['B3']],
[self['C1'], self['C2'], self['C3']],
[self['A1'], self['B1'], self['C1']],
[self['A2'], self['B2'], self['C2']],
[self['A3'], self['B3'], self['C3']],
[self['A1'], self['B2'], self['C3']],
[self['A3'], self['B2'], self['C1']]]
X_or_O = ''
for element in winner_list:
if element == ['X', 'X', 'X']:
X_or_O = 'X'
elif element == ['O', 'O', 'O']:
X_or_O = 'O'
if X_or_O:
self.winner = X_or_O + ' wins!'
return (X_or_O + ' wins!')
else:
for value in self.values():
if value == ' ':
return 'Game in progress.'
return 'Draw!'

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.178s

OK

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

Димитър обнови решението на 12.04.2013 02:50 (преди около 11 години)

+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+
+
+class TicTacToeBoard(dict):
+ def __init__(self):
+ dict.__init__(self)
+ self.update({
+ 'A1': ' ',
+ 'A2': ' ',
+ 'A3': ' ',
+ 'B1': ' ',
+ 'B2': ' ',
+ 'B3': ' ',
+ 'C1': ' ',
+ 'C2': ' ',
+ 'C3': ' ', })
+
+ def __setitem__(self, key, item):
+ x_count = len([x for x in self.values() if x == 'X'])
+ o_count = len([o for o in self.values() if o == 'O'])
+ try:
+ if key not in self.keys():
+ raise InvalidKey
+ if self[key] == 'O' or self[key] == 'X':
+ raise InvalidMove
+ if item not in ['X', 'O']:
+ raise InvalidValue
+ if (x_count > o_count and item == 'X' or
+ x_count < o_count and item == 'O'):
+ raise NotYourTurn
+ super(TicTacToeBoard, self).__setitem__(key, item)
+ except InvalidKey:
+ raise InvalidKey
+ except InvalidMove:
+ raise InvalidMove
+ except InvalidValue:
+ raise InvalidValue
+ except NotYourTurn:
+ raise NotYourTurn
+
+ def __str__(self):
+ return '\n -------------\n' +\
+ '3 | ' + self['A3'] + ' | ' + self['B3'] + ' | ' \
+ + self['C3'] + ' |\n' +\
+ ' -------------\n' +\
+ '2 | ' + self['A2'] + ' | ' + self['B2'] + ' | ' \
+ + self['C2'] + ' |\n' +\
+ ' -------------\n' +\
+ '1 | ' + self['A1'] + ' | ' + self['B1'] + ' | ' \
+ + self['C1'] + ' |\n' +\
+ ' -------------\n' +\
+ ' A B C \n'
+
+ def game_status(self):
+ x = 'X'
+ o = 'O'
+ x_A_wins = self['A1'] == x and self['A2'] == x and self['A3'] == x
+ x_B_wins = self['B1'] == x and self['B2'] == x and self['B3'] == x
+ x_C_wins = self['C1'] == x and self['C2'] == x and self['C3'] == x
+ x_1_wins = self['A1'] == x and self['B1'] == x and self['C1'] == x
+ x_2_wins = self['A2'] == x and self['B2'] == x and self['C2'] == x
+ x_3_wins = self['A3'] == x and self['B3'] == x and self['C3'] == x
+ x_slash_wins = self['A1'] == x and self['B2'] == x and self['C3'] == x
+ x_bslash_wins = self['A3'] == x and self['B2'] == x and self['C1'] == x
+ o_A_wins = self['A1'] == o and self['A2'] == o and self['A3'] == o
+ o_B_wins = self['B1'] == o and self['B2'] == o and self['B3'] == o
+ o_C_wins = self['C1'] == o and self['C2'] == o and self['C3'] == o
+ o_1_wins = self['A1'] == o and self['B1'] == o and self['C1'] == o
+ o_2_wins = self['A2'] == o and self['B2'] == o and self['C2'] == o
+ o_3_wins = self['A3'] == o and self['B3'] == o and self['C3'] == o
+ o_slash_wins = self['A1'] == o and self['B2'] == o and self['C3'] == o
+ o_bslash_wins = self['A3'] == o and self['B2'] == o and self['C1'] == o
+ if (x_A_wins or x_B_wins or x_C_wins or x_1_wins or x_2_wins or
+ x_3_wins or x_slash_wins or x_bslash_wins):
+ return 'X wins!'
+ elif (o_A_wins or o_B_wins or o_C_wins or o_1_wins or o_2_wins
+ or o_3_wins or o_slash_wins or o_bslash_wins):
+ return 'O wins!'
+ else:
+ for value in self.values():
+ if value == ' ':
+ return 'Game in progress.'
+ return 'Draw!'

Димитър обнови решението на 12.04.2013 20:08 (преди около 11 години)

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard(dict):
def __init__(self):
dict.__init__(self)
self.update({
'A1': ' ',
'A2': ' ',
'A3': ' ',
'B1': ' ',
'B2': ' ',
'B3': ' ',
'C1': ' ',
'C2': ' ',
'C3': ' ', })
def __setitem__(self, key, item):
x_count = len([x for x in self.values() if x == 'X'])
o_count = len([o for o in self.values() if o == 'O'])
try:
if key not in self.keys():
raise InvalidKey
if self[key] == 'O' or self[key] == 'X':
raise InvalidMove
if item not in ['X', 'O']:
raise InvalidValue
if (x_count > o_count and item == 'X' or
x_count < o_count and item == 'O'):
raise NotYourTurn
super(TicTacToeBoard, self).__setitem__(key, item)
except InvalidKey:
raise InvalidKey
except InvalidMove:
raise InvalidMove
except InvalidValue:
raise InvalidValue
except NotYourTurn:
raise NotYourTurn
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self['A3'] + ' | ' + self['B3'] + ' | ' \
+ self['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self['A2'] + ' | ' + self['B2'] + ' | ' \
+ self['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self['A1'] + ' | ' + self['B1'] + ' | ' \
+ self['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def game_status(self):
- x = 'X'
- o = 'O'
- x_A_wins = self['A1'] == x and self['A2'] == x and self['A3'] == x
- x_B_wins = self['B1'] == x and self['B2'] == x and self['B3'] == x
- x_C_wins = self['C1'] == x and self['C2'] == x and self['C3'] == x
- x_1_wins = self['A1'] == x and self['B1'] == x and self['C1'] == x
- x_2_wins = self['A2'] == x and self['B2'] == x and self['C2'] == x
- x_3_wins = self['A3'] == x and self['B3'] == x and self['C3'] == x
- x_slash_wins = self['A1'] == x and self['B2'] == x and self['C3'] == x
- x_bslash_wins = self['A3'] == x and self['B2'] == x and self['C1'] == x
- o_A_wins = self['A1'] == o and self['A2'] == o and self['A3'] == o
- o_B_wins = self['B1'] == o and self['B2'] == o and self['B3'] == o
- o_C_wins = self['C1'] == o and self['C2'] == o and self['C3'] == o
- o_1_wins = self['A1'] == o and self['B1'] == o and self['C1'] == o
- o_2_wins = self['A2'] == o and self['B2'] == o and self['C2'] == o
- o_3_wins = self['A3'] == o and self['B3'] == o and self['C3'] == o
- o_slash_wins = self['A1'] == o and self['B2'] == o and self['C3'] == o
- o_bslash_wins = self['A3'] == o and self['B2'] == o and self['C1'] == o
- if (x_A_wins or x_B_wins or x_C_wins or x_1_wins or x_2_wins or
- x_3_wins or x_slash_wins or x_bslash_wins):
- return 'X wins!'
- elif (o_A_wins or o_B_wins or o_C_wins or o_1_wins or o_2_wins
- or o_3_wins or o_slash_wins or o_bslash_wins):
- return 'O wins!'
+ winner_list = [[self['A1'], self['A2'], self['A3']],
+ [self['B1'], self['B2'], self['B3']],
+ [self['C1'], self['C2'], self['C3']],
+ [self['A1'], self['B1'], self['C1']],
+ [self['A2'], self['B2'], self['C2']],
+ [self['A3'], self['B3'], self['C3']],
+ [self['A1'], self['B2'], self['C3']],
+ [self['A3'], self['B2'], self['C1']]]
+ X_or_O = ''
+ for element in winner_list:
+ if element == ['X', 'X', 'X']:
+ X_or_O = 'X'
+ elif element == ['O', 'O', 'O']:
+ X_or_O = 'O'
+ if X_or_O:
+ return (X_or_O + ' wins!')
else:
for value in self.values():
if value == ' ':
return 'Game in progress.'
return 'Draw!'

Димитър обнови решението на 12.04.2013 20:30 (преди около 11 години)

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard(dict):
def __init__(self):
+ self.winner = ''
dict.__init__(self)
self.update({
'A1': ' ',
'A2': ' ',
'A3': ' ',
'B1': ' ',
'B2': ' ',
'B3': ' ',
'C1': ' ',
'C2': ' ',
'C3': ' ', })
def __setitem__(self, key, item):
x_count = len([x for x in self.values() if x == 'X'])
o_count = len([o for o in self.values() if o == 'O'])
try:
if key not in self.keys():
raise InvalidKey
if self[key] == 'O' or self[key] == 'X':
raise InvalidMove
if item not in ['X', 'O']:
raise InvalidValue
if (x_count > o_count and item == 'X' or
x_count < o_count and item == 'O'):
raise NotYourTurn
super(TicTacToeBoard, self).__setitem__(key, item)
except InvalidKey:
raise InvalidKey
except InvalidMove:
raise InvalidMove
except InvalidValue:
raise InvalidValue
except NotYourTurn:
raise NotYourTurn
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self['A3'] + ' | ' + self['B3'] + ' | ' \
+ self['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self['A2'] + ' | ' + self['B2'] + ' | ' \
+ self['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self['A1'] + ' | ' + self['B1'] + ' | ' \
+ self['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def game_status(self):
+ if self.winner:
+ return self.winner
winner_list = [[self['A1'], self['A2'], self['A3']],
[self['B1'], self['B2'], self['B3']],
[self['C1'], self['C2'], self['C3']],
[self['A1'], self['B1'], self['C1']],
[self['A2'], self['B2'], self['C2']],
[self['A3'], self['B3'], self['C3']],
[self['A1'], self['B2'], self['C3']],
[self['A3'], self['B2'], self['C1']]]
X_or_O = ''
for element in winner_list:
if element == ['X', 'X', 'X']:
X_or_O = 'X'
elif element == ['O', 'O', 'O']:
X_or_O = 'O'
if X_or_O:
+ self.winner = X_or_O + ' wins!'
return (X_or_O + ' wins!')
else:
for value in self.values():
if value == ' ':
return 'Game in progress.'
return 'Draw!'

Димитър обнови решението на 12.04.2013 22:38 (преди около 11 години)

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard(dict):
def __init__(self):
self.winner = ''
dict.__init__(self)
self.update({
'A1': ' ',
'A2': ' ',
'A3': ' ',
'B1': ' ',
'B2': ' ',
'B3': ' ',
'C1': ' ',
'C2': ' ',
'C3': ' ', })
def __setitem__(self, key, item):
x_count = len([x for x in self.values() if x == 'X'])
o_count = len([o for o in self.values() if o == 'O'])
try:
if key not in self.keys():
raise InvalidKey
if self[key] == 'O' or self[key] == 'X':
raise InvalidMove
if item not in ['X', 'O']:
raise InvalidValue
if (x_count > o_count and item == 'X' or
x_count < o_count and item == 'O'):
raise NotYourTurn
super(TicTacToeBoard, self).__setitem__(key, item)
except InvalidKey:
raise InvalidKey
except InvalidMove:
raise InvalidMove
except InvalidValue:
raise InvalidValue
except NotYourTurn:
raise NotYourTurn
+ else:
+ first_winner = self.game_status()
+ if 'win' in first_winner and not self.winner:
+ self.winner = first_winner
def __str__(self):
return '\n -------------\n' +\
'3 | ' + self['A3'] + ' | ' + self['B3'] + ' | ' \
+ self['C3'] + ' |\n' +\
' -------------\n' +\
'2 | ' + self['A2'] + ' | ' + self['B2'] + ' | ' \
+ self['C2'] + ' |\n' +\
' -------------\n' +\
'1 | ' + self['A1'] + ' | ' + self['B1'] + ' | ' \
+ self['C1'] + ' |\n' +\
' -------------\n' +\
' A B C \n'
def game_status(self):
if self.winner:
return self.winner
winner_list = [[self['A1'], self['A2'], self['A3']],
[self['B1'], self['B2'], self['B3']],
[self['C1'], self['C2'], self['C3']],
[self['A1'], self['B1'], self['C1']],
[self['A2'], self['B2'], self['C2']],
[self['A3'], self['B3'], self['C3']],
[self['A1'], self['B2'], self['C3']],
[self['A3'], self['B2'], self['C1']]]
X_or_O = ''
for element in winner_list:
if element == ['X', 'X', 'X']:
X_or_O = 'X'
elif element == ['O', 'O', 'O']:
X_or_O = 'O'
if X_or_O:
self.winner = X_or_O + ' wins!'
return (X_or_O + ' wins!')
else:
for value in self.values():
if value == ' ':
return 'Game in progress.'
return 'Draw!'