本文整理汇总了Python中board.Board.possible_moves方法的典型用法代码示例。如果您正苦于以下问题:Python Board.possible_moves方法的具体用法?Python Board.possible_moves怎么用?Python Board.possible_moves使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.possible_moves方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
class Game:
def __init__(self, user_sign, computer_sign, mode,
size, board, start_stones):
self.user = Player(user_sign)
self.computer = Player(computer_sign)
self.board = Board(size, start_stones, board, self.user, self.computer)
self.mode = mode
self.does_user_win = None
self.running = True
def play_user_turn(self, from_row, from_col, to_row, to_col):
return self.board.move_stone(self.user, self.computer,
from_row, from_col, to_row, to_col)
def play_computer_turn(self):
moves = self.board.possible_moves(self.computer, self.user)
if moves:
move = self.__choose_move(moves)
from_row, from_col = move[0][0], move[0][1]
to_row, to_col = move[1][0], move[1][1]
self.board.move_stone(self.computer, self.user,
from_row, from_col, to_row, to_col)
user_moves = self.board.possible_moves(self.user, self.computer)
if not user_moves:
self.end(self.user)
else:
self.end(self.computer)
def __choose_move(self, moves):
if self.mode == "easy":
index = randint(0, len(moves) - 1)
move = moves[index]
else:
sorted_by_benefit = sorted(moves, key=lambda tup: tup[2])
move = sorted_by_benefit[-1]
return move
def end(self, player):
total = self.board.size * self.board.size
player_points = player.count_of_stones()
opponent_points = total - player_points
if player_points != opponent_points:
self.does_user_win = ((player is self.user and
player_points > opponent_points) or
(player is self.computer and
player_points < opponent_points))
self.running = False
示例2: move
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
def move(self, board):
if type(board) == str:
board = Board(board_string=board)
taking_moves = {}
possible_moves = board.possible_moves(self.colour)
if len(possible_moves) > 0:
for possible_move in possible_moves:
temp_board = board.duplicate()
taken_piece = temp_board.move(possible_move)
if taken_piece:
taking_moves[possible_move] = taken_piece
if len(taking_moves) > 0:
chosen_move = random.choice(taking_moves.keys())
else:
chosen_move = random.choice(possible_moves)
return chosen_move
示例3: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
#.........这里部分代码省略.........
def show_cursor(self):
"""Show the cursor."""
if not self.__is_windows:
sys.stdout.write('\033[?25h')
def loop(self):
"""
main game loop. returns the final score.
"""
margins = {'left': 4, 'top': 4, 'bottom': 4}
move_str = {
Board.UP: 'Up',
Board.DOWN: 'Down',
Board.LEFT: 'Left',
Board.RIGHT: 'Right'
}
atexit.register(self.show_cursor)
try:
self.hide_cursor()
change = None
can_move = True
while True:
self.clear_screen()
print(self.__str__(margins=margins, change=change))
if not can_move:
if self.__ai is not None:
self.__ai.action_callback(self.board.cells, None)
break
if self.__ai is not None:
m = self.__ai.action_callback(self.board.cells,
self.board.possible_moves())
else:
m = None
while m is None:
m = self.read_move()
num_empty_old = 0
for i in range(4):
for j in range(4):
if self.board.cells[i][j] == 0:
num_empty_old = num_empty_old + 1
score_inc = self.board.move(m)
self.increment_score(score_inc)
change = (score_inc, move_str.get(m))
num_empty_new = 0
for i in range(4):
for j in range(4):
if self.board.cells[i][j] == 0:
num_empty_new = num_empty_new + 1
can_move = self.board.can_move()
if not can_move:
score_inc -= 10000
if self.__ai is not None:
self.__ai.reward_callback((num_empty_new - num_empty_old + 1) * (np.log(score_inc + 1.0)))
print ((num_empty_new - num_empty_old + 1) * (np.log(score_inc + 1.0)))
示例4: move
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
def move(self, board):
if type(board) == str:
board = Board(board_string=board)
possible_moves = board.possible_moves(self.colour)
if len(possible_moves) > 0:
return random.choice(possible_moves)
示例5: Board
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
from board import Board
from AI import ReversiAI
initial_state = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 0, 0, 0],
[0, 0, 0, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
board = Board(initial_state)
print board.possible_moves(1)
print board.is_move_allowed(2, 3, 1)
directions = ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
for d in directions:
print d, board.get_affected_checks((5, 3), d, 1)
#
initial_state = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 0, 0, 0],
[0, 0, 0, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
示例6: BoardTests
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
#.........这里部分代码省略.........
def test_move_stone_not_a_player_stone_at_from_field(self):
self.assertFalse(
self.board.move_stone(self.player2, self.player1, 2, 1, 1, 1))
def test_move_stone_not_a_free_to_field(self):
self.assertFalse(
self.board.move_stone(self.player2, self.player1, 3, 1, 3, 2))
def test_move_stone_out_of_range(self):
self.assertFalse(
self.board.move_stone(self.player2, self.player1, 3, 3, 2, 4))
self.assertFalse(
self.board.move_stone(self.player2, self.player1, 3, 4, 2, 1))
self.assertFalse(
self.board.move_stone(self.player2, self.player1, 3, 0, 1, 3))
def test__is_possible_move_clone_step(self):
self.board.size = 5
self.board._Board__board = [['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', 'X', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_']]
self.assertTrue(self.board._Board__is_possible_move(2, 2, 1, 1))
self.assertTrue(self.board._Board__is_possible_move(2, 2, 3, 3))
def test__is_possible_move_jump_step(self):
self.board.size = 5
self.board._Board__board = [['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', 'X', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_']]
self.assertTrue(self.board._Board__is_possible_move(2, 2, 0, 0))
self.assertTrue(self.board._Board__is_possible_move(2, 2, 4, 4))
def test__is_possible_move_from_field_to_same_field(self):
self.assertFalse(self.board._Board__is_possible_move(3, 1, 3, 1))
def test__is_possible_move_out_of_board_bounds(self):
self.assertFalse(self.board._Board__is_possible_move(3, 3, 2, 4))
self.assertFalse(self.board._Board__is_possible_move(3, 4, 2, 1))
def test__is_possible_move_out_of_field_range(self):
self.assertFalse(self.board._Board__is_possible_move(3, 0, 1, 3))
def test__is_in_board_range(self):
self.assertFalse(self.board._Board__is_in_board_range(7))
self.assertTrue(self.board._Board__is_in_board_range(3))
def test__is_in_field_range(self):
self.assertFalse(self.board._Board__is_in_field_range(2, 5))
self.assertTrue(self.board._Board__is_in_field_range(2, 0))
def test__convert_stones(self):
self.board._Board__board = [['O', 'X', 'X'],
['O', '_', 'X'],
['O', 'O', 'X']]
self.board._Board__convert_stones(1, 1, self.player1, self.player2)
self.assertTrue([['X', 'X', 'X'],
['X', '_', 'X'],
['X', 'X', 'X']], self.board._Board__board)
def test__opponent_stones(self):
self.board._Board__board = [['O', 'X', 'X'],
['O', '_', 'X'],
['O', 'O', 'X']]
self.assertEqual([(0, 0), (1, 0), (2, 0), (2, 1)],
self.board._Board__opponent_stones(
1, 1, self.player2))
def test__is_jump_step(self):
self.board.size = 5
self.board._Board__board = [['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', 'X', '_', '_'],
['_', '_', '_', '_', '_'],
['_', '_', '_', '_', '_']]
self.assertFalse(self.board._Board__is_jump_step(2, 2, 2, 3))
self.assertTrue(self.board._Board__is_jump_step(2, 2, 0, 4))
self.assertTrue(self.board._Board__is_jump_step(2, 2, 4, 0))
def test_possible_moves(self):
possibilities = [((0, 0), (1, 0), 1), ((0, 0), (1, 1), 1),
((0, 0), (1, 2), 0), ((0, 0), (2, 0), 2),
((0, 0), (2, 1), 3), ((0, 0), (2, 2), 3)]
self.assertEqual(possibilities, self.board.possible_moves(
self.player1, self.player2)[:6])
def test__str__method(self):
output = (" 0 1 2 3 \n\n" +
"0 X X X X \n" +
"1 _ _ _ _ \n" +
"2 _ _ _ _ \n" +
"3 O O O O \n")
self.assertEqual(output, str(self.board))
def test_database_format(self):
self.assertEqual("X|X,X,X,X|_,_,_,_|_,_,_,_|O,O,O,O",
self.board.database_format())
示例7: BoardTest
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import possible_moves [as 别名]
#.........这里部分代码省略.........
self.b.moves = [' ', 'X', 'O']
self.assertTrue(self.b.is_free(0))
self.assertFalse(self.b.is_free(1))
self.assertFalse(self.b.is_free(2))
def test_prepare_for_print(self):
self.b.moves = [' ', 'X', 'O', ' ', 'X', 'O']
self.assertEqual(self.b.prepare_for_print(),
[[' ', 'X', 'O'], [' ', 'X', 'O']])
def test_is_winner_top_row_should_be_true(self):
self.b.moves = ['X', 'X', 'X',
' ', ' ', ' ',
' ', ' ', ' ']
self.assertTrue(self.b.check_winning('X'))
def test_is_winner_top_row_should_be_false(self):
self.b.moves = ['X', 'O', 'X',
' ', ' ', ' ',
' ', ' ', ' ']
self.assertFalse(self.b.check_winning('X'))
def test_is_winner_middle_row_should_be_true(self):
self.b.moves = [' ', ' ', ' ',
'X', 'X', 'X',
' ', ' ', ' ']
self.assertTrue(self.b.check_winning('X'))
def test_is_winning_middle_row_should_be_false(self):
self.b.moves = [' ', ' ', ' ',
'O', 'X', 'X',
' ', ' ', ' ']
self.assertFalse(self.b.check_winning('X'))
def test_is_winning_bottom_row_should_be_true(self):
self.b.moves = [' ', ' ', ' ',
' ', ' ', ' ',
'X', 'X', 'X']
self.assertTrue(self.b.check_winning('X'))
def test_is_winning_bottom_row_should_be_false(self):
self.b.moves = [' ', ' ', ' ',
' ', ' ', ' ',
'X', 'X', 'O']
self.assertFalse(self.b.check_winning('X'))
def test_is_winning_diagonals_should_be_true(self):
self.b.moves = ['X', ' ', ' ',
' ', 'X', ' ',
' ', ' ', 'X']
self.assertTrue(self.b.check_winning('X'))
self.b.moves = [' ', ' ', 'X',
' ', 'X', ' ',
'X', ' ', ' ']
self.assertTrue(self.b.check_winning('X'))
def test_is_winning_diagonals_should_be_false(self):
self.b.moves = ['X', ' ', ' ',
' ', 'O', ' ',
' ', ' ', 'X']
self.assertFalse(self.b.check_winning('X'))
self.b.moves = [' ', ' ', 'X',
' ', 'X', ' ',
'O', ' ', ' ']
self.assertFalse(self.b.check_winning('X'))
def test_is_valid_move_should_be_true(self):
self.b.moves = [' ', 'X', 'O']
self.assertTrue(self.b.is_valid_move(0))
def test_is_valid_move_should_be_false(self):
self.b.moves = [' ', 'X', 'O']
self.assertFalse(self.b.is_valid_move(1))
self.assertFalse(self.b.is_valid_move(2))
def test_is_full_should_be_false(self):
self.b.moves = [' ', 'X', 'O']
self.assertFalse(self.b.is_full())
def test_is_full_should_be_true(self):
self.b.moves = ['X', 'X', 'O']
self.assertTrue(self.b.is_full())
def test_make_move_should_be_true(self):
self.b.moves = [' ', 'X', 'O']
self.assertTrue(self.b.make_move(0, 'X'))
self.assertEqual(self.b.moves[0], 'X')
def test_make_move_should_be_false(self):
self.b.moves = [' ', 'X', 'O']
self.assertFalse(self.b.make_move(1, 'O'))
self.assertEqual(self.b.moves[1], 'X')
def test_possible_moves(self):
self.b.moves = [' ', 'X', 'O',
'O', 'X', 'O',
'X', 'X', 'O']
self.assertEqual(self.b.possible_moves(), [0])