Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class
- GAME
- create
- make
- feature {NONE} -- Initialization
- make (n: INTEGER)
- -- Create a game with `n' players.
- require
- n_in_bounds: Min_player_count <= n and n <= Max_player_count
- local
- i: INTEGER
- p: PLAYER
- do
- create die_1.roll
- create die_2.roll
- create players.make (1, n)
- create winners.make (1, n)
- from
- i := 1
- until
- i > players.count
- loop
- create p.make ("Player" + i.out)
- p.set_position (1)
- p.set_balance (7)
- game_over := false
- players [i] := p
- print (p.name + " joined the game.%N")
- i := i + 1
- end
- print ("All players receive 7 CHF!%N")
- print ("%N")
- end
- feature -- Basic operations
- play
- -- Start a game.
- local
- round, i: INTEGER
- do
- from
- round := 1
- print ("The game begins.%N")
- print_board
- until
- game_over = true
- loop
- print ("%NRound #" + round.out + "%N%N")
- from
- i := 1
- until
- game_over = true or else i > players.count
- loop
- players [i].play (die_1, die_2)
- if players [i].position > Square_count then
- search_winner
- game_over := true
- end
- i := i + 1
- end
- print_board
- round := round + 1
- end
- ensure
- has_winner: game_over = true
- end
- search_winner
- -- Calculates who won
- local
- c: INTEGER
- do
- create final_balances.make (1, players.count)
- from
- c := 1
- until
- c > players.count
- loop
- final_balances [c] := players [c].balance
- c := c + 1
- end
- set_highest_balance
- from
- c := 1
- until
- c > players.count
- loop
- if
- players [c].balance >= highest_balance
- then
- winners [c] := players [c].name
- end
- c := c+ 1
- end
- end
- feature -- Constants
- Min_player_count: INTEGER = 2
- -- Minimum number of players.
- Max_player_count: INTEGER = 6
- -- Maximum number of players.
- Square_count: INTEGER = 40
- -- Number of squares.
- feature -- Access
- players: V_ARRAY [PLAYER]
- -- Container for players.
- die_1: DIE
- -- The first die.
- die_2: DIE
- -- The second die.
- final_balances: V_ARRAY [INTEGER]
- -- All end-balances
- winners: V_ARRAY [STRING]
- -- All winners
- game_over: BOOLEAN
- -- Game over?
- highest_balance: INTEGER
- -- The highest balance
- feature -- Compute
- set_highest_balance
- -- Sets the highest balance
- local
- q: INTEGER
- temp: INTEGER
- do
- temp := 0
- from
- q := 1
- until
- q > players.count
- loop
- if
- final_balances [q] > temp
- then
- temp := final_balances [q]
- end
- q := q + 1
- end
- highest_balance := temp
- end
- feature {NONE} -- Implementation
- print_board
- -- Output players positions on the board.
- local
- i, j: INTEGER
- board: STRING
- do
- io.new_line
- board := "."
- board.multiply (Square_count)
- print (board)
- io.new_line
- from
- i := 1
- until
- i > players.count
- loop
- from
- j := 1
- until
- j >= players [i].position
- loop
- print (" ")
- j := j + 1
- end
- print (i)
- io.new_line
- i := i + 1
- end
- end
- invariant
- dice_exist: die_1 /= Void and die_2 /= Void
- players_exist: players /= Void
- number_of_players_consistent: Min_player_count <= players.count and players.count <= Max_player_count
- end
Advertisement
RAW Paste Data
Copied
Advertisement