Difference between revisions of "CSC111 Homework 6 2018"

From dftwiki3
Jump to: navigation, search
 
(8 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 10/29/2015 at 11:55 p.m.  The autograder will be available later during the week, and its availability will be announced on Piazza.  Please stay tuned!
+
This homework is due on Thursday 3/30/2018 at 11:55 p.m.   
 
</bluebox>
 
</bluebox>
 
<br />
 
<br />
Line 33: Line 33:
 
<font color="magenta">Make sure your program outputs a blank line before the number.</font>
 
<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.
+
Submit your program to Moodle, Section HW 6 PB 1.   
 
<br />
 
<br />
 
==Example of Interaction With the User==
 
==Example of Interaction With the User==
Line 40: Line 40:
 
   
 
   
 
  RR
 
  RR
RS
+
  RS
 
  RS
 
  RS
 
  RS
 
  RS
Line 59: Line 59:
 
* 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.
 
* 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=
 
<br />
 
<br />
Same setup as Problem 1.  Your program should be called '''hw6_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.  
+
Same setup as Problem 1.  Your program should be called '''hw6_2.py''' and should contain a main() function.  This time your program will stop processing the information in a text file, and as soon as the difference in points between Player1 and Player2 is 3.  
 
For example, assume the file contains:
 
For example, assume the file contains:
 
<br />
 
<br />
 
::<source lang="text">
 
::<source lang="text">
 +
RS
 
RS
 
RS
RS
+
  RS
RS
 
SR
 
SR
 
 
SR
 
SR
 +
SR
 
SR
 
SR
 
SR
 
SR
 
SR
 
SR
 
SR
 
SR
 +
  SR
 
SR
 
SR
 
SS
 
SS
Line 85: Line 86:
 
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 7 PB 2. You will be able to evaluate your program.
+
Submit your program to Moodle, Section HW 6 PB 2.  
 
<br />
 
<br />
 +
 
=Problem #3=
 
=Problem #3=
 
<br />
 
<br />
Line 117: Line 119:
 
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 7 PB 3 section.   
+
Submit your program on Moodle in the HW 6 PB 3 section.   
 
<br />
 
<br />
 
==Example of User Interaction==
 
==Example of User Interaction==
Line 178: Line 180:
 
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 7 PB 4 section.   <font color="magenta">You will not be able to evaluate this program</font>.  Make sure you test it thoroughly.   
+
Submit your program on Moodle, in the HW 6 PB 4 section. Make sure you test your program thoroughly.   
  
 
Here are special cases your program should be able to handle.
 
Here are special cases your program should be able to handle.
Line 209: Line 211:
 
<!-- =============================================================== -->
 
<!-- =============================================================== -->
 
<br />
 
<br />
<showafterdate after="20180330 08:00" before="20180601 00:00">
+
</onlydft>
 +
<showafterdate after="20180402 23:55" before="20180601 00:00">
 
=Solution Programs=
 
=Solution Programs=
 
<br />
 
<br />
Line 326: Line 329:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# hw7_2.py Rock Paper Scissors
+
# hw6_2.py Rock Paper Scissors
 
# D. Thiebaut
 
# D. Thiebaut
 
# Solution program for Homework #8 Problem 2.
 
# Solution program for Homework #8 Problem 2.
Line 335: Line 338:
 
# The program computes the winner who is the player to get 3 points
 
# 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  
 
# over the score of the other one.  The plays are taken from the same  
# text file as hw7_1.py.
+
# text file as hw6_1.py.
 
# The program outputs 1 if Player 1 (the first letter) wins, 2 if it's Player 2 (the
 
# 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).
 
# second letter, or 0 in case of a tie (but that shouldn't happen).
Line 444: Line 447:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# hw7_3.py Rock Paper Scissors
+
# hw6_3.py Rock Paper Scissors
 
# D. Thiebaut
 
# D. Thiebaut
 
# Solution program for Homework #8 Problem 3.
 
# Solution program for Homework #8 Problem 3.
Line 576: Line 579:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
# hw7_4.py Rock Paper Scissors
+
# hw6_4.py Rock Paper Scissors
 
# D. Thiebaut
 
# D. Thiebaut
 
# Solution program for Homework #8 Problem 3.
 
# Solution program for Homework #8 Problem 3.
Line 707: Line 710:
 
<br />
 
<br />
 
::<source lang="python">
 
::<source lang="python">
#hw7_5.py
+
#hw6_5.py
 
from graphics import *
 
from graphics import *
 
from random import *
 
from random import *
  
 
def main():
 
def main():
     win = GraphWin( "CSC111 Homework 7", 600, 600 )
+
     win = GraphWin( "CSC111 Homework 6", 600, 600 )
  
 
     for x in range( 0, 600, 25 ):
 
     for x in range( 0, 600, 25 ):
Line 727: Line 730:
 
     r.draw( win )
 
     r.draw( win )
 
      
 
      
     t = Text( Point(300,300), "CSC111 Homework 7" )
+
     t = Text( Point(300,300), "CSC111 Homework 6" )
 
     t.draw( win )
 
     t.draw( win )
 
      
 
      

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>