Class: WSGame::Game
- Inherits:
-
Object
- Object
- WSGame::Game
- Defined in:
- lib/wsgame/class_game.rb
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #hight_score ⇒ Object
-
#initialize(name = "Some list") ⇒ Game
constructor
A new instance of Game.
- #load(from_file) ⇒ Object
- #play(rounds = 1) ⇒ Object
- #print_statistic(players) ⇒ Object
- #save(to_file = "my_favorite_players.csv") ⇒ Object
- #statistic ⇒ Object
- #total_treasures ⇒ Object
Constructor Details
#initialize(name = "Some list") ⇒ Game
Returns a new instance of Game.
12 13 14 15 |
# File 'lib/wsgame/class_game.rb', line 12 def initialize(name="Some list") @name=name.capitalize @players=[] end |
Instance Method Details
#add_player(player) ⇒ Object
17 18 19 |
# File 'lib/wsgame/class_game.rb', line 17 def add_player(player) @players << player end |
#hight_score ⇒ Object
85 86 87 88 89 90 |
# File 'lib/wsgame/class_game.rb', line 85 def hight_score puts "\n#{@name} Scores:" @players.sort.each do |p| puts "#{p.pname.ljust(20,'.')} #{p.score}" end end |
#load(from_file) ⇒ Object
92 93 94 95 96 |
# File 'lib/wsgame/class_game.rb', line 92 def load(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds = 1) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wsgame/class_game.rb', line 22 def play(rounds=1) greet = "Welcome!" gtime=Time.new puts "\nThe game started on #{gtime.strftime('%A %d/%m/%Y at %H:%M%p')}\n\n" puts "There are #{@players.length} players in the game #{@name}:" puts @players treasures= TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures in the game:" treasures.each do |tr| puts "#{tr.name} (#{tr.points})" end 1.upto(rounds) do |cnt| puts "\n Round #{cnt}:" @players.each do |p| GameTurn.take_turn(p) tr = TreasureTrove.random p. found_treasure(tr) #puts p end end end |
#print_statistic(players) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/wsgame/class_game.rb', line 47 def print_statistic(players) players.each do |p| puts "#{p.pname} (#{p.phealth})" end end |
#save(to_file = "my_favorite_players.csv") ⇒ Object
98 99 100 101 102 |
# File 'lib/wsgame/class_game.rb', line 98 def save(to_file="my_favorite_players.csv") File.open(to_file, "w") do |file| file.puts @players.sort.map { |p| p.to_csv } end end |
#statistic ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/wsgame/class_game.rb', line 61 def statistic puts "\n#{@name} Statistics:" @players.sort.each do |m| puts "\n#{m.pname}'s treasure points: " m.each_treasure do |treasure| puts "-- #{treasure.points} total #{treasure.name} points" end puts " Total: #{m.all_treasures} points" end puts "Total treasures piont in the Game: #{total_treasures}" strong,wimpy= @players.partition {|p| p.strong?} puts "\n #{strong.length} Strong:" print_statistic(strong) puts "\n #{wimpy.length} Wimpy:" print_statistic(wimpy) end |
#total_treasures ⇒ Object
54 55 56 57 58 |
# File 'lib/wsgame/class_game.rb', line 54 def total_treasures @players.reduce(0) do |sum, m| sum + m.all_treasures end end |