[= Tutorial Sample - Tic Tac Toe 1A Command: grarls -png 320x200 tictactoe_1A.grs Purpose: =] [= Main Configuration Parameters =] Const Number PageWidth = 320, PageHeight = 200; Const Number VBorder = 20; Const Number Cells = 3; [= Derived Constants =] Const Number CellSize = ( PageHeight - ( 2 * VBorder ) ) // Cells; Const Number TokenSize = CellSize * 0.8; Const Number HBorder = ( PageWidth - Cells * CellSize ) // 2; Program () Begin [= draw the main grid =] Grid(); [= draw all the moves =] Cross() => CellCentre(1, 1); Nought() => CellCentre(1, 2); Cross() => CellCentre(2, 1); Nought() => CellCentre(3, 1); Cross() => CellCentre(2, 2); Nought() => CellCentre(3, 3); Cross() => CellCentre(2, 3); End; [= GRID ---- Draw the standard grid. Each time around the loop it draws one vertical and one horizontal line. Use the LineTo because we know where line start and how long it is. =] Shape Grid() Number i; Begin [= draw required number of lines =] For i From 1 To Cells - 1 Do [= a vertival line =] LineTo( { 0, Cells * CellSize } ) => { HBorder + i * CellSize, VBorder }; [= a horizontal line =] LineTo( { Cells * CellSize, 0 } ) => { HBorder, VBorder + i * CellSize }; EndFor; End; [= NOUGHT ------ Just a simple circle. =] Shape Nought() Begin [= radius is half the size of the token =] Circle(TokenSize / 2 ); End; [= CROSS ----- Two diagonal line. =] Shape Cross() [= effective 'radius' half size of token =] Number len = TokenSize / 2; Begin Line( {-len, -len}, {len, len} ); Line( {len, -len}, {-len, len} ); End; [= CELL CENTRE ----------- Calculate pixel coordinates of cell centre. Use the global constants for cells size and borders. =] Function Point CellCentre(Number row, Number col) Begin [= return a calculated Point object =] Return { HBorder + ( col - 1/2 ) * CellSize, VBorder + ( row - 1/2 ) * CellSize }; End;