class CardGame::Game
Attributes
name[R]
players[R]
Public Class Methods
new(name)
click to toggle source
# File lib/card_game/game.rb, line 11 def initialize(name) @name = name.capitalize @players = [] @round = 20 end
Public Instance Methods
add_players(player)
click to toggle source
# File lib/card_game/game.rb, line 17 def add_players(player) @players.push(player) end
play(rounds)
click to toggle source
# File lib/card_game/game.rb, line 21 def play(rounds) 1.upto(rounds) do |round| GameTurn.turn(@players) end end
print_stats()
click to toggle source
# File lib/card_game/game.rb, line 43 def print_stats sorted_players = @players.sort sorted_players.each do |player| puts "\n" puts player puts "#{player.name}'s Deck:" sorted_deck = player.deck.sort sorted_deck .each do |card| puts "Name: #{card.name.to_s.capitalize}\t Health: #{card.health}\t Damage: #{card.damage}" end end end
save_score(to_file = "high_score.txt")
click to toggle source
# File lib/card_game/game.rb, line 27 def save_score(to_file = "high_score.txt") name = "Name".ljust(10) rank = "Rank".ljust(10) gold = "Gold".ljust(10) score = "Score".ljust(10) sorted_players = @players.sort puts "Saving high score into #{to_file}" File.open(to_file, "w") do |file| file.puts "#{@name}'s High scores" file.puts "#{name}#{rank}#{gold}#{score}" sorted_players.each do |player| file.puts "#{player.name.ljust(10)}#{String(player.rank).ljust(10)}#{String(player.gold).ljust(10)}#{String(player.score).ljust(10)}" end end end