Exemple #1
0
    def test200_010_AcceptanceTest2(self):
        messageDictionary = {
            "direction": "left",
            "board": {
                "columnCount": 4,
                "rowCount": 4,
                "grid": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            }
        }
        resultDictionary = swipe.swipe(messageDictionary)
        result = resultDictionary["score"]
        expected = 4
        self.assertEquals(expected, result)

        def test300_010_AcceptanceTest1(self):
            messageDictionary = {
                "direction": "up",
                "board": {
                    "columnCount": 4,
                    "rowCount": 4,
                    "grid": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                }
            }
            resultDictionary = swipe.swipe(messageDictionary)
            result = resultDictionary["score"]
            expected = 0
            self.assertEquals(expected, result)
Exemple #2
0
def dispatch(messageJson=None):
    """
        dispatch is the microservice dispatcher for IndigoGirls, a 2048-like game.  It routes
        requests for game state transformations to the appropriate functions
        :param
            messageJson: JSON string that describes the state of the game needed for the
                        requested transformation
            :return:    A JSON string that describes the state of the game after the requested transformation
                        has taken place.
    """
    def buildErrorString(diagnostic=None):
        """
            returns a dictionary containing the specified key and accompanying diagnostic information
            :param
                diagnostic:     A string that describes the error
            :return:    A dictionary that contains the specified error key having a value that
                        consists of the specfied error string followed by a free-form diagnostic message
        """
        ERROR_PROPERTY = u'gameStatus'
        ERROR_PREFIX = u'error:  '
        return {ERROR_PROPERTY: ERROR_PREFIX + diagnostic}

    #Validate JSONness of input be converting the string to an equivalent dictionary
    try:
        messageDictionary = json.loads(messageJson)
    except:
        resultDictionary = json.dumps(
            buildErrorString('input JSON string is invalid'))
        return resultDictionary

    #Validate presence of dispatching code
    if (u"op" not in messageDictionary):
        resultDictionary = json.dumps(buildErrorString('op is missing'))
        return resultDictionary

    #Perform the game transformation as directed by the value of the "op" key
    #  input to each function:  a dictionary containing the name-value pairs of the input JSON string
    #  output of each function:  a dictionary containing name-value pairs to be encoded as a JSON string
    if (messageDictionary[u"op"] == u"initializeGame"):
        resultDictionary = initializeGame(
            messageDictionary)  #<---------- Added my implementation
    elif (messageDictionary[u"op"] == u"swipe"):
        resultDictionary = swipe(messageDictionary)
    elif (messageDictionary[u"op"] == u"recommend"):
        resultDictionary = recommend(messageDictionary)
    elif (messageDictionary[u"op"] == u"status"):
        resultDictionary = status(messageDictionary)
    elif (messageDictionary[u"op"] == u"predict"):
        resultDictionary = predict(messageDictionary)
    else:
        resultDictionary = buildErrorString('op is invalid')

    #Covert the dictionary back to a string in JSON format
    resultJson = json.dumps(resultDictionary)
    return resultJson
Exemple #3
0
 def test500_010_AcceptanceTest5(self):
     messageDictionary = {
         "board": {
             "columnCount": 4,
             "rowCount": 4,
             "grid": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
         }
     }
     resultDictionary = swipe.swipe(messageDictionary)
     result = resultDictionary[u"gameStatus"]
     expected = "error: direction missing"
     self.assertEquals(expected, result)
def predict(messageDictionary):
    if (u"board" not in messageDictionary):  #checking direction
        resultDictionary = {"gameStatus": 'error: board missing'}
        return resultDictionary

    #reading in values:
    board = messageDictionary["board"]
    errorDictionary, validity, rowCount, columnCount = initialize.validateRowColumn(
        board)
    if (validity == False):
        return errorDictionary

    if (u"grid" not in board):  # check if grid is missing
        resultDictionary = {"gameStatus": 'error: grid missing'}
        return resultDictionary

    currentGrid = board["grid"]
    oldGrid = currentGrid[:]
    gridSize = rowCount * columnCount

    errorDictionary, validity = swipe.validateGrid(messageDictionary, gridSize,
                                                   currentGrid, oldGrid)
    if (validity == False):
        return errorDictionary

    #Get move count
    moves = 0
    if (u"moves" not in messageDictionary):  #checking moves
        moves = 1
    else:
        if not (isinstance(messageDictionary["moves"], int)):
            resultDictionary = {
                "gameStatus": "error: improper value for moves"
            }
            return resultDictionary
        else:
            if (messageDictionary["moves"] < 0):
                resultDictionary = {
                    "gameStatus": "error: negative value for moves"
                }
                return resultDictionary
            if (messageDictionary["moves"] == 0):
                resultDictionary = {
                    'score': 0,
                    'board': board,
                    "gameStatus": "underway"
                }
                return resultDictionary
            moves = messageDictionary[u"moves"]

    #calculating prediction moves
    totalScore = 0
    totalScore2 = 0
    highScore = 0
    lowScore = 0
    averageScore = 0
    inputDict = 0
    j = 0
    while (j < moves):
        inputDict, inputDict2 = {
            "direction": "up",
            "board": board
        }, {
            "direction": "up",
            "board": board
        }
        upMove = swipe.swipe(inputDict)
        if (u"score" not in upMove):
            upScore = -1
        else:
            upScore = upMove["score"]
        inputDict, inputDict2 = {
            "direction": "down",
            "board": board
        }, {
            "direction": "down",
            "board": board
        }
        downMove = swipe.swipe(inputDict)
        if (u"score" not in downMove):
            downScore = -1
        else:
            downScore = downMove["score"]
        inputDict, inputDict2 = {
            "direction": "left",
            "board": board
        }, {
            "direction": "left",
            "board": board
        }
        leftMove = swipe.swipe(inputDict)
        if (u"score" not in leftMove):
            leftScore = -1
        else:
            leftScore = leftMove["score"]
        inputDict, inputDict2 = {
            "direction": "right",
            "board": board
        }, {
            "direction": "right",
            "board": board
        }
        rightMove = swipe.swipe(inputDict)
        if (u"score" not in rightMove):
            rightScore = -1
        else:
            rightScore = rightMove["score"]

        #check if loss
        if ((upMove["gameStatus"] != "underway")
                and (downMove["gameStatus"] != "underway")
                and (leftMove["gameStatus"] != "underway")
                and (rightMove["gameStatus"] != "underway")):
            resultDictionary = {
                "gameStatus":
                "error: no tiles can be shifted for number of given moves"
            }
            return resultDictionary

        highScore, lowScore, averageScore = upScore, upScore, upScore
        if (highScore < downScore):
            highScore = downScore
        if (highScore < leftScore):
            highScore = leftScore
        if (highScore < rightScore):
            highScore = rightScore

        if (highScore == upScore):
            inputDict = upMove
        if (highScore == downScore):
            inputDict = downMove
        if (highScore == leftScore):
            inputDict = leftMove
        if (highScore == rightScore):
            inputDict = rightMove

        if (lowScore > downScore):
            lowScore = downScore
        if (lowScore > leftScore):
            lowScore = leftScore
        if (lowScore > rightScore):
            lowScore = rightScore

        if (lowScore == upScore):
            inputDict2 = upMove
        if (lowScore == downScore):
            inputDict2 = downMove
        if (lowScore == leftScore):
            inputDict2 = leftMove
        if (lowScore == rightScore):
            inputDict2 = rightMove

        totalScore = totalScore + inputDict["score"]
        totalScore2 = totalScore2 + inputDict2["score"]
        if (j == 0):
            resultDictionary = {
                "score": inputDict["score"],
                "board": inputDict["board"]
            }
        j = j + 1

    averageScore = math.floor((highScore + lowScore) / 2)
    resultDictionary["gameStatus"] = inputDict["gameStatus"]
    predictDictionary = {
        "highScore": highScore,
        "lowScore": lowScore,
        "averageScore": averageScore
    }
    resultDictionary = predictDictionary
    resultDictionary["gameStatus"] = "underway"

    return resultDictionary
Exemple #5
0
def status(messageDictionary):
    if (u"board" not in messageDictionary):  #checking direction
        resultDictionary = {"gameStatus": 'error: board missing'}
        return resultDictionary

    #reading in values:
    board = messageDictionary["board"]

    board = messageDictionary["board"]
    errorDictionary, validity, rowCount, columnCount = initialize.validateRowColumn(
        board)
    if (validity == False):
        return errorDictionary

    if (u"grid" not in board):  # check if grid is missing
        resultDictionary = {"gameStatus": 'error: grid missing'}
        return resultDictionary

    currentGrid = board["grid"]
    oldGrid = currentGrid[:]
    gridSize = rowCount * columnCount

    errorDictionary, validity = swipe.validateGrid(messageDictionary, gridSize,
                                                   currentGrid, oldGrid)
    if (validity == False):
        return errorDictionary

    #Get move count
    moves = 0
    if (u"moves" not in messageDictionary):  #checking moves
        moves = 1
    else:
        if not (isinstance(messageDictionary["moves"], int)):
            resultDictionary = {
                "gameStatus": "error: improper value for moves"
            }
            return resultDictionary
        moves = messageDictionary[u"moves"]

    #check tile
    tile = 0
    if (u"tile" not in messageDictionary):  # check if grid is missing
        tile = 2**round(rowCount * columnCount * 0.6875)
    else:
        tile = messageDictionary["tile"]
        if (tile < 2 or tile > 2**(rowCount * columnCount)):
            resultDictionary = {"gameStatus": "error: invalid tile value"}
            return resultDictionary

    #Check if win
    tileVal = math.log(tile, 2)
    j = 0
    while (j < gridSize):
        if (currentGrid[j] >= tileVal):
            resultDictionary = {"gameStatus": "win"}
            return resultDictionary
        j = j + 1

    #check if loss
    inputDict = {"direction": "up", "board": board}
    upMove = swipe.swipe(inputDict)
    inputDict = {"direction": "down", "board": board}
    downMove = swipe.swipe(inputDict)
    inputDict = {"direction": "left", "board": board}
    leftMove = swipe.swipe(inputDict)
    inputDict = {"direction": "right", "board": board}
    rightMove = swipe.swipe(inputDict)

    if ((upMove["gameStatus"] != "underway")
            and (downMove["gameStatus"] != "underway")
            and (leftMove["gameStatus"] != "underway")
            and (rightMove["gameStatus"] != "underway")):
        resultDictionary = {"gameStatus": "lose"}
        return resultDictionary

    resultDictionary = {"gameStatus": "underway"}
    return resultDictionary
Exemple #6
0

class LoginHandler(tornado.web.RequestHandler):
    """
    request handler for Login page
    """
       
    @sid  
    def get(self):

        # render login page
        self.render(r"frontend/login.html")


# setup hanlders
handlers = [
    (r'/', AccountHandler),
    (r'/login/', LoginHandler)
]


# setup swipe app
MyApp = swipe(App)


if __name__ == '__main__':

    MyApp(handlers, config).run(8080)


    
            swipe = 'left'
        if x < 0:
            swipe = 'right'
    # print swipe
    pygame.event.post(pygame.event.Event(SWIPE, value=swipe))
    return True

def longPress(downTime):
    if pygame.time.get_ticks() - longPressTime > downTime:
        return True
    else:
        return False

# Run our main loop

swype = swipe()
pygame.event.set_blocked([1,4])
while not quit:
    for event in pygame.event.get():
        # if swype.event_handler(event):
            # continue
        event_used = pluginScreens[screenindex].event_handler(event)
        if not event_used:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_q:
                quit = True
            if event.type == pygame.QUIT:
                quit = True
            if event.type == piscreenevents['toggle_fps'].type:
                SHOW_FPS = not SHOW_FPS
            if (event.type == pygame.MOUSEBUTTONDOWN):
                # swipe()