Решение на Морски шах от Александър Петков

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

Към профила на Александър Петков

Резултати

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

Код

import re
indexes = {"A3": 21, "B3": 25, "C3": 29, "A2": 53,
"B2": 57, "C2": 61, "A1": 85, "B1": 89, "C1": 93}
columns = [["A1", "A2", "A3"], ["B1", "B2", "B3"], ["C1", "C2", "C3"]]
rows = [["A3", "B3", "C3"], ["A2", "B2", "C2"], ["A1", "B1", "C1"]]
diagonals = [["A3", "B2", "C1"], ["A1", "B2", "C3"]]
first_part = "\n -------------\n3 | | | |\n -------------\n"
second_part = "2 | | | |\n -------------\n1 | | "
third_part = " | |\n -------------\n A B C \n"
class TicTacToeBoard:
def __init__(self):
self.matrix = first_part + second_part + third_part
self.status = "Game in progress."
self.last_played = ""
def __str__(self):
return self.matrix
def __getitem__(self, key):
return self.matrix[indexes[key]]
def __setitem__(self, key, sign):
if re.match("^[ABC][123]$", key) is None:
raise InvalidKey
if self.matrix[indexes[key]] != ' ':
raise InvalidMove
if re.match("^[XO]$", sign) is None:
raise InvalidValue
if self.last_played == sign:
raise NotYourTurn
self.matrix = change_string(self.matrix, indexes[key], sign)
self.last_played = sign
if self.status == "Game in progress.":
for keys in [rows, columns, diagonals]:
if check_keys(self.matrix, keys, sign):
self.status = "{} wins!".format(sign)
return
if not ' ' in map(lambda x: self.matrix[indexes[x]],
list(indexes.keys())):
self.status = "Draw!"
def game_status(self):
return self.status
def change_string(string, index, new_char):
list_of_chars = list(string)
list_of_chars[index] = new_char
return ''.join(list_of_chars)
def check_keys(matrix, keys, sign):
for row in keys:
if len([elem for elem in row if matrix[indexes[elem]] == sign]) == 3:
return True
return False
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass

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

..E.....
======================================================================
ERROR: 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-6ftamj/test.py", line 57, in test_input_format
    o['A1'] = None
  File "/tmp/d20130415-29081-6ftamj/solution.py", line 35, in __setitem__
    if re.match("^[XO]$", sign) is None:
  File "/opt/python3.3/lib/python3.3/re.py", line 156, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or buffer

----------------------------------------------------------------------
Ran 8 tests in 0.015s

FAILED (errors=1)

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

Александър обнови решението на 15.04.2013 03:49 (преди над 11 години)

+import re
+
+
+indexes = {"A3": 21, "B3": 25, "C3": 29, "A2": 53,
+ "B2": 57, "C2": 61, "A1": 85, "B1": 89, "C1": 93}
+
+columns = [["A1", "A2", "A3"], ["B1", "B2", "B3"], ["C1", "C2", "C3"]]
+
+rows = [["A3", "B3", "C3"], ["A2", "B2", "C2"], ["A1", "B1", "C1"]]
+
+diagonals = [["A3", "B2", "C1"], ["A1", "B2", "C3"]]
+
+first_part = "\n -------------\n3 | | | |\n -------------\n"
+second_part = "2 | | | |\n -------------\n1 | | "
+third_part = " | |\n -------------\n A B C \n"
+
+
+class TicTacToeBoard:
+ def __init__(self):
+ self.matrix = first_part + second_part + third_part
+ self.status = "Game in progress."
+ self.last_played = ""
+
+ def __str__(self):
+ return self.matrix
+
+ def __getitem__(self, key):
+ return self.matrix[indexes[key]]
+
+ def __setitem__(self, key, sign):
+ if re.match("^[ABC][123]$", key) is None:
+ raise InvalidKey
+ if self.matrix[indexes[key]] != ' ':
+ raise InvalidMove
+ if re.match("^[XO]$", sign) is None:
+ raise InvalidValue
+ if self.last_played == sign:
+ raise NotYourTurn
+ self.matrix = change_string(self.matrix, indexes[key], sign)
+ self.last_played = sign
+ if self.status == "Game in progress.":
+ for keys in [rows, columns, diagonals]:
+ if check_keys(self.matrix, keys, sign):
+ self.status = "{} wins!".format(sign)
+ return
+ if not ' ' in map(lambda x: self.matrix[indexes[x]],
+ list(indexes.keys())):
+ self.status = "Draw!"
+
+ def game_status(self):
+ return self.status
+
+
+def change_string(string, index, new_char):
+ list_of_chars = list(string)
+ list_of_chars[index] = new_char
+ return ''.join(list_of_chars)
+
+
+def check_keys(matrix, keys, sign):
+ for row in keys:
+ counter = 0
+ for element in row:
+ if matrix[indexes[element]] == sign:
+ counter += 1
+ if counter == 3:
+ return True
+ return False
+
+
+class InvalidMove(Exception):
+ pass
+
+
+class InvalidValue(Exception):
+ pass
+
+
+class InvalidKey(Exception):
+ pass
+
+
+class NotYourTurn(Exception):
+ pass

check_keys можеш да напишеш по малко по питонски(без брояч) начин чрез list comprehension и len()

Не можа ли да измислиш нещо по-адекватно от това да си пазиш състоянието в низ, който да изследваш и променяш постоянно? Ако те накарам да добавиш по един символ на дъската на края на всеки ред ще трябва да минеш и ръчно да коригираш всички отмествания.

Александър обнови решението на 15.04.2013 13:54 (преди над 11 години)

import re
indexes = {"A3": 21, "B3": 25, "C3": 29, "A2": 53,
"B2": 57, "C2": 61, "A1": 85, "B1": 89, "C1": 93}
columns = [["A1", "A2", "A3"], ["B1", "B2", "B3"], ["C1", "C2", "C3"]]
rows = [["A3", "B3", "C3"], ["A2", "B2", "C2"], ["A1", "B1", "C1"]]
diagonals = [["A3", "B2", "C1"], ["A1", "B2", "C3"]]
first_part = "\n -------------\n3 | | | |\n -------------\n"
second_part = "2 | | | |\n -------------\n1 | | "
third_part = " | |\n -------------\n A B C \n"
class TicTacToeBoard:
def __init__(self):
self.matrix = first_part + second_part + third_part
self.status = "Game in progress."
self.last_played = ""
def __str__(self):
return self.matrix
def __getitem__(self, key):
return self.matrix[indexes[key]]
def __setitem__(self, key, sign):
if re.match("^[ABC][123]$", key) is None:
raise InvalidKey
if self.matrix[indexes[key]] != ' ':
raise InvalidMove
if re.match("^[XO]$", sign) is None:
raise InvalidValue
if self.last_played == sign:
raise NotYourTurn
self.matrix = change_string(self.matrix, indexes[key], sign)
self.last_played = sign
if self.status == "Game in progress.":
for keys in [rows, columns, diagonals]:
if check_keys(self.matrix, keys, sign):
self.status = "{} wins!".format(sign)
return
if not ' ' in map(lambda x: self.matrix[indexes[x]],
list(indexes.keys())):
self.status = "Draw!"
def game_status(self):
return self.status
def change_string(string, index, new_char):
list_of_chars = list(string)
list_of_chars[index] = new_char
return ''.join(list_of_chars)
def check_keys(matrix, keys, sign):
for row in keys:
- counter = 0
- for element in row:
- if matrix[indexes[element]] == sign:
- counter += 1
- if counter == 3:
+ if len([elem for elem in row if matrix[indexes[elem]] == sign]) == 3:
return True
return False
class InvalidMove(Exception):
pass
class InvalidValue(Exception):
pass
class InvalidKey(Exception):
pass
class NotYourTurn(Exception):
pass