BFOIT - Introduction to Computer Programming

Mastermind Project - FAQs

Can I Use the Grid Toolkit for the Guess Boxes?

An Enhanced Grid Toolkit

The last lesson in which we worked with the Grid Toolkit was the Words and Sentences lesson. When you look at the array of guess boxes that we need for the Mastermind game,  But on closer inspection, there are a couple of differences between what we need for Mastermind's guess boxes and what our Grid Toolkit provides.

  1. The choice boxes have gaps between the rows and columns of cells.
  2. The grid grows.  It starts out as a single row of four cells and grows as each guess fails. The Grid Toolkit provides no way to change its size.

I've added functionality to the Grid Toolkit that addresses both of these shortfalls.  Here is the new version.

 ; Grid Toolkit (Lesson 17 - Mastermind Project)
 ;-----------------------------------------------
 
 ; number of rows in the grid
 global gridNumRow
 
 
 ; color for a cell's background
 ; SYMBOLIC CONSTANT
 to gridCellColor
   output 7  ; white
   end
 
 ; color of grid's frame
 ; SYMBOLIC CONSTANT
 to gridFrameColor
   output 0  ; black
   end
 
 ; size of the sides of a grid cell
 ; SYMBOLIC CONSTANT
 to gridCellSize
   output 20
   end
 
 ; one-half the size of a side of a grid cell
 to gridHafCellSiz
   output quotient gridCellSize 2
   end
 
 ; size of the gap between cells in the grid
 ; SYMBOLIC CONSTANT
 to gridCellGapSize
   output 5
   end
 
 ; width of one column/height of one row
 ; sum of gridCellSize and gridCellGapSize
 to gridColRowSize
   output sum gridCellSize gridCellGapSize
   end
 
 ; number of columns in the grid
 ; SYMBOLIC CONSTANT
 to gridNumCol
   output 4
   end
 
 ; X coordinate for left side of the grid
 ; SYMBOLIC CONSTANT
 to gridLeftX
   output -15
   end
 
 ; Y coordinate for top of the grid
 ; SYMBOLIC CONSTANT
 to gridTopY
   output 140
   end
 
 to gridRowTopY :row
   output difference gridTopY (product :row gridColRowSize)
   end
 
 ; number of cells in the grid
 to gridNumCells
   output product gridNumCol :gridNumRow
   end
 
 ; height of grid in turtle steps
 to gridHeight
   output sum (product :gridNumRow gridCellSize) (product (difference :gridNumRow 1) gridCellGapSize)
   end
 
 ; width of grid in turtle steps
 to gridWidth
   output sum (product gridNumCol gridCellSize) (product (difference gridNumCol 1) gridCellGapSize)
   end
 
 ; move turtle to top-left corner of grid
 to gridGotoTopLeft
   penup
   setxy gridLeftX gridTopY
   end
 
 ; move turtle to top-left corner of specified cell
 to gridGotoCell :idx
   gridGotoTopLeft
   setheading 180
   forward product gridColRowSize (int quotient :idx gridNumCol)
   setheading 90
   forward product gridColRowSize (remainder :idx gridNumCol)
   end
 
 ; move turtle to top-left corner of specified cell
 to gridGotoRowColCell :rowCol
   gridGotoTopLeft
   setheading 180
   forward product gridColRowSize (first :rowCol)
   setheading 90
   forward product gridColRowSize (last :rowCol)
   end
 
 ; with the turtle at the top-left corner of
 ; a cell, move it to the center of the cell
 to gotoCellCtrHelper
   setheading 90 forward gridHafCellSiz
   right 90 forward gridHafCellSiz
   end
 
 ; move turtle to the center of specified cell
 to gridGotoCellCtr :idx
   gridGotoCell :idx
   gotoCellCtrHelper
   end
 
 ; move turtle to the center of specified cell
 to gridGotoRowColCellCtr :rowCol
   gridGotoRowColCell :rowCol
   gotoCellCtrHelper
   end
 
 ; with the turtle at the top-left corner of a cell,
 ; repaint the cell with a provided background color
 to fillCellHelper :color
   setpensize gridCellSize setpencolor :color
   setheading 0 pendown
   forward gridHafCellSiz back gridCellSize forward gridHafCellSiz
   penup forward gridHafCellSiz left 90 forward gridHafCellSiz
   setpensize 1 setpencolor gridFrameColor
   setheading 90 pendown
   repeat 4 [forward gridCellSize right 90]
   end
 
 ; draw/redraw one cell of the grid
 to gridCellFill :idx :color
   gridGotoCellCtr :idx
   fillCellHelper :color
   end
 
 ; draw/redraw one cell of the grid
 to gridRowColCellFill :rowCol :color
   gridGotoRowColCellCtr :rowCol
   fillCellHelper :color
   end
 
 ; with the turtle at the top-left corner of a cell, label
 ; the cell with a provided character in a provided color
 to labelCellHelper :ch :color
   setheading 180
   forward quotient (difference gridCellSize 22) 2
   forward 22
   left 90
   forward quotient (difference gridCellSize 18) 2
   setlabelheight 32
   setpencolor :color
   label :ch
   end
 
 ; label the specified cell with a character
 to gridCellLabel :idx :ch :color
   gridGotoCell :idx
   labelCellHelper :ch :color
   end
 
 ; label the specified cell with a character
 to gridRowColCellLabel :rowCol :ch :color
   gridGotoRowColCell :rowCol
   labelCellHelper :ch :color
   end
 
 ; paint a single column of :gridNumRow cells
 to gridPaintCol
   setpensize 1 setpencolor gridFrameColor
   repeat :gridNumRow [pd repeat 4 [fd gridCellSize lt 90] pu fd gridColRowSize]
   back difference gridHeight gridCellSize
   end
 
 ; paint a single row of gridNumCol cells
 to gridPaintRow
   setpensize 1 setpencolor gridFrameColor
   repeat gridNumCol [pd repeat 4 [fd gridCellSize rt 90] pu fd gridColRowSize]
   back difference gridWidth gridCellSize
   end
 
 ; draw the grid
 to gridPaint
   gridGotoTopLeft
   setheading 180 forward (quotient gridHeight 2) setheading 90
   setpensize gridHeight setpencolor gridCellColor
   pendown forward gridWidth
   gridGotoTopLeft
   setheading 90 pendown
   repeat :gridNumRow [gridPaintRow right 90 forward gridColRowSize left 90]
   end
 
 ; add a row to the bottom of grid
 ; the gap above the new set of cells, and the cells are painted
 ; with gridCellColor before the grid frames are drawn
 to gridAddRow
   make "gridNumRow (sum :gridNumRow 1)
   gridGotoRowColCell sentence (difference :gridNumRow 1) 0
   setheading 180 forward (difference (quotient gridColRowSize 2) (difference gridCellGapSize 1))
   left 90 setpensize gridColRowSize setpencolor gridCellColor
   pendown forward gridWidth
   gridGotoRowColCell sentence (difference :gridNumRow 1) 0
   setheading 90 gridPaintRow
   end
 
 ; End Grid Toolkit
 ;------------------ 


Back to Mastermind Project

Public Domain Mark
This work (BFOIT: Introduction to Computer Programming, by Guy M. Haas),
identified by Berkeley Foundation for Opportunities in IT (BFOIT),
is free of known copyright restrictions.