Advertisement
Guest User

Untitled

a guest
Jan 7th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 2.47 KB | None | 0 0
  1. note
  2.     description: "A position on a snooker table 356.9 cm by 177.8cm."
  3.     author: "JSO"
  4.  
  5. class
  6.     BALL_POINT
  7.  
  8. inherit
  9.  
  10.     CONSTANTS
  11.         redefine
  12.             out,
  13.             is_equal
  14.         end
  15.  
  16.     DEBUG_OUTPUT
  17.         redefine
  18.             out,
  19.             is_equal
  20.         end
  21.  
  22. create
  23.     make,
  24.     make_from_tuple2
  25.  
  26. convert -- safe type conversion
  27.  
  28.     make_from_tuple2 ({TUPLE2})
  29.  
  30. feature {NONE}
  31.  
  32.     make (a_x, a_y: DECIMAL)
  33.             -- create snooker ball at position [a_x, a_y]
  34.         require
  35.             x_precondition: Zero <= a_x and a_x <= Width
  36.             y_precondition: Zero <= a_y and a_y <= Length
  37.         ensure
  38.             points_correct:
  39.                 x = a_x and y = a_y
  40.         end
  41.  
  42.     make_from_tuple2 (t: TUPLE2)
  43.             -- create a snooker ball at position `t`
  44.         require
  45.                 safe (t.x, t.y)
  46.         ensure
  47.                 x = t.x and y = t.y
  48.         end
  49.  
  50. feature -- queries
  51.  
  52.     x: DECIMAL
  53.             -- x-coordinate of the ball
  54.  
  55.     y: DECIMAL
  56.             -- y-coordinate of the ball
  57.  
  58.     safe (a_x, a_y: DECIMAL): BOOLEAN
  59.             -- is position [a_x, a_y] within the dimensions
  60.             -- of the snooker table?
  61.         ensure
  62.             class  -- static
  63.             Result = (Zero <= a_x
  64.                 and a_x <= Width
  65.                 and Zero <= a_y
  66.                 and a_y <= Length)
  67.         end
  68.  
  69.  
  70.  
  71.     t2ball (t: TUPLE2): like Current
  72.             -- convert tuple `t` to a ball position
  73.         require
  74.                 safe (t.x, t.y)
  75.         ensure
  76.                 class
  77.                 Result ~ create {BALL_POINT}.make_from_tuple2 (t)
  78.         end
  79.  
  80. feature -- comparison
  81.  
  82.     is_equal (other: like Current): BOOLEAN
  83.             -- Is other attached to an object considered
  84.             -- equal to current object?
  85.         ensure then
  86.                 Result = (x ~ other.x and y ~ other.y)
  87.         end
  88.  
  89.     distance (other: like Current): TUPLE2
  90.             -- distance as a tuple from  current ball
  91.             -- to position of other ball
  92.         ensure
  93.             Result ~
  94.                create {TUPLE2}.make_from_tuple ([other.x - x, other.y - y])
  95.         end
  96.  
  97. feature -- commands
  98.  
  99.     cue_delta (d_x, d_y: DECIMAL)
  100.             -- move ball from current position by a delta [d_x, d_y]
  101.         require
  102.             x_cue_precondition: Zero <= x + d_x and x + d_x <= Width
  103.             y_cue_precondition: Zero <= y + d_y and y + d_y <= Length
  104.         ensure
  105.                 x ~ (old x + d_x)
  106.                 y ~ (old y + d_y)
  107.         end
  108.  
  109. feature -- out
  110.  
  111.     out: STRING_8
  112.             -- New string containing terse printable representation
  113.             -- of current object
  114.         do
  115.             Result := "(" + x.precise_out + "," + y.precise_out + ")"
  116.         end
  117.  
  118.     debug_output: STRING_8
  119.         -- String that should be displayed in debugger to represent Current.
  120.         do
  121.             Result := out
  122.         end
  123.  
  124. invariant
  125.     x_constraint:
  126.         Zero <= y and y <= Length
  127.     y_constraint:
  128.         Zero <= x and x <= Width
  129.  
  130. end -- class BALL_POINT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement