Difference between revisions of "CSC111 Draw Chessboard: Rough Solution"

From dftwiki3
Jump to: navigation, search
(Created page with "# top-down example from graphics import * N = 8 def drawRow( r, side, win, color ): for c in range( N ): x = side + side*c y = side + side*r box = R...")
 
Line 1: Line 1:
 +
<source lang="python">
 
# top-down example
 
# top-down example
 
from graphics import *
 
from graphics import *
Line 53: Line 54:
  
 
main()
 
main()
 +
</source>
 +
<br />
 +
[[Category:CSC111]][[Category:Python]]

Revision as of 12:53, 17 November 2011

# top-down example
from graphics import *

N = 8

def drawRow( r, side, win, color ):
    for c in range( N ):
        x = side + side*c
        y = side + side*r
        box = Rectangle( Point( x,y ), Point( x+side, y+side ) )
        box.setFill( color )
        if color=="blue":
            color = "green"
        else:
            color = "blue"
        box.draw( win )

    
def    drawChessBoard( win, w, h ):
    # compute side
    side = min( w, h )/ (N+2)
    
    #draw each row
    for r in range( N ):
        if r%2 == 0:  # even row
            color = "blue"
        else:
            color = "green"
        drawRow( r, side, win, color ) 
        
    return

def    drawSolution( win, solution ):
    return


def main():
    # open graphics window
    w = 600
    h = 400
    win = GraphWin( "chessboard", w, h )
    
    # draw the whole chess board
    drawChessBoard( win, w, h )

    # draw solution
    solution =  [3, 6, 2, 7, 1, 4, 0, 5]
    drawSolution( win, solution )

    #close window
    win.getMouse()
    win.close()

main()