Tuesday, December 22, 2020

Developing for Monopoly board game

 Developing for Monopoly


    I started by creating a .json describing parts of the board. This will have been updated later, since .json allows mixed and missing properties for each object in the list.

    Starting with this is the key to the whole project. It allows imagination to exist in a worfklow, state of flow context.

    The next step was writing a sketch of tests that would have to pass. This usually starts as a thought experiment when designing OOP (object oriented programming) classes. This gitlab commit contains the first step, a test function.


        The main class should be the board itself, on top of which the players change their position, assign ownership to positions, and trigger events. The MonopolyBoard() is created at line 9, then the number of players is set at line 10, the players[] list will contain Player objects with properties such as cash, and owned properties. To save time, I gave up Properties class, and changed properties.Add() call to a function AddProperties within the Player() class itself.


     
    In the later commit, BoardProperties simply has a .json parsing Python class as an interface to the data contained in the .json file. 



The player will be this:

    

    And the monopoly board:

        


        

    That's the last of the pictures, the code is enough with small tweaks to pass the first batch of tests. The next and last part of the piecing together a minimal functioning collection of routines, from which OOP style will emerge with refactoring, is the DiceSimulation() and WorstCase()... you can imagine what they do.

    Some of the highlights are this list

            probability = [0, 0, 2.78, 5.56, 8.33, 11.11, 13.89,
                     16.67, 
                         13.89, 11.11, 8.33, 5.56, 2.78
      ]

    The list is merely the probability of rolling the index of the list as the value. After implementing ownership check (a simple routine of parsing players and seeing if property is in their properties member), a structure can be returned containing position, probability, cost (negative or 0) and investment value (positive or 0). This structure of a tuple of 4 will is called "cost" in the snippet: cost = board.WorstCase(player_id = 1)

        Next things to be performed are:

    1. unittest / pytest
    2. refactoring
    3. packaging
    4. extending to a web frontend like django
    5. optionally rendering a more complicated, 3d view, from scratch                

11.09.2020                     After the WorstCase computation


It was easy enough to piece together a Django skeleton onto which I projected the MonopolyBoard class using the session system in Django. With some tweaking I used the Django templating to display a table for each player with his resources and owned properties. The next step is merely to add more buttons and controls to perform the actions from the webpage instead of .cfg files or changing code. 

I got to point 4 without much refactoring, packaging, or unittest/pytest. The idea is that the few methods of the .json controller solve most of the tasks using only player IDs and board positions, I didn't have to create a whole new Property object. This works with 2 python files, the Board and the JSON_Controller, and the Player class.

Ended up concluding development after a three spree series of commits separated by frontend/backend cycles.

The image was then packaged with docker and tested on localhost.