Решение на Морски шах от Георги Урумов

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

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

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 7 успешни тест(а)
  • 1 неуспешни тест(а)

Код

import re
class InvalidMove(Exception):
def __init__(self):
self.message = "Invalid move! Field already contains a value!"
class InvalidValue(Exception):
def __init__(self):
self.message = "Invalid value! Allowed values are 'X' and 'O'!"
class InvalidKey(Exception):
def __init__(self):
self.message = ("Invalid coordinates! Vallid coordinates are cappital"
"letters A-C followed by numbers 1-3")
class NotYourTurn(Exception):
def __init__(self):
self.message = "It is the other players turn!"
class TicTacToeBoard(object):
def __init__(self):
self.table = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
self.lastTurn = ""
self.first = 1
self.gameStatus = 'Game in progress.'
def __str__(self):
return ('\n -------------\n'
'3 | {0} | {1} | {2} |\n'
' -------------\n'
'2 | {3} | {4} | {5} |\n'
' -------------\n'
'1 | {6} | {7} | {8} |\n'
' -------------\n'
' A B C \n'.format(self.table[0][0], self.table[0][1],
self.table[0][2], self.table[1][0], self.table[1][1],
self.table[1][2], self.table[2][0], self.table[2][1],
self.table[2][2]))
def __setitem__(self, name, value):
if (name is not None and value is not None):
keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
valueMatches = bool(re.search(r'^[XO]$', value.__str__()))
if (not keyMatches):
raise InvalidKey
elif (not valueMatches):
raise InvalidValue
else:
if (self.first):
self.first = 0
elif (self.lastTurn == value.__str__()):
raise NotYourTurn
self.setField(name.__str__()[0],
name.__str__()[1], value.__str__())
def __getitem__(self, name):
if (name is not None):
keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
if (not keyMatches):
raise InvalidKey
else:
self.getField(name.__str__()[0], name.__str__()[1])
def setField(self, collumn, row, value):
realCollumn = ord(collumn) - ord('A')
realRow = 3 - (ord(row) - ord('0'))
field = self.table[realRow][realCollumn]
if (field != " "):
raise InvalidMove
else:
self.table[realRow][realCollumn] = value
self.lastTurn = value
self.checkState(realRow, realCollumn, value)
def getField(self, collumn, row):
realCollumn = ord(collumn) - ord('A')
realRow = 3 - (ord(row) - ord('0'))
print ("'{0}'".format(self.table[realRow][realCollumn]))
def game_status(self):
return self.gameStatus
def checkState(self, row, collumn, value):
if (self.gameStatus == 'Game in progress.'):
win = [value, value, value]
a = []
[a.extend(x) for x in self.table]
if (" " not in a):
self.gameStatus = 'Draw!'
elif (
win in [self.table[row], [self.table[0][collumn],
self.table[1][collumn], self.table[2][collumn]],
[self.table[0][0], self.table[1][1], self.table[2][2]],
[self.table[2][0], self.table[1][1], self.table[0][2]]]):
self.gameStatus = '{0} wins!'.format(
self.table[row][collumn])

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

..F.....
======================================================================
FAIL: test_input_format (test.TicTacHomeworkTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130415-29081-ou3jhi/test.py", line 57, in test_input_format
    o['A1'] = None
AssertionError: InvalidValue not raised

----------------------------------------------------------------------
Ran 8 tests in 0.010s

FAILED (failures=1)

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

Георги обнови решението на 15.04.2013 15:08 (преди над 11 години)

+import re
+
+
+class InvalidMove(Exception):
+
+ def __init__(self):
+ self.message = "Invalid move! Field already contains a value!"
+
+
+class InvalidValue(Exception):
+
+ def __init__(self):
+ self.message = "Invalid value! Allowed values are 'X' and 'O'!"
+
+
+class InvalidKey(Exception):
+
+ def __init__(self):
+ self.message = ("Invalid coordinates! Vallid coordinates are cappital"
+ "letters A-C followed by numbers 1-3")
+
+
+class NotYourTurn(Exception):
+
+ def __init__(self):
+ self.message = "It is the other players turn!"
+
+
+class TicTacToeBoard(object):
+
+ def __init__(self):
+ self.table = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
+ self.lastTurn = ""
+ self.first = 1
+ self.gameStatus = 'Game in progress.'
+
+ def __str__(self):
+ return ('\n -------------\n'
+ '3 | {0} | {1} | {2} |\n'
+ ' -------------\n'
+ '2 | {3} | {4} | {5} |\n'
+ ' -------------\n'
+ '1 | {6} | {7} | {8} |\n'
+ ' -------------\n'
+ ' A B C \n'.format(self.table[0][0], self.table[0][1],
+ self.table[0][2], self.table[1][0], self.table[1][1],
+ self.table[1][2], self.table[2][0], self.table[2][1],
+ self.table[2][2]))
+
+ def __setitem__(self, name, value):
+ if (name is not None and value is not None):
+ keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
+ valueMatches = bool(re.search(r'^[XO]$', value.__str__()))
+ if (not keyMatches):
+ raise InvalidKey
+ elif (not valueMatches):
+ raise InvalidValue
+ else:
+ if (self.first):
+ self.first = 0
+ elif (self.lastTurn == value.__str__()):
+ raise NotYourTurn
+ self.setField(name.__str__()[0],
+ name.__str__()[1], value.__str__())
+
+ def __getitem__(self, name):
+ if (name is not None):
+ keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
+ if (not keyMatches):
+ raise InvalidKey
+ else:
+ self.getField(name.__str__()[0], name.__str__()[1])
+
+ def setField(self, collumn, row, value):
+ realCollumn = ord(collumn) - ord('A')
+ realRow = 3 - (ord(row) - ord('0'))
+ field = self.table[realRow][realCollumn]
+ if (field != " "):
+ raise InvalidMove
+ else:
+ self.table[realRow][realCollumn] = value
+ self.lastTurn = value
+ self.checkState(realRow, realCollumn, value)
+
+ def getField(self, collumn, row):
+ realCollumn = ord(collumn) - ord('A')
+ realRow = 3 - (ord(row) - ord('0'))
+ print ("'{0}'".format(self.table[realRow][realCollumn]))
+
+ def game_status(self):
+ return self.gameStatus
+
+ def checkState(self, row, collumn, value):
+ if (self.gameStatus == 'Game in progress.'):
+ win = [value, value, value]
+ a = []
+ [a.extend(x) for x in self.table]
+ if (" " not in a):
+ self.gameStatus = 'Draw!'
+ elif (
+ win in [self.table[row], [self.table[collumn][0],
+ self.table[collumn][1], self.table[collumn][2]],
+ [self.table[0][0], self.table[1][1], self.table[2][2]],
+ [self.table[2][0], self.table[1][1], self.table[0][2]]]):
+ self.gameStatus = '{0} wins!'.format(
+ self.table[row][collumn])

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

import re
class InvalidMove(Exception):
def __init__(self):
self.message = "Invalid move! Field already contains a value!"
class InvalidValue(Exception):
def __init__(self):
self.message = "Invalid value! Allowed values are 'X' and 'O'!"
class InvalidKey(Exception):
def __init__(self):
self.message = ("Invalid coordinates! Vallid coordinates are cappital"
"letters A-C followed by numbers 1-3")
class NotYourTurn(Exception):
def __init__(self):
self.message = "It is the other players turn!"
class TicTacToeBoard(object):
def __init__(self):
self.table = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
self.lastTurn = ""
self.first = 1
self.gameStatus = 'Game in progress.'
def __str__(self):
return ('\n -------------\n'
'3 | {0} | {1} | {2} |\n'
' -------------\n'
'2 | {3} | {4} | {5} |\n'
' -------------\n'
'1 | {6} | {7} | {8} |\n'
' -------------\n'
' A B C \n'.format(self.table[0][0], self.table[0][1],
self.table[0][2], self.table[1][0], self.table[1][1],
self.table[1][2], self.table[2][0], self.table[2][1],
self.table[2][2]))
def __setitem__(self, name, value):
if (name is not None and value is not None):
keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
valueMatches = bool(re.search(r'^[XO]$', value.__str__()))
if (not keyMatches):
raise InvalidKey
elif (not valueMatches):
raise InvalidValue
else:
if (self.first):
self.first = 0
elif (self.lastTurn == value.__str__()):
raise NotYourTurn
self.setField(name.__str__()[0],
name.__str__()[1], value.__str__())
def __getitem__(self, name):
if (name is not None):
keyMatches = bool(re.search(r'^[ABC][123]$', name.__str__()))
if (not keyMatches):
raise InvalidKey
else:
self.getField(name.__str__()[0], name.__str__()[1])
def setField(self, collumn, row, value):
realCollumn = ord(collumn) - ord('A')
realRow = 3 - (ord(row) - ord('0'))
field = self.table[realRow][realCollumn]
if (field != " "):
raise InvalidMove
else:
self.table[realRow][realCollumn] = value
self.lastTurn = value
self.checkState(realRow, realCollumn, value)
def getField(self, collumn, row):
realCollumn = ord(collumn) - ord('A')
realRow = 3 - (ord(row) - ord('0'))
print ("'{0}'".format(self.table[realRow][realCollumn]))
def game_status(self):
return self.gameStatus
def checkState(self, row, collumn, value):
if (self.gameStatus == 'Game in progress.'):
win = [value, value, value]
a = []
[a.extend(x) for x in self.table]
if (" " not in a):
self.gameStatus = 'Draw!'
elif (
- win in [self.table[row], [self.table[collumn][0],
- self.table[collumn][1], self.table[collumn][2]],
+ win in [self.table[row], [self.table[0][collumn],
+ self.table[1][collumn], self.table[2][collumn]],
[self.table[0][0], self.table[1][1], self.table[2][2]],
[self.table[2][0], self.table[1][1], self.table[0][2]]]):
self.gameStatus = '{0} wins!'.format(
- self.table[row][collumn])
+ self.table[row][collumn])