secret
Created

bingo4.rb

  • Download Gist
bingo4.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.