📄 hamster.e
字号:
-------------------------------------------------------------------------- File : hamster.e-- Contents: hamster class, client side-- sends commands to stdout, receives from stdin-- Authors : Ilona Bl"umel and Christian Borgelt-- History : 21.10.1997 file created-- 02.11.1997 function hms_move simplified-- 09.03.1999 translated to EIFFEL -- Ilona Bl"umel------------------------------------------------------------------------class HAMSTERcreation make-------------------------------------------------------------------------- Protocol for Pipe Communication-------------------------------------------------------------------------- request meaning reply-- c create hamster c <id> <x> <y> <dir><look><heap><load>-- c -1 on error-- d <id> delete hamster d <id>-- m <id> move hamster m <id> <x> <y> <look> <heap>-- t <id> <turn> turn hamster t <id> <dir> <look>-- l <id> <n> (un)load corn l <id> <heap> <load>-------------------------------------------------------------------------- Constants------------------------------------------------------------------------featuremax_x: INTEGER is 64; -- maximal x-extension of mazemax_y: INTEGER is 64; -- maximal y-extension of mazemax_corn: INTEGER is 255; -- maximal size of corn heapmax_load: INTEGER is 12; -- maximal load of corn in cheeks-- results of get_direast: INTEGER is 0; -- hamster is looking eastnorth: INTEGER is 1; -- hamster is looking northwest: INTEGER is 2; -- hamster is looking westsouth: INTEGER is 3; -- hamster is looking south-- results of get_lookempty: INTEGER is 0; -- there is an empty field aheadcorn: INTEGER is 1; -- there is a field with corn aheadwall: INTEGER is 2; -- there is a wall ahead-- parameters of turnpos: INTEGER is 1; -- positive turn (counterclockwise)neg: INTEGER is -1; -- negative turn (clockwise)left: INTEGER is 1; -- left turn (counterclockwise)right: INTEGER is -1; -- right turn (clockwise)-------------------------------------------------------------------------- Object Variables------------------------------------------------------------------------feature {NONE}id: INTEGER; -- hamster identifierx, y: INTEGER; -- relative position in mazedir: INTEGER; -- direction (line of sight)look: INTEGER; -- outlook in current directionheap: INTEGER; -- amount of corn on current fieldload: INTEGER; -- amount of corn in cheeks-------------------------------------------------------------------------- Auxiliary Functions (for internal use only)------------------------------------------------------------------------feature {NONE}read_cmd_id (cmd: CHARACTER; ident: INTEGER): INTEGER isdo -- read command and hamster identifier from io.read_character; until not io.last_character.is_separator loop -- skip all blank characters io.read_character; -- which may preceed the reply end -- sent by the server program check -- check whether the command given io.last_character = cmd -- is echoed correctly end io.read_integer; -- read the hamster identifier and check -- check it against the given identifier ident < 0 or else io.last_integer = ident end Result := io.last_integer; -- return the hamster identifier readend -- read_cmd_id-------------------------------------------------------------------------- Methods------------------------------------------------------------------------featuremake is -- create a hamsterdo io.put_string("c%N"); -- send create message ('c') io.flush; -- and flush the buffer (force writing) id := read_cmd_id('c', -1); -- scan reply and get the hamster id io.read_integer; x := io.last_integer; io.read_integer; y := io.last_integer; io.read_integer; dir := io.last_integer; io.read_integer; look := io.last_integer; io.read_integer; heap := io.last_integer; io.read_integer; load := io.last_integer;end -- make -- get other hamster data------------------------------------------------------------------------delete is -- delete a hamsterdo io.put_string("d "); -- send delete message ('d') io.put_integer(id); -- qualified by the hamster identifier io.put_new_line; io.flush; -- and flush the buffer (force writing) id := read_cmd_id('d', id); -- scan the reply from the serverend -- delete------------------------------------------------------------------------get_x: INTEGER is -- get hamster x-positiondo -- return the x-coordinate Result := x; -- of the current hamster positionend -- get_x------------------------------------------------------------------------get_y: INTEGER is -- get hamster y-positiondo -- return the y-coordinate Result := y; -- of the current hamster positionend -- get_y------------------------------------------------------------------------get_dir: INTEGER is -- get hamster directiondo -- return the direction Result := dir; -- the hamster is currently looking intoend -- get_dir------------------------------------------------------------------------get_look: INTEGER is -- get outlook in current directiondo -- return what the hamster sees Result := look; -- in the direction it is looking intoend -- get_look------------------------------------------------------------------------get_corn: INTEGER is -- get amount of corn on fielddo -- return the size of the corn heap Result := heap; -- (number of grains of corn)end -- get_corn -- on the current field------------------------------------------------------------------------get_load: INTEGER is -- get amount of corn in cheeksdo -- return the number of grains of corn Result := load; -- the hamster currently carriesend -- get_load -- in its cheeks------------------------------------------------------------------------move: INTEGER is -- move hamster forwardlocal old_x, old_y: INTEGER; -- old hamster positiondo old_x := get_x; -- note the current hamster position old_y := get_y; -- for a later check for success io.put_string("m "); -- send move message ('m') io.put_integer(id); -- qualified by the hamster identifier io.put_new_line; io.flush; -- and flush the buffer (force writing) id := read_cmd_id('m',id); -- scan reply to get new hamster data io.read_integer; x := io.last_integer; io.read_integer; y := io.last_integer; io.read_integer; look := io.last_integer; io.read_integer; heap := io.last_integer; if x = old_x and y = old_y -- if the hamster is still then Result := -1; -- on the same field, else Result := 0; -- return an error code, end -- otherwise return 'ok'end -- move-------------------------------------------------------------------------- 'move' will fail, if there is a wall in the direction the hamster-- is looking into. In this case the hamster is not moved, but stays-- on the field it is currently on.------------------------------------------------------------------------turn (t: INTEGER) is -- turn hamster 90 degreesdo -- t: turn direction (pos, neg) io.put_string("t "); -- send turn message ('t') io.put_integer(id) -- qualified by the hamster identifier io.put_spaces(1); -- and paramaterized by io.put_integer(t); -- the turn direction io.put_new_line; io.flush; -- and flush the buffer (force writing) id:=read_cmd_id('t', id); -- scan reply to get new hamster data io.read_integer; dir := io.last_integer; io.read_integer; look := io.last_integer;end -- turn-------------------------------------------------------------------------- In contrast to 'move', 'turn' cannot fail.------------------------------------------------------------------------take (amount: INTEGER): INTEGER is -- take some cornlocal -- amount: number of grains to take old_load: INTEGER; -- old number of grains in cheeksdo old_load := load; -- note current load io.put_string("l "); -- send load message ('l') io.put_integer(id); -- qualified by the hamster identifier io.put_spaces(1); -- and parameterized by io.put_integer(amount); -- the number of grains to take/drop io.put_new_line; io.flush; -- and flush the buffer (force writing) id := read_cmd_id('l',id); -- scan reply to get new hamster data io.read_integer; heap := io.last_integer; io.read_integer; load := io.last_integer; Result := load - old_load; -- return the real amount taken/droppedend -- take-------------------------------------------------------------------------- If 'amount' is greater than the number of grains on the current field-- or if it is greater than the number of additional grains the hamster-- can carry, 'amount' is reduced to fit the restrictions.------------------------------------------------------------------------drop (amount: INTEGER): INTEGER is -- drop some corndo -- amount: number of grains to drop Result := -take(-amount); -- call 'take' with a negative amountend -- drop -- (simpler than a reimplementation)-------------------------------------------------------------------------- If 'amount' is greater than the number of grains that can be placed-- on the current field or if it is greater than the number of grains-- the hamster currently carries, 'amount' is reduced to fit the-- restrictions.------------------------------------------------------------------------end -- HAMSTER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -