Last active
June 28, 2023 17:13
-
-
Save labe/5521248 to your computer and use it in GitHub Desktop.
For @dhouk24, our resident gambling squirrel. (end of week 2 @ dbc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# v0.2 features | |
# (to be created): | |
# implement flashcards program as option to increase bankroll | |
# (give flashcards program ability to offer list of decks to user to choose from) | |
# implement "help" call to display rules, etc. | |
# DONE: | |
# increased payout on 2 ruby gems, increased cost of each slot pull | |
# fixed convert_result ranges (14 was allowed to be both a ruby and a seven) | |
# create Player class // DONE | |
# assign Player a saveable bankroll // DONE | |
class SlotMachine | |
attr_reader :first, :second, :third, :ruby_count, :seven_count, :balance, :player | |
SEVEN =[ | |
" ", | |
" ========, ", | |
" // ", | |
" // ", | |
" // ", | |
" // ", | |
" " | |
] | |
RUBY = [ | |
" ", | |
" _________ ", | |
"/_|_____|_\\", | |
"'. \\ / .'", | |
" '.\\ /.' ", | |
" '.' ", | |
" " | |
] | |
ZERO = [ | |
" ", | |
" ========= ", | |
"||\\\\\\|///||", | |
"||ooooooo||", | |
"||\\\\\\|///||", | |
" ========= ", | |
" " | |
] | |
SPACE = " " * 5 | |
def initialize | |
@player = Player.new("Gambling Dan") | |
end | |
def pull! | |
@player.balance -= 0.20 | |
@first = convert_result(rand(16)) | |
@second = convert_result(rand(16)) | |
@third = convert_result(rand(16)) | |
@ruby_count = 0 | |
@seven_count = 0 | |
end | |
def convert_result(value) | |
case value | |
when 0..11 then ZERO | |
when 12..14 then RUBY | |
when 15 then SEVEN | |
end | |
end | |
def format_second | |
i = -1 | |
@second.map do |line| | |
i += 1 | |
@first[i] + SPACE + line | |
end | |
end | |
def format_third | |
i = -1 | |
@third.map do |line| | |
i += 1 | |
@first[i] + SPACE + @second[i] + SPACE + line | |
end | |
end | |
def count_wins | |
slot_result = [] | |
slot_result << @first << @second << @third | |
slot_result.each do |slot| | |
@ruby_count += 1 if slot == RUBY | |
@seven_count += 1 if slot == SEVEN | |
end | |
end | |
def update_balance | |
@player.balance += 0.10 if @ruby_count == 1 | |
@player.balance += 0.30 if @ruby_count == 2 | |
@player.balance += 1.00 if @ruby_count == 3 | |
@player.balance += 0.40 if @seven_count == 1 | |
@player.balance += 1.00 if @seven_count == 2 | |
@player.balance += 2.00 if @seven_count == 3 | |
end | |
def save_balance | |
File.open('balance.txt', 'wb') { |file| file.puts @player.balance } | |
end | |
end | |
class Player | |
attr_accessor :balance | |
def initialize(name = "") | |
@name = name | |
@balance = File.read('balance.txt').to_f | |
end | |
end | |
class SlotControl | |
attr_accessor :viewer, :machine | |
def initialize | |
@viewer = SlotView.new | |
@machine = SlotMachine.new | |
@response = "" | |
end | |
def show_slot(slot) | |
viewer.move_to_home! | |
viewer.print_balance(@machine.player.balance) | |
viewer.print_slot(slot) | |
sleep(0.7) | |
end | |
def update_balance | |
machine.count_wins | |
machine.update_balance | |
viewer.print_new_balance(@machine.player.balance) | |
end | |
def play! | |
until @response == "q" | |
viewer.clear_screen! | |
machine.pull! | |
show_slot(machine.first) | |
show_slot(machine.format_second) | |
show_slot(machine.format_third) | |
update_balance | |
viewer.play_again | |
@response = gets.chomp.downcase | |
end | |
machine.save_balance | |
end | |
end | |
class SlotView | |
def move_to_home! | |
print "\e[H" | |
end | |
def clear_screen! | |
print "\e[2J" | |
end | |
def print_slot(slot) | |
slot.each { |line| puts line } | |
end | |
def print_balance(balance) | |
printf("Starting bankroll: $%.2f\n", balance) | |
end | |
def print_new_balance(balance) | |
printf("Ending bankroll: $%.2f\n", balance) | |
end | |
def print_win(winnings) | |
print "Yay, you won $" + winnings | |
end | |
def print_lose | |
print "Sorry, you did not win anything this turn." | |
end | |
def play_again | |
puts "Press ENTER to play again!" | |
puts "(or type \"q\" at any time to cash out)" | |
end | |
end | |
SlotControl.new.play! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment