CSC111 Lab 6 Solution Programs

From dftwiki3
Jump to: navigation, search

Program 1

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    for i in range( 0, N, 2 ):
        x = border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    drawRow( win, H, W, N, border, side, 0 )
    drawRow( win, H, W, N, border, side, 1 )

def main():
    N = 8
    border = 20
    W = H = 200
    side  = ( W - 2*border ) / N

    win = GraphWin( "board" )
    
    drawBoard( win, H, W, N, border, side )
    win.getMouse()
    win.close()

Program 2

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    drawRow( win, H, W, N, border, side, 0 )
    drawRow( win, H, W, N, border, side, 1 )

def main():
    N = 8
    border = 20
    W = H = 200
    side  = ( W - 2*border ) / N

    win = GraphWin( "board" )
    
    drawBoard( win, H, W, N, border, side )
    win.getMouse()
    win.close()


main()

Program 3

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row )

def main():
    N = 8
    border = 20
    W = H = 200
    side  = ( W - 2*border ) / N

    win = GraphWin( "board" )
    
    drawBoard( win, H, W, N, border, side )
    win.getMouse()
    win.close()


main()

Program 4

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row )

def main():
    N = 12
    border = 20
    W = H = 200
    side  = ( W - 2*border ) / N

    win = GraphWin( "board" )
    
    drawBoard( win, H, W, N, border, side )
    win.getMouse()
    win.close()


main()

Program 5

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row )

def main():
    N = 12
    border = 20
    side   = 20
    W = H = border * 2 + N * side

    win = GraphWin( "board", W, H )
    
    drawBoard( win, H, W, N, border, side )
    win.getMouse()
    win.close()


main()

Program 6

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row )

def drawQueens( win, H, W, N, border, side, solution ):
    row = solution[0][0]
    col = solution[0][1]
    centerX = border + col * side + side/2
    centerY = border + row * side + side/2
    c = Circle( Point( centerX, centerY ), side/4 )
    c.setFill( "red" )
    c.draw( win )

def main():
    N = 8
    border = 20
    side   = 20
    W = H = border * 2 + N * side

    win = GraphWin( "board", W, H )
    
    #--- draw the board ---
    drawBoard( win, H, W, N, border, side )

    #--- position 8 queens ---
    solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
    drawQueens( win, H, W, N, border, side, solution )

    win.getMouse()
    win.close()


main()

Program 7

from graphics import *

def drawRow( win, H, W, N, border, side, row ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( "black" )
        r.draw( win )

def     drawBoard( win, H, W, N, border, side ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row )

def drawQueens( win, H, W, N, border, side, solution ):
    for i in range( N ):
        row = solution[i][0]
        col = solution[i][1]
        centerX = border + col * side + side/2
        centerY = border + row * side + side/2
        c = Circle( Point( centerX, centerY ), side/4 )
        c.setFill( "red" )
        c.draw( win )

def main():
    N = 8
    border = 20
    side   = 20
    W = H = border * 2 + N * side

    win = GraphWin( "board", W, H )
    
    #--- draw the board ---
    drawBoard( win, H, W, N, border, side )

    #--- position 8 queens ---
    solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
    drawQueens( win, H, W, N, border, side, solution )

    win.getMouse()
    win.close()


main()

Program 8

from graphics import *

def drawRow( win, H, W, N, border, side, row, color ):
    offset = (row % 2) * side
    for i in range( 0, N, 2 ):
        x = offset + border + i * side
        y = border + row * side
        x1 = x + side
        y1 = y + side
        r = Rectangle( Point( x, y ),
                       Point( x1, y1 ) )
        r.setFill( color )
        r.draw( win )

def drawBoard( win, H, W, N, border, side, color ):
    # draw border
    r = Rectangle( Point( border, border ),
                   Point( W-border, H-border ) )
    r.draw( win )

    # draw small squares
    for row in range( N ):
        drawRow( win, H, W, N, border, side, row, color )

def drawQueens( win, H, W, N, border, side, solution, queenColor ):
    for i in range( N ):
        row = solution[i][0]
        col = solution[i][1]
        centerX = border + col * side + side/2
        centerY = border + row * side + side/2
        c = Circle( Point( centerX, centerY ), side/4 )
        c.setFill( queenColor )
        c.draw( win )

def main():
    N = 8
    border = 20
    side   = 20
    W = H = border * 2 + N * side

    darkColor = raw_input( "What color do you want for the dark squares? " )
    queenColor = raw_input( "what color should the queens be? " )

    win = GraphWin( "board", W, H )
    
    #--- draw the board ---
    drawBoard( win, H, W, N, border, side, darkColor )

    #--- position 8 queens ---
    solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
    drawQueens( win, H, W, N, border, side, solution, queenColor )

    win.getMouse()
    win.close()


main()