indexing description: "load the state of a puzzle saved before in a .puz file." author: "Markus Neidhart and UC Team" date: "$Date$" revision: "$Revision$" class UC_LOAD_PUZZLE inherit SHARED_QUAD_CUTTER create make feature -- creation make (a_filename: STRING) is -- load a .puz file and return a puzzle_start_window require a_filename_not_Void: a_filename /= Void local psw: PUZZLE_START_WINDOW do if (a_filename.count < 4) or else not (a_filename.substring (a_filename.count - 3,a_filename.count).is_equal (".puz")) then a_filename.append (".puz") create file.make_open_read (a_filename) -- loads a puz file else create file.make_open_read (a_filename) -- loads a puz file end end feature -- Implementation read_file: PUZZLE_START_WINDOW is -- read `file' in and create a puzzle_start_window local from_space: UC_FROM_SPACE wsp1,wsp2: UC_WORKSPACE to_space: UC_TO_SPACE x,y,orientation,ppx,ppy, nop: INTEGER locked: BOOLEAN str: STRING ppv: UC_PUZZLE_PART_VIEW do create str.make_empty file.read_line -- read filename where the puzzle should be file.read_integer -- read the number of parts nop := file.last_integer cutter.make_from_image (file.last_string, nop) -- now the workspaces will be filled create from_space.make_from_puzzle (cutter.last_puzzle) -- make from_space from cutter.last_puzzle from_space.wipe_out -- remove all puzzle_part_views from the from_space -- now read all puzzle_part_views from the file from file.read_line until file.last_string.is_empty loop str.make_from_string (file.last_string) -- store the whole line in str x := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace y := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the y-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace orientation := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the orientation of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace locked := str.substring (1, str.substring_index (" ", 1)).to_boolean -- take out if the ppv is locked str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppx := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the specified pp which belongs to the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppy := str.to_integer -- take out the y-coordinate of the specified pp which belongs to the ppv create ppv.make_from_puzzle_part (cutter.last_puzzle.get_puzzle_part (ppx, ppy)) if locked then ppv.lock -- lock ppv if locked = True end from until orientation = ppv.orientation loop ppv.rotate -- rotate ppv as long as orientation /= ppv.orientation end ppv.set_x_y (x, y) -- set coordinates of ppv from_space.extend (ppv) file.read_line end from_space.set_x_y (10, 10) -- workspace1 create wsp1.make_from_puzzle (cutter.last_puzzle) -- make from_space from cutter.last_puzzle wsp1.wipe_out -- remove all puzzle_part_views from the from_space -- now read all puzzle_part_views from the file from file.read_line until file.last_string.is_empty loop str.make_from_string (file.last_string) -- store the whole line in str x := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace y := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the y-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace orientation := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the orientation of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace locked := str.substring (1, str.substring_index (" ", 1)).to_boolean -- take out if the ppv is locked str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppx := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the specified pp which belongs to the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppy := str.to_integer -- take out the y-coordinate of the specified pp which belongs to the ppv create ppv.make_from_puzzle_part (cutter.last_puzzle.get_puzzle_part (ppx, ppy)) if locked then ppv.lock -- lock ppv if locked = True end from until orientation = ppv.orientation loop ppv.rotate -- rotate ppv as long as orientation /= ppv.orientation end ppv.set_x_y (x, y) -- set coordinates of ppv wsp1.extend (ppv) file.read_line end wsp1.set_x_y (10, 10) -- workspace2 create wsp2.make_from_puzzle (cutter.last_puzzle) -- make from_space from cutter.last_puzzle wsp2.wipe_out -- remove all puzzle_part_views from the from_space -- now read all puzzle_part_views from the file from file.read_line until file.last_string.is_empty loop str.make_from_string (file.last_string) -- store the whole line in str x := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace y := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the y-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace orientation := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the orientation of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace locked := str.substring (1, str.substring_index (" ", 1)).to_boolean -- take out if the ppv is locked str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppx := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the specified pp which belongs to the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppy := str.to_integer -- take out the y-coordinate of the specified pp which belongs to the ppv create ppv.make_from_puzzle_part (cutter.last_puzzle.get_puzzle_part (ppx, ppy)) if locked then ppv.lock -- lock ppv if locked = True end from until orientation = ppv.orientation loop ppv.rotate -- rotate ppv as long as orientation /= ppv.orientation end ppv.set_x_y (x, y) -- set coordinates of ppv wsp2.extend (ppv) file.read_line end wsp2.set_x_y (10, 10) -- to_space create to_space.make to_space.wipe_out -- remove all puzzle_part_views from the from_space -- now read all puzzle_part_views from the file from file.read_line until file.last_string.is_empty loop str.make_from_string (file.last_string) -- store the whole line in str x := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace y := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the y-coordinate of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace orientation := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the orientation of the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace locked := str.substring (1, str.substring_index (" ", 1)).to_boolean -- take out if the ppv is locked str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppx := str.substring (1, str.substring_index (" ", 1)).to_integer -- take out the x-coordinate of the specified pp which belongs to the ppv str.remove_substring (1, str.substring_index (" ", 1)) -- now remove this substring including the whitespace ppy := str.to_integer -- take out the y-coordinate of the specified pp which belongs to the ppv create ppv.make_from_puzzle_part (cutter.last_puzzle.get_puzzle_part (ppx, ppy)) if locked then ppv.lock -- lock ppv if locked = True end from until orientation = ppv.orientation loop ppv.rotate -- rotate ppv as long as orientation /= ppv.orientation end ppv.set_x_y (x, y) -- set coordinates of ppv to_space.extend (ppv) file.read_line end -- now to the creation of the puzzle_start_window create Result Result.set_from_space (from_space) -- set the from_space of psw Result.set_workspaces (wsp1, wsp2) -- set both workspaces Result.set_to_space (to_space) -- set to_space Result.set_number_of_parts (nop) Result.initialize_loaded end feature {NONE} -- Access file: PLAIN_TEXT_FILE invariant file_not_Void: file /= Void end -- class UC_SAVE_PUZZLE