本文整理汇总了Python中board.Board.move_piece方法的典型用法代码示例。如果您正苦于以下问题:Python Board.move_piece方法的具体用法?Python Board.move_piece怎么用?Python Board.move_piece使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.move_piece方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
class FanoronaGame:
"""This class represents the game as is it."""
def __init__(self):
"""Ctor"""
self.board = Board()
def start(self):
"""Starts the game with GUI"""
gui = GUI(self.board)
gui.start_main_loop()
def start_text(self):
"""Starts the game in text mode"""
black = False # white begin the game
while not self.board.game_is_finished():
self.board.print_board()
if black:
prompt = "black> "
else:
prompt = "white> "
move = raw_input(prompt)
self.board.move_piece(move, black)
black = not black
示例2: testMovePieceWithoutCollision
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def testMovePieceWithoutCollision(self):
board = Board(5, 5)
player = Player()
board.rows[2][3] = player
board.move_piece((2,3), (3, 4))
self.assertEqual(board.rows[3][4], player)
self.assertIsNone(board.rows[2][3])
示例3: testMoveNonExistantPiece
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def testMoveNonExistantPiece(self):
board = Board(5, 5)
player = Player()
board.rows[3][4] = player
board.move_piece((1, 3), (3, 4))
self.assertEqual(board.rows[3][4], player)
self.assertIsNone(board.rows[1][3])
board.move_piece((1, 3), (2, 3))
self.assertIsNone(board.rows[1][3])
self.assertIsNone(board.rows[2][3])
示例4: testMoveOnDestroyedPiece
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def testMoveOnDestroyedPiece(self):
board = Board(5, 5)
player = Player()
enemy = Enemy()
enemy.destroyed = True
board.rows[1][1] = player
board.rows[2][2] = enemy
board.move_piece((1, 1), (2, 2))
self.assertEqual(board.rows[2][2], player)
self.assertIsNone(board.rows[1][1])
self.assertEqual(player.hits, MAX_HEALTH)
示例5: test_move_a_pawn
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def test_move_a_pawn(self):
board = Board(6, 6)
piece_pawn_black = Piece(Piece.PAWN, Piece.BLACK)
piece_pawn_white = Piece(Piece.PAWN, Piece.WHITE)
board.add(piece_pawn_black, 3, 3)
board.add(piece_pawn_white, 4, 3)
self.assertFalse(board.move_piece(3,3,4,3))
self.assertEqual(board.get_piece_in_pos(3,3), piece_pawn_black)
self.assertFalse(board.move_piece(4,3,3,3))
self.assertEqual(board.get_piece_in_pos(4,3), piece_pawn_white)
示例6: test_board_does_not_move_when_move_validator_disallows_it
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def test_board_does_not_move_when_move_validator_disallows_it(self):
move_validator = fmv()
b = Board(5,5,'0',move_validator)
start_place = (0,0,0)
end_place = (0,1,-1)
p = Piece('_')
b.place_piece(lp(p, start_place,0))
b.move_piece(start_place, end_place)
self.assertTrue(b.pieces_dict[start_place] == p
and end_place not in b.pieces_dict)
示例7: test_move_no_pawn
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def test_move_no_pawn(self):
board = Board(6, 6)
piece_king_white = Piece(Piece.KING, Piece.WHITE)
piece_queen_black = Piece(Piece.QUEEN, Piece.BLACK)
piece_rook_black = Piece(Piece.ROOK, Piece.BLACK)
board.add(piece_king_white, 0, 2)
board.add(piece_queen_black, 3, 3)
board.add(piece_rook_black, 4, 3)
self.assertTrue(board.move_piece(3,3,0,0))
self.assertEqual(board.get_piece_in_pos(0,0), piece_queen_black)
self.assertTrue(board.move_piece(4,3,3,3))
self.assertEqual(board.get_piece_in_pos(3,3), piece_rook_black)
示例8: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
class Game:
'''Manages game state'''
def __init__(self):
self.board = Board()
self.players = {
'White': 'human',
'Black': 'ai'
}
self.active_player = 'White'
self.ai = ChessAi()
def move_piece(self, piece, x, y):
res = validate_move(self.board, self.active_player, piece, x, y)
if res == 'move' or res == 'capture':
self.board.move_piece(piece, x, y)
self.toggle_active_player()
elif res == 'castle':
self.board.castle(piece, x, y)
self.toggle_active_player()
elif res == 'en_passant':
raise NotImplemented('En passant not implemented')
return res
def active_player_is_ai(self):
return self.players[self.active_player] == 'ai'
def toggle_active_player(self):
self.active_player = 'Black' if self.active_player == 'White' else 'White'
def enumerate_board_coordinates(self):
return self.board.enumerate_coordinates()
def get_piece(self, x, y):
return self.board.get_piece(x, y)
def make_ai_move(self):
assert self.active_player_is_ai()
piece, pos = self.ai.pick_move(self.board, self.active_player)
x, y = pos
self.move_piece(piece, x, y)
示例9: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
### CREATING THE GAME INSTANCE ###
game = Game(player_one, player_two)
### CHOOSING THE BOARD ###
board = Board(6, 4)
board.initialize_king_and_rook()
game_stats = Stats(game, x)
### MAIN GAME LOOP ###
start = time.time()
while game.is_terminal(board) is False:
search = SeachMethod(SeachMethod.AIMA_ALPHABETA_SEARCH, 2)
game_stats.set_search_method(SeachMethod.AIMA_ALPHABETA_SEARCH)
move = search.search(board, game)
game_stats.add_expanded_nodes(game.get_player_turn(), search.get_expand_nodes())
board.move_piece(move[0][0], move[0][1], move[1][0], move[1][1])
game_stats.add_visit_nodes(game.get_player_turn().get_color())
game.change_turn()
print board.to_string()
game_stats.set_game_time(time.time() - start)
print game.get_player_turn().to_string() + ", YOU HAVE BEEN DEFEATED!"
game_stats.set_winner(game.get_player_turn())
### CREATING THE GAME INSTANCE ###
game = Game(player_two, player_one)
### CHOOSING THE BOARD ###
board = Board(6, 4)
board.initialize_king_and_rook()
示例10: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
#.........这里部分代码省略.........
# be voided by a move is uniquely determined by the starting index
# of the move - regardless of what piece moves from that position
# (excluding chess variants like chess960).
rights_map = {0: 'q', 4: 'kq', 7: 'k',
56: 'Q', 60: 'KQ', 63: 'K'}
void_set = ''.join([rights_map.get(start, ''),
rights_map.get(end, '')])
new_rights = [r for r in self.state.rights if r not in void_set]
fields[1] = ''.join(new_rights) or '-'
# set en passant target square when a pawn advances two spaces
if piece.lower() == 'p' and abs(start - end) == 16:
fields[2] = Game.i2xy((start + end) // 2)
# reset the half move counter when a pawn moves or is captured
fields[3] = self.state.ply + 1
if piece.lower() == 'p' or target.lower() != ' ':
fields[3] = 0
# Increment the turn counter when the next move is from white, i.e.,
# the current player is black
fields[4] = self.state.turn
if self.state.player == 'b':
fields[4] = self.state.turn + 1
# check for pawn promotion
if len(move) == 5:
piece = move[4]
if self.state.player == 'w':
piece = piece.upper()
# record the move in the game history and apply it to the board
self.move_history.append(move)
self.board.move_piece(start, end, piece)
# move the rook to the other side of the king in case of castling
c_type = {62: 'K', 58: 'Q', 6: 'k', 2: 'q'}.get(end, None)
if piece.lower() == 'k' and c_type and c_type in self.state.rights:
coords = {'K': (63, 61), 'Q': (56, 59),
'k': (7, 5), 'q': (0, 3)}[c_type]
r_piece = self.board.get_piece(coords[0])
self.board.move_piece(coords[0], coords[1], r_piece)
# in en passant remove the piece that is captured
if piece.lower() == 'p' and self.state.en_passant != '-' \
and Game.xy2i(self.state.en_passant) == end:
ep_tgt = Game.xy2i(self.state.en_passant)
if ep_tgt < 24:
self.board.move_piece(end + 8, end + 8, ' ')
elif ep_tgt > 32:
self.board.move_piece(end - 8, end - 8, ' ')
# state update must happen after castling
self.set_fen(' '.join(str(x) for x in [self.board] + list(fields)))
def get_moves(self, player=None, idx_list=range(64)):
"""
Get a list containing the legal moves for pieces owned by the
specified player that are located at positions included in the
idx_list. By default, it compiles the list for the active player
(i.e., self.state.player) by filtering the list of _all_moves() to
eliminate any that would expose the player's king to check.
"""
if not self.validate:
return self._all_moves(player=player, idx_list=idx_list)
示例11: testMovePieseOutsideOfBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
def testMovePieseOutsideOfBoard(self):
board = Board(5, 5)
player = Player()
board.rows[1][1] = player
board.move_piece((1, 1), (-6, 7))
self.assertEqual(board.rows[1][1], player)
示例12: print
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import move_piece [as 别名]
tmp_list.extend(tmp)
my_graphs_dict = {}
game_nbr=0
try:
for header, game in games_dict.iteritems():
print(header)
tmp_list = []
my_board = Board()
tmp_list.append(Graph(my_board.board))
i=0
for move in game:
#if move[0]=="B": pdb.set_trace()
print(i)
print(move)
my_board.move_piece(i, move)
my_board.showScreen()
#raw_input()
tmp_list.append(Graph(my_board.board))
i = (i+1) %2
my_graphs_dict[game_nbr] = tmp_list
game_nbr+=1
except:
traceback.print_exc()
pdb.set_trace()