[code=thinbasic]
'Example of three types of array initializations:
'
'1) The global declarations assign one value to the entire array
'2) The local declarations assign each array element its value
'3) The for loops compute and assign a value to each element of the array.
'
USES "console"
CONST NL AS STRING * 2 VALUE crlf
CONST UNKNOWN AS INTEGER VALUE 0
CONST MAXCELL AS INTEGER VALUE 81
GLOBAL cellsolved( MAXCELL ) AS INTEGER VALUE %false
GLOBAL cellvalue( MAXCELL ) AS INTEGER VALUE UNKNOWN
GLOBAL countpossible( MAXCELL ) AS INTEGER VALUE 9
DIM neighbor ( MAXCELL, MAXCELL ) AS INTEGER
LOCAL cell2box( MAXCELL ) AS INTEGER VALUE _
1, 1, 1, 2, 2, 2, 3, 3, 3, _
1, 1, 1, 2, 2, 2, 3, 3, 3, _
1, 1, 1, 2, 2, 2, 3, 3, 3, _
4, 4, 4, 5, 5, 5, 6, 6, 6, _
4, 4, 4, 5, 5, 5, 6, 6, 6, _
4, 4, 4, 5, 5, 5, 6, 6, 6, _
7, 7, 7, 8, 8, 8, 9, 9, 9, _
7, 7, 7, 8, 8, 8, 9, 9, 9, _
7, 7, 7, 8, 8, 8, 9, 9, 9
LOCAL cell2col( MAXCELL ) AS INTEGER VALUE _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9, _
1, 2, 3, 4, 5, 6, 7, 8, 9
LOCAL cell2row( MAXCELL ) AS INTEGER VALUE _
1, 1, 1, 1, 1, 1, 1, 1, 1, _
2, 2, 2, 2, 2, 2, 2, 2, 2, _
3, 3, 3, 3, 3, 3, 3, 3, 3, _
4, 4, 4, 4, 4, 4, 4, 4, 4, _
5, 5, 5, 5, 5, 5, 5, 5, 5, _
6, 6, 6, 6, 6, 6, 6, 6, 6, _
7, 7, 7, 7, 7, 7, 7, 7, 7, _
8, 8, 8, 8, 8, 8, 8, 8, 8, _
9, 9, 9, 9, 9, 9, 9, 9, 9
DIM icell, jcell AS INTEGER
'Two cells are neighbors if they share the same box, column, or row.
'Set up then neighbor relationship test.
FOR icell = 1 TO MAXCELL
FOR jcell = 1 TO MAXCELL
IF _
cell2box( icell ) = cell2box( jcell ) OR _
cell2col( icell ) = cell2col( jcell ) OR _
cell2row( icell ) = cell2row( jcell ) _
THEN
neighbor ( icell, jcell ) = %true
ELSE
neighbor ( icell, jcell ) = %false
ENDIF
NEXT 'jcell
neighbor ( icell, icell ) = %false 'A cell is not its own neighbor.
NEXT 'icell
''show the neighbors of cell 22
'FOR icell = 1 TO MAXCELL
' IF MOD ( icell, 9 ) = 1 THEN
' CONSOLE_WRITE NL
' ENDIF
' IF neighbor ( 22, icell ) = %true THEN
' CONSOLE_WRITE "o "
' ELSE
' CONSOLE_WRITE ". "
' ENDIF
'NEXT 'icell
''. . . o o o . . .
''. . . o o o . . .
''o o o . o o o o o
''. . . o . . . . .
''. . . o . . . . .
''. . . o . . . . .
''. . . o . . . . .
''. . . o . . . . .
''. . . o . . . . .
[/code]
Bookmarks