本文整理汇总了Python中board.Board.place_tile方法的典型用法代码示例。如果您正苦于以下问题:Python Board.place_tile方法的具体用法?Python Board.place_tile怎么用?Python Board.place_tile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.place_tile方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
def main(player_first = True):
WIDTH = 3
HEIGHT = 3
board = Board(WIDTH, HEIGHT)
interaction = Interaction(WIDTH, HEIGHT)
computer = Computer()
turn = 'X'
if player_first:
computer_tile = 'O'
else:
computer_tile = 'X'
while board.get_winner() is None and not board.is_cat_game():
if turn == computer_tile:
x, y = computer.get_move(board, computer_tile)
board.place_tile(turn, x, y)
else:
x, y = interaction.get_move(turn, board)
board.place_tile(turn, x, y)
if turn == 'X':
turn = 'O'
else:
turn = 'X'
if board.is_cat_game():
interaction.display_cat_game(board)
else:
interaction.display_winner(board, board.get_winner() != computer_tile)
示例2: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
class Interaction:
def __init__(self, width, height):
self.width = width
self.height = height
# Used to map numbers to locations for the user
self.number_board = Board(width, height)
for x in range(0, width):
for y in range(0, height):
self.number_board.place_tile(self.x_y_to_number(x, y), x, y)
def get_move(self, turn, board):
print("Current board:")
board.display()
print("Board locations:")
self.number_board.display()
while True:
move = input("Where would " + str(turn) + " like to move? ")
move_num = -1
try:
move_num = int(move)
x, y = self.number_to_x_y(move_num)
if move_num < 0 or move_num > self.width * self.height - 1:
print("Must be a proper location number.")
elif board.is_occupied(x, y):
print("That location is already occupied.")
else:
return (x, y)
except:
print("Must be a proper location number.")
def display_winner(self, board, player_won):
print("Final board:")
board.display()
if player_won:
print("Congratuations! You are the winner! :)")
else:
print("Sorry, the computer has bested you. :(")
def display_cat_game(self, board):
print("Final board:")
board.display()
print("It was a tie (CAT game)! :|")
def number_to_x_y(self, num):
return (num % self.width, num // self.width)
def x_y_to_number(self, x, y):
return y * self.width + x
示例3: TestComputer
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
class TestComputer(unittest.TestCase):
WIDTH = 3
HEIGHT = 3
def setUp(self):
self.computer = Computer()
self.board = Board(self.WIDTH, self.HEIGHT)
def update_board_to(self, new_board):
for y in range(0, self.HEIGHT):
for x in range(0, self.WIDTH):
tile = new_board[y][x] # Swap to match
if tile is not '-':
self.board.place_tile(tile, x, y)
def assert_board(self, board, tile, expected_move):
self.update_board_to(board)
move = self.computer.get_move(self.board, tile)
self.assertEqual(move, expected_move)
def test_win(self):
self.assert_board(
[['X', 'O', 'X'],
['X', 'O', 'X'],
['-', '-', 'O']],
'X', (0, 2))
def test_stop_loss(self):
self.assert_board(
[['X', 'O', 'X'],
['X', '-', 'O'],
['-', '-', 'O']],
'O', (0, 2))
def test_beat_trap(self):
self.update_board_to(
[['X', '-', '-'],
['-', 'O', '-'],
['-', '-', 'X']])
move = self.computer.get_move(self.board, 'O')
self.assertNotEqual(move, (0, 2))
self.assertNotEqual(move, (2, 0))
示例4: TestManager
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
class TestManager(unittest.TestCase):
def setUp(self):
self.b = Board()
self.manager = Manager(self.b)
def test_row_direction(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
self.b.place_tile(a, 1, 1)
self.b.place_tile(b, 1, 2)
self.b.place_tile(c, 1, 3)
self.b.place_tile(d, 1, 4)
self.assertTrue(self.manager.check_direction())
def test_col_direction(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
self.b.place_tile(a, 1, 1)
self.b.place_tile(b, 2, 1)
self.b.place_tile(c, 3, 1)
self.b.place_tile(d, 4, 1)
self.assertTrue(self.manager.check_direction())
def test_multi_direction(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
self.b.place_tile(a, 1, 1)
self.b.place_tile(b, 2, 1)
self.b.place_tile(c, 2, 2)
self.b.place_tile(d, 2, 3)
self.assertFalse(self.manager.check_direction())
def test_connectivity_middle(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
e = Tile('e', 5)
self.b.place_tile(a, 2, 1)
self.b.place_tile(b, 2, 2)
self.b.place_tile(c, 2, 3)
self.b.place_tile(d, 2, 4)
self.b.place_tile(e, 2, 5)
self.b.board[2][3].state = BoardPositionState.FULL
self.manager.check_direction()
self.assertTrue(self.manager.check_connectivity())
def test_connectivity_end(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
e = Tile('e', 5)
self.b.place_tile(a, 1, 1)
self.b.place_tile(b, 2, 1)
self.b.place_tile(c, 3, 1)
self.b.place_tile(d, 4, 1)
self.b.place_tile(e, 5, 1)
self.b.board[5][1].state = BoardPositionState.FULL
self.manager.check_direction()
self.assertTrue(self.manager.check_connectivity())
def test_connectivity_top(self):
a = Tile('a', 1)
b = Tile('b', 2)
c = Tile('c', 3)
d = Tile('d', 4)
e = Tile('e', 5)
f = Tile('f', 6)
g = Tile('g', 7)
h = Tile('h', 8)
self.b.place_tile(a, 2, 1)
self.b.place_tile(b, 2, 2)
self.b.place_tile(c, 2, 3)
self.b.place_tile(d, 2, 4)
self.b.place_tile(e, 2, 5)
#.........这里部分代码省略.........
示例5: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
class TestBoard(unittest.TestCase):
def setUp(self):
self.b = Board()
def test_constructor(self):
tw = BoardPosition(5)
tl = BoardPosition(4)
dw = BoardPosition(3)
dl = BoardPosition(2)
self.assertTrue(self.b.board[0][0] == tw)
self.assertTrue(self.b.board[7][7] == dw)
self.assertTrue(self.b.board[0][3] == dl)
self.assertTrue(self.b.board[5][1] == tl)
def test_place_tile(self):
tile = Tile('a', 1)
isValid = self.b.place_tile(tile, 0, 0)
self.assertTrue(self.b.board[0][0].state == BoardPositionState.PENDING)
self.assertTrue(self.b.board[0][0].tile == tile)
self.assertTrue(isValid)
second_tile = Tile('b', 4)
self.b.board[9][9].state = BoardPositionState.FULL
isValid = self.b.place_tile(second_tile, 9, 9)
self.assertTrue(not isValid)
isValid = self.b.place_tile(second_tile, 0, 0)
self.assertTrue(not isValid)
def test_retrieve_tile(self):
tile = Tile('a', 1)
self.b.place_tile(tile, 4, 8)
retrieved = self.b.retrieve_tile(4, 8)
self.assertTrue(retrieved == tile)
self.assertTrue(self.b.board[4][8].state == BoardPositionState.EMPTY)
self.assertTrue(self.b.board[4][8].tile == None)
def test_retrieve_tile_full(self):
tile = Tile('a', 1)
self.b.place_tile(tile, 4, 8)
self.b.board[4][8].state = BoardPositionState.FULL
retrieved = self.b.retrieve_tile(4, 8)
self.assertTrue(not retrieved)
def test_retrieve_all(self):
tileA = Tile('a', 1)
tileB = Tile('b', 4)
tileC = Tile('c', 3)
tileD = Tile('d', 2)
tileE = Tile('e', 1)
should_retrieve = [tileA, tileB, tileC]
self.b.place_tile(tileA, 4, 7)
self.b.place_tile(tileB, 6, 3)
self.b.place_tile(tileC, 1, 4)
self.b.place_tile(tileD, 12, 2)
self.b.place_tile(tileE, 8, 14)
self.b.board[12][2].state = BoardPositionState.FULL
self.b.board[8][14].state = BoardPositionState.FULL
retrieved_tiles = self.b.retrieve_all()
# should not leave anything that is PENDING -- scan the board for PENDING
for row in xrange(15):
for col in xrange(15):
self.assertTrue(self.b.board[row][col] != BoardPositionState.PENDING)
# should not retrieve FULL tiles or reset its tiles to None
self.assertTrue(self.b.board[12][2].state == BoardPositionState.FULL)
self.assertTrue(self.b.board[12][2].tile == tileD)
self.assertTrue(self.b.board[8][14].state == BoardPositionState.FULL)
self.assertTrue(self.b.board[8][14].tile == tileE)
# any space that was PENDING should now be EMPTY
self.assertTrue(self.b.board[4][7].state == BoardPositionState.EMPTY)
self.assertTrue(self.b.board[6][3].state == BoardPositionState.EMPTY)
self.assertTrue(self.b.board[1][4].state == BoardPositionState.EMPTY)
# any space that was PENDING should now have None tile
self.assertTrue(self.b.board[4][7].tile == None)
self.assertTrue(self.b.board[6][3].tile == None)
self.assertTrue(self.b.board[1][4].tile == None)
# should return an array of tiles that were on the board
self.assertTrue(len(should_retrieve) == len(retrieved_tiles))
for tile in should_retrieve:
found_index = retrieved_tiles.index(tile)
found_tile = retrieved_tiles.pop(found_index)
self.assertTrue(found_tile == tile)
示例6: GameWindow
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_tile [as 别名]
class GameWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
self.master.title('2048')
self.board = Board()
self.expectimax = ExpectimaxChooser(self.board)
self.board.place_tile(self.board.state)
self.grid_cells = []
self.init_grid()
self.update_view(self.board.state.board)
self.bind("<KeyPress>", self.onKeyPress)
#end
def onKeyPress(self,direction):
print (self.board.state, "here ")
if self.board.move(direction, self.board.state):
print (self.board.state, "but then")
self.update_view(self.board.state.board)
self.board.place_tile(self.board.state)
self.update_view(self.board.state.board)
print (self.expectimax.recommend_move(self.board.state))
def do_one_move(self):
move = self.expectimax.recommend_move(self.board.state, 4, False)
if self.board.move(move,self.board.state):
self.board.place_tile(self.board.state)
self.update_view(self.board.state.board)
self.after(10, lambda : self.do_one_move())
else:
print ("FAIL")
def init_grid(self):
background = Frame(self, bg = BACKGROUND_COLOR_GAME, width = SIZE, height = SIZE )
background.grid()
for i in range(GRID_LEN):
# Loop rows
grid_row = []
for j in range(GRID_LEN):
# Loop columns
cell = Frame(
background,
bg = BACKGROUND_COLOR_CELL_EMPTY,
width = SIZE/GRID_LEN,
height = SIZE/GRID_LEN)
cell.grid(row=i, column=j, padx=GRID_PADDING, pady=GRID_PADDING)
t = Label(master=cell, text="", bg=BACKGROUND_COLOR_CELL_EMPTY, justify=CENTER, font=FONT, width=4, height=2)
t.grid()
grid_row.append(t)
self.grid_cells.append(grid_row)
#end
def update_view(self, board ):
for i in range( GRID_LEN ):
for j in range( GRID_LEN ):
digit = board[i][j]
if digit == 0:
self.grid_cells[i][j].configure(
text = "",
bg = BACKGROUND_COLOR_CELL_EMPTY)
else:
foreground_color = '#f9f6f2' if digit > 4 else '#776e65'
number = digit # the human friendly representation
self.grid_cells[i][j].configure(
text = str( digit),
bg = BACKGROUND_COLOR_DICT[ digit ],
fg = foreground_color )
self.update_idletasks()