예제 #1
0
 def test_320For1Move(self):
     result = {}
     messageDictionary = {
         'op': 'recommend',
         "moves": 1,
         'board': {
             'columnCount': 4,
             'rowCount': 4,
             'grid': [0, 0, 0, 2, 0, 0, 0, 2, 4, 4, 0, 0, 2, 0, 4, 4]
         }
     }
     score = 64
     result = recommend(messageDictionary)
     self.assertEquals(result['score'], score)
예제 #2
0
 def test_120For0Moves(self):
     result = {}
     messageDictionary = {
         'op': 'recommend',
         'moves': 0,
         'board': {
             'columnCount': 4,
             'rowCount': 4,
             'grid': [0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 0, 0, 1, 0, 2, 2]
         }
     }
     score = [16, 4]
     result = recommend(messageDictionary)
     self.assertIn(result['score'], score)
예제 #3
0
 def test_410For2Moves(self):
     result = {}
     messageDictionary = {
         'op': 'recommend',
         "moves": 2,
         'board': {
             'columnCount': 4,
             'rowCount': 4,
             'grid': [1, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 1, 0, 1, 2, 2]
         }
     }
     score = 28
     result = recommend(messageDictionary)
     self.assertEquals(result['score'], score)
예제 #4
0
 def test_440For3Moves(self):
     result = {}
     messageDictionary = {
         'op': 'recommend',
         "moves": 3,
         'board': {
             'columnCount': 4,
             'rowCount': 4,
             'grid': [0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 0, 0, 1, 0, 2, 2]
         }
     }
     result = recommend(messageDictionary)
     score = 16
     self.assertEquals(result['score'], score)
예제 #5
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.
    """

    #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)
    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')

    #Convert the dictionary back to a string in JSON format
    resultJson = json.dumps(resultDictionary)
    return resultJson