Difference between revisions of "CSC111 drawChessBoard.py"

From dftwiki3
Jump to: navigation, search
(Created page with '<source lang="python"> . # chess.py # D. Thiebaut # Draws a board with 8 queens on it, in such a way # that none of them can take each other. # The text exquivalent is shown bel…')
 
 
Line 20: Line 20:
 
from graphics import *
 
from graphics import *
  
def drawOneRow( win, xOffset, yOffset ):
+
def drawBoard( win, N, side, border ):
     """draws a row of squares, alternating with the first one
+
     """draw the board, including border and black and white
     being black, then white, etc."""
+
    squares.  N is the number of squares on the side.  side
     for x in range( xOffset, xOffset+ 8*20, 40 ):
+
     is the width of a square. border is the gutter around the
        coloredR = Rectangle( Point( x, yOffset ), Point( x+20, yOffset+20) )  
+
    squares"""
        coloredR.setFill( "black" )
+
     #--- draw the border first ---
        coloredR.draw( win )
+
    r = Rectangle( Point( border, border ),
       
+
                  Point( border+N*side, border+N*side ) )
def drawBoard( win ):
+
    r.draw( win )
    """ draw the whole board of colored squares"""
+
 
     #--- draw bigRectangle ---
+
     #--- then all the black squares only ---
     topLeft = Point( 20, 20 )
+
     for row in range( N ):
    botRight= Point( 180, 180 )
+
        drawRow( win, N, side, border, row )
    bigR = Rectangle( topLeft, botRight )
+
 
     bigR.draw( win )
+
def drawRow( win, N, side, border, row ):
 +
    """draws one row of black squares on the board.  if row is 0
 +
     the first top row is drawn."""
  
     #--- draw first line ---
+
     #--- offset every even row by 1 square width ---
     x = 20
+
     offset = (row % 2) * side
    for y in range( 20, 8*20+1, 20 ):
 
        drawOneRow( win, x, y )
 
        x = 60 - x
 
  
def drawQueens( win, solution ):
+
    #--- place half of N black squares ---
    """ Puts the 8 queens on the board, in the right place.
+
    for c in range( 0, N, 2 ):
    Solution is a list containing the column location of each
+
        x = border + offset + c * side
    queens, assuming that the first queen goes on the top row,
+
        y = border + row * side  # must use row somehow
    the second queen on the next row, etc."""
+
        x2 = x + side
 +
        y2 = y + side
 +
        r = Rectangle( Point( x, y ), Point( x2, y2 ) )
 +
        r.setFill( "black" )
 +
        r.draw( win )
  
     # pick each column one after the other
+
def drawSolution( win, N, side, border, solution ):
 +
     """Given a solution for N queens on the board, show them as
 +
    red circles"""
 
     for row, col in solution:
 
     for row, col in solution:
         # transform row and col into a y and x position
+
         centerX = border + col*side + side/2
        x = col * 20 + 20 + 10
+
         centerY = border + row*side + side/2
         y = row * 20 + 20 + 10
+
         queen = Circle( Point( centerX, centerY ), side/4 )
       
+
         queen.setFill( "red" )
        # put a circle at x,y
+
         queen.draw( win )
         center = Point( x, y )
 
        cir = Circle( center, 8 )
 
         cir.draw( win )
 
         cir.setFill( "red" )
 
 
          
 
          
 
def main():
 
def main():
     #--- initialize the board ---
+
     #--- the controlling parameters ---
     win = GraphWin( "Queens" )
+
     N        = 8          # number of squares on a side
    drawBoard( win )
+
     border  = 20      # gutter around the board
 
+
     side    = (200-2*border) / N      # size of a square
     #--- the solution, where each number is a column number ---
 
     #--- we assume that
 
 
     solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
 
     solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
    drawQueens( win, solution )
 
 
      
 
      
     #--- wait till user clicks mouse to close up ---
+
    #--- open a window ---
     win.getMouse()
+
    win = GraphWin( "Chessboard" )
 +
 
 +
    #--- draw the board, then the queens ---
 +
    drawBoard( win, N, side, border )
 +
    drawSolution( win, N, side, border, solution )
 +
 
 +
     #--- wait for users to click mouse on window ---
 +
     win.getMouse( )
 
     win.close()
 
     win.close()
   
 
  
 
main()
 
main()

Latest revision as of 10:41, 3 March 2010

.

# chess.py
# D. Thiebaut
# Draws a board with 8 queens on it, in such a way
# that none of them can take each other.
# The text exquivalent is shown below.
"""
Q . . . . . . .
. . . . Q . . .
. . . . . . . Q
. . . . . Q . .
. . Q . . . . .
. . . . . . Q .
. Q . . . . . .
. . . Q . . . .
"""

from graphics import *

def drawBoard( win, N, side, border ):
    """draw the board, including border and black and white
    squares.  N is the number of squares on the side.  side 
    is the width of a square.  border is the gutter around the
    squares"""
    #--- draw the border first ---
    r = Rectangle( Point( border, border ),
                   Point( border+N*side, border+N*side ) )
    r.draw( win )

    #--- then all the black squares only ---
    for row in range( N ):
        drawRow( win, N, side, border, row )

def drawRow( win, N, side, border, row ):
    """draws one row of black squares on the board.  if row is 0
    the first top row is drawn."""

    #--- offset every even row by 1 square width ---
    offset = (row % 2) * side

    #--- place half of N black squares ---
    for c in range( 0, N, 2 ):
        x = border + offset + c * side
        y = border + row * side  # must use row somehow
        x2 = x + side
        y2 = y + side
        r = Rectangle( Point( x, y ), Point( x2, y2 ) )
        r.setFill( "black" )
        r.draw( win )

def drawSolution( win, N, side, border, solution ):
    """Given a solution for N queens on the board, show them as
    red circles"""
    for row, col in solution:
        centerX = border + col*side + side/2
        centerY = border + row*side + side/2
        queen = Circle( Point( centerX, centerY ), side/4 )
        queen.setFill( "red" )
        queen.draw( win )
        
def main():
    #--- the controlling parameters ---
    N        = 8           # number of squares on a side 
    border   = 20       # gutter around the board
    side     = (200-2*border) / N      # size of a square
    solution = [ [0,0],[1,4],[2,7],[3,5],[4,2],[5,6],[6,1],[7,3] ]
    
    #--- open a window ---
    win = GraphWin( "Chessboard" )

    #--- draw the board, then the queens ---
    drawBoard( win, N, side, border )
    drawSolution( win, N, side, border, solution )

    #--- wait for users to click mouse on window ---
    win.getMouse(  )
    win.close()

main()

.