[= Tutorial Sample - Tic Tac Toe 1B Command: grarls -png 320x200 tictactoe_1B.grs Purpose: =] [= Main Configuration Parameters =] Const Number PageWidth = 320, PageHeight = 200; Const Number VBorder = 20; Const Number Cells = 3; [= token codes =] Const Number X = 1, O = 2; [= 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 =] Token(X, 1, 1); Token(O, 1, 2); Token(X, 2, 1); Token(O, 3, 1); Token(X, 2, 2); Token(O, 3, 3); Token(X, 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 For i From 1 To Cells - 1 Do LineTo( { 0, Cells * CellSize } ) => { HBorder + i * CellSize, VBorder }; LineTo( { Cells * CellSize, 0 } ) => { HBorder, VBorder + i * CellSize }; EndFor; End; [= TOKEN ----- Draw the specified playing token in the specified cell. =] Shape Token(Number token, Number row, Number col) Point centre; Begin [= calculate cell centre =] centre := CellCentre(row, col); [= select which token to draw =] Select token From Case X Do Cross() => centre; Case O Do Nought() => centre; EndSelect; End; [= NOUGHT ------ Just a simple circle. =] Shape Nought() Begin Circle(TokenSize / 2 ); End; [= CROSS ----- Two diagonal line. =] Shape Cross() 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;