vendredi 13 novembre 2020

testing of 2D game moves in pthon

I assume that the gaming world is two-dimensional, rectangular (with square being a special rectangle), and every possible place in the world has exact coordinates. Each coordinate can be either 'free' (e.g., grass land) or 'blocked' (e.g., a rock). The player can freely move around in this world, but cannot leave the world and can only walk on 'free' fields. Two large rock formations ("#") exist in the top left and the lower right of the world, the rest of the world is freely walkable (" "). The current player position is marked with an "o". Based on the rules defined before, the player could move left, right, and up, but not down, because this way is blocked by a rock. You task is to test and implement a function move that takes two arguments: 1) a game state and 2) a direction into which the player marker should be moved. The function should check whether this move is possible and, if yes, return the mutated game state, as well as all possible walking directions in the new state, ordered alphabetically (i.e., down < left < right < up).

def move(state, direction):
    pass

and

s1 = (
    "#####   ",
    "###    #",
    "#   o ##",
    "   #####"
)
s2 = move(s1, "right")

print("= New State =")
print("\n".join(s2[0]))
print("\nPossible Moves: {}".format(s2[1]))

The testing script is

from unittest import TestCase
from public.script import move
class MoveTestSuite(TestCase):

    def test_move_right(self):
        state = (
            "#####   ",
            "###    #",
            "#   o ##",
            "   #####"
        )
        actual = move(state, "right")
        expected = (
            (
                "#####   ",
                "###    #",
                "#    o##",
                "   #####"
            ),
            ("left", "up")
        )
        self.assertEqual(expected, actual)

    def test_move_up(self):
        # NOTE: this test case is buggy and needs fixing!
        state = (
            "#####   ",
            "###    #",
            "#   o ##",
            "   #####"
        )
        actual = move(state, "up")
        expected = (
            (
                "#####   ",
                "###  o #",
                "#     ##",
                "   #####"
            ),
            ("left", "up", "right")
        )
        self.assertEqual(expected, actual)

Aucun commentaire:

Enregistrer un commentaire