Пример #1
0
 def handleEvent(self, event):
     if event[0] == soya.sdlconst.QUIT:
         raise Exception
     elif event[0] == soya.sdlconst.MOUSEMOTION:
         if self.rightClicking:
             dx,dz = event[3],event[4] # mouse cursor movement
             self.alpha -= dx*0.02
             self.beta  -= dz*0.02
         elif not self.viewOnly:
             cube2dPositions = []
             for cubeCenter in self.cubeCenters:
                 cube3dPos = soya.Point(self.parent,cubeCenter[0],cubeCenter[1],cubeCenter[2])
                 cube2dPositions.append(self.camera.coord3d_to_2d(cube3dPos))
             cube2dPositions = vstack(cube2dPositions)
             i = argmin(map(norm, \
                         cube2dPositions - array([event[1],event[2]])))
             self.selectedCube = self.cubeToBlk.keys()[i]
             if self.selectedCube != self.previouslySelectedCube:
                 self.updateCursor()
                 self.previouslySelectedCube = self.selectedCube
     elif event[0] == soya.sdlconst.MOUSEBUTTONDOWN:
         if event[1] == soya.sdlconst.BUTTON_WHEELDOWN:
             self.selectedVariant += 1
             self.updateCursor()
         elif event[1] == soya.sdlconst.BUTTON_WHEELUP:
             self.selectedVariant -= 1
             self.updateCursor()
         elif event[1] == soya.sdlconst.BUTTON_RIGHT:
             self.rightClicking = True
         elif event[1] == soya.sdlconst.BUTTON_LEFT and not self.viewOnly:
             raise SelectionPerformed(self.selectedMove)
     elif event[0] == soya.sdlconst.MOUSEBUTTONUP:
         if event[1] == soya.sdlconst.BUTTON_RIGHT:
             self.rightClicking = False
     elif event[0] == soya.sdlconst.KEYDOWN:
         if event[1] == soya.sdlconst.K_LEFT:
             self.alpha += 0.1
         elif event[1] == soya.sdlconst.K_RIGHT:
             self.alpha -= 0.1
         elif event[1] == soya.sdlconst.K_DOWN:
             self.beta -= 0.1
         elif event[1] == soya.sdlconst.K_UP:
             self.beta += 0.1
         elif event[1] == soya.sdlconst.K_PAGEUP:
             self.radius = max(self.radius - 0.5, 0.01)
         elif event[1] == soya.sdlconst.K_PAGEDOWN:
             self.radius = min(self.radius + 0.5, 20.0)
         elif event[1] == soya.sdlconst.K_ESCAPE:
             raise Exception
Пример #2
0
 def computeAllBlockPlacements(cls, legalMoves):
     cubeToBlk = {} # cube coords -> (blkId, blkVar)
     for coords, blkId, blkVarId in legalMoves:
         for cube in imap(tuple, coords+blockVarWithOrigin(blkId,blkVarId)):
             if not cubeToBlk.has_key(cube):
                 cubeToBlk[cube] = []
             cubeToBlk[cube].append( (coords,blkId,blkVarId) )
     # If their is a cube higher than another, move its entries to the lower cube
     S = reversed(sortCubes2(array(cubeToBlk.keys())))
     lastOne = tuple(next(S))
     try:
         while True:
             nextToLastOne = tuple(next(S))
             # Same xy coordinates ?
             if lastOne[0]==nextToLastOne[0] and lastOne[1]==nextToLastOne[1] :
                 # nextToLastOne has smaller z
                 L = cubeToBlk.pop(lastOne)
                 cubeToBlk[nextToLastOne].extend( L )
             lastOne = nextToLastOne
     except StopIteration:
         pass
     # Compute the center of each cube key in the soya world frame
     cubeCenters = vstack([array([c[0],c[2],c[1]]) for c in cubeToBlk.keys()])
     return cubeToBlk, cubeCenters
Пример #3
0
def bruteForceTree(gs, root=(None,[]), saveGs=False, depth=2):
    if depth <= 0:
        return root
    knownMoves = []
    if root[0]!=None:
        # Get the next player moves already registered
        # through children nodes
        if len(root[1]) > 0 and root[1][0]['move'] != None:
            knownMoves = vstack(node[0]['move'] for node in root[1])
    # Get the legal moves of the next player
    lm = gs.legalMoves()
    if lm == []:
        lm = [None]
    else:
        # Filter moves that are already known
        lm = [move for move in lm if move not in knownMoves]
    # Add nodes for those which are new
    for move in lm:
        dic = {'player':gs.nextPlayer,'move':move}
        root[1].append( (dic,[]) )
    # Evaluate each move and perform recursion
    for i,node in enumerate(root[1]):
        # Play the move
        move = node[0]['move']
        nextGs = gs.clone().playMove(move)
        if saveGs:
            node[0]['gs'] = nextGs
        # Evaluate the scores
        node[0]['baseScores'] = nextGs.baseScores()
        node[0]['penalty'] = nextGs.penalty()
        # Recursion
        nextGs.bruteForceTree(root=node,saveGs=saveGs,depth=depth-1)
        # DEBUG
        if depth==2:
            print "done node %d/%d" % (i,len(root[1]))
    return root
Пример #4
0
 def test_2D_array2(self):
     a = array([1, 2])
     b = array([1, 2])
     res = vstack([a, b])
     desired = array([[1, 2], [1, 2]])
     assert_array_equal(res, desired)
Пример #5
0
 def test_1D_array(self):
     a = array([1])
     b = array([2])
     res = vstack([a, b])
     desired = array([[1], [2]])
     assert_array_equal(res, desired)
Пример #6
0
 def test_generator(self):
     with assert_warns(FutureWarning):
         vstack((np.arange(3) for _ in range(2)))
 def test_2D_array2(self):
     a = array([1,2]); b = array([1,2]);
     res=vstack([a,b])
     desired = array([[1,2],[1,2]])
     assert_array_equal(res,desired)
 def test_1D_array(self):
     a = array([1]); b = array([2]);
     res=vstack([a,b])
     desired = array([[1],[2]])
     assert_array_equal(res,desired)
Пример #9
0
 def test_2D_array(self):
     a = array([[1], [2]])
     b = array([[1], [2]])
     res = vstack([a, b])
     desired = array([[1], [2], [1], [2]])
     assert_array_equal(res, desired)
Пример #10
0
 def test_generator(self):
     with assert_warns(FutureWarning):
         vstack((np.arange(3) for _ in range(2)))
Пример #11
0
 def test_2D_array(self):
     a = array([[1],[2]]); b = array([[1],[2]]);
     res=vstack([a,b])
     desired = array([[1],[2],[1],[2]])
     assert_array_equal(res,desired)
Пример #12
0
 def test_casting_and_dtype_type_error(self):
     a = np.array([1, 2, 3])
     b = np.array([2.5, 3.5, 4.5])
     with pytest.raises(TypeError):
         vstack((a, b), casting="safe", dtype=np.int64)