Beispiel #1
0
    def test_0depthRecommend4(self):

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

        moves = 0
        leftMoves = 0
        rightMoves = 0

        input = {}
        input["board"] = board
        input["moves"] = moves

        trials = 1000
        i = 0

        while (i < trials):
            i += 1
            output = recommend(input)

            self.assertNotIn("error", output["gameStatus"])
            grid = output["board"]["grid"]

            if (grid[0] == 3):
                leftMoves += 1
            else:
                rightMoves += 1

        percentMoves = float(leftMoves) / float(trials)
        self.assertEqual(leftMoves + rightMoves, trials)
        self.assertAlmostEqual(percentMoves, 0.5, 1)
Beispiel #2
0
    def test_badBoard1(self):
        input = {}
        input["board"] = "muffin"

        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #3
0
    def test_badBoard2(self):
        input = {}
        board = {}
        input["board"] = board
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #4
0
    def test_noBoard01(self):

        input = {}

        output = recommend(input)

        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
Beispiel #5
0
    def test_noBoard02(self):

        input = {}
        input["tile"] = 4
        input["moves"] = 1

        output = recommend(input)
        #error from assignment 7 could not be replicated
        self.assertIn("gameStatus", output)
        self.assertIn("error", output["gameStatus"])
Beispiel #6
0
    def test_badBoard3(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2

        input = {}
        input["board"] = board
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #7
0
    def test_badBoard5_2(self):
        board = {}
        board["columnCount"] = 2
        board["grid"] = [2, 2, 2, 2, 2, 0]

        input = {}
        input["board"] = board
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #8
0
    def test_goodBoard1(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2] * (3 * 2)

        input = {}
        input["board"] = board
        output = recommend(input)

        self.assertNotIn("error", output["gameStatus"])
Beispiel #9
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
Beispiel #10
0
    def test_badmoves3(self):  #no moves
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2] * (3 * 2)

        input = {}
        input["board"] = board
        input["moves"] = "nan"
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #11
0
    def test_badBoard5_6(self):
        board = {}
        board["columnCount"] = 3
        board["rowCount"] = 101
        board["grid"] = [0] * (3 * 101)
        board["grid"][0] = 1
        board["grid"][1] = 2

        input = {}
        input["board"] = board
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #12
0
    def test_badmoves2(self):
        board = {}
        board["rowCount"] = 3
        board["columnCount"] = 2
        board["grid"] = [2] * (3 * 2)

        moves = -1

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #13
0
    def test_0depthRecommend2(self):
        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        board["grid"] = [2, 5, 1, 3]

        moves = 0

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        self.assertIn("error", output["gameStatus"])
Beispiel #14
0
    def test_1depthRecommend1(self):

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

        moves = 1

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        self.assertEqual(output["score"], 12)
Beispiel #15
0
    def test_1depthRecommend3(self):

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

        moves = 1

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        self.assertEqual(output["score"], 2**2)
        self.assertTrue(output["board"]["grid"][0] == 2
                        or output["board"]["grid"][1] == 2)
Beispiel #16
0
    def test_0depthRecommend3(self):
        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        board["grid"] = [2, 3, 0, 0]

        moves = 0

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        boardOut = output["board"]

        self.assertNotIn("error", output["gameStatus"])
        self.assertEqual(boardOut["grid"][2], 2)
        self.assertEqual(boardOut["grid"][3], 3)
Beispiel #17
0
    def test_1depthRecommend2(self):

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

        moves = 1

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)

        self.assertNotIn("error", output["gameStatus"])
        grid = output["board"]["grid"]

        self.assertTrue(grid[0] == 3 or grid[1] == 3)
Beispiel #18
0
    def test_2depthRecommend1(self):

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

        moves = 2

        input = {}
        input["board"] = board
        input["moves"] = moves
        output = recommend(input)
        outputGrid = output["board"]["grid"]

        self.assertEqual(output["score"], 16)
        self.assertEqual(outputGrid[0], 3)
        self.assertEqual(outputGrid[1], 1)
        self.assertEqual(outputGrid[2], 4)
        self.assertEqual(outputGrid[5], 4)
Beispiel #19
0
    def test_0depthRecommend5(self):

        board = {}
        board["rowCount"] = 2
        board["columnCount"] = 2
        grid = [3, 3, 3, 0]
        board["grid"] = list(grid)

        moves = 0
        leftMoves = 0
        rightMoves = 0
        upMoves = 0
        downMoves = 0

        input = {}
        input["moves"] = moves

        trials = 1000
        i = 0

        while (i < trials):
            i += 1
            input["board"] = copyBoard(board)
            output = recommend(input)

            self.assertNotIn("error", output["gameStatus"])
            grid = output["board"]["grid"]

            if (grid[0] == 4 and grid[2] == 3):
                leftMoves += 1
            elif (grid[1] == 4 and grid[3] == 3):
                rightMoves += 1
            elif (grid[2] == 4 and grid[3] == 3):
                downMoves += 1
            else:
                upMoves += 1

        percentMoves = float(upMoves) / float(trials)
        self.assertEqual(leftMoves + rightMoves + downMoves + upMoves, trials)
        self.assertAlmostEqual(percentMoves, 0.25, 1)