Решение на Морски шах от Георги Шопов

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

Към профила на Георги Шопов

Резултати

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

Код

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif value != 'O' and value != 'X':
raise InvalidValue
elif self.field[key] != ' ':
raise InvalidMove
elif self.previous_player == value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
self._determine_status()
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def _determine_status(self):
if self.status == 'Game in progress.':
for prefix in ['A', 'B', 'C']:
if (self.field[prefix + '1'] == self.field[prefix + '2'] ==
self.field[prefix + '3'] != ' '):
self.status = self.field[prefix + '1'] + ' wins!'
break
if self.status == 'Game in progress.':
for postfix in ['111', '222', '333', '123', '321']:
if (self.field['A' + postfix[0]] ==
self.field['B' + postfix[1]] ==
self.field['C' + postfix[2]] != ' '):
self.status = (self.field['A' + postfix[0]] +
' wins!')
break
if (self.status == 'Game in progress.' and
' ' not in self.field.values()):
self.status = 'Draw!'
def game_status(self):
return self.status

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.160s

OK

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

Георги обнови решението на 12.04.2013 18:45 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
+ 'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
+
+ self.previous_player = None
+ self.status = 'Game in progress.'
+
+ def __getitem__(self, key):
+ return self.field[key]
+
+ def __setitem__(self, key, value):
+
+ if key not in self.field.keys():
+ raise InvalidKey
+ elif bool(not re.match(r'O|X', value)):
+ raise InvalidValue
+ elif self.field[key] is not ' ':
+ raise InvalidMove
+ elif self.previous_player is value:
+ raise NotYourTurn
+ else:
+ self.field[key] = value
+ self.previous_player = value
+
+ field_values = (self.field['A3'] + self.field['B3'] +
+ self.field['C3'] + '\n' + self.field['A2'] +
+ self.field['B2'] + self.field['C2'] + '\n' +
+ self.field['A1'] + self.field['B1'] +
+ self.field['C1'] + '\n')
+
+ if self.status == 'Game in progress.':
+ search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
+ (O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4| \
+ .(O|X).\n.\5.\n.\5|..(O|X)\n..\6\n.\
+ .\6', field_values)
+ if bool(search_result):
+ if 'O' in search_result.groups():
+ self.status = 'O wins!'
+ else:
+ self.status = 'X wins!'
+ else:
+ if ' ' not in self.field.values():
+ self.status = 'Draw!'
+
+ def __str__(self):
+ return ("\n -------------\n" +
+ "3 | {} | {} | {} |\n" +
+ " -------------\n" +
+ "2 | {} | {} | {} |\n" +
+ " -------------\n" +
+ "1 | {} | {} | {} |\n" +
+ " -------------\n" +
+ " A B C \n").format(self.field['A3'], self.field['B3'],
+ self.field['C3'], self.field['A2'],
+ self.field['B2'], self.field['C2'],
+ self.field['A1'], self.field['B1'],
+ self.field['C1'])
+
+ def game_status(self):
+ return self.status

Георги обнови решението на 12.04.2013 18:46 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
-
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
-
+
if key not in self.field.keys():
raise InvalidKey
elif bool(not re.match(r'O|X', value)):
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
field_values = (self.field['A3'] + self.field['B3'] +
self.field['C3'] + '\n' + self.field['A2'] +
self.field['B2'] + self.field['C2'] + '\n' +
self.field['A1'] + self.field['B1'] +
self.field['C1'] + '\n')
if self.status == 'Game in progress.':
- search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
+ search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2| \
(O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4| \
.(O|X).\n.\5.\n.\5|..(O|X)\n..\6\n.\
.\6', field_values)
if bool(search_result):
if 'O' in search_result.groups():
self.status = 'O wins!'
else:
self.status = 'X wins!'
else:
if ' ' not in self.field.values():
self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Много е интересно че си използвал регулярен израз за game_status обаче това усложнява функцията ти адски много. Ако направиш тази част на питон, а не на regex ще е много по-четливо.

Георги обнови решението на 14.04.2013 13:51 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
+ self.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' '"",
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
-
+
if key not in self.field.keys():
raise InvalidKey
elif bool(not re.match(r'O|X', value)):
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
- field_values = (self.field['A3'] + self.field['B3'] +
- self.field['C3'] + '\n' + self.field['A2'] +
- self.field['B2'] + self.field['C2'] + '\n' +
- self.field['A1'] + self.field['B1'] +
- self.field['C1'] + '\n')
-
if self.status == 'Game in progress.':
- search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2| \
- (O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4| \
- .(O|X).\n.\5.\n.\5|..(O|X)\n..\6\n.\
- .\6', field_values)
- if bool(search_result):
- if 'O' in search_result.groups():
- self.status = 'O wins!'
- else:
- self.status = 'X wins!'
- else:
- if ' ' not in self.field.values():
- self.status = 'Draw!'
+ for prefix in ['AAA', 'BBB', 'CCC']:
+ if bool(re.match(r'OOO|XXX', self.field[prefix[0] + '1'] +
+ self.field[prefix[1] + '2'] +
+ self.field[prefix[2] + '3'])):
+
+ self.status = self.field[prefix[0] + '1'] + ' wins!'
+
+ for postfix in ['111', '222', '333', '123', '321']:
+ if bool(re.match(r'OOO|XXX', self.field['A' + postfix[0]] +
+ self.field['B' + postfix[1]] +
+ self.field['C' + postfix[2]])):
+
+ self.status = self.field['A' + postfix[0]] + ' wins!'
+
+ if ' ' not in self.field.values():
+ self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Георги обнови решението на 14.04.2013 14:05 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' '"",
+ self.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif bool(not re.match(r'O|X', value)):
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
- if self.status == 'Game in progress.':
- for prefix in ['AAA', 'BBB', 'CCC']:
- if bool(re.match(r'OOO|XXX', self.field[prefix[0] + '1'] +
- self.field[prefix[1] + '2'] +
- self.field[prefix[2] + '3'])):
+ field_values = (self.field['A3'] + self.field['B3'] +
+ self.field['C3'] + '\n' + self.field['A2'] +
+ self.field['B2'] + self.field['C2'] + '\n' +
+ self.field['A1'] + self.field['B1'] +
+ self.field['C1'] + '\n')
- self.status = self.field[prefix[0] + '1'] + ' wins!'
-
- for postfix in ['111', '222', '333', '123', '321']:
- if bool(re.match(r'OOO|XXX', self.field['A' + postfix[0]] +
- self.field['B' + postfix[1]] +
- self.field['C' + postfix[2]])):
-
- self.status = self.field['A' + postfix[0]] + ' wins!'
-
- if ' ' not in self.field.values():
- self.status = 'Draw!'
+ if self.status == 'Game in progress.':
+ search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
+ (O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4|\
+ (O|X).\n.\5.\n.\5|\
+ (O|X)\n..\6\n..\6', field_values)
+ if bool(search_result):
+ if 'O' in search_result.groups():
+ self.status = 'O wins!'
+ else:
+ self.status = 'X wins!'
+ else:
+ if ' ' not in self.field.values():
+ self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Георги обнови решението на 14.04.2013 14:07 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif bool(not re.match(r'O|X', value)):
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
field_values = (self.field['A3'] + self.field['B3'] +
self.field['C3'] + '\n' + self.field['A2'] +
self.field['B2'] + self.field['C2'] + '\n' +
self.field['A1'] + self.field['B1'] +
self.field['C1'] + '\n')
if self.status == 'Game in progress.':
search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
(O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4|\
(O|X).\n.\5.\n.\5|\
(O|X)\n..\6\n..\6', field_values)
if bool(search_result):
if 'O' in search_result.groups():
self.status = 'O wins!'
else:
self.status = 'X wins!'
- else:
- if ' ' not in self.field.values():
- self.status = 'Draw!'
+ elif ' ' not in self.field.values():
+ self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Георги обнови решението на 14.04.2013 14:09 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif bool(not re.match(r'O|X', value)):
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
- field_values = (self.field['A3'] + self.field['B3'] +
- self.field['C3'] + '\n' + self.field['A2'] +
- self.field['B2'] + self.field['C2'] + '\n' +
- self.field['A1'] + self.field['B1'] +
- self.field['C1'] + '\n')
-
if self.status == 'Game in progress.':
+ field_values = (self.field['A3'] + self.field['B3'] +
+ self.field['C3'] + '\n' + self.field['A2'] +
+ self.field['B2'] + self.field['C2'] + '\n' +
+ self.field['A1'] + self.field['B1'] +
+ self.field['C1'] + '\n')
+
search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
(O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4|\
(O|X).\n.\5.\n.\5|\
(O|X)\n..\6\n..\6', field_values)
+
if bool(search_result):
if 'O' in search_result.groups():
self.status = 'O wins!'
else:
self.status = 'X wins!'
elif ' ' not in self.field.values():
self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Георги обнови решението на 14.04.2013 20:44 (преди над 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.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
- elif bool(not re.match(r'O|X', value)):
+ elif value != 'O' and value != 'X':
raise InvalidValue
elif self.field[key] is not ' ':
raise InvalidMove
elif self.previous_player is value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
if self.status == 'Game in progress.':
- field_values = (self.field['A3'] + self.field['B3'] +
- self.field['C3'] + '\n' + self.field['A2'] +
- self.field['B2'] + self.field['C2'] + '\n' +
- self.field['A1'] + self.field['B1'] +
- self.field['C1'] + '\n')
+ for prefix in ['A', 'B', 'C']:
+ if (self.field[prefix + '1'] == self.field[prefix + '2'] ==
+ self.field[prefix + '3'] != ' '):
- search_result = re.search(r'(O|X)\1\1\n|(O|X)..\n.\2.\n..\2|\
- (O|X)\n.\3.\n\3|(O|X)..\n\4..\n\4|\
- (O|X).\n.\5.\n.\5|\
- (O|X)\n..\6\n..\6', field_values)
+ self.status = self.field[prefix + '1'] + ' wins!'
+ break
- if bool(search_result):
- if 'O' in search_result.groups():
- self.status = 'O wins!'
- else:
- self.status = 'X wins!'
- elif ' ' not in self.field.values():
- self.status = 'Draw!'
+ if self.status == 'Game in progress.':
+ for postfix in ['111', '222', '333', '123', '321']:
+ if (self.field['A' + postfix[0]] ==
+ self.field['B' + postfix[1]] ==
+ self.field['C' + postfix[2]] != ' '):
+
+ self.status = (self.field['A' + postfix[0]] +
+ ' wins!')
+ break
+
+ if (self.status == 'Game in progress.' and
+ ' ' not in self.field.values()):
+ self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Да има някакъв проблем с \ , когато го използвам за да мога да пиша и на следващия ред. Ако е на 1 ред регулярният израз работи и за диагоналните победи. Както и да е преминах към if-ове :)

Георги обнови решението на 14.04.2013 21:28 (преди над 11 години)

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif value != 'O' and value != 'X':
raise InvalidValue
- elif self.field[key] is not ' ':
+ elif self.field[key] != ' ':
raise InvalidMove
- elif self.previous_player is value:
+ elif self.previous_player == value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
if self.status == 'Game in progress.':
for prefix in ['A', 'B', 'C']:
if (self.field[prefix + '1'] == self.field[prefix + '2'] ==
self.field[prefix + '3'] != ' '):
self.status = self.field[prefix + '1'] + ' wins!'
break
if self.status == 'Game in progress.':
for postfix in ['111', '222', '333', '123', '321']:
if (self.field['A' + postfix[0]] ==
self.field['B' + postfix[1]] ==
self.field['C' + postfix[2]] != ' '):
self.status = (self.field['A' + postfix[0]] +
' wins!')
break
if (self.status == 'Game in progress.' and
' ' not in self.field.values()):
self.status = 'Draw!'
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
def game_status(self):
return self.status

Георги обнови решението на 14.04.2013 22:44 (преди над 11 години)

class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass
class TicTacToeBoard:
def __init__(self):
self.field = {'A1': ' ', 'A2': ' ', 'A3': ' ', 'B1': ' ', 'B2': ' ',
'B3': ' ', 'C1': ' ', 'C2': ' ', 'C3': ' '}
self.previous_player = None
self.status = 'Game in progress.'
def __getitem__(self, key):
return self.field[key]
def __setitem__(self, key, value):
if key not in self.field.keys():
raise InvalidKey
elif value != 'O' and value != 'X':
raise InvalidValue
elif self.field[key] != ' ':
raise InvalidMove
elif self.previous_player == value:
raise NotYourTurn
else:
self.field[key] = value
self.previous_player = value
+ self._determine_status()
- if self.status == 'Game in progress.':
- for prefix in ['A', 'B', 'C']:
- if (self.field[prefix + '1'] == self.field[prefix + '2'] ==
- self.field[prefix + '3'] != ' '):
-
- self.status = self.field[prefix + '1'] + ' wins!'
- break
-
- if self.status == 'Game in progress.':
- for postfix in ['111', '222', '333', '123', '321']:
- if (self.field['A' + postfix[0]] ==
- self.field['B' + postfix[1]] ==
- self.field['C' + postfix[2]] != ' '):
-
- self.status = (self.field['A' + postfix[0]] +
- ' wins!')
- break
-
- if (self.status == 'Game in progress.' and
- ' ' not in self.field.values()):
- self.status = 'Draw!'
-
def __str__(self):
return ("\n -------------\n" +
"3 | {} | {} | {} |\n" +
" -------------\n" +
"2 | {} | {} | {} |\n" +
" -------------\n" +
"1 | {} | {} | {} |\n" +
" -------------\n" +
" A B C \n").format(self.field['A3'], self.field['B3'],
self.field['C3'], self.field['A2'],
self.field['B2'], self.field['C2'],
self.field['A1'], self.field['B1'],
self.field['C1'])
+
+ def _determine_status(self):
+ if self.status == 'Game in progress.':
+ for prefix in ['A', 'B', 'C']:
+ if (self.field[prefix + '1'] == self.field[prefix + '2'] ==
+ self.field[prefix + '3'] != ' '):
+
+ self.status = self.field[prefix + '1'] + ' wins!'
+ break
+
+ if self.status == 'Game in progress.':
+ for postfix in ['111', '222', '333', '123', '321']:
+ if (self.field['A' + postfix[0]] ==
+ self.field['B' + postfix[1]] ==
+ self.field['C' + postfix[2]] != ' '):
+
+ self.status = (self.field['A' + postfix[0]] +
+ ' wins!')
+ break
+
+ if (self.status == 'Game in progress.' and
+ ' ' not in self.field.values()):
+ self.status = 'Draw!'
def game_status(self):
return self.status