本文整理汇总了Python中board.Board.clear方法的典型用法代码示例。如果您正苦于以下问题:Python Board.clear方法的具体用法?Python Board.clear怎么用?Python Board.clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.clear方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_all_pits_empty
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
def test_all_pits_empty(self):
b = Board().clear()
self.assertTrue(b.all_pits_empty())
for player in Board.players:
b.clear().pit(player, 1).inc(1)
self.assertFalse(b.all_pits_empty())
b.clear().pit(player, Board.home_pit).inc(1)
self.assertTrue(b.all_pits_empty())
示例2: BoardTestCase
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class BoardTestCase(unittest.TestCase):
def setUp(self):
self.board = Board(dim=(WIDTH, HEIGHT), allowed=ALLOWED, clearval=CLEARVAL)
random.seed()
def testBoardInit(self):
assert len(self.board) == WIDTH * HEIGHT
assert not any(self.board)
def randomCoords(self):
return (random.randint(0,WIDTH-1), random.randint(0,HEIGHT-1))
def randomValue(self):
return random.choice(ALLOWED)
def testBoardBounds(self):
for x in range(WIDTH):
for y in range(HEIGHT):
assert self.board.isWithinBounds(x,y)
assert not self.board.isWithinBounds(-1,1)
assert not self.board.isWithinBounds(1,-1)
assert not self.board.isWithinBounds(WIDTH,0)
assert not self.board.isWithinBounds(0,HEIGHT)
def testBoardSetGet(self):
for i in range(NR_RANDOM_TESTS):
x,y = self.randomCoords()
v = self.randomValue()
self.board.setValue(x,y,v)
assert self.board.getValue(x,y) == v
def testIsAllowed(self):
for a in ALLOWED:
assert self.board.isAllowed(a)
for a in DISALLOWED:
assert not self.board.isAllowed(a)
def testClear(self):
assert self.board.isClear(5,5)
self.board.setValue(5,5,ALLOWED[0])
assert not self.board.isClear(5,5)
self.board.clear(5,5)
assert self.board.isClear(5,5)
for x in range(WIDTH):
for y in range(HEIGHT):
self.board.setValue(x,y,ALLOWED[0])
assert all(self.board)
self.board.clearAll()
assert not any(self.board)
示例3: test_put_gems
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
def test_put_gems(self):
board = Board()
a = Board.player_a
b = Board.player_b
# easy cases
board.clear()
self.verify_board(board, {a: [0, 0, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.put_gems(a, 0, 1), PlayerAndPit(a, 0))
self.verify_board(board, {a: [1, 0, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.put_gems(a, 0, 1), PlayerAndPit(a, 0))
self.verify_board(board, {a: [2, 0, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.put_gems(a, 1, 1), PlayerAndPit(a, 1))
self.verify_board(board, {a: [2, 1, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 0]})
# corner cases
self.assertEquals(board.clear().put_gems(a, Board.home_pit, 2), PlayerAndPit(b, 0))
self.verify_board(board, {a: [0, 0, 0, 0, 0, 0, 1], b: [1, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.clear().put_gems(b, Board.home_pit, 2), PlayerAndPit(a, 0))
self.verify_board(board, {a: [1, 0, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 1]})
# multiple with turn-around
self.assertEquals(board.clear().put_gems(a, 0, 7), PlayerAndPit(a, 6))
self.verify_board(board, {a: [1, 1, 1, 1, 1, 1, 1], b: [0, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.clear().put_gems(a, 0, 8), PlayerAndPit(b, 0))
self.verify_board(board, {a: [1, 1, 1, 1, 1, 1, 1], b: [1, 0, 0, 0, 0, 0, 0]})
self.assertEquals(board.clear().put_gems(a, 0, 15), PlayerAndPit(a, 0))
self.verify_board(board, {a: [2, 1, 1, 1, 1, 1, 1], b: [1, 1, 1, 1, 1, 1, 1]})
示例4: new_game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
def new_game(bot, update):
# Check our state
double_reset = bot.double_resets[str(update.message.chat_id)]
# Load the board
board = get_board(update.message.chat_id)
# If double_reset is a number
if double_reset != True and double_reset != False:
# Create a new board and save it
# Otherwise just clear it
if board.size != double_reset:
board = Board(double_reset)
else:
board.clear()
save_board(board, update.message.chat_id)
bot.double_resets[str(update.message.chat_id)] = False
send_board_image(bot, update)
示例5: Scene
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
#.........这里部分代码省略.........
def get_arrow_colors(self, bt):
return Scene.ARROW_COLORS_DICT[bt]
def get_arrow_action_colors(self, bt):
return Scene.ARROW_ACTION_COLORS
def get_text_colors(self, bt):
return Scene.TEXT_COLORS_DICT[bt]
def get_text_action_colors(self, bt):
return Scene.TEXT_ACTION_COLORS
def get_field_marker_colors(self, bt):
return Scene.FIELD_MARKER_COLORS_DICT[bt]
def format_return_values(self, filename, size_x=None, size_y=None):
# size_x, size_y :: int | float | None
width = size_x
height = size_y
if isinstance(size_x, float):
width = int( size_x * GD.DEFAULT_BOARD_RENDERING_SIZE )
if isinstance(size_y, float):
height = int( size_y * GD.DEFAULT_BOARD_RENDERING_SIZE )
return filename, width, height
def intro_piece(self, bt, piece_type=None):
bt = BoardType(bt)
self.board = Board(bt, 2, 2)
self.board.clear()
self.delete_all_marks()
piece_type = piece_type or bt.get_newly_introduced_piece()
if piece_type is None:
return None
self.board.set_pieces([(0, 0, PieceType(piece_type)),
(1, 0, PieceType(piece_type)),
(0, 1, PieceType(-piece_type)),
(1, 1, PieceType(-piece_type))])
return piece_type
def intro_castling(self, bt):
bt = BoardType(bt)
self.board = Board(bt, bt.get_size(), 1)
self.board.clear()
self.delete_all_marks()
pos_king_h = bt.get_size() // 2
offset = 1 if bt.does_contain(PieceType.Star) else 0
pos_rook_r = bt.get_size() - 1 - offset
self.board.set_piece(pos_king_h, 0, PieceType.King)
self.board.set_piece(offset, 0, PieceType.Rook)
self.board.set_piece(pos_rook_r, 0, PieceType.Rook)
if bt.does_contain(PieceType.Star):
self.board.set_piece(0, 0, PieceType.Star)
self.board.set_piece(bt.get_size() - 1, 0, -PieceType.Star)
get_text_position = SH.get_func_get_text_position(left=0.05, top=1.0, right=0.75, bottom=0.05)
示例6: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
#.........这里部分代码省略.........
accelerator='Ctrl+Z')
edit_menu.add_command(label='Redo', command=self.board.redo,
accelerator='Ctrl+Y')
edit_menu.add_command(label='Delete selected',
command=self.board._delete_selected_items, accelerator='Delete')
edit_menu.add_command(label='Rotate selected',
command=self.board._rotate_selected_item, accelerator='r')
edit_menu.add_command(label='Toggle cursor', command=self._toggle_cursor,
accelerator='D')
self._menu.add_cascade(label='Edit', menu=edit_menu)
self._root.config(menu=self._menu)
def _setup_shortcuts(self):
"""
Adds basic shortcuts.
"""
self.board.parent.bind('<Control-n>', lambda event: self._new_file())
self.board.parent.bind('<Control-N>', lambda event: self._new_file())
self.board.parent.bind('<Control-o>', lambda event: self._open_file())
self.board.parent.bind('<Control-O>', lambda event: self._open_file())
self.board.parent.bind('<Control-q>', lambda event: self.board.quit())
self.board.parent.bind('<Control-Q>', lambda event: self.board.quit())
self.board.parent.bind('<Control-s>', lambda event: self._save_file())
self.board.parent.bind('<Control-S>', lambda event: self._save_file())
self.board.parent.bind('<Control-y>', lambda event: self.board.redo())
self.board.parent.bind('<Control-Y>', lambda event: self.board.redo())
self.board.parent.bind('<Control-z>', lambda event: self.board.undo())
self.board.parent.bind('<Control-Z>', lambda event: self.board.undo())
self.board.add_key_binding('d', self._toggle_cursor)
def _init_board(self):
"""
(Re)Initializes the board based on this app's specific needs, as per
self._on_init.
"""
self.board.clear()
self._on_init(self.board)
self.board.reset()
def _on_changed(self, board_changed):
"""
Callback for when the board is changed. Updates the title of the app window
to indicate whether we need to save the file.
"""
self._root.title('%s (%s) %s %s' % (self._app_name, self._dev_stage,
strip_file_name(self._file_name), '*' if board_changed else ''))
def _save_as(self, file_name=None):
"""
Saves the current state of the app with the given |file_name|.
"""
saved_file_name = save_board(self.board, file_name, self._app_name,
self._file_extension)
if saved_file_name:
self._file_name = saved_file_name
self.board.set_changed(False)
def _save_file(self):
"""
Saves the current state of the app with its current file name (asks for a
a file path if it currently does not have one).
"""
self._save_as(self._file_name)
def _request_save(self):
"""
Requests for a file save if necessary. Returns True if there isn't anything
to save or, in the case that there is something to save, if the user
either decides to save the file (will be presented with a dialog to do
so) or decides not to save the file. Returns False if the user cancels
the request for save (i.e. does neither).
"""
示例7: TestStrategy
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class TestStrategy(unittest.TestCase):
def setUp(self):
self.board = Board()
self.strategy = Strategy(self.board, 'X')
def test_find_v_shape(self):
self.board.X(0,0)
self.board.X(1,1)
x, y = self.strategy.find_v_shape()
self.assertEqual(0, x)
self.assertEqual(2, y)
self.board.clear()
self.board.X(0, 2)
self.board.X(1, 1)
self.board.O(0, 0)
x, y = self.strategy.find_v_shape()
self.assertEqual(2, x)
self.assertEqual(2, y)
def test_is_corner_unchallenged(self):
self.assertTrue(self.strategy.is_corner_unchallenged(0,0))
self.board.O(0,0)
self.assertFalse(self.strategy.is_corner_unchallenged(0, 2))
self.assertFalse(self.strategy.is_corner_unchallenged(2, 0))
self.assertTrue(self.strategy.is_corner_unchallenged(2, 2))
def test_find_unchallenged_corner(self):
self.board.O(0, 0)
x, y = self.strategy.find_unchallenged_corner()
self.assertEqual(2, x)
self.assertEqual(2, y)
def test_opponent_value(self):
self.assertEqual('O', self.strategy.opponent_value())
strategy = Strategy(self.board, 'O')
self.assertEqual('X', strategy.opponent_value())
def test_row_is_winnable(self):
self.board.O(0, 0)
self.assertFalse(self.strategy.row_is_winnable(0));
self.assertTrue(self.strategy.row_is_winnable(1));
self.assertTrue(self.strategy.row_is_winnable(2));
def test_columns_is_winnable(self):
self.board.O(0, 0)
self.assertFalse(self.strategy.column_is_winnable(0));
self.assertTrue(self.strategy.column_is_winnable(1));
self.assertTrue(self.strategy.column_is_winnable(2));
def test_diagonal_is_winnable(self):
self.board.O(0, 0)
self.assertFalse(self.strategy.diagonal_is_winnable(0, 0))
self.assertFalse(self.strategy.diagonal_is_winnable(2, 2))
self.assertTrue(self.strategy.diagonal_is_winnable(0, 2))
self.assertTrue(self.strategy.diagonal_is_winnable(2, 0))
def test_find_winnable_corner(self):
self.board.O(0, 0)
self.board.O(0, 1)
self.board.O(0, 2)
corner = self.strategy.find_winnable_corner()
print corner
self.assertTrue(corner == (2, 2) or corner == (2, 0))
self.board.clear()
self.board.O(2, 2)
self.board.O(0, 2)
self.board.O(1, 2)
self.board.O(2, 1)
x, y = self.strategy.find_winnable_corner()
self.assertTrue(corner == (0, 0) or corner == (2, 0))
def test_next_move_defend(self):
self.board.X(0, 0)
self.board.O(1, 1)
self.board.X(2, 2)
self.board.O(0, 1)
self.assertEqual((2, 1), self.strategy.next_move())
def test_has_winner(self):
board_string = 'OOOX X '
self.assertTrue(strategy.has_winner(board_string))
board_string = 'OOXXXOOXX'
self.assertFalse(strategy.has_winner(board_string))
def test_number_winning_combos(self):
val = 'XXXOOOXXX'
self.assertEqual(3, strategy.number_winning_combos(val))
val = 'OOXXXOOXX'
self.assertEqual(0, strategy.number_winning_combos(val))
val = 'OXXXOXOXO'
self.assertEqual(1, strategy.number_winning_combos(val))
def test_is_winning_combo(self):
combo = 'XXX'
#.........这里部分代码省略.........
示例8: Board
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
#assert dice3 == value, '"value" has same value of "dice3.value", should return True.'
### Board tests ###
from board import Board
board = Board()
assert type(board.board) == list, "Board should be type list."
assert len(board.board) == 0, "Board should be clean when initiated."
board.start()
assert len(board.board) == 4, "After calling start board should have 4 rows."
assert len(board.board[0]) == 4, "After calling start should have 4 columns in each row."
assert type(board.board[0][0]) == Dice, "Every item in every row of the board should be type Dice, an instance of Dice class."
board.clear()
assert len(board.board) == 0, "Board should be empty after calling clean()."
board.start()
assert type(board.get_item(1,2)) == Dice, "Should return the the 3rd item from the second row, an Dice object"
assert len(board.get_surrounds(3,3)) == 3, "Should return a list with 3 Dices, the surrounds of the dice on 4th row and 4th column."
assert len(board.get_surrounds(1,1)) == 8, "Should return a list with 8 Dices, the surrounds of the dice on 2nd row and 2nd column."
assert len(board.get_surrounds(0,2)) == 5, "Should return a list with 5 Dices, the surrounds of the dice on 1st row and 3rd column."
### Boggle tests ###
from boggle import Boggle
boggle = Boggle()
boggle.start()
示例9: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class ReGo:
def __init__(self):
"""
Constructor of the class ReGo
"""
# known commands
self.knownCmds = [ 'protocol_version' , 'name' , 'version' , 'known_command' , 'list_commands' ,'quit' , 'boardsize' , 'clear_board' , 'komi' , 'play' , 'genmove' ]
# open log
self.log = Log()
self.log.open()
# board
self.board = Board(19,self.log)
# score
self.score = 0.0
# players
self.bPlayer = Player('b',self.board,self.log)
self.wPlayer = Player('w',self.board,self.log)
def __decodeLine__(self,line):
"""
Decodes an input line read from stdin
"""
vals = line.split(' ')
return (vals[0],vals[1:])
def __decodeColor__(self,colorStr):
"""
Decodes a color string
"""
if colorStr.upper() in ('B','BLACK'):
return 'b'
elif colorStr.upper() in ('W','WHITE'):
return 'w'
else:
raise SyntaxError('illegal color')
def __decodePosition__(self,posStr):
"""
Decodes a position string
"""
try:
posLetter = posStr[0].upper()
posNumber = int(posStr[1:])
except Exception:
raise SyntaxError('invalid position')
else:
return posLetter,posNumber
def __handleError__(self):
"""
Checks for the exception raised and returns the error string. It logs also the exception stack-trace in case of an unexpected error.
"""
e_type, e, tb = sys.exc_info()
# syntax error
if e_type == SyntaxError:
output_line = '? syntax error: '+e.message+'\n\n'
# consistency error
elif e_type == ConsistencyError:
output_line = '? consistency error: '+e.message+'\n\n'
# unexpected error
else:
output_line = '? unexpected error\n\n'
self.log.logException(e_type, e, tb )
return output_line
def protocol_version(self,args):
"""
Prints GTP protocol version
"""
return '2'
def name(self,args):
"""
Prints the name of this GO engine
"""
return 'Rego'
def version(self,args):
"""
Prints the version of this GO engine
"""
return '0.1'
def know_command(self,args):
"""
Returns true if the command specified is known, false otherwise
"""
#.........这里部分代码省略.........
示例10: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class Game:
cascade = True
def __init__(self, width, height, mines):
self.width = width
self.height = height
self.mines = mines
self.board = Board(width, height)
self.mine_array = MineArray(width, height, mines)
def randomize_mines(self):
self.mine_array.randomize()
def start(self):
self.board.clear()
def new_game(self):
self.randomize_mines()
self.start()
def neighbour_count(self, row, col):
neighbours = self.mine_array.neighbours(row, col)
return sum(self.mine_array.get(*coord) for coord in neighbours)
def on_board(self, row, col):
return (0 <= row < self.height) and (0 <= col < self.width)
def click(self, row, col):
if not self.on_board(row, col):
raise RuntimeError("({}, {}) is not within the bounds of the board.".format(row, col))
if self.mine_array.is_mine(row, col):
for index, val in enumerate(self.mine_array.array):
if val:
self.board.array[index] = Tile.MINE
self.board.set(row, col, Tile.BLAST)
else:
if self.cascade:
self.cascade_open(row, col)
else:
num = self.neighbour_count(row, col)
self.board.set(row, col, Tile.num(num))
def cascade_open(self, row, col):
to_do = [(row, col)]
board = self.board
while to_do:
row, col = to_do.pop()
n_count = self.neighbour_count(row, col)
board.set(row, col, Tile.num(n_count))
if n_count == 0:
for (i, j) in self.mine_array.neighbours(row, col):
if self.board.get(i, j).is_num():
continue
if (i, j) in to_do:
continue
to_do.append((i, j))
def is_solved(self):
for row in range(self.height):
for col in range(self.width):
mine = self.mine_array.get(row, col)
if mine == 0:
if not self.board.get(row, col).is_num():
return False
return True
def __str__(self):
return str(self.board)
示例11: Map
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class Map(EventObject):
def __init__(self, dimension, grid, player_color, food_score, bonus_score, flag = 0):
self.grid = grid
self.food_score = food_score
self.bonus_score = bonus_score
self.view = View(dimension, self.grid, player_color, flag)
self.board = Board(self.grid)
self.connect(self.board, "grid_changed", self, "update_grid")
self.connect(self, "update_view_at", self.view, "update_at")
self.connect(self, "clear_grid",self.view, "clear_grid")
self.reload()
def direction_changed(self, direction):
"""
This method is invoked by the Engine. Changing the direction of the map.
"""
if self.direction == Directions.DOWN and direction == Directions.UP:
return
if self.direction == Directions.UP and direction == Directions.DOWN:
return
if self.direction == Directions.RIGHT and direction == Directions.LEFT:
return
if self.direction == Directions.LEFT and direction == Directions.RIGHT:
return
self.direction = direction
def reload(self):
"""
Restart the map. It clears the board and view, and also position the snake in the middle with size = grid / 3.
One random food is placed, too.
"""
self.direction = Directions.RIGHT
self.prev_direction = Directions.RIGHT
self.number_food_eaten = 0
self.number_obstacle_created = 0
self.prev_food = None
self.clean_tongue = False
self.board.clear()
self.view.clean_all()
self.board.init_snake_in_middle(int(self.grid / 3))
self.board.put_random_item(Types.FOOD)
def update_grid(self, row, column, prev, new):
"""
This method is a bridge between the board and the view.
The board is updated, and then it invokes this method. Then this method does the calculating to tell the view which grid it is going to be draw.
"""
### If the previous type is equal the new one. No need to tell the view to update the same grid ###
if prev == new:
return
self.emit("clear_grid", [row, column]) ### Tells the view to clean this grid ###
if new == Types.SNAKE: ### The new item is a snake part ###
### Draws the head and the tongue ###
if self.direction == Directions.UP:
self.emit("update_view_at", [row, column, "mouth_up"])
tmp_item_position = self.board.round_position(row - 1, column)
if self.board.get_at(*tmp_item_position) == Types.FOOD:
self.prev_food = tmp_item_position
self.emit("update_view_at", [*self.prev_food, "tongue_up"])
self.clean_tongue = False
elif self.direction == Directions.DOWN:
self.emit("update_view_at", [row, column, "mouth_down"])
tmp_item_position = self.board.round_position(row + 1, column)
if self.board.get_at(*tmp_item_position) == Types.FOOD:
self.prev_food = tmp_item_position
self.emit("update_view_at", [*self.prev_food, "tongue_down"])
self.clean_tongue = False
elif self.direction == Directions.RIGHT:
self.emit("update_view_at", [row, column, "mouth_right"])
tmp_item_position = self.board.round_position(row, column + 1)
if self.board.get_at(*tmp_item_position) == Types.FOOD:
self.prev_food = tmp_item_position
self.emit("update_view_at", [*self.prev_food, "tongue_right"])
self.clean_tongue = False
elif self.direction == Directions.LEFT:
self.emit("update_view_at", [row, column, "mouth_left"])
tmp_item_position = self.board.round_position(row, column - 1)
if self.board.get_at(*tmp_item_position) == Types.FOOD:
self.prev_food = tmp_item_position
self.emit("update_view_at", [*self.prev_food, "tongue_left"])
self.clean_tongue = False
### End of drawing head and tongue code ###
if len(self.board.snake) > 1:
### Clean grid to replace for tail ###
self.emit("clear_grid", [*self.board.get_snake_tail()])
tail_direction = self.board.get_tail_direction()
if tail_direction == Directions.RIGHT:
self.emit("update_view_at", [*self.board.get_snake_tail(), "tail_right"])
elif tail_direction == Directions.LEFT:
self.emit("update_view_at", [*self.board.get_snake_tail(), "tail_left"])
elif tail_direction == Directions.UP:
self.emit("update_view_at", [*self.board.get_snake_tail(), "tail_up"])
else:
self.emit("update_view_at", [*self.board.get_snake_tail(), "tail_down"])
prev_row = self.board.snake[1][0]
prev_column = self.board.snake[1][1]
### Clean the grid in the previous position ###
self.emit("clear_grid", [prev_row, prev_column])
#.........这里部分代码省略.........
示例12: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class TestBoard(TestCase):
def setUp(self):
self.board = Board()
def test_clear_board_on_start(self):
self.assertTrue(self.board.is_clear())
def test_clear_board(self):
self.assertTrue(self.board.is_clear())
self.board.put(0, 0, HUMAN)
self.assertFalse(self.board.is_clear())
self.board.clear()
self.assertTrue(self.board.is_clear())
def test_full_board(self):
self.assertFalse(self.board.is_full())
self.board.put(1, 1, HUMAN)
self.assertFalse(self.board.is_full())
self.board.board = [[COMPUTER, COMPUTER, COMPUTER],
[COMPUTER, HUMAN, COMPUTER],
[COMPUTER, COMPUTER, COMPUTER]]
self.assertTrue(self.board.is_full())
def test_can_put_on_board(self):
self.assertTrue(self.board.can_put(0, 0))
self.assertTrue(self.board.put(0, 0, HUMAN))
self.assertFalse(self.board.can_put(0, 0))
self.assertTrue(self.board.can_put(0, 1))
self.assertTrue(self.board.can_put(1, 1))
self.assertFalse(self.board.put(0, 0, HUMAN))
self.assertEqual(HUMAN, self.board.owner_of(0, 0))
def test_next_player(self):
self.board.player_starts(COMPUTER)
self.assertEqual(HUMAN, self.board.next_player())
self.assertEqual(COMPUTER, self.board.next_player())
self.assertEqual(HUMAN, self.board.next_player())
def test_walk(self):
self.board.player_starts(COMPUTER)
self.board.player_starts(HUMAN)
self.assertEqual(HUMAN, self.board.player_turn())
self.assertTrue(self.board.put(0, 0, HUMAN))
self.assertEqual(COMPUTER, self.board.player_turn())
def test_game_winner(self):
self.board.board = [[HUMAN, EMPTY, EMPTY],
[EMPTY, HUMAN, EMPTY],
[EMPTY, EMPTY, HUMAN]]
self.assertEqual(HUMAN, self.board.get_winner())
self.board.board = [[HUMAN, EMPTY, COMPUTER],
[HUMAN, HUMAN, HUMAN],
[COMPUTER, COMPUTER, EMPTY]]
self.assertEqual(HUMAN, self.board.get_winner())
self.board.board = [[COMPUTER, HUMAN, COMPUTER],
[COMPUTER, HUMAN, COMPUTER],
[HUMAN, COMPUTER, HUMAN]]
self.assertEqual(EMPTY, self.board.get_winner())
self.board.board = [[EMPTY, HUMAN, COMPUTER],
[COMPUTER, HUMAN, COMPUTER],
[EMPTY, COMPUTER, HUMAN]]
self.assertEqual(None, self.board.get_winner())
self.board.board = [[EMPTY, HUMAN, COMPUTER],
[HUMAN, COMPUTER, HUMAN],
[COMPUTER, COMPUTER, HUMAN]]
self.assertEqual(COMPUTER, self.board.get_winner())
self.board.board = [[EMPTY, HUMAN, HUMAN],
[HUMAN, COMPUTER, HUMAN],
[COMPUTER, COMPUTER, COMPUTER]]
self.assertEqual(COMPUTER, self.board.get_winner())
self.board.board = [[EMPTY, HUMAN, HUMAN],
[HUMAN, COMPUTER, HUMAN],
[COMPUTER, COMPUTER, COMPUTER]]
self.assertEqual(COMPUTER, self.board.get_winner())
self.board.board = [[HUMAN, COMPUTER, HUMAN],
[HUMAN, COMPUTER, HUMAN],
[HUMAN, HUMAN, COMPUTER]]
self.assertEqual(HUMAN, self.board.get_winner())
self.board.board = [[HUMAN, COMPUTER, HUMAN],
[COMPUTER, COMPUTER, HUMAN],
[HUMAN, EMPTY, HUMAN]]
self.assertEqual(HUMAN, self.board.get_winner())
def test_evaluate_move(self):
board = [[EMPTY, EMPTY, EMPTY],
[COMPUTER, HUMAN, EMPTY],
[COMPUTER, EMPTY, HUMAN]]
self.board.board = deepcopy(board)
self.assertEqual(1, self.board.evaluate_move(0, 0, HUMAN))
self.assertEqual(1, self.board.evaluate_move(0, 0, COMPUTER))
#.........这里部分代码省略.........
示例13: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import clear [as 别名]
class TestBoard(unittest.TestCase):
def setUp(self):
self.board = Board()
def test_line(self):
input_values = ['X', '', 'O']
expected = 'X| |O'
self.assertEqual(expected, self.board.line(input_values))
def test_all_lines(self):
self.board.X(0,1)
self.board.O(1,1)
self.board.X(2,2)
one = self.board.line(['', 'X', ''])
two = self.board.line(['','O',''])
three = self.board.line(['','','X'])
expected = "%s\n_____\n%s\n_____\n%s" % (one, two, three)
self.assertEqual(expected, self.board.all_lines());
def test_is_winner(self):
self.assertTrue(self.board._is_winner(['X','X','X']))
self.assertFalse(self.board._is_winner([' ','X','X']))
self.assertFalse(self.board._is_winner(['O','X','X']))
def test_has_winner(self):
self.assertFalse(self.board.has_winner())
for x in range(0,3):
#By row
for y in range(0,3):
self.board.X(x, y)
self.assertTrue(self.board.has_winner())
self.board.clear()
self.assertFalse(self.board.has_winner())
#By column
for y in range(0,3):
self.board.X(y, x)
self.assertTrue(self.board.has_winner())
self.board.clear()
self.assertFalse(self.board.has_winner())
def test_is_winning_move(self):
self.board.X(0,0)
self.board.X(0,1)
self.assertTrue(self.board.is_winning_move(0,2,'X'))
def test_is_not_winning_move(self):
self.board.X(0,0)
self.assertFalse(self.board.is_winning_move(0,1,'X'))
self.board.X(0,1)
self.assertTrue(self.board.is_winning_move(0,2,'X'))
self.assertFalse(self.board.is_winning_move(1,0,'X'))
def test_board_count(self):
self.assertEqual(0, self.board.board_count());
i = 0
for x in range(0,3):
for y in range(0,3):
self.assertEqual(i, self.board.board_count())
self.board.X(x,y)
i += 1
self.assertEqual(i, self.board.board_count())
#def main():
# unittest.main()
#if __name__ == '__main__':
# main()