CSC111 Homework 8 2015

From dftwiki3
Revision as of 12:28, 24 March 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 09:30, 24 March 2015 (EDT)


<showafterdate after="20150326 17:00" before="20150606 00:00">

This homework is due on 03/31/2015 at 12:55 p.m..



Problem #1


Write a program called hw8_1.py that reads a text file called "plays.txt" that contains the plays made by two players, Player 1 and Player 2, who are challenging each other with a game of Rock, Paper, Scissors. The name of the file is hard-coded, and is always going to be plays.txt. For this reason, do not make your program ask the user for a file name. Instead, use the method illustrated below to open the file in your program.

# at the top of your program
FILENAME = "plays.txt"   # file containing plays by 2 players

...

     # somewhere in a function:
     file = open( FILENAME, "r" )
     ...


Here is an example of the possible contents of plays.txt:

RR
RS
SS
PS
PR
PP
PR
SS


Each line contains 2 letters, glued to each other. There are no spaces between them. There are only 2 letters per line. Each letter is guaranteed to be R, P, or S. Nothing else will be found in the file. There might be space before or after the 2 letters, though.
Your program will read the file and output only 1 line containing 1 number. This number will either be 1, 2, or 0. 1 will indicate that Player 1 (the first letter on the line) wins, 2 will indicate that Player 2 (the second letter) wins, and 0 will indicate a tie.
In the example above, we have the following: 4 ties, Player 1 wins 3 times, and Player 2 wins once. The output of your program should be

1

since Player 1 has won.
Submit your program to Moodle, Section HW 8 PB 1. You will be able to evaluate this program yourself.

Problem 2


Same setup as Problem 1. Your program should be called hw8_2.py. This time your program will stop reading the information in a text file (also called plays.txt) as soon as the difference in points between Player1 and Player2 is 3. For example, assume the file contains:

RS
RS
RS
SR
SR
SR
SR
SR
SR
SR
SR
SS


Your program will output 1 because the first 3 lines correspond to 3 plays when Player 1 wins. After your program has processed the letters from the 3rd line of the file, the counter associated with Player 1 will be 3 and that of Player 2 will be 0. The difference is 3, and your program will stop, indicating that Player 1 has won. You see that the file contains additional lines that would make Player 2 wins in the end, but we are only interested in the case where the difference between Player 1 and Player 2 reaches 3 points.
You an assume that the file will always contain enough lines to create a difference of 3 points between the two players.
Once again, make sure your program only outputs one of three possible numbers: 1, 2, or 0.
Submit your program to Moodle, Section HW 8 PB 2. You will be able to evaluate your program.

Problem 3


Your program will be called hw8_3.py

It is similar to the solution for Problem 2, but this time the text file may contain invalid lines. An invalid line is a line that does not contain 2 letters only, that are each, either R, P, or S, upper or lower case.

Examples of invalid lines:

RRR
R
S
         
12
R:S
R-S


Examples of valid lines:

RR
RS
rs
SS       
sP


Your program will stop when the difference in points between Player 1 and Player 2 is 3. There will always be enough lines with the right plays in the text file to make the difference in points reach 3.
Submit your program on Moodle in the HW 8 PB 3 section. Evaluation will turned off for this program. (Remember, we're slowly removing your training wheels!)

Problem 4


Write a program called hw8_4.py, and modify the rules of the game so that a new "figure" is added: the Hole. The hole is made by putting one's hand in the shape of a cylinder.
The new rules with the hole are as followed:

  • R H: Hole wins, as Rock falls in Hole
  • H H: Tie
  • S H: Hole wins, as Scissors fall in Hole
  • H P: Paper wins, as paper covers Hole


You will have noticed that the new rules are unfair. Holes wins more often than it loses. That's ok for this game.
The text file, still called plays.txt will now contains text lines, some valid, some invalid, where the valid lines contain only 2 character, upper or lower case, each either 'R', 'P', 'S', or 'H'.
Make your program stop when

  1. the difference in point between Player 1 and Player 2 is 3, or
  2. or when the text file has been exhausted, and there are no more lines to read. In this case, the difference between the two players' points could be 0, 1, 2 or 3.


The output of your program is different from the previous programs. Your program should now output 2 numbers, on the same line, separated by a space. The first number is 1 if Player 1 wins, 2 if Player 2 wins, and 0 if it's a tie. The second number is the number of plays they had to go through to get a difference of 3 points. For example:

 1 10

would indicate that Player 1 wins after 10 rounds.

 0 1000

would indicate that the players played for 1000 rounds, and finished with a tie. Because they finish with a difference in point that is less than 3, 1000 must be the number of valid lines in the text file.
Submit your program on Moodle, in the HW 8 PB 4 section. You will not be able to evaluate this program. Make sure you test it thoroughly.

Here are special cases your program should be able to handle.

  1. the text file is empty or contains only invalid lines. In this case the output should be 0 0.
  2. the text file contains 3 valid lines, and Player 1 (conversely Player 2) wins on all three lines. The output in this case should be 1 3.
  3. the text file contains 10000 lines of the form RR. The output should be 0 10000.


</showafterdate>