Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class
- PLAYER
- create
- make
- feature {NONE} -- Initialization
- make (n: STRING)
- -- Create a player with name `n'.
- require
- name_exists: n /= Void and then not n.is_empty
- do
- name := n.twin
- ensure
- name_set: name ~ n
- end
- feature -- Access
- name: STRING
- -- Player name.
- balance: INTEGER
- -- Player balance.
- position: INTEGER
- -- Current position on the board.
- feature -- Balance keeping
- set_balance (v: INTEGER)
- -- Set player-balance to `v'.
- do
- balance := v
- ensure
- balance_not_negative: balance >= 0
- end
- feature -- Moving
- set_position (pos: INTEGER)
- -- Set position to `pos'.
- do
- position := pos
- ensure
- position_set: position = pos
- end
- feature -- Basic operations
- play (d1, d2: DIE)
- -- Play a turn with dice `d1', `d2'.
- require
- dice_exist: d1 /= Void and d2 /= Void
- do
- d1.roll
- d2.roll
- set_position (position + d1.face_value + d2.face_value)
- print (name + " rolled " + d1.face_value.out + " and " + d2.face_value.out + ". Moves to " + position.out + ".%N")
- if
- position = 5 or position = 15 or position = 25 or position = 35
- then
- if
- balance >= 5
- then
- print ("This was a bad investment! " + name + " loses 5 CHF.%N")
- set_balance (balance - 5)
- print ("Remaining balance is " + balance.out + "CHF.%N")
- else
- print ("This was a bad investment! " + name + " loses " + balance.out + " CHF.%N")
- set_balance (0)
- print ("Remaining balance is " + balance.out +"CHF.%N")
- end
- elseif
- position = 10 or position = 20 or position = 30 or position = 40
- then
- print ("This was a lottery win! " + name + " gets 10 CHF.%N")
- set_balance (balance + 10)
- print ("New balance is " + balance.out + "CHF.%N")
- end
- end
- invariant
- name_exists: name /= Void and then not name.is_empty
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement