Решение на Морски шах от Ния Дончева

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

Към профила на Ния Дончева

Резултати

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

Код

class TicTacToeBoard:
def __init__(self):
self.fields = {'A1': ' ', 'A2': ' ', 'A3': ' ',
'B1': ' ', 'B2': ' ', 'B3': ' ',
'C1': ' ', 'C2': ' ', 'C3': ' '}
self.values = ('X', 'O')
self.count = 0
self.last_move = None
def __setitem__(self, field, value):
if field not in self.fields:
raise InvalidKey
if value not in self.values:
raise InvalidValue
if self.fields[field] != ' ':
raise InvalidMove
if self.last_move == value:
raise NotYourTurn
self.fields[field] = value
self.last_move = value
self.count += 1
def __getitem__(self, key):
return self.fields[key]
def __str__(self):
row = '{} | {} | {} | {} |\n'
place = self.fields
board = '\n -------------\n' +\
row.format('3', place['A3'], place['B3'], place['C3']) +\
' -------------\n' +\
row.format('2', place['A2'], place['B2'], place['C2']) +\
' -------------\n' +\
row.format('1', place['A1'], place['B1'], place['C1']) +\
' -------------\n' +\
' A B C \n'
return board
def check_for_win(self, mark):
win_rows = [[self.fields['A1'], self.fields['B1'], self.fields['C1']],
[self.fields['A2'], self.fields['B2'], self.fields['C2']],
[self.fields['A3'], self.fields['B3'], self.fields['C3']],
[self.fields['A1'], self.fields['A2'], self.fields['A3']],
[self.fields['B1'], self.fields['B2'], self.fields['B3']],
[self.fields['C1'], self.fields['C2'], self.fields['C3']],
[self.fields['C1'], self.fields['B2'], self.fields['A3']],
[self.fields['A1'], self.fields['B2'], self.fields['C3']]]
for row in win_rows:
if row[0] == row[1] == row[2] == mark:
return True
return False
def game_status(self):
if self.check_for_win('X'):
return 'X wins!'
elif self.check_for_win('O'):
return 'O wins!'
elif self.count == 9:
return 'Draw!'
else:
return 'Game in progress.'
class InvalidKey(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidMove(Exception):
pass
class NotYourTurn(Exception):
pass

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.149s

OK

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

Ния обнови решението на 13.04.2013 01:40 (преди над 11 години)

+class TicTacToeBoard:
+ def __init__(self):
+ self.fields = {'A1': ' ', 'A2': ' ', 'A3': ' ',
+ 'B1': ' ', 'B2': ' ', 'B3': ' ',
+ 'C1': ' ', 'C2': ' ', 'C3': ' '}
+ self.values = ('X', 'O')
+ self.count = 0
+ self.last_move = None
+
+ def __setitem__(self, field, value):
+ if field not in self.fields:
+ raise InvalidKey
+ if value not in self.values:
+ raise InvalidValue
+ if self.fields[field] != ' ':
+ raise InvalidMove
+ if self.last_move == value:
+ raise NotYourTurn
+
+ self.fields[field] = value
+ self.last_move = value
+ self.count += 1
+
+ def __getitem__(self, key):
+ return self.fields[key]
+
+ def __str__(self):
+ board = '\n -------------\n' +\
+ '3 | ' + self.fields['A3'] + ' | ' + self.fields['B3'] +\
+ ' | ' + self.fields['C3'] + ' |\n' +\
+ ' -------------\n' +\
+ '2 | ' + self.fields['A2'] + ' | ' + self.fields['B2'] +\
+ ' | ' + self.fields['C2'] + ' |\n' +\
+ ' -------------\n' +\
+ '1 | ' + self.fields['A1'] + ' | ' + self.fields['B1'] +\
+ ' | ' + self.fields['C1'] + ' |\n' +\
+ ' -------------\n' +\
+ ' A B C \n'
+ return board
+
+ def check_for_win(self, mark):
+ win_rows = [[self.fields['A1'], self.fields['B1'], self.fields['C1']],
+ [self.fields['A2'], self.fields['B2'], self.fields['C2']],
+ [self.fields['A3'], self.fields['B3'], self.fields['C3']],
+ [self.fields['A1'], self.fields['A2'], self.fields['A3']],
+ [self.fields['B1'], self.fields['B2'], self.fields['B3']],
+ [self.fields['C1'], self.fields['C2'], self.fields['C3']],
+ [self.fields['C1'], self.fields['B2'], self.fields['A3']],
+ [self.fields['A1'], self.fields['B2'], self.fields['C3']]]
+
+ for row in win_rows:
+ if row[0] == row[1] == row[2] == mark:
+ return True
+ return False
+
+ def game_status(self):
+ if self.check_for_win('X'):
+ return 'X wins!'
+ elif self.check_for_win('O'):
+ return 'O wins!'
+ elif self.count == 9:
+ return 'Draw!'
+ else:
+ return 'Game in progress.'
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass
+

Ния обнови решението на 13.04.2013 19:13 (преди над 11 години)

class TicTacToeBoard:
def __init__(self):
self.fields = {'A1': ' ', 'A2': ' ', 'A3': ' ',
'B1': ' ', 'B2': ' ', 'B3': ' ',
'C1': ' ', 'C2': ' ', 'C3': ' '}
self.values = ('X', 'O')
self.count = 0
self.last_move = None
def __setitem__(self, field, value):
if field not in self.fields:
raise InvalidKey
if value not in self.values:
raise InvalidValue
if self.fields[field] != ' ':
raise InvalidMove
if self.last_move == value:
raise NotYourTurn
self.fields[field] = value
self.last_move = value
self.count += 1
def __getitem__(self, key):
return self.fields[key]
def __str__(self):
+ row = '{} | {} | {} | {} |\n'
+ field = self.fields
+
board = '\n -------------\n' +\
- '3 | ' + self.fields['A3'] + ' | ' + self.fields['B3'] +\
- ' | ' + self.fields['C3'] + ' |\n' +\
+ row.format('3', field['A3'], field['B3'], field['C3']) +\
' -------------\n' +\
- '2 | ' + self.fields['A2'] + ' | ' + self.fields['B2'] +\
- ' | ' + self.fields['C2'] + ' |\n' +\
+ row.format('2', field['A2'], field['B2'], field['C2']) +\
' -------------\n' +\
- '1 | ' + self.fields['A1'] + ' | ' + self.fields['B1'] +\
- ' | ' + self.fields['C1'] + ' |\n' +\
+ row.format('1', field['A1'], field['B1'], field['C1']) +\
' -------------\n' +\
' A B C \n'
+
return board
def check_for_win(self, mark):
win_rows = [[self.fields['A1'], self.fields['B1'], self.fields['C1']],
[self.fields['A2'], self.fields['B2'], self.fields['C2']],
[self.fields['A3'], self.fields['B3'], self.fields['C3']],
[self.fields['A1'], self.fields['A2'], self.fields['A3']],
[self.fields['B1'], self.fields['B2'], self.fields['B3']],
[self.fields['C1'], self.fields['C2'], self.fields['C3']],
[self.fields['C1'], self.fields['B2'], self.fields['A3']],
[self.fields['A1'], self.fields['B2'], self.fields['C3']]]
for row in win_rows:
if row[0] == row[1] == row[2] == mark:
return True
return False
def game_status(self):
if self.check_for_win('X'):
return 'X wins!'
elif self.check_for_win('O'):
return 'O wins!'
elif self.count == 9:
return 'Draw!'
else:
return 'Game in progress.'
class InvalidKey(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidMove(Exception):
pass
class NotYourTurn(Exception):
pass
-

Ния обнови решението на 13.04.2013 19:23 (преди над 11 години)

class TicTacToeBoard:
def __init__(self):
self.fields = {'A1': ' ', 'A2': ' ', 'A3': ' ',
'B1': ' ', 'B2': ' ', 'B3': ' ',
'C1': ' ', 'C2': ' ', 'C3': ' '}
+
self.values = ('X', 'O')
self.count = 0
self.last_move = None
def __setitem__(self, field, value):
if field not in self.fields:
raise InvalidKey
if value not in self.values:
raise InvalidValue
if self.fields[field] != ' ':
raise InvalidMove
if self.last_move == value:
raise NotYourTurn
self.fields[field] = value
self.last_move = value
self.count += 1
def __getitem__(self, key):
return self.fields[key]
def __str__(self):
row = '{} | {} | {} | {} |\n'
- field = self.fields
+ place = self.fields
board = '\n -------------\n' +\
- row.format('3', field['A3'], field['B3'], field['C3']) +\
+ row.format('3', place['A3'], place['B3'], place['C3']) +\
' -------------\n' +\
- row.format('2', field['A2'], field['B2'], field['C2']) +\
+ row.format('2', place['A2'], place['B2'], place['C2']) +\
' -------------\n' +\
- row.format('1', field['A1'], field['B1'], field['C1']) +\
+ row.format('1', place['A1'], place['B1'], place['C1']) +\
' -------------\n' +\
' A B C \n'
return board
def check_for_win(self, mark):
win_rows = [[self.fields['A1'], self.fields['B1'], self.fields['C1']],
[self.fields['A2'], self.fields['B2'], self.fields['C2']],
[self.fields['A3'], self.fields['B3'], self.fields['C3']],
[self.fields['A1'], self.fields['A2'], self.fields['A3']],
[self.fields['B1'], self.fields['B2'], self.fields['B3']],
[self.fields['C1'], self.fields['C2'], self.fields['C3']],
[self.fields['C1'], self.fields['B2'], self.fields['A3']],
[self.fields['A1'], self.fields['B2'], self.fields['C3']]]
for row in win_rows:
if row[0] == row[1] == row[2] == mark:
return True
return False
def game_status(self):
if self.check_for_win('X'):
return 'X wins!'
elif self.check_for_win('O'):
return 'O wins!'
elif self.count == 9:
return 'Draw!'
else:
return 'Game in progress.'
class InvalidKey(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidMove(Exception):
pass
class NotYourTurn(Exception):
pass