Advertisement
PhilDrummer

Assignment_7 04 PLAYER.e

Nov 19th, 2014
3,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 1.87 KB | None | 0 0
  1. class
  2.     PLAYER
  3.  
  4. create
  5.     make
  6.  
  7. feature {NONE} -- Initialization
  8.  
  9.     make (n: STRING)
  10.             -- Create a player with name `n'.
  11.         require
  12.             name_exists: n /= Void and then not n.is_empty
  13.         do
  14.             name := n.twin
  15.         ensure
  16.             name_set: name ~ n
  17.         end
  18.  
  19. feature -- Access
  20.  
  21.     name: STRING
  22.             -- Player name.
  23.  
  24.     balance: INTEGER
  25.             -- Player balance.
  26.  
  27.     position: INTEGER
  28.             -- Current position on the board.
  29.  
  30. feature -- Balance keeping
  31.  
  32.     set_balance (v: INTEGER)
  33.             -- Set player-balance to `v'.
  34.         do
  35.             balance := v
  36.         ensure
  37.             balance_not_negative: balance >= 0
  38.         end
  39.  
  40. feature -- Moving
  41.  
  42.     set_position (pos: INTEGER)
  43.             -- Set position to `pos'.
  44.         do
  45.             position := pos
  46.         ensure
  47.             position_set: position = pos
  48.         end
  49.  
  50. feature -- Basic operations
  51.  
  52.     play (d1, d2: DIE)
  53.             -- Play a turn with dice `d1', `d2'.
  54.         require
  55.             dice_exist: d1 /= Void and d2 /= Void
  56.         do
  57.             d1.roll
  58.             d2.roll
  59.             set_position (position + d1.face_value + d2.face_value)
  60.             print (name + " rolled " + d1.face_value.out + " and " + d2.face_value.out + ". Moves to " + position.out + ".%N")
  61.             if
  62.                 position = 5 or position = 15 or position = 25 or position = 35
  63.             then
  64.                 if
  65.                     balance >= 5
  66.                 then
  67.                     print ("This was a bad investment! " + name + " loses 5 CHF.%N")
  68.                     set_balance (balance - 5)
  69.                     print ("Remaining balance is " + balance.out + "CHF.%N")
  70.                 else
  71.                     print ("This was a bad investment! " + name + " loses " + balance.out + " CHF.%N")
  72.                     set_balance (0)
  73.                     print ("Remaining balance is " + balance.out +"CHF.%N")
  74.                 end
  75.             elseif
  76.                 position = 10 or position = 20 or position = 30 or position = 40
  77.             then
  78.                 print ("This was a lottery win! " + name + " gets 10 CHF.%N")
  79.                 set_balance (balance + 10)
  80.                 print ("New balance is " + balance.out + "CHF.%N")
  81.             end
  82.         end
  83.  
  84. invariant
  85.     name_exists: name /= Void and then not name.is_empty
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement