Introduction to Computer Programming

Program 2: Simple game REPL

Due date: Thursday, October 4 at 5pm


Instructions


Guidelines

Unless explicitly indicated otherwise:


The assignment itself

  1. Complete string_to_int(s) so that it returns a nonnegative integer corresponding to the string s assuming it is nonempty and consists entirely of digits; if not, it returns None. Examples:

    >>> string_to_int('0')
    0
    >>> string_to_int('41933')
    41933
    >>> string_to_int('000041933')     # leading 0s are ok
    41933
    >>> string_to_int('-1') is None    # minus sign not allowed
    True
    >>> string_to_int('3.14') is None
    True
    >>> string_to_int('5x7') is None
    True
    >>> string_to_int('') is None
    True

    Hint: use an if-else statement, the is_digit method, and the int conversion operation.

  2. Complete read_number_in_range(start, stop) so that it returns a number between start (inclusive) and stop (exclusive) by repeatedly prompting the user for such a number until a valid one is provided. Your code should call string_to_int. Example:

    >>> read_number_in_range(5, 12)
    Enter a value between 5 and 12: 19
    19 is out of range. Please try again.
    Enter a value between 5 and 12: x
    x is not a number. Please try again.
    Enter a value between 5 and 12: 3
    3 is out of range. Please try again.
    Enter a value between 5 and 12: 10
    10

    At this point, when running the game, the program should correctly ask the user to choose the number of rows and columns for the game board.

  3. Complete normalize_action(action) so it returns a string that represents a lowercase normalization of the action string. This should allow for case insensitivity and single-letter abbreviations. Examples:

     >>> normalize_action('East')
    'east'
     >>> normalize_action('e')
    'east'
     >>> normalize_action('STATUS')
     'status'
     >>> normalize_action('H')
     'help'
     >>> normalize_action('WhatEver')   # if action not valid command, still lowercased
     'whatever'
  4. Complete request_yes(prompt) so that it returns True if the user, given the string prompt, inputs yes or y (case insensitive). Returns False if the user enters no (or n, case insensitive). It should repeat the request for user input until a valid yes or no is supplied. Examples:

    >>> request_yes('Keep playing? ')
    Keep playing? what?
    Enter "yes" or "no".
    Keep playing? ok
    Enter "yes" or "no".
    Keep playing? n
    False
    >>> request_yes('Need help? ')
    Need help? who me?
    Enter "yes" or "no".
    Need help? YES
    True
  5. Fix welcome so that it matches its specification. Examples:

    >>> welcome()
    Welcome to Find the Flag!
    
    Would you like to see the instructions? what?
    Enter "yes" or "no".
    Would you like to see the instructions? n
    
    >>> welcome()
    Welcome to Find the Flag!
    
    Would you like to see the instructions? yes
    
    Your goal is to find the flag hidden on one cell of an invisible
    grid in as few moves as possible.
    
    At the outset of the game...
  6. Modify the main REPL, get_column_change and get_row_change so that the cardinal direction actions work: namely, moving east, north, south or west, moves the player's row and column one position at a time. Be sure to only allow the move if the coordinates remain within the bounds of the game board. If a move is successful, the move counter should be increased by 1. Examples:

    %> e
    ok.
    
    %> s
    ok.
    
    %> status
    You are at row 5 and column 4.
    You are 5 units from the flag.
    You have made 2 moves so far.
    
    %> s
    ok.
    
    %> status
    You are at row 6 and column 4.
    You are 6 units from the flag.
    You have made 3 moves so far.
    
    %> s
    You cannot go that way.
    
    %> status
    You are at row 6 and column 4.
    You are 6 units from the flag.
    You have made 3 moves so far.
  7. Modify the initial position of the flag so that it is placed at a random row and column. (Follow the example of how the player's position is randomly selected.) Add a while loop to guaranteed that the flag position is at least MIN_DISTANCE away from the player's starting position.

  8. Implement winning: if the player reaches the flag, the game ends. For example:

    %> n
    Congratulations, you have captured the flag!
    You played a total of 5 moves.
    Thank you for playing.
  9. Implement losing: if the player fails to reach the flag before max_moves are exhausted, the game ends. The maximum number of moves can be computed in relation to the size of the board (you can choose precisely what that relation is). An example of losing:

    %> s
    Unfortunately, you have run out of time.
    You played a total of 8 moves.
    Thank you for playing.
  10. Add warmer/colder hints for the player so that when they move if they are getting closer to the flag they receive a "warmer" response, away from the flag, a "colder" response. Examples:

    %> e
       Warmer!
    %> s
       Warmer!
    %> s
       Colder.
    %> w
       Colder.
    %> e
       Warmer!
    %> e
       Warmer!
    %> e
       Warmer!
    %> n
    Congratulations, you have captured the flag!
    You played a total of 8 moves.
    Thank you for playing.

Extra credit