Skip to content

Instantly share code, notes, and snippets.

@holdenhinkle
Last active January 14, 2016 00:49
Show Gist options
  • Save holdenhinkle/ddc25c866adeca08c1a1 to your computer and use it in GitHub Desktop.
Save holdenhinkle/ddc25c866adeca08c1a1 to your computer and use it in GitHub Desktop.
PowerBall Picker
require 'pry'
WHITE_BALLS = [*1..69]
POWERBALL = [*1..26]
NUM_WHITE_BALLS_NEEDED = 5
FIRST_WHITE_BALL = WHITE_BALLS.first
LAST_WHITE_BALL = WHITE_BALLS.last
FIRST_POWERBALL = POWERBALL.first
LAST_POWERBALL = POWERBALL.last
class Ball
def initialize(num)
@num = num
end
def get_num
@num
end
end
class PowerBall < Ball
def initialize(num)
super(num)
end
end
class WhiteBall < Ball
def initialize(num)
super(num)
end
end
class PowerBallPicker
attr_accessor :white_balls, :powerball
def initialize
@white_balls = []
@powerball = []
end
def play
system 'clear'
puts "Welcome to the PowerBall Picker!"
case choice
when '1'
check_if_user_picks_white_balls
computer_picks_white_balls if white_balls.size < NUM_WHITE_BALLS_NEEDED
check_if_user_picks_powerball
computer_picks_powerball if powerball.empty?
when '2'
computer_picks_white_balls
computer_picks_powerball
end
display_powerball_pick
end
def choice
puts "Make a selection by entering a number:"
puts "1 - You pick some numbers and the computer picks some."
puts "2 - Computer picks all numbers."
begin
choice = gets.chomp
puts "Sorry, I don't get that." if !%w(1 2).include?(choice)
end until %w(1 2).include?(choice)
choice
end
def check_if_user_picks_white_balls
system 'clear'
puts "You need #{NUM_WHITE_BALLS_NEEDED} white balls and 1 PowerBall."
puts "Would you like to pick any white ball numbers? (y / n)"
response = get_yes_or_no
user_picks_white_balls if response == 'y'
end
def user_picks_white_balls
system 'clear'
puts "How many white ball numbers would you like to pick? (1 - #{NUM_WHITE_BALLS_NEEDED})"
begin
how_many_nums_to_pick = gets.chomp.to_i
puts "Invalid. Try again." if ![*1..NUM_WHITE_BALLS_NEEDED].include?(how_many_nums_to_pick) || how_many_nums_to_pick >= NUM_WHITE_BALLS_NEEDED
end until [*1..NUM_WHITE_BALLS_NEEDED].include?(how_many_nums_to_pick) && how_many_nums_to_pick <= NUM_WHITE_BALLS_NEEDED
how_many_nums_to_pick.times do |iteration|
system 'clear'
puts "Pick the #{convert_num_to_word(iteration + 1)} number, #{FIRST_WHITE_BALL} - #{LAST_WHITE_BALL}:"
begin
num = gets.chomp.to_i
puts "Invalid. Try again." if !valid_pick?(num, white_balls, WHITE_BALLS)
end until valid_pick?(num, white_balls, WHITE_BALLS)
self.white_balls << WhiteBall.new(num)
end
end
def check_if_user_picks_powerball
system 'clear'
puts "Would you like to pick the PowerBall? (y / n)"
response = get_yes_or_no
user_picks_powerball if response == 'y'
end
def user_picks_powerball
puts "Pick a number, #{FIRST_POWERBALL} - #{LAST_POWERBALL}:"
begin
num = gets.chomp.to_i
puts "Invalid. Try again." if !valid_pick?(num, powerball, POWERBALL)
end until valid_pick?(num, powerball, POWERBALL)
self.powerball << PowerBall.new(num)
end
def computer_picks_white_balls
num_times = NUM_WHITE_BALLS_NEEDED - white_balls.size
num_times.times do
random_ball = WHITE_BALLS.sample
WHITE_BALLS.sample until valid_pick?(random_ball, white_balls, WHITE_BALLS)
self.white_balls << WhiteBall.new(random_ball)
end
end
def computer_picks_powerball
self.powerball = PowerBall.new(POWERBALL.sample)
end
def sort_white_balls
white_ball_array = []
white_balls.each do |ball|
white_ball_array << ball.get_num
end
white_ball_array.sort
end
def display_powerball_pick
system 'clear'
puts "Here's your Powerball Pick!"
puts "Whiteballs: #{sort_white_balls.join(", ")}"
puts "Powerball: #{powerball.get_num}"
puts ""
puts "Good Luck!"
end
def get_yes_or_no
begin
response = gets.chomp.downcase
puts "Sorry, I don't get that. Try again." if !%w(y n).include?(response)
end until %w(y n).include?(response)
response
end
def convert_num_to_word(num)
case num
when 1
"first"
when 2
"second"
when 3
"third"
when 4
"forth"
when 5
"fifth"
end
end
def valid_pick?(just_chosen_number, chosen_balls, ball_set)
ball_set.include?(just_chosen_number) && !chosen_balls.any? { |chosen_ball| chosen_ball.get_num.eql?(just_chosen_number) }
end
end
PowerBallPicker.new.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment