Difference between revisions of "CSC111 Homework 7 2015b"
(11 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<showafterdate after="20151022 17:00" before="20151231 00:00"> | <showafterdate after="20151022 17:00" before="20151231 00:00"> | ||
<bluebox> | <bluebox> | ||
− | This homework is due on Thursday | + | This homework is due on Thursday 3/29/2015 at 11:55 p.m. You can work '''individually''' on this homework assignment. |
</bluebox> | </bluebox> | ||
− | + | <br /> | |
+ | =Video Introduction= | ||
+ | <br /> | ||
+ | <center><videoflash>1uA-pLITer0</videoflash></center> | ||
<br /> | <br /> | ||
=Problem #1= | =Problem #1= | ||
<br /> | <br /> | ||
− | Write a program called hw7_1.py that prompts the user for the name of a text file containing the plays selected by two users playing a game of Rock-Scissors-Paper. | + | [[Image:RockPaperScissors.png| right|150px]] |
+ | Write a program called hw7_1.py that prompts the user for the name of a text file containing the plays selected by two users playing a game of [https://en.wikipedia.org/wiki/Rock-paper-scissors Rock-Scissors-Paper]. If Player 1 picks "Scissors" and Player 2 picks "Rock", then one line of the file will simply contains "PR." If the 2 players play Rock-Paper, Rock-Paper, Paper-Scissors, then the file will contain 3 lines: | ||
+ | |||
+ | RP | ||
+ | RP | ||
+ | PS | ||
+ | |||
− | |||
<br /> | <br /> | ||
− | + | Each line will contain 2 letters, without any spaces between them. In this problem, there are only 2 letters per line. Each letter is guaranteed to be R, P, S, r, p, or s. Nothing else will be found in the file. There might be space before or after the 2 letters, though, and the letters can be upper- or lower-case. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | Each line | ||
<br /> | <br /> | ||
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. | 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. | ||
Line 36: | Line 31: | ||
since Player 1 has won. | since Player 1 has won. | ||
<br /> | <br /> | ||
− | <font color="magenta">Make sure | + | <font color="magenta">Make sure your program outputs a blank line before the number.</font> |
<br /> | <br /> | ||
− | + | Submit your program to Moodle, Section HW 7 PB 1. You will be able to evaluate this program yourself. | |
− | + | <br /> | |
− | + | ==Example of Interaction With the User== | |
− | |||
<br /> | <br /> | ||
− | + | * Assume that there is a file called '''data1.txt''' in the same directory where the program resides, and the file contains the following lines: | |
+ | |||
+ | RR | ||
+ | RS | ||
+ | RS | ||
+ | RS | ||
+ | RS | ||
+ | |||
+ | * Here's the exchange in the console when the user starts her program: | ||
+ | |||
+ | Python 3.5.0b1 (v3.5.0b1:071fefbb5e3d, May 23 2015, 18:22:54) | ||
+ | [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin | ||
+ | Type "copyright", "credits" or "license()" for more information. | ||
+ | >>> ================================ RESTART ================================ | ||
+ | >>> | ||
+ | File name? data1.txt | ||
+ | |||
+ | 1 | ||
+ | >>> | ||
+ | |||
+ | * Note the blank line between the input of the name of the file, and the output of the winner. Your program must provide this blank line. This will be true of the other programs in this assignment, except for the graphics program at the end. | ||
<br /> | <br /> | ||
=Problem 2= | =Problem 2= | ||
Line 71: | Line 85: | ||
Once again, make sure your program only outputs one of three possible numbers: 1, 2, or 0. | Once again, make sure your program only outputs one of three possible numbers: 1, 2, or 0. | ||
<br /> | <br /> | ||
− | Submit your program to Moodle, Section HW | + | Submit your program to Moodle, Section HW 7 PB 2. You will be able to evaluate your program. |
<br /> | <br /> | ||
− | =Problem 3= | + | =Problem #3= |
<br /> | <br /> | ||
Your program will be called '''hw7_3.py''' and should contain a main() function. | Your program will be called '''hw7_3.py''' and should contain a main() function. | ||
Line 103: | Line 117: | ||
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. | 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. | ||
<br /> | <br /> | ||
− | Submit your program on Moodle in the HW | + | Submit your program on Moodle in the HW 7 PB 3 section. |
<br /> | <br /> | ||
− | =Problem 4= | + | ==Example of User Interaction== |
+ | <br /> | ||
+ | * Imagine that we have stored the following information in a file called '''data1.txt''': | ||
+ | |||
+ | RR | ||
+ | RS | ||
+ | RS | ||
+ | 123 | ||
+ | r sss | ||
+ | sr | ||
+ | sr | ||
+ | Sr | ||
+ | ss | ||
+ | RS | ||
+ | RS | ||
+ | RS | ||
+ | RS | ||
+ | |||
+ | * Here's the behavior of the program when the user runs it: | ||
+ | |||
+ | Python 3.5.0b1 (v3.5.0b1:071fefbb5e3d, May 23 2015, 18:22:54) | ||
+ | [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin | ||
+ | Type "copyright", "credits" or "license()" for more information. | ||
+ | >>> ================================ RESTART ================================ | ||
+ | >>> | ||
+ | File name? data1.txt | ||
+ | |||
+ | 1 | ||
+ | >>> | ||
+ | |||
+ | <br /> | ||
+ | =Problem #4= | ||
<br /> | <br /> | ||
Write a program called '''hw7_4.py''', that contains a main() function. It should be similar to the program for Problem 3, but you need to modify the rules of the game to include a new "figure": the '''Hole'''. The hole is made by putting one's hand in the shape of a cylinder. | Write a program called '''hw7_4.py''', that contains a main() function. It should be similar to the program for Problem 3, but you need to modify the rules of the game to include a new "figure": the '''Hole'''. The hole is made by putting one's hand in the shape of a cylinder. | ||
Line 133: | Line 178: | ||
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. | 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. | ||
<br /> | <br /> | ||
− | Submit your program on Moodle, in the HW | + | Submit your program on Moodle, in the HW 7 PB 4 section. <font color="magenta">You will not be able to evaluate this program</font>. Make sure you test it thoroughly. |
Here are special cases your program should be able to handle. | Here are special cases your program should be able to handle. | ||
Line 149: | Line 194: | ||
* Your name should appear in a white box centered in the middle of the window. | * Your name should appear in a white box centered in the middle of the window. | ||
* Store your program in a file called hw7_5.py and submit it in the HW7 PB 5 PROGRAM section. You won't be able to evaluate this program, as Moodle can't automatically grade graphics program. Your program will be graded by hand. | * Store your program in a file called hw7_5.py and submit it in the HW7 PB 5 PROGRAM section. You won't be able to evaluate this program, as Moodle can't automatically grade graphics program. Your program will be graded by hand. | ||
− | * Submit a screen capture of the window generated by your program. Submit it in the HW7 PB5 IMAGE section. | + | * Submit a screen capture of the window generated by your program. Submit it in the HW7 PB5 IMAGE section. <font color="magenta">Note: Programs submitted without a screen capture will automatically be given a grade of 0.</font> |
* ''Hints'': you may want to play with this simple program before you start: | * ''Hints'': you may want to play with this simple program before you start: | ||
<br /> | <br /> | ||
Line 157: | Line 202: | ||
print( i, j ) | print( i, j ) | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <center><videoflash>bail3ir5WwI</videoflash></center> | ||
<br /> | <br /> | ||
</showafterdate> | </showafterdate> | ||
Line 162: | Line 209: | ||
<!-- =============================================================== --> | <!-- =============================================================== --> | ||
<br /> | <br /> | ||
− | <showafterdate after=" | + | <showafterdate after="20151030 00:00" before="20151231 00:00"> |
=Solution Programs= | =Solution Programs= | ||
<br /> | <br /> | ||
Line 272: | Line 319: | ||
print( 0 ) | print( 0 ) | ||
− | + | main() | |
− | |||
</source> | </source> | ||
Line 391: | Line 437: | ||
print( 0 ) | print( 0 ) | ||
− | + | main() | |
− | |||
</source> | </source> | ||
Line 524: | Line 569: | ||
print( 0 ) | print( 0 ) | ||
− | + | main() | |
− | |||
</source> | </source> | ||
Line 656: | Line 700: | ||
print( 0, count ) | print( 0, count ) | ||
− | + | main() | |
− | |||
</source> | </source> | ||
<br /> | <br /> | ||
+ | =Problem #5= | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | #hw7_5.py | ||
+ | from graphics import * | ||
+ | from random import * | ||
+ | |||
+ | def main(): | ||
+ | win = GraphWin( "CSC111 Homework 7", 600, 600 ) | ||
+ | |||
+ | for x in range( 0, 600, 25 ): | ||
+ | for y in range( 0, 600, 25 ): | ||
+ | r = Rectangle( Point( x, y ), Point( x+25, y+25 ) ) | ||
+ | red = randint( 0, 255 ) | ||
+ | green = randint( 0, 255 ) | ||
+ | blue = (red+green)//2 | ||
+ | r.setFill( color_rgb( red, green, blue ) ) | ||
+ | r.draw( win ) | ||
+ | |||
+ | r = Rectangle( Point(100,250), Point( 600-100, 600-250 ) ) | ||
+ | r.setFill( "white" ) | ||
+ | r.draw( win ) | ||
+ | |||
+ | t = Text( Point(300,300), "CSC111 Homework 7" ) | ||
+ | t.draw( win ) | ||
+ | |||
+ | win.getMouse() | ||
+ | win.close() | ||
+ | main() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
</showafterdate> | </showafterdate> | ||
<br /> | <br /> |
Latest revision as of 06:26, 20 March 2018
--D. Thiebaut (talk) 22:07, 20 October 2015 (EDT)
<showafterdate after="20151022 17:00" before="20151231 00:00">
This homework is due on Thursday 3/29/2015 at 11:55 p.m. You can work individually on this homework assignment.
Contents
Video Introduction
Problem #1
Write a program called hw7_1.py that prompts the user for the name of a text file containing the plays selected by two users playing a game of Rock-Scissors-Paper. If Player 1 picks "Scissors" and Player 2 picks "Rock", then one line of the file will simply contains "PR." If the 2 players play Rock-Paper, Rock-Paper, Paper-Scissors, then the file will contain 3 lines:
RP RP PS
Each line will contain 2 letters, without any spaces between them. In this problem, there are only 2 letters per line. Each letter is guaranteed to be R, P, S, r, p, or s. Nothing else will be found in the file. There might be space before or after the 2 letters, though, and the letters can be upper- or lower-case.
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.
Make sure your program outputs a blank line before the number.
Submit your program to Moodle, Section HW 7 PB 1. You will be able to evaluate this program yourself.
Example of Interaction With the User
- Assume that there is a file called data1.txt in the same directory where the program resides, and the file contains the following lines:
RR RS RS RS RS
- Here's the exchange in the console when the user starts her program:
Python 3.5.0b1 (v3.5.0b1:071fefbb5e3d, May 23 2015, 18:22:54) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> File name? data1.txt 1 >>>
- Note the blank line between the input of the name of the file, and the output of the winner. Your program must provide this blank line. This will be true of the other programs in this assignment, except for the graphics program at the end.
Problem 2
Same setup as Problem 1. Your program should be called hw7_2.py and should contain a main() function. This time your program will stop processing the information in a text file (which is 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 can 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 7 PB 2. You will be able to evaluate your program.
Problem #3
Your program will be called hw7_3.py and should contain a main() function.
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 7 PB 3 section.
Example of User Interaction
- Imagine that we have stored the following information in a file called data1.txt:
RR RS RS 123 r sss sr sr Sr ss RS RS RS RS
- Here's the behavior of the program when the user runs it:
Python 3.5.0b1 (v3.5.0b1:071fefbb5e3d, May 23 2015, 18:22:54) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> File name? data1.txt 1 >>>
Problem #4
Write a program called hw7_4.py, that contains a main() function. It should be similar to the program for Problem 3, but you need to modify the rules of the game to include a new "figure": 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 follows:
- 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 contain text lines, some valid, some invalid, where the valid lines contain only 2 characters, upper or lower case, each either 'R', 'P', 'S', or 'H'.
Make your program stop when
- the difference in points between Player 1 and Player 2 is 3, or
- 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 7 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.
- the text file is empty or contains only invalid lines. In this case the output should be 0 0
- 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.
- the text file contains 10000 lines of the form RR. The output should be 0 10000.
Problem 5
- Write a graphics program that produces a 600x600 window similar to this one.
- The squares should be 25x25 pixels in width and height. Their color should be random.
- Your name should appear in a white box centered in the middle of the window.
- Store your program in a file called hw7_5.py and submit it in the HW7 PB 5 PROGRAM section. You won't be able to evaluate this program, as Moodle can't automatically grade graphics program. Your program will be graded by hand.
- Submit a screen capture of the window generated by your program. Submit it in the HW7 PB5 IMAGE section. Note: Programs submitted without a screen capture will automatically be given a grade of 0.
- Hints: you may want to play with this simple program before you start:
for i in range( 0, 5 ): for j in range( 0, 5 ): print( i, j )
</showafterdate>
<showafterdate after="20151030 00:00" before="20151231 00:00">
Solution Programs
Problem 1
# hw7_1.py Rock Paper Scissors
# D. Thiebaut
# Solution program for Homework #8
# This program reads plays between two players from a text file.
# Each line in the text file contains 2 letters that are either R, P, or S.
# The first letter indicates the play by the first player, and the second
# letter the play by the second player.
# The program computes the winner of all the plays stored in the text
# file and outputs 1 if it's Player 1 (the first letter), 2 if it's Player 2 (the
# second letter, or 0 in case of a tie.
#
PLAYER1WINS= 1
PLAYER2WINS= 2
TIE = 0
FILENAME = "plays.txt"
# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
# playRound: plays a round between PLAYER1 and PLAYER2.
# returns the winner of the round (PLAYER1 or PLAYER2)
def playRound( player1, player2 ):
if player1==player2:
#print( "It's a tie!" )
return TIE
if ( player1=="P" and player2=="S" ) \
or ( player1=="R" and player2=="P" ) \
or ( player1=="S" and player2=="R" ):
#print( "I win this round!" )
return PLAYER2
#print( "You win this round!" )
return PLAYER1
# playRound: plays a round between PLAYER2 and PLAYER1. PLAYER1
# picks a letter, PLAYER2 picks a letter at random.
# displays outcome of the game. Returns winner
def playRound( p1, p2 ):
# checks for ties
if p1 == p2:
return TIE
# look at conditions where PLAYER2 wins
if (p1=="P" and p2=="S") \
or (p1=="R" and p2=="P") \
or (p1=="S" and p2=="R"):
return PLAYER2WINS
# PLAYER1 must have won
return PLAYER1WINS
# readFile: reads the contents of the file and returns it
# as a list of lines.
def readFile():
list = []
file = open( FILENAME, "r" )
lines = file.readlines()
file.close()
# remove the "\n" at the end of each line
# and make sure all characters are upper case
plays = []
for line in lines:
plays.append( line.strip().upper() )
return plays
# main: plays several rounds of the rock-paper-scissors game
# until one player gets 3 more points than the other. Then
# displays the winner.
def main():
# counters for winning rounds
player1Count = 0
player2Count = 0
# read the text file with all the plays
plays = readFile()
for play in plays:
player1 = play[0]
player2 = play[1]
winner = playRound( player1, player2 )
if winner == PLAYER1WINS:
player1Count += 1
if winner == PLAYER2WINS:
player2Count += 1
# print 1 if player1 wins, 2 if player2 wins,
# or 0 if tie
if player1Count > player2Count:
print( 1 )
elif player1Count < player2Count:
print( 2 )
else:
print( 0 )
main()
Problem 2
# hw7_2.py Rock Paper Scissors
# D. Thiebaut
# Solution program for Homework #8 Problem 2.
# This program reads plays between two players from a text file.
# Each line in the text file contains 2 letters that are either R, P, or S.
# The first letter indicates the play by the first player, and the second
# letter the play by the second player.
# The program computes the winner who is the player to get 3 points
# over the score of the other one. The plays are taken from the same
# text file as hw7_1.py.
# The program outputs 1 if Player 1 (the first letter) wins, 2 if it's Player 2 (the
# second letter, or 0 in case of a tie (but that shouldn't happen).
#
PLAYER1WINS= 1
PLAYER2WINS= 2
TIE = 0
FILENAME = "plays.txt"
# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
# playRound: plays a round between PLAYER1 and PLAYER2.
# returns the winner of the round (PLAYER1 or PLAYER2)
def playRound( player1, player2 ):
if player1==player2:
#print( "It's a tie!" )
return TIE
if ( player1=="P" and player2=="S" ) \
or ( player1=="R" and player2=="P" ) \
or ( player1=="S" and player2=="R" ):
#print( "I win this round!" )
return PLAYER2
#print( "You win this round!" )
return PLAYER1
# playRound: plays a round between PLAYER2 and PLAYER1. PLAYER1
# picks a letter, PLAYER2 picks a letter at random.
# displays outcome of the game. Returns winner
def playRound( p1, p2 ):
# checks for ties
if p1 == p2:
return TIE
# look at conditions where PLAYER2 wins
if (p1=="P" and p2=="S") \
or (p1=="R" and p2=="P") \
or (p1=="S" and p2=="R"):
return PLAYER2WINS
# PLAYER1 must have won
return PLAYER1WINS
# readFile: reads the contents of the file and returns it
# as a list of lines.
def readFile():
list = []
file = open( FILENAME, "r" )
lines = file.readlines()
file.close()
# remove the "\n" at the end of each line
# and make sure all characters are upper case
plays = []
for line in lines:
plays.append( line.strip().upper() )
return plays
# main: plays several rounds of the rock-paper-scissors game
# until one player gets 3 more points than the other. Then
# displays the winner.
def main():
# counters for winning rounds
player1Count = 0
player2Count = 0
# read the text file with all the plays
plays = readFile()
for play in plays:
player1 = play[0]
player2 = play[1]
winner = playRound( player1, player2 )
if winner == PLAYER1WINS:
player1Count += 1
if winner == PLAYER2WINS:
player2Count += 1
# stop if one player gets 3 points over the other's score.
if abs( player1Count - player2Count ) >= 3:
break
# print 1 if player1 wins, 2 if player2 wins,
# or 0 if tie
if player1Count > player2Count:
print( 1 )
elif player1Count < player2Count:
print( 2 )
else:
print( 0 )
main()
Problem 3
# hw7_3.py Rock Paper Scissors
# D. Thiebaut
# Solution program for Homework #8 Problem 3.
# This program reads plays between two players from a text file which
# may contain invalid lines.
# Each line in the text file contains 2 letters that are either R, P, or S.
# The first letter indicates the play by the first player, and the second
# letter the play by the second player.
# The program computes the winner of all the plays stored in the text
# file and outputs 1 if it's Player 1 (the first letter), 2 if it's Player 2 (the
# second letter, or 0 in case of a tie.
#
PLAYER1WINS= 1
PLAYER2WINS= 2
TIE = 0
FILENAME = "plays.txt"
# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
# playRound: plays a round between PLAYER1 and PLAYER2.
# returns the winner of the round (PLAYER1 or PLAYER2)
def playRound( player1, player2 ):
if player1==player2:
#print( "It's a tie!" )
return TIE
if ( player1=="P" and player2=="S" ) \
or ( player1=="R" and player2=="P" ) \
or ( player1=="S" and player2=="R" ):
#print( "I win this round!" )
return PLAYER2
#print( "You win this round!" )
return PLAYER1
# playRound: plays a round between PLAYER2 and PLAYER1. PLAYER1
# picks a letter, PLAYER2 picks a letter at random.
# displays outcome of the game. Returns winner
def playRound( p1, p2 ):
# checks for ties
if p1 == p2:
return TIE
# look at conditions where PLAYER2 wins
if (p1=="P" and p2=="S") \
or (p1=="R" and p2=="P") \
or (p1=="S" and p2=="R"):
return PLAYER2WINS
# PLAYER1 must have won
return PLAYER1WINS
# readFile: reads the contents of the file and returns it
# as a list of lines.
def readFile():
list = []
file = open( FILENAME, "r" )
lines = file.readlines()
file.close()
# remove the "\n" at the end of each line
# and make sure all characters are upper case
plays = []
for line in lines:
# strip the line and make it uppercase
line = line.strip().upper()
# reject lines that do not have 2 characters
if len( line ) != 2:
continue
# test both characters and reject if not R, P, or S.
for i in range( 2 ):
if not line[i] in ["R", "P", "S" ]:
continue
# otherwise the line is good
plays.append( line )
# return the good lines
return plays
# main: plays several rounds of the rock-paper-scissors game
# until one player gets 3 more points than the other. Then
# displays the winner.
def main():
# counters for winning rounds
player1Count = 0
player2Count = 0
# read the text file with all the plays
plays = readFile()
for play in plays:
player1 = play[0]
player2 = play[1]
winner = playRound( player1, player2 )
if winner == PLAYER1WINS:
player1Count += 1
if winner == PLAYER2WINS:
player2Count += 1
# stop if one player gets 3 points over the other's score.
if abs( player1Count - player2Count ) >= 3:
break
# print 1 if player1 wins, 2 if player2 wins,
# or 0 if tie
if player1Count > player2Count:
print( 1 )
elif player1Count < player2Count:
print( 2 )
else:
print( 0 )
main()
Problem 4
# hw7_4.py Rock Paper Scissors
# D. Thiebaut
# Solution program for Homework #8 Problem 3.
# This program reads plays between two players from a text file which
# may contain invalid lines.
# Each line in the text file contains 2 letters that are either R, P, H, or S.
# The first letter indicates the play by the first player, and the second
# letter the play by the second player.
# The rules are the rules of Rock-Paper-Scissors, augmented with the
# following conditions:
#
# 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
#
# The program computes the winner of all the plays stored in the text
# file and outputs 1 if it's Player 1 (the first letter), 2 if it's Player 2 (the
# second letter, or 0 in case of a tie.
#
PLAYER1WINS= 1
PLAYER2WINS= 2
TIE = 0
FILENAME = "plays.txt"
# pickALetter: prompts the user for a letter, and doe not return
# until it is valid.
# playRound: plays a round between PLAYER1 and PLAYER2.
# returns the winner of the round (PLAYER1 or PLAYER2)
def playRound( player1, player2 ):
if player1==player2:
#print( "It's a tie!" )
return TIE
if ( player1=="P" and player2=="S" ) \
or ( player1=="R" and player2=="P" ) \
or ( player1=="S" and player2=="R" ) \
or ( player1=="R" and player2=="H" ) \
or ( player1=="S" and player2=="H" ):
#print( "I win this round!" )
return PLAYER2WINS
#print( "You win this round!" )
return PLAYER1WINS
# readFile: reads the contents of the file and returns it
# as a list of lines.
def readFile():
list = []
file = open( FILENAME, "r" )
lines = file.readlines()
file.close()
# remove the "\n" at the end of each line
# and make sure all characters are upper case
plays = []
for line in lines:
# strip the line and make it uppercase
line = line.strip().upper()
# reject lines that do not have 2 characters
if len( line ) != 2:
continue
# test both characters and reject if not R, P, or S.
for i in range( 2 ):
if not line[i] in ["R", "P", "S", "H" ]:
continue
# otherwise the line is good
plays.append( line )
# return the good lines
return plays
# main: plays several rounds of the rock-paper-scissors game
# until one player gets 3 more points than the other. Then
# displays the winner.
def main():
# counters for winning rounds
player1Count = 0
player2Count = 0
# read the text file with all the plays
plays = readFile()
#print( plays )
# keep track of number of plays
count = 0
for play in plays:
count += 1
player1 = play[0]
player2 = play[1]
winner = playRound( player1, player2 )
if winner == PLAYER1WINS:
player1Count += 1
if winner == PLAYER2WINS:
player2Count += 1
#print( player1, player2, player1Count, player2Count, count )
# stop if one player gets 3 points over the other's score.
if abs( player1Count - player2Count ) >= 3:
break
# print 1 if player1 wins, 2 if player2 wins,
# or 0 if tie
if player1Count > player2Count:
print( 1, count )
elif player1Count < player2Count:
print( 2, count )
else:
print( 0, count )
main()
Problem #5
#hw7_5.py from graphics import * from random import * def main(): win = GraphWin( "CSC111 Homework 7", 600, 600 ) for x in range( 0, 600, 25 ): for y in range( 0, 600, 25 ): r = Rectangle( Point( x, y ), Point( x+25, y+25 ) ) red = randint( 0, 255 ) green = randint( 0, 255 ) blue = (red+green)//2 r.setFill( color_rgb( red, green, blue ) ) r.draw( win ) r = Rectangle( Point(100,250), Point( 600-100, 600-250 ) ) r.setFill( "white" ) r.draw( win ) t = Text( Point(300,300), "CSC111 Homework 7" ) t.draw( win ) win.getMouse() win.close() main()
</showafterdate>