class BattleRoyal::Game

Attributes

players[RW]
title[RW]
toasty[RW]

Public Class Methods

new(title) click to toggle source
# File lib/battle_royal/game.rb, line 10
def initialize(title)
  @title = title
  @players = [].sort
  @total_points = 0
  @toasty = []
end

Public Instance Methods

add_player(player) click to toggle source
# File lib/battle_royal/game.rb, line 17
def add_player(player)
  @players << player
end
attack_player() click to toggle source
# File lib/battle_royal/game.rb, line 42
def attack_player
  @players.sample
end
fatality?(player) click to toggle source
# File lib/battle_royal/game.rb, line 46
def fatality?(player)
  player.health < 0
end
load_players(some_file) click to toggle source
# 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) { || ... } click to toggle source
# 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
print_player_and_health(criteria) click to toggle source
result() click to toggle source
# 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_scores() click to toggle source
# 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) click to toggle source
# 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_game() click to toggle source
# 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_points() click to toggle source
# File lib/battle_royal/game.rb, line 114
def total_points
  @players.inject(0) { |sum, player| sum + player.points }
end
winning() click to toggle source
# 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