当前位置: 首页>>代码示例>>Python>>正文


Python Board.getSize方法代码示例

本文整理汇总了Python中Board.getSize方法的典型用法代码示例。如果您正苦于以下问题:Python Board.getSize方法的具体用法?Python Board.getSize怎么用?Python Board.getSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Board的用法示例。


在下文中一共展示了Board.getSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BoardTest

# 需要导入模块: import Board [as 别名]
# 或者: from Board import getSize [as 别名]
class BoardTest(unittest.TestCase):
	def setUp(self):
		self.board = Board(8)
		self.board2 = Board(3)
		self.board3 = Board(4)
		
	def testBoardSize(self):
		self.assertEqual(8,self.board.getSize())
		self.assertEqual(3,self.board2.getSize())
		self.assertEqual(4,self.board3.getSize())
		
	def test_Remove_From_Board(self):
		self.board.removePiece(2,1)
		self.assertEqual(0,self.board.countAiPieces())
		self.board.removePiece(6,6)
		self.assertEqual(0,self.board.countPlayerPieces())
		
	def test_Add_To_Board(self):
		# board 1
		self.board.addPiece("AI",0,1,1)
		self.board.addPiece("Player",0,6,6)
		self.assertEqual(1,self.board.countPlayerPieces())
		self.assertEqual(1,self.board.countAiPieces())
		self.assertTrue(self.board.pieceAt(1, 1))
		self.assertTrue(self.board.pieceAt(6, 6))
		
		# board 2		
		self.board2.addPiece("test",0,1,1)
		self.board2.addPiece("AI",3,1,2)
		self.assertEqual(0,self.board2.countPlayerPieces())
		self.assertEqual(0,self.board2.countAiPieces())
		self.assertFalse(self.board2.pieceAt(1, 1))
		self.assertFalse(self.board2.pieceAt(1, 2))
		
		self.board2.addPiece("AI",1,2,1)
		self.assertEqual(0,self.board2.countPlayerPieces())
		self.assertEqual(1,self.board2.countAiPieces())
		self.assertTrue(self.board2.pieceAt(2, 1))
		
		# board 3
		self.board3.addPiece("AI",0,1,1)
		self.assertEqual(0,self.board3.countPlayerPieces())
		self.assertEqual(1,self.board3.countAiPieces())
		self.assertTrue(self.board3.pieceAt(1, 1))
	
	def test_Get_Piece_Info(self):
		# board 1
		self.piece = self.board.getPieceAt(6, 6)
		self.assertEqual("Player", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
		# board 2
		self.piece = self.board2.getPieceAt(2, 1)
		self.assertEqual("AI", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
		self.piece = self.board2.getPieceAt(1, 2)
		self.assertIsNone(self.piece)
		
		self.piece = self.board2.getPieceAt(1, 1)
		self.assertIsNone(self.piece)

		# board 3
		self.piece = self.board3.getPieceAt(1, 1)
		self.assertEqual("AI", self.piece.getOwner())
		self.assertEqual(0, self.piece.getType())
		
	def test_Move_Piece(self):
		self.assertTrue(self.board.pieceAt(1, 1))
		self.board.movePiece(1, 1, 2, 1)
		self.assertFalse(self.board.pieceAt(1, 1))
		self.assertTrue(self.board.pieceAt(2, 1))

		self.assertTrue(self.board2.pieceAt(2, 1))
		self.board2.movePiece(2, 1, 2, 2)
		self.assertFalse(self.board2.pieceAt(2, 1))
		self.assertTrue(self.board2.pieceAt(2, 2))

		self.assertTrue(self.board3.pieceAt(1, 1))
		self.board3.movePiece(1, 1, 2, 1)
		self.assertFalse(self.board3.pieceAt(1, 1))
		self.assertTrue(self.board3.pieceAt(2, 1))
	
	def test_Update_Piece(self):
		self.board.updatePieceType(1,6,6)
		self.piece = self.board.getPieceAt(6, 6)
		self.assertEqual("Player", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
	def tearDown(self):
		self.board = None
		self.board2 = None
		self.board3 = None
	
	def main(self):
		self.setUp()
		self.testBoardSize()
		self.test_Add_To_Board()
		self.test_Update_Piece()
		self.test_Get_Piece_Info()
#.........这里部分代码省略.........
开发者ID:astrok100,项目名称:Checkers-Minimax-PyQt,代码行数:103,代码来源:testing.py

示例2: __init__

# 需要导入模块: import Board [as 别名]
# 或者: from Board import getSize [as 别名]
class Checkers:
    """
        default constructor creates the board and populates
        the board with pieces 
    """
    def __init__(self):
        self.board = Board(8)
        self.size = self.board.getSize()
        self.turn = 0
        self.best_move = None
        self.game_over = False
        self.jumpAgain = (False,None,None)
        
        for y in range(0,3):
            for x in range(self.size):
                if(y%2 ==0 and x%2 ==1 ):
                    self.board.addPiece("AI", 0, x, y)
                if(y%2==1 and x%2 ==0):
                    self.board.addPiece("AI", 0, x, y)
        
        for y in range(5,self.size):
            for x in range(self.size):
                if(y%2 ==0 and x%2 ==1 ):
                    self.board.addPiece("Player", 0, x, y)
                if(y%2==1 and x%2 ==0):
                    self.board.addPiece("Player", 0, x, y)
                    
    def loadBoard(self,board):
        self.board = board
        
    # returns the number of Ai pieces
    def getAI(self):
        return self.board.countAiPieces()
    
    # returns the number of player pieces
    def getPlayer(self):
        return self.board.countPlayerPieces()
    
    # ends the game
    def resign(self):
        self.game_over = True
        
    # returns piece at position X Y on the Board
    def getPiece(self,x,y):
        return self.board.getPieceAt(int(x),int (y))
    
    """
         gets if any of the players pieces can jump and adds it to
         the array, and returns it
    """
    def forceJump(self,player):
        moves = []
        for i in range (self.size):
            for j in range (self.size):
                tmp = self.board.getPieceAt(i, j)
                # checks if tmp is not none and player == the piece
                if tmp and player == tmp.getOwner() :
                    jumpF = self.canJump(i, j)
                    # if jumpF array is not empty append i , j and each value in the array
                    # for that piece
                    if jumpF :
                        for n in jumpF:
                            moves.append(n)
        return moves

    # prints the game.    
    def printGame(self):
        count = 0
        tmp = " |0|1|2|3|4|5|6|7|\n"
        for y in range (self.board.getSize()): 
            tmp += str(y) +"|"
            for x in range (self.board.getSize()):
                if(self.board.pieceAt(x, y)== True):
                    count+=1
                    pi = Piece()
                    pi =self.board.getPieceAt(x, y)
                    if(pi.getOwner() == "AI"):
                        tmp = tmp + "0|"
                    else:
                        tmp = tmp + "X|"
                else:
                    tmp = tmp + " |"
            tmp += "\n" 
        print (str(tmp))
        
    # returns if the game is over    
    def isOver(self):
        return self.game_over
    
    # checks to see if the game has ended and sets game_over to true
    def checkWin(self):
        if self.board.countPlayerPieces ==0 or len(self.getMoves("Player")) == 0:
            self.game_over = True
            return "AI"
        elif self.board.countAiPieces ==0 or len(self.getMoves("AI")) == 0: 
            self.game_over = True
            return "Player"
    
    """ 
        validates if piece at X,Y can move to x1 , y1
#.........这里部分代码省略.........
开发者ID:astrok100,项目名称:Checkers-Minimax-PyQt,代码行数:103,代码来源:Checkers.py


注:本文中的Board.getSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。