Решение на Морски шах от Николай Русев

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

Към профила на Николай Русев

Резултати

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

Код

class TicTacToeBoard:
def __init__(self):
self._moves = 0
self._status = 0
self._board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
self._turn = 0
def __getitem__(self, position):
self._check_position(position)
x = ord(position[0]) - ord('A')
y = ord(position[1]) - ord('1')
return self._board[x][y]
def __setitem__(self, position, value):
if self._check_status() != 0:
return
self._check_position(position)
self._check_value(value)
x = ord(position[0]) - ord('A')
y = ord(position[1]) - ord('1')
self._board[x][y] = value
self._moves += 1
if value == 'X':
self._turn = 2
else:
self._turn = 1
def __str__(self):
boarder = '\n -------------\n'
result = boarder
for i in range(0, 3):
result += str(3 - i) + ' '
for j in range(0, 3):
result += '| ' + self._board[j][2 - i] + ' '
result += '|' + boarder
result += ' A B C \n'
return result
def _check_position(self, position):
if (type(position) is not str or
len(position) != 2 or
position[0] < 'A' or
position[0] > 'C' or
position[1] < '1' or
position[1] > '3'):
raise InvalidKey()
x = ord(position[0]) - ord('A')
y = ord(position[1]) - ord('1')
if self._board[x][y] != ' ':
raise InvalidMove()
def _check_value(self, value):
if (value != 'X' and
value != 'O'):
raise InvalidValue()
if (self._turn == 1 and value != 'X' or
self._turn == 2 and value != 'O'):
raise NotYourTurn()
return
def game_status(self):
self._check_status()
if self._status == 0:
return 'Game in progress.'
if self._status == 1:
return 'X wins!'
if self._status == 2:
return 'O wins!'
return 'Draw!'
def _check_status(self):
if self._status != 0:
return self._status
if self._moves == 9:
self._status = 3
return self._status
for i in range(0, 3):
if self._board[i][i] == ' ':
continue
if (self._board[i][0] == self._board[i][1] and
self._board[i][1] == self._board[i][2] and
self._board[i][0] != ' '):
self._set_winner(self._board[i][0])
return self._status
if (self._board[0][i] == self._board[1][i] and
self._board[1][i] == self._board[2][i] and
self._board[0][i] != ' '):
self._set_winner(self._board[0][i])
return self._status
if self._board[1][1] != ' ':
if (self._board[0][0] == self._board[1][1] and
self._board[1][1] == self._board[2][2] or
self._board[0][2] == self._board[1][1] and
self._board[1][1] == self._board[2][0]):
self._set_winner(self._board[1][1])
return self._status
def _set_winner(self, value):
if value == 'X':
self._status = 1
else:
self._status = 2
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.092s

OK

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

Николай обнови решението на 15.04.2013 14:19 (преди над 11 години)

+class TicTacToeBoard:
+ def __init__(self):
+ self._moves = 0
+ self._status = 0
+ self._board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
+ self._turn = 0
+
+ def __getitem__(self, position):
+ self._check_position(position)
+ x = ord(position[0]) - ord('A')
+ y = ord(position[1]) - ord('1')
+ return self._board[x][y]
+
+ def __setitem__(self, position, value):
+ if self._check_status() != 0:
+ return
+ self._check_position(position)
+ self._check_value(value)
+ x = ord(position[0]) - ord('A')
+ y = ord(position[1]) - ord('1')
+ self._board[x][y] = value
+ self._moves += 1
+ if value == 'X':
+ self._turn = 2
+ else:
+ self._turn = 1
+
+ def __str__(self):
+ boarder = '\n -------------\n'
+ result = boarder
+ for i in range(0, 3):
+ result += str(3 - i) + ' '
+ for j in range(0, 3):
+ result += '| ' + self._board[j][2 - i] + ' '
+ result += '|' + boarder
+ result += ' A B C \n'
+ return result
+
+ def _check_position(self, position):
+ if (type(position) is not str or
+ len(position) != 2 or
+ position[0] < 'A' or
+ position[0] > 'C' or
+ position[1] < '1' or
+ position[1] > '3'):
+ raise InvalidKey()
+
+ x = ord(position[0]) - ord('A')
+ y = ord(position[1]) - ord('1')
+ if self._board[x][y] != ' ':
+ raise InvalidMove()
+
+ def _check_value(self, value):
+ if (value != 'X' and
+ value != 'O'):
+ raise InvalidValue()
+
+ if (self._turn == 1 and value != 'X' or
+ self._turn == 2 and value != 'O'):
+ raise NotYourTurn()
+ return
+
+ def game_status(self):
+ self._check_status()
+ if self._status == 0:
+ return 'Game in progress.'
+ if self._status == 1:
+ return 'X wins!'
+ if self._status == 2:
+ return 'O wins!'
+ return 'Draw!'
+
+ def _check_status(self):
+ if self._status != 0:
+ return self._status
+ if self._moves == 9:
+ self._status = 3
+ return self._status
+ for i in range(0, 3):
+ if self._board[i][i] == ' ':
+ continue
+ if (self._board[i][0] == self._board[i][1] and
+ self._board[i][1] == self._board[i][2] and
+ self._board[i][0] != ' '):
+ self._set_winner(self._board[i][0])
+ return self._status
+ if (self._board[0][i] == self._board[1][i] and
+ self._board[1][i] == self._board[2][i] and
+ self._board[0][i] != ' '):
+ self._set_winner(self._board[0][i])
+ return self._status
+ if self._board[1][1] != ' ':
+ if (self._board[0][0] == self._board[1][1] and
+ self._board[1][1] == self._board[2][2] or
+ self._board[0][2] == self._board[1][1] and
+ self._board[1][1] == self._board[2][0]):
+ self._set_winner(self._board[1][1])
+ return self._status
+
+ def _set_winner(self, value):
+ if value == 'X':
+ self._status = 1
+ else:
+ self._status = 2
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass