本文整理汇总了Python中board.Board.isLegalMove方法的典型用法代码示例。如果您正苦于以下问题:Python Board.isLegalMove方法的具体用法?Python Board.isLegalMove怎么用?Python Board.isLegalMove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.isLegalMove方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_legal_move_illegal_kills
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_illegal_kills(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board1
#Black pawn moves forward to kill white pawn
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [6, 4, 6, 3]))
#Knight moves forward three spaces to kill black pawn
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [0, 2, 0, 5]))
示例2: test_is_legal_move_moving_into_occupied_space
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_moving_into_occupied_space(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board1
#White rook moves into white pawn
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [0, 0, 0, 1]))
#Black king moves into black bishop
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [4, 7, 5, 7]))
示例3: test_is_legal_move_legal_kills
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_legal_kills(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board1
#White knight kills black pawn
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER,[5, 2, 6, 4]))
#Black queen kills white bishop
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER,[5, 3, 2, 0]))
#White bishop kills black queen
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER,[2, 0, 5, 3]))
示例4: test_is_legal_move_improper_movement
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_improper_movement(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board2
#Pawn - move forward three
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [7, 1, 7, 4]))
#Pawn - move left
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [6, 6, 7, 6]))
#Pawn - move backwards
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [3, 1, 3, 0]))
#Rook - move diagonally
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [1, 2, 2, 3]))
#Knight - move forward two
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [4, 2, 4, 4]))
#Bishop - move forward one
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [2, 0, 2, 1]))
#Queen - move like knight
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [3, 3, 1, 4]))
#King - move forward two
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 4, 4]))
示例5: test_is_legal_move_piece_not_moved
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_piece_not_moved(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board2
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [2, 0, 2, 0]))
示例6: test_is_legal_move_blocked_move
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move_blocked_move(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board1
#Pawn tries to move forward two spaces, is blocked by knight
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [0, 1, 0, 3]))
#Rook tries to move forward, is blocked by pawn
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [7, 7, 7, 4]))
#Rook tries to move across, is blocked by several pieces
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [7, 7, 3, 7]))
#Knight 'jumps' - can't be blocked by any pieces
#Bishop tries to move left diagonally, is blocked by pawn
self.assertFalse(b.isLegalMove(constants.WHITE_PLAYER, [5, 0, 2, 3]))
#Queen tries to move across, is blocked by other queen
self.assertFalse(b.isLegalMove(constants.BLACK_PLAYER, [5, 3, 2, 3]))
示例7: test_is_legal_move
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
def test_is_legal_move(self):
from board import Board
import constants
b = Board()
b._board = ChessTest.board2
#Pawn - move forward one space
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [3, 1, 3, 2]))
#Rook - move forward several spaces
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [7, 7, 7, 3]))
#Rook - move across several spaces
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [7, 7, 3, 7]))
#Knight - move forward and right
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [4, 2, 5, 4]))
#Knight - move left and up
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [1, 7, 3, 6]))
#Bishop - move diagnoally left
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [2, 0, 0, 2]))
#Bishop - move back and right
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [2, 5, 1, 6]))
#Queen - move right one space
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [3, 3, 4, 3]))
#Queen - move left several spaces
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [3, 3, 2, 3]))
#Queen - move left diagonally
self.assertTrue(b.isLegalMove(constants.WHITE_PLAYER, [3, 3, 1, 5]))
#King - move forward
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 4, 5]))
#King - move backward
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 4, 7]))
#King - move left
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 5, 6]))
#King - move right
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 5, 6]))
#King - move right diagonally
self.assertTrue(b.isLegalMove(constants.BLACK_PLAYER, [4, 6, 5, 7]))
示例8: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isLegalMove [as 别名]
class Game(object):
def __init__(self):
"""
Initializes new game.
"""
#Create new game board
self._board = Board()
#Record whose turn it is
self._currentPlayer = constants.WHITE_PLAYER
#Print welcome text
os.system('clear')
welcome = "Welcome to Solidarity Bros. Chess!\n" \
"by Chris Wang, Dmitriy Chukhin, and Jim Ladd\n" \
"\n" \
"To move a piece, give the starting and ending coordinates.\n" \
"To move the white knight for example, type 'b1c3'.\n" \
"To quit, type 'quit'.\n\n" \
"Enjoy!\n\n"
print welcome
def play(self):
"""
Main game loop.
"""
#Print initial board
self._board.printBoard()
while True:
self._nextTurn()
def _nextTurn(self):
"""
Contains logic for executing a player's turn.
Exits when game is finished.
"""
#Get next move from player
move = self._getPlayersNextMove()
move = self._board._moveConverter(move)
while self._board.isLegalMove(self._currentPlayer, move) != True:
move = self._getPlayersNextMove()
move = self._board._moveConverter(move)
#Executes move
self._board.movePiece(self._currentPlayer, move)
#Prints board
os.system('clear')
self._board.printBoard()
#Creates variable other_player
if self._currentPlayer == constants.WHITE_PLAYER:
self._otherPlayer = constants.BLACK_PLAYER
else:
self._otherPlayer = constants.WHITE_PLAYER
#End game conditions
if board_analyzer.isCheckMate(self._board, self._otherPlayer) == True:
print "Player",self._currentPlayer,"has won the game!"
choice = None
while choice != 'quit':
choice = raw_input("Type 'quit' to exit. ")
else:
sys.exit(0)
#Switches players
if self._currentPlayer == constants.WHITE_PLAYER:
self._currentPlayer = constants.BLACK_PLAYER
else:
self._currentPlayer = constants.WHITE_PLAYER
def _getPlayersNextMove(self):
"""
Retrieves player's next move. (e.g. "b1d4").
Ensures that move is well-formed.
@return: Player's next move (e.g. "b1d4")
"""
while True:
playerColor = ""
if self._currentPlayer == constants.WHITE_PLAYER:
playerColor = "White"
else:
playerColor = "Black"
prompt = "%s> " % playerColor
response = raw_input(prompt)
#Check response length
if len(response) != 4:
self._printHelp()
continue
#Does user want to quit?
if response.lower() == 'quit':
sys.exit(0)
#Check row/column values
validColumns = "abcdefgh"
#.........这里部分代码省略.........