Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print game stats to console #472

Closed
wants to merge 1 commit into from
Closed

Conversation

orilc
Copy link

@orilc orilc commented Aug 10, 2023

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)

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.
@orilc
Copy link
Author

orilc commented Aug 10, 2023

@sleviim Please review:)

@nirs
Copy link
Member

nirs commented Aug 27, 2023

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.

Comment on lines +15 to +18
self.penguin_collected = 0
self.water_collected = 0
self.crack_collected = 0
self.collision_count = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.penguin_collected = 0
self.water_collected = 0
self.crack_collected = 0
self.collision_count = 0
self.penguin_collected = None
self.water_collected = None
self.crack_collected = None
self.collision_count = None

the convention is to init to None and then use reset to set to 0

Comment on lines +11 to +12
self.crack_counter = 0
self.water_counter = 0
Copy link
Member

@yaacov yaacov Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cracks and waters are never counted, need to add counters or remove them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better set to None, and set to zero in the reset method.

Comment on lines +25 to +26
if obstacles.PENGUIN in self._matrix[0]:
self.penguin_counter += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to count at the _generate_row method

@@ -39,6 +49,7 @@ def clear(self, x, y):
self._matrix[y][x] = obstacles.NONE

def reset(self):
self.penguin_counter = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to reset all the other counters too

lines.append(line)
log.info("%s", os.linesep.join(lines))

def loop(self):
self.track.update()
self.track.searching_penguin()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we count obstacles in the generate method, we don't need this call

@@ -107,13 +107,24 @@ def drive_player(self, name, info):
def print_stats(self):
lines = ['Stats:']
top_scorers = sorted(six.itervalues(self.players), reverse=True)
line = '%2s | %15s | %5s | %9s | %9s | %9s | %10s |' % \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with #472 (comment), a stats struct and yaml dump makes more sense to me too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants