def test_predictDepth2_02(self):
        input = {}

        board = {}
        board["grid"] = [0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 0, 0, 1, 0, 2, 2]
        board["rowCount"] = 4
        board["columnCount"] = 4

        input["board"] = board
        input["direction"] = "left"
        input["moves"] = 2

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("underway", output["gameStatus"])
        self.assertIn("highScore", output)
        self.assertIn("lowScore", output)
        self.assertIn("averageScore", output)

        highScore = output["highScore"]
        lowScore = output["lowScore"]
        averageScore = output["averageScore"]

        self.assertEqual(highScore, 20)
        self.assertEqual(lowScore, 16)
        self.assertEqual(averageScore, 19)
    def test_predictDepth3_02(self):
        input = {}

        board = {}
        board["grid"] = [1, 1, 3, 2]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = "right"
        input["moves"] = 3

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("underway", output["gameStatus"])
        self.assertIn("highScore", output)
        self.assertIn("lowScore", output)
        self.assertIn("averageScore", output)

        highScore = output["highScore"]
        lowScore = output["lowScore"]
        averageScore = output["averageScore"]

        self.assertEqual(highScore, 28)
        self.assertEqual(lowScore, 4)
        self.assertEqual(averageScore, 16)
    def test_predictWithNoSpecifiedMoves01(self):
        input = {}

        board = {}
        board["grid"] = [1, 1, 0, 0]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = "left"
        #input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("underway", output["gameStatus"])
        self.assertIn("highScore", output)
        self.assertIn("lowScore", output)
        self.assertIn("averageScore", output)

        highScore = output["highScore"]
        lowScore = output["lowScore"]
        averageScore = output["averageScore"]

        self.assertEqual(highScore, 4)
        self.assertEqual(lowScore, 4)
        self.assertEqual(averageScore, 4)
    def test_unicodeDirection(self):
        input = {}

        board = {}
        board["grid"] = [1, 1, 0, 0]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = u"right"
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("underway", output["gameStatus"])
        self.assertIn("highScore", output)
        self.assertIn("lowScore", output)
        self.assertIn("averageScore", output)

        highScore = output["highScore"]
        lowScore = output["lowScore"]
        averageScore = output["averageScore"]

        self.assertEqual(highScore, 4)
        self.assertEqual(lowScore, 4)
        self.assertEqual(averageScore, 4)
    def test_noBoard01(self):

        input = {}
        input["direction"] = "up"
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
Ejemplo n.º 6
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)
    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
    def test_predictNoValidMove_03(self):
        input = {}

        board = {}
        board["grid"] = [1, 2, 3, 4]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = "down"
        input["moves"] = 2

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_MissingDirection01(self):
        input = {}

        board = {}
        board["grid"] = [1, 2, 2, 0]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        #input["direction"] = "up"
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_badDirection02(self):
        input = {}

        board = {}
        board["grid"] = [1, 2, 2, 0]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = None
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_badBoard08(self):
        input = {}

        board = {}
        board["grid"] = [1, 2, -1, 8, 4, 5, 6, 7, 4, 0, 0]
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = "up"
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_badBoard02(self):

        input = {}

        board = {}
        #no grid
        board["rowCount"] = 2
        board["columnCount"] = 2

        input["board"] = board
        input["direction"] = "up"
        input["moves"] = 1

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
    def test_predictManyMoves_01(self):
        input = {}

        board = {}
        board["grid"] = [0] * (100 * 100)
        board["rowCount"] = 100
        board["columnCount"] = 100

        board["grid"][3] = 4
        board["grid"][2] = 5
        board["grid"][23] = 2

        input["board"] = board
        input["direction"] = "down"
        input["moves"] = 1000000

        output = predict(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])