-- Joseph Curtis -- ID: 3952239 -- COMP1100 Assignment 3 class SANDL_GAME -- Simulation of the game Snakes and Ladders creation make feature sl_board : GAME_BOARD -- the Snakes and Ladders board sl_die : DIE -- Die that generates the random numbers num_players : INTEGER -- number of players players : ARRAY[PLAYER] -- Array the players' info is stored in winner : PLAYER -- Winner of the game max_turns : INTEGER is 200 -- limit on the length of a game make is local seed : INTEGER -- seed to initialise die p : PLAYER ip : INTEGER do -- die io.put_string("Seed for die : ") io.read_integer; seed := io.last_integer !!sl_die.make(seed) -- players io.put_string("Number of players (>=2) : ") io.read_integer; num_players := io.last_integer !!players.make(1, num_players) from -- Setup the players array ip := players.lower until ip > players.upper loop !!p.make(ip) players.put(p, ip) ip := ip + 1 end -- loop display_players -- board !!sl_board.make -- Make the game board sl_board.display_board -- game play_game display_players -- report winner if winner /= Void then io.put_string("The winner is player ") io.put_integer(winner.id); io.put_new_line else io.put_string("Game discontinued"); io.put_new_line end -- if -- sl_die.print_stats end -- make display_players is -- Prints the players' info on the screen local i : INTEGER do io.put_new_line from i := players.lower until i > players.upper loop players.item(i).print_player_info io.put_new_line i := i + 1 end io.put_new_line end -- display_players play_turn (p : PLAYER) is -- This procedure rolls the dice and updates the players position -- If there is a snake or a ladder it again moves the piece -- accordingly. It also checks to see wether a player has reached -- or passed the 100th square, if so it marks them as the winner local update_by : INTEGER -- The amount the player's position is changed -- by do sl_die.throw if(p.position+sl_die.last_value<101) then update_by := sl_die.last_value + sl_board.board.item(p.position + sl_die.last_value ) else update_by := sl_die.last_value end if(update_by + p.position > 99) then -- Check to see if they've won p.update_position(update_by) -- update position winner := clone(p) else p.update_position(update_by) -- update position end end -- play_turn print_debug_info( turn: INTEGER; die_val: INTEGER; p: PLAYER ) is -- Print turn, die value and player info for the current turn. do io.put_string("Turn "); io.put_integer(turn) io.put_string(" Die "); io.put_integer(die_val) io.put_string(" "); p.print_player_info io.put_new_line end -- print_debug_info play_game is -- The players play in turn until one wins by reaching the end -- of the board. local turn : INTEGER -- turn number it is ip : INTEGER do from winner := Void -- which player is the winner is turn := 0 until winner /= Void or turn = max_turns loop -- order of players is 1, 2, ... num_players, 1, 2, etc ip := 1 + turn\\num_players turn := turn + 1 play_turn(players.item(ip)) -- debugging information following each turn --sl_die.throw print_debug_info(turn, sl_die.last_value, players.item(ip)) end -- loop end -- play_game end -- class SANDL_GAME