Class: BattleRoyal::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/battle_royal/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Game

Returns a new instance of Game.



10
11
12
13
14
15
# File 'lib/battle_royal/game.rb', line 10

def initialize(title)
  @title = title
  @players = [].sort
  @total_points = 0
  @toasty = []
end

Instance Attribute Details

#playersObject

Returns the value of attribute players.



9
10
11
# File 'lib/battle_royal/game.rb', line 9

def players
  @players
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/battle_royal/game.rb', line 9

def title
  @title
end

#toastyObject

Returns the value of attribute toasty.



9
10
11
# File 'lib/battle_royal/game.rb', line 9

def toasty
  @toasty
end

Instance Method Details

#add_player(player) ⇒ Object



17
18
19
# File 'lib/battle_royal/game.rb', line 17

def add_player(player)
  @players << player
end

#attack_playerObject



42
43
44
# File 'lib/battle_royal/game.rb', line 42

def attack_player
  @players.sample
end

#fatality?(player) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/battle_royal/game.rb', line 46

def fatality?(player)
  player.health < 0
end

#load_players(some_file) ⇒ Object



21
22
23
24
25
# File 'lib/battle_royal/game.rb', line 21

def load_players(some_file)
  CSV.foreach(some_file).each do |line|
    add_player(Player.from_csv(line))
  end
end

#play(rounds) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/battle_royal/game.rb', line 50

def play(rounds)
  start_of_game
  1.upto(rounds) do
    # if a block is given AND the block returns true, break out of loop.
    break if yield if block_given?
    if @players.count > 1
      @players.shuffle.each do |player|
        if !fatality?(player)
          Roll.turn(player)
          sleep(0.5)
          player.attack(attack_player, player.found_weapon(Roll.weapon(player)))
          sleep(0.5)
          player.points
        elsif fatality?(player)
          @toasty << @players.find { |x| x == player }
          puts "\n#{player.name} is no longer with us"
          @players.delete(player)
          sleep(0.5)
        end
      end
    else
      puts "\n#{@players[0].name.upcase} IS THE LAST MAN STANDING!!! "
      break
    end
  end
  @players |= @toasty
end


78
79
80
81
82
83
84
85
86
87
# File 'lib/battle_royal/game.rb', line 78

def print_player_and_health(criteria)
  criteria.sort_by(&:score).reverse_each do |player|
    puts "\n#{player.name}'s point totals: "
    player.each_found_weapon do |weapon|
      puts "#{weapon.points} total #{weapon.name} points"
    end
    puts "#{player.points} grand total points"
    puts "health: #{player.health}"
  end
end

#resultObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/battle_royal/game.rb', line 89

def result
  stronger, weaker = @players.partition(&:strong?)
  puts "\n Statistics:"

  puts "\n#{stronger.size} strong players:"
  print_player_and_health(stronger)

  puts "\n#{weaker.size} weaker players:"
  print_player_and_health(weaker)

  puts "\n#{total_points} total points for this match"
end

#save_high_scoresObject



27
28
29
30
31
32
33
34
# File 'lib/battle_royal/game.rb', line 27

def save_high_scores
  File.open("#{title}.txt", 'w') do |file|
    file.puts "#{@title} High Scores:"
    @players.sort_by(&:score).reverse_each do |player|
      file.puts sort_and_score(player)
    end
  end
end

#sort_and_score(player) ⇒ Object



109
110
111
112
# File 'lib/battle_royal/game.rb', line 109

def sort_and_score(player)
  formatted_name = player.name.ljust(20, '.')
  "#{formatted_name} #{player.score}"
end

#start_of_gameObject



36
37
38
39
40
# File 'lib/battle_royal/game.rb', line 36

def start_of_game
  puts "There are #{@players.size} players and #{WeaponChest::WEAPONS.count} weapons available in this game: "

  WeaponChest::WEAPONS.each { |x| puts "A #{x.name} is worth #{x.points} points" }
end

#total_pointsObject



114
115
116
# File 'lib/battle_royal/game.rb', line 114

def total_points
  @players.inject(0) { |sum, player| sum + player.points }
end

#winningObject



102
103
104
105
106
107
# File 'lib/battle_royal/game.rb', line 102

def winning
  puts "\nScoreboard: "
  @players.sort_by(&:score).reverse_each do |player|
    puts sort_and_score(player)
  end
end