CSc 2010
Lab Assignment 3
Due: 4 March, 2010

There are two problems as in Lab 2. The non-robot problem is an individual assignment and the robot problem is a team assignment.

  1. A non-robot problem (Should be submitted independently). Building on the previous lab, write a Python program to play the Bulls and Cows game: Here are two sample runs of the program:
    >>> from Bulls import *
    >>> main()
    
    Welcome to the game of BULLS and COWS.
    The objective in this game is for you to guess a 4-digit number
    The computer responds with how close your guess is to the target
    BULLS = # common digits with exact matches and
    COWS  = # common digits in wrong position.
    
    Enter guess number 1: 1876
    Bulls =  1   Cows =  1
    Enter guess number 2: 1183
    Your guess should not contain repeating digits
    Enter guess number 2: 1596
    Bulls =  1   Cows =  2
    Enter guess number 3: 1956
    Bulls =  3   Cows =  0
    Enter guess number 4: 1954
    Bulls =  3   Cows =  0
    Enter guess number 5: 1958
    Bulls =  4   Cows =  0
    Congratulations You Won!!
    >>>
    >>> main()
    
    Welcome to the game of BULLS and COWS.
    The objective in this game is for you to guess a 4-digit number
    The computer responds with how close your guess is to the target
    BULLS = # common digits with exact matches and
    COWS  = # common digits in wrong position.
    
    Enter guess number 1: 1234
    Bulls =  0   Cows =  1
    Enter guess number 2: 2345
    Bulls =  0   Cows =  0
    Enter guess number 3: 1678
    Bulls =  0   Cows =  3
    Enter guess number 4: 1679
    Bulls =  0   Cows =  3
    Enter guess number 5: 1670
    Bulls =  0   Cows =  4
    Enter guess number 6: 6170
    Bulls =  0   Cows =  4
    Enter guess number 7: 6710
    Bulls =  1   Cows =  3
    Enter guess number 8: 7610
    Bulls =  2   Cows =  2
    Enter guess number 9: 7601
    Bulls =  1   Cows =  3
    Enter guess number 10: 6701
    Bulls =  0   Cows =  4
    Sorry You ran out of guesses!!
    The correct number was:  7016
    >>>
    
    Submit the code under the file Bulls.py.

  2. A robot problem (Done as a team with one submission per team).

    Purposes of Assignment

  • Learn how to use the urllib module.
  • Gain additional experience with lists and strings.
  • Learn how to write conditionals using if.
  • Learn how to use return to return a value from a function.

    Description of Program

    You are to write a program named SecretCommands that uses a secret web page to control the robot. The URL for the web page is:

    http://tinman.cs.gsu.edu/~raj/2010/sp10/message1.txt

    This page contains a message in which certain words represent robot commands:

    Word Meaning
    forward Move forward
    backward Move backward
    left Turn left
    right Turn right
    one, two, ..., nine Execute future robot commands for this many seconds

    For example, suppose that the secret web page contains the following text:

    The three branches of government must remain independent
    to maintain a balance between left and right. If we are
    to remain one nation, we must move forward, not backward.
    
    Note: Your program should work for any file (not just this file). The robot will perform the following commands:
    Turn left for 3 seconds
    Turn right for 3 seconds
    Move forward for 1 second
    Move backward for 1 second
    
    The robot will speak each word on the web page, stopping to execute a robot command for the words forward, backward, left, and right. The robot must stop executing a command if its stall sensor indicates a stall. All robot movements are done at full speed. The default duration for a command (if the web page doesn't specify) is one second.

    Write your program as four functions:

    1. executeCommand(word, seconds): Speaks word, then checks to see if it is a movement command (forward, backward, left, or right). If so, the command is executed for the specified number of seconds. word must be lowercase and not contain any punctuation marks.
    2. modifyWord(word): Returns word after it has been converted to lowercase and any trailing punctuation has been removed. For example, modifyWord("Backward.") returns "backward".
    3. wordToNumber(word): Returns the numerical value of word if it is one, two, ..., eight, or nine. Otherwise, returns 0.
    4. main(): Reads the web page as a string and splits it into a list of words. For each word in the list, invokes modifyWord to "clean up" the word, invokes executeCommand to execute the word as a command, and invokes wordToNumber to convert the word into a number. If wordToNumber returns a value greater than zero, this value becomes the number of seconds for which commands are executed in the future.
    You must put comments at the beginning of the program giving the program name, your name, the course number (CSc 2010), and the date.

    Program Submission

    Email your SecretCommands.py file to raj@cs.gsu.edu by 11:59 p.m. on the assigned date.

    Hints

    1. Don't forget to import the urllib module into your program:
      from urllib import *
      
    2. To determine whether a word is a number, write a loop that searches the following list:
      ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
      
      If the word is a number, the position in which the word appears in the list represents its numerical value.
    3. To convert a word to lowercase, use the lower method:
      word = word.lower()
      
    4. To remove trailing punctuation marks from a word, use the rstrip method:
      word = word.rstrip(".,;:?!)")
      
    5. Use split (Chapter 5) to convert the contents of the web page into a list of words.

    IMPORTANT: Please include "CSc 2010 Bulls.py" and "CSc 2010 RandomMoves.py" in the SUBJECT of your email.