# L-36 MCS 260 Mon 11 Apr 2016 : class_frog.py

"""
This script defines a simple object-oriented model of a frog.
The class diagram is
+-----------+
|   Frog    |
+-----------+
| position  |   coordinates (x,y) for its location
| hop limit |   number of hops done by the frog
| step size |   largest step size a frog can do
| rest time |   time while frog rests between steps
+-----------+
|   hop     |   wait a bit and hop to the next spot
|   run     |   do some fixed number of steps
+-----------+
"""

from time import sleep
from random import randint

class Frog(object):
    """
    Object oriented model of a frog.
    """
    def __init__(self, n, x, y, h, d, m):
        """
        A frog with name n at (x,y)
        makes m hops of size at most h,
        resting at most d seconds.
        """
        self.name = n
        self.position = (x, y)
        self.hop_limit = m
        self.step_size = h
        self.rest_time = d

    def hop(self):
        """
        The frog waits a bit before hopping.
        """
        name = self.name
        pos = self.position
        rnd = randint(0, self.rest_time)
        print(name + ' at ' + str(pos) \
          + ' waits ' + str(rnd) + ' seconds')
        sleep(rnd)
        hopstep = self.step_size
        (xps, yps) = self.position
        nxp = xps + randint(-1, +1)*hopstep
        nyp = yps + randint(-1, +1)*hopstep
        self.position = (nxp, nyp)
        print(name + ' hops to ' + str((nxp, nyp)))

    def run(self):
        """
        The frog does as many hops
        as the value set by self.hop_limit.
        """
        while self.hop_limit > 0:
            self.hop()
            self.hop_limit = self.hop_limit - 1

def main():
    """
    Kermit our hero hops around!
    """
    print('Kermit the frog is born ...')
    k = Frog('Kermit', +10, +10, 5, 2, 4)
    print('Kermit starts hopping ...')
    k.run()

if __name__ == "__main__":
    main()
