Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Safeway's Monopoly
#1
I wrote some code to keep track of Safeway's current Monopoly game:

"""
monopoly.py

Code for Safeway's Monopoly game (Series MON-10, 2017).

Written by Craig "Ichabod" O'Brien, 4/10/2017. Released to the public domain.

Constants:
LETTERS: Letters used at the end of markers. (str)

Functions:
load_data: Load the prize and marker data from monopoly.txt (dict, list)
check_goals: Check the prizes to see if you have the winning markers. (None)
"""

import collections

# Letters used at the end of markers. (str)
LETTERS = 'ABCDEFGH'

def load_data():
    """
    Load the prize and marker data from monopoly.txt (dict, list)

    The return value is a dictionary of prizes and the markers needed to win them,
    and a list of the markers obtained so far.
    """
    with open('monopoly.txt') as data_file:
        # Load the prize data
        goals = collections.OrderedDict()
        for line in data_file:
            # A blank line indicates the end of the prize data.
            if not line.strip():
                break
            # Get the prize and the components for caluclating the markers.
            goal, prefix, start, num_markers = line.strip().split(',')
            # Calcualte the markers needed.
            start = int(start)
            num_markers = int(num_markers)
            numbers = [(start + index) % 10 for index in range(num_markers)]
            markers = []
            for number, character in zip(numbers, LETTERS):
                markers.append('{}{}{}'.format(prefix, number, character))
            # Store the markers needed.
            goals[goal] = markers
        # Load the marker data.
        markers = []
        for line in data_file:
            # Each line is the tickets from one packet.
            markers.extend(line.strip().split())
    # Return the loaded data.
    return goals, markers

def check_goals(goals, markers, verbose=False):
    """
    Check the prizes to see if you have the winning markers. (None)

    Any winning prizes are printed out. If verbose is True, prizes with at least
    one marker show how many markers have been obtained.

    Parameters:
    goals: The prizes and the markers required to win them. (dict)
    markers: The markers obtained to date. (list of str)
    verbose: A flag for output of partially complete goals. (bool)
    """
    # Check all goals
    for goal, targets in goals.items():
        targets_found = sum([marker in markers for marker in targets])
        # Print any wins.
        if targets_found == len(targets):
            print('\nYou have won {}!'.format(goal))
            print('Tickets required to win: {}.\n'.format(', '.join(markers)))
        # Print partial wins in verbose mode.
        elif verbose and targets_found:
            print('{}: {} out of {}'.format(goal, targets_found, len(targets)))


if __name__ == '__main__':
    goals, markers = load_data()
    check_goals(goals, markers, verbose = True)
    marker_counts = collections.Counter(markers)
    print('\nTop five duplicated tickets:')
    print(marker_counts.most_common(5))
The monopoly.txt file should look like this:

Output:
$5 Cash,9J3,6,4 $5 Grocery Gift Card,9H3,2,4 $10 Cash,9G2,8,4 $10 Grocery Gift Card,9F2,4,4 $15 Grocery Gift Card,9E2,0,4 $25 Fandango Gift Card,9D1,6,4 $25 Cash,9C1,2,4 $25 Gift Card Mall,8X1,7,4 $25 Grocery Gift Card,8W2,1,4 $50 Grocery Gift Card,8V2,5,4 $100 Cash,8T2,9,4 $100 Grocery Gift Card,8S3,3,4 $200 Cash,8R3,7,4 $200 Family Picnic,8Q4,1,4 $300 Smart Watch,8P4,5,4 $500 Grocery Gift Card,8N4,9,4 $1000 Laptop Computer,8M5,3,4 $1000 Grocery Gift Card,8L5,7,4 $1000 Cash,8K6,1,4 $1500 LED HD TV,8J6,5,4 $1500 Gas Grill & Groceries,8H6,9,4 $5000 Groceries,8G7,3,4 $5000 Cash,8F7,7,4 $10000 Family Vacation,9B0,7,5 $10000 4-Wheeler,9A0,2,5 $20000 College Tuition,8B9,6,5 $35000 Vehicle,8C9,1,5 $40000 Home Makeover,8D8,6,5 $100000 Cash or Car,8E8,1,5 $1000000 Vacation Home,8Y0,9,8 $1000000 Cash,8Z0,1,8 9H33B 8N50B 8Y15G 8G76D 8K61A 8W23C 8Z06F 9A06E ...
You enter the markers space delimited, with one row per pack of tickets. Actually, as long as they are space delimited, it shouldn't matter how many tickets you enter per row.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
Fair warning: since I didn't win anything, there's obviously a bug in the above code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020