def __init__(self, cr): NodePath.__init__(self, "DistributedCheckers") DistributedNode.DistributedNode.__init__(self,cr) self.cr = cr self.reparentTo(render) self.boardNode = loader.loadModel("phase_6/models/golf/regular_checker_game.bam") self.boardNode.reparentTo(self) self.board = CheckersBoard() #game variables self.exitButton = None self.inGame = False self.waiting = True self.startButton = None self.playerNum = None self.turnText = None self.isMyTurn = False self.wantTimer = True self.leaveButton = None self.screenText = None self.turnText = None self.exitButton = None self.numRandomMoves = 0 self.blinker = Sequence() self.moveList = [] self.mySquares = [] self.myKings = [] self.isRotated = False #Mouse picking required stuff self.accept('mouse1', self.mouseClick) self.traverser = base.cTrav self.pickerNode = CollisionNode('mouseRay') self.pickerNP = camera.attachNewNode(self.pickerNode) self.pickerNode.setFromCollideMask(ToontownGlobals.WallBitmask) self.pickerRay = CollisionRay() self.pickerNode.addSolid(self.pickerRay) self.myHandler = CollisionHandlerQueue() self.traverser.addCollider(self.pickerNP, self.myHandler) self.buttonModels = loader.loadModel("phase_3.5/models/gui/inventory_gui") self.upButton = self.buttonModels.find("**//InventoryButtonUp") self.downButton = self.buttonModels.find("**/InventoryButtonDown") self.rolloverButton = self.buttonModels.find("**/InventoryButtonRollover") self.clockNode = ToontownTimer() self.clockNode.setPos(1.16, 0, -0.83) self.clockNode.setScale(0.3) self.clockNode.hide() #[0] GREEN [1] YELLOW [2] PURPLE [3] BLUE [4] PINK [5] RED self.playerColors = [ Vec4(0,0,1,1), Vec4(0,1,0,1) ] self.tintConstant = Vec4(.25,.25,.25,.5) self.ghostConstant = Vec4(0,0,0,.8) #starting positions are used to check and see if a player has gone into #his opposing players starting position, thus to tell if he won. self.startingPositions = [[0,1,2,3,4,5,6,7,8,9,10,11], [20,21,22,23,24,25,26,27,28,29,30,31]] self.knockSound = base.loadSfx("phase_5/audio/sfx/GUI_knock_1.mp3") self.clickSound = base.loadSfx("phase_3/audio/sfx/GUI_balloon_popup.mp3") self.moveSound = base.loadSfx("phase_6/audio/sfx/CC_move.mp3") self.accept('stoppedAsleep', self.handleSleep) ####################### #Fsm and State Data # from direct.fsm import ClassicFSM,State self.fsm = ClassicFSM.ClassicFSM('ChineseCheckers', [State.State('waitingToBegin', self.enterWaitingToBegin, self.exitWaitingToBegin, ['playing','gameOver']), State.State('playing', self.enterPlaying, self.exitPlaying, ['gameOver']), State.State('gameOver', self.enterGameOver, self.exitGameOver, ['waitingToBegin'])], # Initial State 'waitingToBegin', # Final State 'waitingToBegin', ) ######################### #Set up the Board Locators ## x = self.boardNode.find("**/locator*") #set up the locator list so we can mess with it self.locatorList = x.getChildren() #tag the locators for "picking" ingame #also add colision spheres for movement tempList = [] for x in range(0,32): self.locatorList[x].setTag("GamePeiceLocator", "%d" % x) tempList.append(self.locatorList[x].attachNewNode(CollisionNode("picker%d" % x))) tempList[x].node().addSolid(CollisionSphere(0,0,0,.39)) for z in self.locatorList: y = loader.loadModel("phase_6/models/golf/regular_checker_piecewhite.bam") y.find("**/checker_k*").hide() zz = loader.loadModel("phase_6/models/golf/regular_checker_pieceblack.bam") zz.find("**/checker_k*").hide() y.reparentTo(z) y.hide() zz.reparentTo(z) zz.hide()
def initialiseComponent(self): # listen to shutdown events ogl_display = OpenGLDisplay.getDisplayService()[0] self.link((ogl_display, "signal"), (self, "control")) # create board self.boardvis = CheckersBoard(position=(0, 0, -15)).activate() self.interactor_comms = {} self.board = {} for i in range(8): self.board[i] = {} for j in range(8): self.board[i][j] = None # create black pieces self.blackPieces = [] self.blackInteractors = [] for i in range(8): for j in range(3): if (i + j) % 2 == 0: x = float(i) - 3.5 y = float(j) - 3.5 piece = CheckersPiece(position=(x, y, -15), colour=(0.6, 0, 0)).activate() self.blackPieces.append(piece) interactor = CheckersInteractor(target=piece, colour='B').activate() self.blackInteractors.append(interactor) intcomms = self.addOutbox("interactor_comms") self.interactor_comms[id(interactor)] = intcomms self.link((self, intcomms), (interactor, "inbox")) self.link((interactor, "outbox"), (self, "inbox")) self.board[i][j] = 'B' # create white pieces self.whitePieces = [] self.whiteInteractors = [] for i in range(8): for j in range(5, 8): if (i + j) % 2 == 0: x = float(i) - 3.5 y = float(j) - 3.5 piece = CheckersPiece(position=(x, y, -15), colour=(0, 0, 0.6)).activate() self.whitePieces.append(piece) interactor = CheckersInteractor(target=piece, colour='B').activate() self.whiteInteractors.append(interactor) intcomms = self.addOutbox("interactor_comms") self.interactor_comms[id(interactor)] = intcomms self.link((self, intcomms), (interactor, "inbox")) self.link((interactor, "outbox"), (self, "inbox")) self.board[i][j] = 'W' return 1
def __init__(self, cr): NodePath.__init__(self, 'DistributedCheckers') DistributedNode.DistributedNode.__init__(self, cr) self.cr = cr self.reparentTo(render) self.boardNode = loader.loadModel( 'phase_6/models/golf/regular_checker_game') self.boardNode.reparentTo(self) self.board = CheckersBoard() self.exitButton = None self.inGame = False self.waiting = True self.startButton = None self.playerNum = None self.turnText = None self.isMyTurn = False self.wantTimer = True self.leaveButton = None self.screenText = None self.turnText = None self.exitButton = None self.numRandomMoves = 0 self.blinker = Sequence() self.moveList = [] self.mySquares = [] self.myKings = [] self.isRotated = False self.accept('mouse1', self.mouseClick) self.traverser = base.cTrav self.pickerNode = CollisionNode('mouseRay') self.pickerNP = camera.attachNewNode(self.pickerNode) self.pickerNode.setFromCollideMask(ToontownGlobals.WallBitmask) self.pickerRay = CollisionRay() self.pickerNode.addSolid(self.pickerRay) self.myHandler = CollisionHandlerQueue() self.traverser.addCollider(self.pickerNP, self.myHandler) self.buttonModels = loader.loadModel( 'phase_3.5/models/gui/inventory_gui') self.upButton = self.buttonModels.find('**//InventoryButtonUp') self.downButton = self.buttonModels.find('**/InventoryButtonDown') self.rolloverButton = self.buttonModels.find( '**/InventoryButtonRollover') self.clockNode = ToontownTimer() self.clockNode.setPos(1.16, 0, -0.83) self.clockNode.setScale(0.3) self.clockNode.hide() self.playerColors = [Vec4(0, 0, 1, 1), Vec4(0, 1, 0, 1)] self.tintConstant = Vec4(0.25, 0.25, 0.25, 0.5) self.ghostConstant = Vec4(0, 0, 0, 0.8) self.startingPositions = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 ]] self.knockSound = base.loader.loadSfx( 'phase_5/audio/sfx/GUI_knock_1.ogg') self.clickSound = base.loader.loadSfx( 'phase_3/audio/sfx/GUI_balloon_popup.ogg') self.moveSound = base.loader.loadSfx('phase_6/audio/sfx/CC_move.ogg') self.accept('stoppedAsleep', self.handleSleep) self.fsm = ClassicFSM.ClassicFSM('ChineseCheckers', [ State.State('waitingToBegin', self.enterWaitingToBegin, self.exitWaitingToBegin, ['playing', 'gameOver']), State.State('playing', self.enterPlaying, self.exitPlaying, ['gameOver']), State.State('gameOver', self.enterGameOver, self.exitGameOver, ['waitingToBegin']) ], 'waitingToBegin', 'waitingToBegin') x = self.boardNode.find('**/locator*') self.locatorList = x.getChildren() tempList = [] for x in range(0, 32): self.locatorList[x].setTag('GamePeiceLocator', '%d' % x) tempList.append(self.locatorList[x].attachNewNode( CollisionNode('picker%d' % x))) tempList[x].node().addSolid(CollisionSphere(0, 0, 0, 0.39)) for z in self.locatorList: y = loader.loadModel( 'phase_6/models/golf/regular_checker_piecewhite') y.find('**/checker_k*').hide() zz = loader.loadModel( 'phase_6/models/golf/regular_checker_pieceblack') zz.find('**/checker_k*').hide() y.reparentTo(z) y.hide() zz.reparentTo(z) zz.hide() return
#=== HELPER METHODS ====================================== def addPiece(pieceID, owner, location, checkersBoard): newPiece = CheckersPiece(pieceID, owner) newPiece.setLocation(location) owner.addToPieceCollection(pieceID, newPiece) checkersBoard.spaces[location[0]][location[1]].setSpaceResident(newPiece) owner.setNumPieces() #=== SCRIPT ============================================== player1 = HumanPlayer(1) player2 = HumanPlayer(2) newGame = GameController() newGame.setGame(CheckersBoard()) # the arguments to create a CheckersHeuristic instance are a checkers board # and the computer player (so that heuristic can tell which pieces are theirs) heuristicFunction = CheckersHeuristic(newGame.game, player1) newGame.game.addObserver(player1) newGame.game.addObserver(player2) # add some pieces to the board addPiece('X00', player1, (0, 0), newGame.game) addPiece('X01', player1, (1, 1), newGame.game) newGame.game.printBoard() print("Utility value =", heuristicFunction.getUtilityValue(newGame.game)) available_moves = newGame.game.getAvailableMoves() for piece, move_type in available_moves: if piece.getOwner() == player1: print(move_type)