Skip to content
Browse files

text is parsed from yml. using colors now

  • Loading branch information...
1 parent abdc360 commit f0487b305b59ccfec88a7065aa3cdfc39f3a45db @jwoertink committed
Showing with 231 additions and 220 deletions.
  1. +48 −79 Game.rb
  2. +136 −136 Player.rb
  3. +15 −0 agent.rb
  4. +2 −2 dopewars.rb
  5. +16 −0 text.yml
  6. +14 −3 utilities.rb
View
127 Game.rb
@@ -2,94 +2,63 @@ class Game
LOCATIONS = ["Las Vegas", "New York", "Miami", "Amsterdam", "Frankfurt", "El-Jazier"]
- # def self.agent?
- # rand(100) > 50
- # end
+ def initialize(defaults = {})
+ defaults.each_pair { |key,value| instance_variable_set("@#{key}", value) }
+ end
+
+ def playable?
+ @playable
+ end
+ def game_over?
+ @days <= 0 or !playable?
+ end
- # 0 - 300 you're safe, no agents
- # 301 - 600 1 agent has spotted you, but you can run
- # 601 - 900 2 agents have spotted you, but you can fight, or run
- # 901 - 1200 you're being chased, can't run, can fight but will probably lose
- # 1201 - 1500 you're caught, no chance to win.
- # def self.random_agent
- # chance = (rand(100) * 30) / 2 #mix it up a bit to make it more random
- # case chance
- # when (0..300)
- # puts "There are no agents following you, but watch your back."
- # when (301..600)
- # puts "An agent has spotted you, but you ran away."
- # when (601..900)
- # puts "Two agents are hot on your trail."
- # puts "You run from one, but you had to unleash your glok on the other one."
- # when (901..1200)
- # puts "The agent that has been following you wants to make a deal."
- # puts "You lose $500.00"
- # when (1201..1500)
- # puts "You have been caught, there is no way out."
- # puts "Sorry, game over."
- # game_over
- # end
- # end
+ def days_remaining
+ @days - @current_day
+ end
- def initialize(defaults = {})
- defaults.each_pair { |key,value| instance_variable_set("@#{key}", value) }
+ #Maybe this should be moved?
+ def select_menu(menu_option)
+ case menu_option
+ when 1
+ @player.buy_drugs
+ when 2
+ @player.sell_drugs
+ when 3
+ @player.fly_to_city
+ when 4
+ @player.go_to_bank
+ when 5
+ @player.check_stats
+ else
+ echo(game_text(:bad_selection), :red)
+ echo(game_text(:main_menu), :blue, 0)
+ menu_option = ask("Select your option:")
+ select_menu(menu_option)
+ end
end
+ #Main game loop
def start!
- puts "starting game"
- puts "Player: #{@player.name}"
-
-
- # if answer.downcase.eql?('s')
- # puts "Welcome new drug dealer, what is your name?"
- # name = gets.chomp
- # @player = Player.new(name)
- # puts "Nice to meet you #{@player.name}. Here is $500.00 and some weed to get you started."
- # sleep 2
- # @player.add_to_drugs({"weed" => 5})
- # @player.funds += 500
- # puts "Now that you have that, there is one more thing you will need."
- # sleep 2
- # puts "Here is a special plane ticket, and boarding pass."
- # sleep 2
- # puts "This will allow you to travel to one of 6 special cities."
- # sleep 2
- # puts "You may also need this bag to carry stuff in. You have 30 days."
- # sleep 3
- # while @player.days_remaining > 0
- # puts "What would you like to do?"
- # puts "1. Buy drugs\n2. Sell drugs\n3. Fly to a new city\n4. Go to the bank.\n5. Check stats."
- # puts "Select your option:"
- # option = gets.chomp
- # case option
- # when 1.to_s
- # @player.buy_drugs
- # when 2.to_s
- # @player.sell_drugs
- # when 3.to_s
- # @player.fly_to_city
- # when 4.to_s
- # @player.go_to_bank
- # when 5.to_s
- # @player.check_stats
- # else
- # puts "You must select a number 1 through 4"
- # end
- #
- # @player.days_remaining -= 1
- # @player.bank_account.add_daily_interest
- # if Game.agent?
- # Game.random_agent
- # end
- # end
- #
- # puts "You've done it. You're now gangsta!"
- # end
+ echo(game_text(:welcome, {:name => @player.name, :amount => @player.wallet, :drug => @player.drugs.keys.first}), :green)
+ echo(game_text(:dealer_introduction), :green)
+ echo("You have #{days_remaining} Days Remaining", :yellow, 1)
+ while days_remaining > 0
+ echo(game_text(:main_menu), :blue, 0)
+ menu_option = ask("Select your option:")
+ select_menu(menu_option)
+ @player.bank_account.add_daily_interest
+ agent = Agent.new if Agent.available?
+ echo("You have #{days_remaining} Days Remaining", :yellow)
+ @current_day += 1
+ break if game_over?
+ end
+ finish!
end
- def end!
+ def finish!
echo("Game Over", :red, 0)
exit
end
View
272 Player.rb
@@ -1,6 +1,6 @@
class Player
#rename funds to wallet
- attr_accessor :name, :drugs, :funds, :current_location, :days_remaining, :bank_account
+ attr_accessor :name, :drugs, :wallet, :current_location, :days_remaining, :bank_account
def initialize(defaults = {})
@name = defaults[:name]
@@ -9,140 +9,140 @@ def initialize(defaults = {})
@bank_account = Bank.new
end
- # # you can buy the drug if you have enough money for the qty of that drug
- # def can_buy_drug?(price, qty)
- # self.funds > (price * qty)
- # end
- #
- # # you can sell the drug if there is a market for it on the streets
- # def can_sell_drug?(drug)
- # drug.can_be_sold?
- # end
- #
- # def add_to_drugs(drugs)
- # drugs.each_pair do |drug, amount|
- # if @drugs.has_key?(drug)
- # @drugs[drug] += drugs[drug]
- # else
- # @drugs.update({drug => amount})
- # end
- # end
- # end
- #
- # def remove_from_drugs(drugs)
- # drugs.each_pair do |drug, amount|
- # @drugs[drug] -= drugs[drug]
- # end
- # end
- #
- # def buy_drugs
- # puts "Select the number of the drug you want to buy."
- # Drug::TYPES.each_with_index do |drug, select_number|
- # @price = Drug.display_price
- # max_amount = @funds / @price
- # puts "#{select_number + 1}. #{drug} @ $#{@price} {#{max_amount}}\n"
- # end
- # drug_choice = gets.chomp
- # drug = Drug::TYPES[drug_choice.to_i - 1]
- # puts "How many?"
- # drug_amount = gets.chomp
- # decrease = (@price * drug_amount.to_i)
- # if self.can_buy_drug?(@price, drug_amount.to_i)
- # self.add_to_drugs({drug => drug_amount.to_i})
- # @funds -= decrease
- # else
- # puts "You can't buy that many."
- # end
- # end
- #
- # def sell_drugs
- # puts "Which of your drugs would you like to sell?"
- # i = 0
- # @drugs.each_pair do |drug, amount|
- # @price = Drug.display_price
- # puts "#{i + 1}. #{drug} x #{amount} @ $#{@price}.00ea.\n"
- # i +=1
- # end
- # drug_choice = gets.chomp
- # drug = @drugs.keys[i - 1]
- # puts "How many?"
- # drug_amount = gets.chomp
- # if drug_amount.to_i <= @drugs[drug]
- # self.remove_from_drugs({drug => drug_amount.to_i})
- # increase = @price * drug_amount.to_i
- # @funds += increase
- # else
- # puts "You can't sell more then #{@drugs[drug]} of #{drug}."
- # end
- # end
- #
- # def fly_to_city
- # puts "Select the number of the city you want to fly to."
- # Game::LOCATIONS.each_with_index do |city, select_number|
- # next if current_location.eql?(city)
- # puts "#{select_number + 1}. #{city}\n"
- # end
- # answer = gets.chomp
- # self.current_location = Game::LOCATIONS[answer.to_i - 1]
- # @days_remaining -= 1
- # end
- #
- # def go_to_bank
- # puts "*****************"
- # puts "***** BANK ******"
- # puts "*****************"
- # puts "Welcome to the swiss bank exchange."
- # puts "Choose an option."
- # puts "1. Take out a loan."
- # puts "2. Put money in savings."
- # puts "3. Leave."
- # answer = gets.chomp
- # loop do
- # case answer
- # when "1"
- # puts "How much?"
- # amount = gets.chomp
- # @funds += amount.to_i
- # @bank_account.increase_loan(amount.to_i)
- # break
- # when "2"
- # puts "How much?"
- # amount = gets.chomp
- # if amount.to_i > @funds
- # puts "Sorry, you don't have that much money."
- # else
- # @funds -= amount.to_i
- # @bank_account.increase_savings(amount.to_i)
- # break
- # end
- # when "3"
- # puts "Goodbye."
- # break
- # else
- # puts "You have to select option 1, 2 or 3"
- # answer = gets.chomp
- # end
- # end
- # end
- #
- # def check_stats
- # puts "**********************"
- # puts "******* STATS ********"
- # puts "**********************"
- # puts "Stats for #{@name}"
- # puts "Drugs:"
- # @drugs.each { |k,v| puts "-#{k} x #{v}" }
- # puts "Wallet: $#{@funds}.00"
- # puts "Current Location: #{@current_location}"
- # puts "Days remaining: #{@days_remaining}"
- # puts "Total savings: #{@bank_account.savings_account}"
- # puts "Total loans: #{@bank_account.loan_amount}"
- # end
- #
- # def fight
- # end
- #
- # def run
- # end
+ # you can buy the drug if you have enough money for the qty of that drug
+ def can_buy_drug?(price, qty)
+ self.funds > (price * qty)
+ end
+
+ # you can sell the drug if there is a market for it on the streets
+ def can_sell_drug?(drug)
+ drug.can_be_sold?
+ end
+
+ def add_to_drugs(drugs)
+ drugs.each_pair do |drug, amount|
+ if @drugs.has_key?(drug)
+ @drugs[drug] += drugs[drug]
+ else
+ @drugs.update({drug => amount})
+ end
+ end
+ end
+
+ def remove_from_drugs(drugs)
+ drugs.each_pair do |drug, amount|
+ @drugs[drug] -= drugs[drug]
+ end
+ end
+
+ def buy_drugs
+ puts "Select the number of the drug you want to buy."
+ Drug::TYPES.each_with_index do |drug, select_number|
+ @price = Drug.display_price
+ max_amount = @funds / @price
+ puts "#{select_number + 1}. #{drug} @ $#{@price} {#{max_amount}}\n"
+ end
+ drug_choice = gets.chomp
+ drug = Drug::TYPES[drug_choice.to_i - 1]
+ puts "How many?"
+ drug_amount = gets.chomp
+ decrease = (@price * drug_amount.to_i)
+ if self.can_buy_drug?(@price, drug_amount.to_i)
+ self.add_to_drugs({drug => drug_amount.to_i})
+ @funds -= decrease
+ else
+ puts "You can't buy that many."
+ end
+ end
+
+ def sell_drugs
+ puts "Which of your drugs would you like to sell?"
+ i = 0
+ @drugs.each_pair do |drug, amount|
+ @price = Drug.display_price
+ puts "#{i + 1}. #{drug} x #{amount} @ $#{@price}.00ea.\n"
+ i +=1
+ end
+ drug_choice = gets.chomp
+ drug = @drugs.keys[i - 1]
+ puts "How many?"
+ drug_amount = gets.chomp
+ if drug_amount.to_i <= @drugs[drug]
+ self.remove_from_drugs({drug => drug_amount.to_i})
+ increase = @price * drug_amount.to_i
+ @funds += increase
+ else
+ puts "You can't sell more then #{@drugs[drug]} of #{drug}."
+ end
+ end
+
+ def fly_to_city
+ puts "Select the number of the city you want to fly to."
+ Game::LOCATIONS.each_with_index do |city, select_number|
+ next if current_location.eql?(city)
+ puts "#{select_number + 1}. #{city}\n"
+ end
+ answer = gets.chomp
+ self.current_location = Game::LOCATIONS[answer.to_i - 1]
+ @days_remaining -= 1
+ end
+
+ def go_to_bank
+ puts "*****************"
+ puts "***** BANK ******"
+ puts "*****************"
+ puts "Welcome to the swiss bank exchange."
+ puts "Choose an option."
+ puts "1. Take out a loan."
+ puts "2. Put money in savings."
+ puts "3. Leave."
+ answer = gets.chomp
+ loop do
+ case answer
+ when "1"
+ puts "How much?"
+ amount = gets.chomp
+ @funds += amount.to_i
+ @bank_account.increase_loan(amount.to_i)
+ break
+ when "2"
+ puts "How much?"
+ amount = gets.chomp
+ if amount.to_i > @funds
+ puts "Sorry, you don't have that much money."
+ else
+ @funds -= amount.to_i
+ @bank_account.increase_savings(amount.to_i)
+ break
+ end
+ when "3"
+ puts "Goodbye."
+ break
+ else
+ puts "You have to select option 1, 2 or 3"
+ answer = gets.chomp
+ end
+ end
+ end
+
+ def check_stats
+ puts "**********************"
+ puts "******* STATS ********"
+ puts "**********************"
+ puts "Stats for #{@name}"
+ puts "Drugs:"
+ @drugs.each { |k,v| puts "-#{k} x #{v}" }
+ puts "Wallet: $#{@funds}.00"
+ puts "Current Location: #{@current_location}"
+ puts "Days remaining: #{@days_remaining}"
+ puts "Total savings: #{@bank_account.savings_account}"
+ puts "Total loans: #{@bank_account.loan_amount}"
+ end
+
+ def fight
+ end
+
+ def run
+ end
end
View
15 agent.rb
@@ -0,0 +1,15 @@
+class Agent
+
+ class << self
+
+ def available?
+ rand(100) % 6 > 3
+ end
+
+ end
+
+ def initialize
+
+ end
+
+end
View
4 dopewars.rb
@@ -2,7 +2,7 @@
require 'bundler'
Bundler.require
require 'highline/import'
-Dir["#{File.expand_path(File.join(File.dirname(__FILE__)))}/*.rb"].each { |file| require file unless file.eql?(File.expand_path(__FILE__)) }
+Dir["#{File.expand_path(File.join(File.dirname(__FILE__), "*.rb"))}"].each { |file| require file unless file.eql?(File.expand_path(__FILE__)) }
include Utilities
@@ -10,7 +10,7 @@
key = ask("[S]tart a new game or [Q]uit?") { |q| q.echo = true }
if key.downcase.eql?('s')
- player_name = ask("What is your name?")
+ player_name = ask(game_text(:greeting))
player_defaults = {name: player_name, wallet: 500, drugs: {weed: 5}}
game = Game.new(game_defaults.merge(player: Player.new(player_defaults)))
game.start!
View
16 text.yml
@@ -0,0 +1,16 @@
+game:
+ greeting: Greetings young dealer. What shall I call you?
+ welcome: "Nice to meet you, %{name}. Here is $%{amount}.00 and some %{drug} to get you started."
+ dealer_introduction:
+ "Now that you have that, there is one more thing you will need.\n
+ Here is a plane ticket, and boarding pass good for 30 days.\n
+ This will allow you to travel to one of 6 specific cities\n
+ You will also need this bag to carry stuff in."
+ main_menu:
+ "What would you like to do?\n
+ 1. Buy drugs\n
+ 2. Sell drugs\n
+ 3. Fly to a new city\n
+ 4. Go to the bank.\n
+ 5. Check stats."
+ bad_selection: "You must choose a menu option"
View
17 utilities.rb
@@ -1,3 +1,5 @@
+require 'yaml'
+
module Utilities
GAME_TITLE = <<-MSG
@@ -7,15 +9,24 @@ module Utilities
******************
MSG
- def echo(message, colour, wait_time = 2)
+ def echo(message, colour = :white, wait_time = 2)
const = ::HighLine.const_get(colour.to_s.upcase)
- puts const
say("<%= color('#{message}', '#{const}') %>")
sleep wait_time
end
def game_defaults
- {:days => 30, :starting_location => Game::LOCATIONS.sort_by { rand }.first}
+ {playable: true, days: 30, current_day: 1, starting_location: Game::LOCATIONS.sort_by { rand }.first}
+ end
+
+ def game_text(key, vars = {})
+ @yml ||= YAML::load(File.open(File.expand_path(File.join(File.dirname(__FILE__), "text.yml"))))["game"]
+ unless vars.empty?
+ vars.keys.each do |k|
+ @yml[key.to_s].gsub!("%{#{k}}", "#{vars[k]}")
+ end
+ end
+ @yml[key.to_s]
end
end

0 comments on commit f0487b3

Please sign in to comment.
Something went wrong with that request. Please try again.