Difference between revisions of "CSC111 Homework 6 2018"
(→Problem #1) |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 07:29, 20 March 2018 (EDT) | [[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 07:29, 20 March 2018 (EDT) | ||
---- | ---- | ||
− | + | <onlydft> | |
<bluebox> | <bluebox> | ||
− | This homework is due on Thursday | + | This homework is due on Thursday 3/30/2018 at 11:55 p.m. |
</bluebox> | </bluebox> | ||
<br /> | <br /> | ||
Line 66: | Line 66: | ||
<br /> | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
+ | RS | ||
RS | RS | ||
− | RS | + | RS |
− | |||
− | |||
− | |||
SR | SR | ||
+ | SR | ||
SR | SR | ||
SR | SR | ||
SR | SR | ||
SR | SR | ||
+ | SR | ||
SR | SR | ||
SS | SS | ||
Line 211: | Line 211: | ||
<!-- =============================================================== --> | <!-- =============================================================== --> | ||
<br /> | <br /> | ||
− | <showafterdate after=" | + | </onlydft> |
+ | <showafterdate after="20180402 23:55" before="20180601 00:00"> | ||
=Solution Programs= | =Solution Programs= | ||
<br /> | <br /> |
Latest revision as of 12:41, 1 June 2018
D. Thiebaut (talk) 07:29, 20 March 2018 (EDT)
<showafterdate after="20180402 23:55" before="20180601 00:00">
Solution Programs
Problem 1
# hw6_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
# hw6_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 hw6_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
# hw6_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
# hw6_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
#hw6_5.py from graphics import * from random import * def main(): win = GraphWin( "CSC111 Homework 6", 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 6" ) t.draw( win ) win.getMouse() win.close() main()
</showafterdate>