Skip to content

Lab Exercise 3 Patch Notes

Updated as of 02/21, 10:46PM

  • Removed __init__() from the MoleInfo and Mole Protocol

  • Changed all instances of base_turns_active to base_active_turns

Summary

  • Protocol descriptions now show just the interface for the objects, and no longer prescribe implementation details (that's up to you to figure out!)
  • Custom Hammer features were removed, for now (they may appear later)
  • Introduced classes that affect how the game behaves
  • Used simpler objects to simplify some type annotations

The deadline for Lab 3 has also been updated.

Updated template files

model.py

# pyright: strict

from __future__ import annotations
from collections.abc import Sequence
from random import Random

from protocols import MoleState, MoleInfo, Mole, MolePopupPlan, GameOverCondition


class WhacAMoleModel:
    ...

whacamole.py

# pyright: strict

from __future__ import annotations
from collections.abc import Sequence
from random import Random

from model import WhacAMoleModel


class WhacAMoleView:
    def ask_for_hole_to_hit(self, moles_info: Sequence[MoleInfo]):
        while True:
            idx = int(input('Enter the hole you want to whack'
                            ' (0-indexed): '))
            if 0 <= idx < len(moles_info):
                break
            print(f'Index {idx} invalid, try again.')
        return idx

    def display_turn(self, turn: int, total_points: int, moles_info: Sequence[MoleInfo]):
        print(f'Turn {turn}')
        print(f'Points: {total_points}')
        print(' '.join([('_' if mole.state == MoleState.INACTIVE else str(mole)) for mole in moles_info]))


class WhacAMoleController:
    def __init__(self, model: WhacAMoleModel, view: WhacAMoleView):
        self._model = model
        self._view = view

    def start(self):
        model = self._model
        view = self._view

        while not model.is_game_over:
            model.start_turn()
            view.display_turn(model.current_turn, model.total_points, model.moles_info)
            idx = view.ask_for_hole_to_hit(model.moles_info)
            model.process_hit(idx)
            model.finish_turn()


if __name__ == '__main__':
    ...

protocols.py

# pyright: strict

from __future__ import annotations
from collections.abc import Sequence
from enum import StrEnum
from random import Random
from typing import Protocol


class MoleState(StrEnum):
    INACTIVE = 'inactive'
    ACTIVE = 'active'
    HIT = 'hit'


class MoleInfo(Protocol):
    ...


class Mole(Protocol):
    ...


class MolePopupPlan(Protocol):
    ...


class GameOverCondition(Protocol):
    ...


Sorry for the inconvenience!