Решение на Морски шах от Димитър Станев

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

Към профила на Димитър Станев

Резултати

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

Код

class TicTacToeBoard():
matrix = [[" "]*3 for i in range(3)]
def restartGame(self):
matrix = [[" "]*3 for i in range(3)]
def __str__(self):
return " -------------\n"
+"3 | "+ self.matrix[0][0] + " | " + self.matrix[0][1] + " | " + self.matrix[0][2] + " |\n"
+" -------------\n"
+"2 | "+ self.matrix[1][0] + " | " + self.matrix[1][1] + " | " + self.matrix[1][2] + " |\n"
+" -------------\n"
+"1 | "+ self.matrix[2][0] + " | " + self.matrix[2][1] + " | " + self.matrix[2][2] + " |\n"
+" -------------\n"
+" A B C "
def __setitem__(self, index, value):
if index[0]=="A" and index[1]=="1":
self.matrix[2][0] = value
elif index[0]=="A" and index[1]=="2":
self.matrix[1][0] = value
elif index[0]=="A" and index[1]=="3":
self.matrix[0][0] = value
elif index[0]=="B" and index[1]=="1":
self.matrix[2][1] = value
elif index[0]=="B" and index[1]=="2":
self.matrix[1][1] = value
elif index[0]=="B" and index[1]=="3":
self.matrix[0][1] = value
elif index[0]=="A" and index[1]=="1":
self.matrix[2][2] = value
elif index[0]=="A" and index[1]=="2":
self.matrix[1][2] = value
elif index[0]=="A" and index[1]=="3":
self.matrix[0][2] = value
def __init__(self):
print("NEW GAME STARTED !")
def game_status(self):
winner = False
for i in range(3):
if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
winner = True
print ("Player " + matrix[0][i] + " wins !")
for i in range(3):
if matrix[0][i] == matrix[1][i] and matrix[0][i] == matrix[2][i]:
winner = true
print ("Player " + matrix[0][i] + " wins !")
if matrix[0][0] == matrix[1][1] and matrix[1][1] == matrix[2][2] or matrix[0][2] == matrix[1][1] and matrix[1][1] == [2][0]:
winner = True
print ("Player " + matrix[1][1] + " wins !")
else:
spaceFound = False
for i in range(3):
for j in range(3):
if matrix[i][j] == " ":
spaceFound = True
if spaceFound == False and winner == False:
print("Game is draw !")
elif spaceFound == True and winner == False:
print("Game in progress !")

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

E
Stdout:
NEW GAME STARTED !
E
Stdout:
NEW GAME STARTED !
E
Stdout:
NEW GAME STARTED !
E
Stdout:
NEW GAME STARTED !
E
Stdout:
NEW GAME STARTED !
F
Stdout:
NEW GAME STARTED !
F
Stdout:
NEW GAME STARTED !
E
Stdout:
NEW GAME STARTED !

======================================================================
ERROR: test_draw (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-1e79zsg/test.py", line 144, in test_draw
    self.assertEqual('Draw!', d.game_status())
  File "/tmp/d20130415-29081-1e79zsg/solution.py", line 40, in game_status
    if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
NameError: global name 'matrix' is not defined

Stdout:
NEW GAME STARTED !

======================================================================
ERROR: test_game_in_progress (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-1e79zsg/test.py", line 151, in test_game_in_progress
    self.assertEqual('Game in progress.', p.game_status())
  File "/tmp/d20130415-29081-1e79zsg/solution.py", line 40, in game_status
    if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
NameError: global name 'matrix' is not defined

Stdout:
NEW GAME STARTED !

======================================================================
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-1e79zsg/test.py", line 44, in test_input_format
    with self.assertRaises(solution.InvalidKey):
AttributeError: 'module' object has no attribute 'InvalidKey'

Stdout:
NEW GAME STARTED !

======================================================================
ERROR: test_o_wins (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-1e79zsg/test.py", line 110, in test_o_wins
    self.assertEqual('O wins!', h.game_status())
  File "/tmp/d20130415-29081-1e79zsg/solution.py", line 40, in game_status
    if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
NameError: global name 'matrix' is not defined

Stdout:
NEW GAME STARTED !

======================================================================
ERROR: test_overwrite_move (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-1e79zsg/test.py", line 64, in test_overwrite_move
    with self.assertRaises(solution.NotYourTurn):
AttributeError: 'module' object has no attribute 'NotYourTurn'

Stdout:
NEW GAME STARTED !

======================================================================
ERROR: test_x_wins (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-1e79zsg/test.py", line 79, in test_x_wins
    self.assertEqual('X wins!', h.game_status())
  File "/tmp/d20130415-29081-1e79zsg/solution.py", line 40, in game_status
    if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
NameError: global name 'matrix' is not defined

Stdout:
NEW GAME STARTED !

======================================================================
FAIL: test_tostring_empty (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-1e79zsg/test.py", line 17, in test_tostring_empty
    self.assertEqual(empty_board, b.__str__())
AssertionError: '\n  -------------\n3 |   |   |   |\n  -------------\n2 |   |   |   |\n  ------- [truncated]... != '  -------------\n'
- 
    -------------
- 3 |   |   |   |
-   -------------
- 2 |   |   |   |
-   -------------
- 1 |   |   |   |
-   -------------
-     A   B   C  


Stdout:
NEW GAME STARTED !

======================================================================
FAIL: test_tostring_full (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-1e79zsg/test.py", line 40, in test_tostring_full
    self.assertEqual(full_board, d.__str__())
AssertionError: '\n  -------------\n3 | O | O | X |\n  -------------\n2 | X | X | O |\n  ------- [truncated]... != '  -------------\n'
- 
    -------------
- 3 | O | O | X |
-   -------------
- 2 | X | X | O |
-   -------------
- 1 | O | X | O |
-   -------------
-     A   B   C  


Stdout:
NEW GAME STARTED !

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

FAILED (failures=2, errors=6)

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

Димитър обнови решението на 15.04.2013 15:24 (преди около 11 години)

+class TicTacToeBoard():
+
+ matrix = [[" "]*3 for i in range(3)]
+
+ def restartGame(self):
+ matrix = [[" "]*3 for i in range(3)]
+ def __str__(self):
+ return " -------------\n"
+ +"3 | "+ self.matrix[0][0] + " | " + self.matrix[0][1] + " | " + self.matrix[0][2] + " |\n"
+ +" -------------\n"
+ +"2 | "+ self.matrix[1][0] + " | " + self.matrix[1][1] + " | " + self.matrix[1][2] + " |\n"
+ +" -------------\n"
+ +"1 | "+ self.matrix[2][0] + " | " + self.matrix[2][1] + " | " + self.matrix[2][2] + " |\n"
+ +" -------------\n"
+ +" A B C "
+ def __setitem__(self, index, value):
+ if index[0]=="A" and index[1]=="1":
+ self.matrix[2][0] = value
+ elif index[0]=="A" and index[1]=="2":
+ self.matrix[1][0] = value
+ elif index[0]=="A" and index[1]=="3":
+ self.matrix[0][0] = value
+ elif index[0]=="B" and index[1]=="1":
+ self.matrix[2][1] = value
+ elif index[0]=="B" and index[1]=="2":
+ self.matrix[1][1] = value
+ elif index[0]=="B" and index[1]=="3":
+ self.matrix[0][1] = value
+ elif index[0]=="A" and index[1]=="1":
+ self.matrix[2][2] = value
+ elif index[0]=="A" and index[1]=="2":
+ self.matrix[1][2] = value
+ elif index[0]=="A" and index[1]=="3":
+ self.matrix[0][2] = value
+ def __init__(self):
+ print("NEW GAME STARTED !")
+ def game_status(self):
+ winner = False
+ for i in range(3):
+ if matrix[i][0] == matrix[i][1] and matrix[i][0] == matrix[i][2]:
+ winner = True
+ print ("Player " + matrix[0][i] + " wins !")
+ for i in range(3):
+ if matrix[0][i] == matrix[1][i] and matrix[0][i] == matrix[2][i]:
+ winner = true
+ print ("Player " + matrix[0][i] + " wins !")
+ if matrix[0][0] == matrix[1][1] and matrix[1][1] == matrix[2][2] or matrix[0][2] == matrix[1][1] and matrix[1][1] == [2][0]:
+ winner = True
+ print ("Player " + matrix[1][1] + " wins !")
+ else:
+ spaceFound = False
+ for i in range(3):
+ for j in range(3):
+ if matrix[i][j] == " ":
+ spaceFound = True
+ if spaceFound == False and winner == False:
+ print("Game is draw !")
+ elif spaceFound == True and winner == False:
+ print("Game in progress !")