CSC111 Draw Chessboard: Rough Solution

From dftwiki3
Revision as of 11:49, 17 November 2011 by Thiebaut (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  1. 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()