Advertisement
Guest User

Untitled

a guest
Jan 7th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 3.12 KB | None | 0 0
  1. note
  2.     description: "[
  3.         snooker table with red and blue ball
  4.         can move red and blue balls with a delta impulse
  5.         terminates if a ball is moved to a pocket.
  6.     ]"
  7.     author: "JSO"
  8. class
  9.     SNOOKER_TABLE
  10. inherit
  11.     CONSTANTS
  12. create
  13.     make
  14. feature {NONE} -- Initialization
  15.  
  16.     make (a_blue, a_red: BALL_POINT)
  17.             -- Initialization for Current.
  18.         require
  19.                 a_blue /~ a_red
  20. feature -- queries
  21.  
  22.     blue    : BALL_POINT
  23.  
  24.     red: BALL_POINT
  25.  
  26.     pocket: BALL_POINT
  27.  
  28.     terminated: BOOLEAN
  29.             -- game terminates when either ball is in pocket
  30.         ensure
  31.                 Result = (blue ~ pocket or red ~ pocket)
  32.         end
  33.  
  34. feature -- commands
  35.  
  36.     impulse_blue (delta: TUPLE2)
  37.             -- move blue ball on table by delta
  38.         require
  39.             delta_safe: blue.safe (blue.x + delta.x, blue.y + delta.y)
  40.             status: not terminated
  41.             not_red: red /~ {BALL_POINT}.t2ball
  42.                 (create {TUPLE2}.make_from_tuple
  43.                     ([blue.x + delta.x, blue.y + delta.y]))
  44.         ensure
  45.             new_blue_x: blue.x ~ old blue.x + delta.x
  46.             new_blue_y: blue.y ~ old blue.y + delta.y
  47.             unchanged: red ~ old red
  48.         end
  49.  
  50.     impulse_red (delta: TUPLE2)
  51.             -- move red ball on table by delta
  52.         require
  53.             delta_safe: red.safe (red.x + delta.x, red.y + delta.y)
  54.             status: not terminated
  55.             not_red: red /~ {BALL_POINT}.t2ball
  56.                 (create {TUPLE2}.make_from_tuple
  57.                     ([red.x + delta.x, red.y + delta.y]))
  58.         ensure
  59.             new_red_x: red.x ~ old red.x + delta.x
  60.             new_red_y: red.y ~ old red.y + delta.y
  61.             unchanged: blue ~ old blue
  62.         end
  63.  
  64. feature --  multi move blue ball
  65.     maximum: detachable TUPLE2
  66.  
  67.     calculated: BOOLEAN -- max was calculated
  68.  
  69.     multi_move_blue (a: ARRAY [TUPLE2])
  70.             -- many blue ball moves, if posible,
  71.             -- and save the maximum move
  72.         require
  73.                 not terminated
  74.                 a.count >= 1
  75.         local
  76.             i: INTEGER_32
  77.             old_blue: like blue
  78.             l_delta: TUPLE2
  79.             l_stop: BOOLEAN
  80.         do
  81.             create maximum.make (Zero, Zero)
  82.             calculated := True
  83.             old_blue := blue.deep_twin
  84.             from
  85.                 i := a.lower
  86.             until
  87.                 i > a.count or l_stop
  88.             loop
  89.                 l_delta := a [i]
  90.                 if safe (l_delta) then
  91.                     impulse_blue (l_delta)
  92.                     if attached maximum as l_max
  93.                         and then l_max < l_delta
  94.                     then
  95.                         maximum := l_delta
  96.                     end
  97.                 else
  98.                     l_stop := True
  99.                     calculated := False
  100.                 end
  101.                 i := i + 1
  102.             end
  103.             if l_stop then
  104.                 blue := old_blue
  105.                 maximum := Void
  106.             end
  107.         ensure
  108.             max_not_calculated:
  109.                 not calculated implies maximum = Void
  110.             max_calculated:
  111.                   calculated
  112.                 implies
  113.                   attached maximum as max
  114.                   and then a.has (max)
  115.                   and then (across a is delta all
  116.                           max >= delta
  117.                         end)
  118.         end
  119.  
  120. feature {NONE} -- implementation
  121.  
  122.     safe (delta: TUPLE2): BOOLEAN
  123.             -- is it safe to move blue by delta?
  124.         local
  125.             new_blue: like blue
  126.         do
  127.             create new_blue.make (blue.x + delta.x, blue.y + delta.y)
  128.             Result := blue.safe (blue.x + delta.x, blue.y + delta.y)
  129.                 and new_blue /~ red and not terminated
  130.         end
  131.  
  132. invariant
  133. no_crash:
  134.     red /~ blue
  135. top_right_terminating_pocket:
  136.     pocket ~ {BALL_POINT}.t2ball (create {TUPLE2}.make_from_tuple ([Width, Length]))
  137.  
  138.     not calculated = (maximum = Void)
  139.  
  140. end -- class SNOOKER_TABLE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement