本文整理汇总了Python中term2048.board.Board.setCell方法的典型用法代码示例。如果您正苦于以下问题:Python Board.setCell方法的具体用法?Python Board.setCell怎么用?Python Board.setCell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类term2048.board.Board
的用法示例。
在下文中一共展示了Board.setCell方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_move_filled
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
def test_move_filled(self):
b = Board(size=1)
b.setCell(0, 0, 42)
b.move(Board.UP)
self.assertSequenceEqual(b.cells, [[42]])
b.move(Board.LEFT)
self.assertSequenceEqual(b.cells, [[42]])
b.move(Board.RIGHT)
self.assertSequenceEqual(b.cells, [[42]])
b.move(Board.DOWN)
self.assertSequenceEqual(b.cells, [[42]])
示例2: test_canMove_no_empty_cell
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
def test_canMove_no_empty_cell(self):
b = Board(size=1)
b.setCell(0, 0, 42)
self.assertFalse(b.canMove())
示例3: TestBoard
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
class TestBoard(unittest.TestCase):
def setUp(self):
self.b = Board()
# == init == #
def test_init_dimensions(self):
self.assertEqual(len(self.b.cells), Board.SIZE)
self.assertEqual(len(self.b.cells[0]), Board.SIZE)
if Board.SIZE > 1:
self.assertEqual(len(self.b.cells[1]), Board.SIZE)
def test_init_dimensions_1(self):
b = Board(size=1)
c = b.cells[0][0]
self.assertTrue(c in [2, 4])
def test_init_dimensions_3_goal_4(self):
b = Board(size=3, goal=4)
self.assertEqual(b.size(), 3)
def test_init_only_two_tiles(self):
t = 0
for x in xrange(Board.SIZE):
for y in xrange(Board.SIZE):
c = self.b.cells[y][x]
if not c == 0:
t += 1
else:
self.assertEqual(c, 0, 'board[%d][%d] should be 0' % (y, x))
self.assertEqual(t, 2)
def test_init_not_won(self):
self.assertFalse(self.b.won())
def test_init_not_filled(self):
self.assertFalse(self.b.filled())
# == .size == #
def test_size(self):
s = 42
b = Board(size=s)
self.assertEqual(b.size(), s)
# == .won == #
def test_won(self):
self.b._Board__won = True
self.assertTrue(self.b.won())
self.b._Board__won = False
self.assertFalse(self.b.won())
# == .canMove == #
def test_canMove_no_empty_cell(self):
b = Board(size=1)
b.setCell(0, 0, 42)
self.assertFalse(b.canMove())
def test_canMove_empty_cell(self):
b = Board(size=2)
self.assertTrue(b.canMove())
def test_canMove_no_empty_cell_can_collapse(self):
b = Board(size=2)
b.cells = [
[2, 2],
[4, 8]
]
self.assertTrue(b.canMove())
# == .filled == #
def test_filled(self):
self.b.cells = [[1]*Board.SIZE for _ in xrange(Board.SIZE)]
self.assertTrue(self.b.filled())
# == .addTile == #
def test_addTile(self):
b = Board(size=1)
b.cells = [[0]]
b.addTile(value=42)
self.assertEqual(b.cells[0][0], 42)
# == .getCell == #
def test_getCell(self):
x, y = 3, 1
v = 42
self.b.cells[y][x] = v
self.assertEqual(self.b.getCell(x, y), v)
# == .setCell == #
def test_setCell(self):
x, y = 2, 3
v = 42
self.b.setCell(x, y, v)
self.assertEqual(self.b.cells[y][x], v)
# == .getLine == #
def test_getLine(self):
b = Board(size=4)
l = [42, 17, 12, 3]
#.........这里部分代码省略.........
示例4: test_getEmptyCells_filled
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
def test_getEmptyCells_filled(self):
b = Board(size=1)
b.setCell(0, 0, 42)
self.assertSequenceEqual(b.getEmptyCells(), [])
示例5: Game
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
#.........这里部分代码省略.........
for j in range(size):
cells.append(str(self.board.getCell(j, i)))
score_str = "%s\n%d" % (' '.join(cells), self.score)
try:
with open(self.store_file, 'w') as f:
f.write(score_str)
except:
return False
return True
def restore(self):
"""
restore the saved game score and data
"""
size = self.board.SIZE
try:
with open(self.store_file, 'r') as f:
lines = f.readlines()
score_str = lines[0]
self.score = int(lines[1])
except:
return False
score_str_list = score_str.split(' ')
count = 0
for i in range(size):
for j in range(size):
value = score_str_list[count]
self.board.setCell(j, i, int(value))
count += 1
return True
def clearScreen(self):
"""Clear the console"""
if self.clear_screen:
os.system('cls' if self.__is_windows else 'clear')
else:
print('\n')
def hideCursor(self):
"""
Hide the cursor. Don't forget to call ``showCursor`` to restore
the normal shell behavior. This is a no-op if ``clear_screen`` is
falsy.
"""
if not self.clear_screen:
return
if not self.__is_windows:
sys.stdout.write('\033[?25l')
def showCursor(self):
"""Show the cursor."""
if not self.__is_windows:
sys.stdout.write('\033[?25h')
def loop(self):
"""
main game loop. returns the final score.
"""
pause_key = self.board.PAUSE
示例6: Game
# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import setCell [as 别名]
#.........这里部分代码省略.........
for j in range(size):
cells.append(str(self.board.getCell(j, i)))
score_str = "%s\n%d" % (" ".join(cells), self.score)
try:
with open(self.store_file, "w") as f:
f.write(score_str)
except:
return False
return True
def restore(self):
"""
restore the saved game score and data
"""
size = self.board.SIZE
try:
with open(self.store_file, "r") as f:
lines = f.readlines()
score_str = lines[0]
self.score = int(lines[1])
except:
return False
score_str_list = score_str.split(" ")
count = 0
for i in range(size):
for j in range(size):
value = score_str_list[count]
self.board.setCell(j, i, int(value))
count += 1
return True
def loop(self):
"""
main game loop. returns the final score.
"""
pause_key = self.board.PAUSE
margins = {"left": 4, "top": 4, "bottom": 4}
try:
while True:
if self.clear_screen:
os.system(Game.__clear)
else:
print("\n")
print(self.__str__(margins=margins))
if self.board.won() or not self.board.canMove():
break
m = self.readMove()
if m == pause_key:
self.saveBestScore()
if self.store():
print("Game successfully saved. " "Resume it with `term2048 --resume`.")
return self.score
print("An error ocurred while saving your game.")
return
self.incScore(self.board.move(m))