class GAME_BOARD -- Snakes and Ladders board creation make feature nrow : INTEGER is 10 -- number of rows ncol : INTEGER is 10 -- number of columns (squares in a row) size : INTEGER -- total number of squares board : ARRAY[INTEGER] -- board is an array of 'squares' sl_squares : ARRAY[INTEGER] -- positions at which a snake or ladder starts sl_values : ARRAY[INTEGER] -- amount by which a player landing on an sl_square has to move make is -- Create a board initialised to an array of nrow*ncol 'squares' -- Ordinary squares contain a value of 0 -- Square at the base of a ladder has a positive value -- Square at the top of a snake has a negative value local i : INTEGER -- index for sl_squares and sl_values do size := nrow * ncol !!board.make(1, size) -- copy of Snakes and Ladders board sl_squares := <<1, 4, 8, 16, 21, 28, 36, 47, 49, 51, 56, 62, 64, 71, 80, 87, 93, 95, 98>> sl_values := <<37, 10, 23, -10, 21, 56, 8, -21, -38, 16, -3, -43, -4, 20, 20, -63, -20, -20, -20>> -- place snake and ladder values on board from i := sl_squares.lower until i > sl_squares.upper loop board.put(sl_values.item(i), sl_squares.item(i)) i := i + 1 end -- loop end -- make square_val(pos : INTEGER) : INTEGER is -- value of square at position pos local do Result := board.item(pos) end -- item display_board is -- Display the Snakes and Ladders board, numbering each square -- and indicating the positions and amounts of the snakes -- and ladders. local hline : STRING bline : STRING ir : INTEGER ic : INTEGER pos : INTEGER sl_val : INTEGER do -- construct hline and bline !!hline.make_from_string("+") !!bline.make_from_string("|") from ic := 1 until ic > ncol loop hline.append("-----+") bline.append(" |") ic := ic + 1 end -- loop -- draw the board from ir := 1 until ir > nrow loop io.put_string(hline); io.put_new_line from io.put_character('|') ic := 1 until ic > ncol loop pos := ic + 10*(ir-1) io.put_integer_format(pos, 5) io.put_character('|'); ic := ic + 1 end -- loop inner1 io.put_new_line from io.put_character('|') ic := 1 until ic > ncol loop pos := ic + 10*(ir-1) sl_val := board.item(pos) if sl_val = 0 then io.put_string(" . ") else io.put_integer_format(sl_val, 4) end -- if io.put_string(" |"); ic := ic + 1 end -- loop inner2 io.put_new_line io.put_string(bline); io.put_new_line ir := ir + 1 end -- loop outer io.put_string(hline); io.put_new_line end -- display_board end -- class GAME_BOARD