Skip to content

Instantly share code, notes, and snippets.

@sml0820
Created August 18, 2013 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sml0820/4aaa1e874ac3a82e3d05 to your computer and use it in GitHub Desktop.
Save sml0820/4aaa1e874ac3a82e3d05 to your computer and use it in GitHub Desktop.
bingo4.rb
require "color_text"
class Bingo
def initialize
#map of all places that are possible wins
@columns = [
[:a1,:a2,:a3,:a4,:a5],
[:b1,:b2,:b3,:b4,:b5],
[:c1,:c2,:c3,:c4,:c5],
[:d1,:d2,:d3,:d4,:d5],
[:e1,:e2,:e3,:e4,:e5],
[:a1,:b1,:c1,:d1,:e1],
[:a2,:b2,:c2,:d2,:e2],
[:a3,:b3,:c3,:d3,:e3],
[:a4,:b4,:c4,:d4,:e4],
[:a5,:b5,:c5,:d5,:e5],
[:a1,:b2,:c3,:d4,:e5],
[:e1,:d2,:c3,:b4,:a5]
]
@user = ' X'.red
#Get Number of Users
put_line
puts "\n RUBY BINGO".purple
print "\n How many human players? ".neon
STDOUT.flush
@users_count = gets.chomp.to_i
put_bar
#Get User's Names
@user_name = []
1.upto(@users_count) do |i|
print "\n Player #{i}, what is your name? ".neon
@user_name << gets.chomp
put_bar
end
@user_score = 0
@cpu_score = 0
start_game(@user == ' X')
end
def start_game(user_goes_first)
#bingo slots
@places = Hash.new { |hash, key| hash[key] = " " }
@places_keys = [
:a1,:a2,:a3,:a4,:a5,
:b1,:b2,:b3,:b4,:b5,
:c1,:c2,:c3,:c4,:c5,
:d1,:d2,:d3,:d4,:d5,
:e1,:e2,:e3,:e4,:e5
]
@bingo_cards = []
fill_cards(@users_count)
p @bingo_cards
user_turn
end
def pick_number(num)
#randomly pick the bingo board numbers
case num
when 0..5
rand(1..15)
when 6..10
rand(16..30)
when 11..12
rand(16..30)
when 13
"X"
when 14..15
rand(16..30)
when 16..20
rand(31..45)
when 21..25
rand(46..60)
else
0
end
end
def fill_cards(number)
#fill up each bingo card with the random numbers and put in bingo array
number.times do
@places_keys.each_with_index do |n,i|
@places[n] = pick_number(i)
end
@bingo_cards << @places.dup
end
end
def restart_game(user_goes_first)
(1...20).each { |i| put_line }
start_game(user_goes_first)
end
def put_line
puts ("-" * 80).gray
end
def put_bar
puts ("#" * 80).gray
puts ("#" * 80).gray
end
def draw_game
#draw out the bingo board for each user
puts ""
puts ""
@bingo_cards.each do |bingo|
puts " 1 #{bingo[:a1]} | #{bingo[:b1]} | #{bingo[:c1]} | #{bingo[:d1]} | #{bingo[:e1]}"
puts " --- --- --- --- ---"
puts " 2 #{bingo[:a2]} | #{bingo[:b2]} | #{bingo[:c2]} | #{bingo[:d2]} | #{bingo[:e2]}"
puts " --- --- --- --- ---"
puts " 3 #{bingo[:a3]} | #{bingo[:b3]} | #{bingo[:c3]} | #{bingo[:d3]} | #{bingo[:e3]}"
puts " --- --- --- --- ---"
puts " 4 #{bingo[:a4]} | #{bingo[:b4]} | #{bingo[:c4]} | #{bingo[:d4]} | #{bingo[:e4]}"
puts " --- --- --- --- ---"
puts " 5 #{bingo[:a5]} | #{bingo[:b5]} | #{bingo[:c5]} | #{bingo[:d5]} | #{bingo[:e5]}"
put_line
puts " Bingo Number: #{@random}".red
end
end
def times_in_column arr, item
#count the number of X's in the column to see if 5 in a row
times = 0
@bingo_cards.each do |bingo|
arr.each do |i|
times += 1 if bingo[i] == item
end
end
times
end
def user_turn
put_line
puts "\n RUBY BINGO".purple
draw_game
print "\n Please type 'go' or type 'exit' to quit: ".neon
STDOUT.flush
input = gets.chomp.downcase.to_str
put_bar
if input.length == 2
@random = rand(1..75)
puts @random
@bingo_cards.each do |bingo|
@places_keys.each do |key|
bingo[key] = @user if bingo[key] == @random
end
end
put_line
check_game(@user)
else
wrong_input unless input == :exit
end
end
def wrong_input
put_line
puts " Please type go or exit".red
user_turn
end
def check_game(next_turn)
game_over = nil
@bingo_cards.each do |bingo|
@columns.each do |column|
# see if user has won
if times_in_column(column, @user) == 5
put_line
draw_game
put_line
puts ""
puts " Game Over -- WINS!!!\n".blue
game_over = true
@user_score += 1
ask_to_play_again(true)
end
end
end
unless game_over
user_turn
end
end
def ask_to_play_again(user_goes_first)
print " Play again? (Yn): "
STDOUT.flush
response = gets.chomp.downcase
case response
when "y" then restart_game(user_goes_first)
when "yes" then restart_game(user_goes_first)
when "n" then #do nothing
when "no" then #do nothing
else ask_to_play_again(user_goes_first)
end
end
end
Bingo.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment