RedHat-Israel / ROSE

ROSE project car race game
GNU General Public License v2.0
34 stars 125 forks source link

Print game stats to console #472

Closed orilc closed 2 months ago

orilc commented 1 year ago

At the end of the game we would like to see details about the each driver's runs, like how many penguins were collected out of possible number of penguins, number of jumps and breaks that scored points, number of collisions with obstacles.

These details are printed to the console.

Example:

2023-08-10 13:30:59,286 INFO    [game] Stats:
 # |            Name | Score |  Penguins |    Cracks |     Water | Collisions |
   |                 |       | Collected | Collected | Collected |            |
___________________________
 1 |          Group5 |   673 |    6 / 10 |         3 |         2 |          1 |
 2 |          Group4 |   350 |    0 / 10 |         0 |         0 |         13 | (game:123)
orilc commented 1 year ago

@sleviim Please review:)

nirs commented 1 year ago
2023-08-10 13:30:59,286 INFO    [game] Stats:
 # |            Name | Score |  Penguins |    Cracks |     Water | Collisions |
   |                 |       | Collected | Collected | Collected |            |
___________________________
 1 |          Group5 |   673 |    6 / 10 |         3 |         2 |          1 |
 2 |          Group4 |   350 |    0 / 10 |         0 |         0 |         13 | (game:123)

Thanks for sharing the example. I don't think it is very clear as is, and the code to created is not clear. What if we keep the stats in a dict, and print the dict in simpler yaml format instead of a table?

$ cat stats.py 
import sys
import yaml

stats = {
    "stats": {
        "obstacles": {
            "penguin": 10,
            "crack": 7,
            "water": 4,
        },
        "players": {
            "gropup4": {
                "penguin": 6,
                "crack": 3,
                "water": 2,
                "collision": 1,
            },
            "gropup5": {
                "penguin": 0,
                "crack": 0,
                "water": 0,
                "collision": 13,
            },
        },
    },
}

yaml.dump(stats, sys.stdout, sort_keys=False)

Example output:

$ python3 stats.py 
stats:
  obstacles:
    penguin: 10
    crack: 7
    water: 4
  players:
    gropup4:
      penguin: 6
      crack: 3
      water: 2
      collision: 1
    gropup5:
      penguin: 0
      crack: 0
      water: 0
      collision: 13

The advantage is keeping stats in an easy to use way inside the program, making the output machine readable so it is easy to consume by other programs, and having no code to maintain for formatting the results.