patternrubyMinor
Small Ruby Bingo Game
Viewed 0 times
gamesmallbingoruby
Problem
The code creates a Ruby bingo game for the console and works properly. I believe the code I wrote needs some work, though, as I am new to Ruby. Any help would be appreciated.
```
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 = []
@user_score = []
1.upto(@users_count) do |i|
print "\n Player #{i}, what is your name? ".neon
@user_name << gets.chomp
@user_score[i-1] = 0
put_bar
end
start_game
end
def start_game
#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)
user_turn
end
def pick_number(num)
#randomly pick the bingo board numbers
case num
when 0..4
rand(1..15)
when 5..9
rand(16..30)
when 10..11
rand(31..45)
when 12
" X".red
when 13..14
rand(31.45)
when 15..19
rand(46..60)
when 20..24
rand(61..75)
else
0
end
end
def fill_cards(number)
#fill up each bingo card with the random numbers and put in bingo array
```
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 = []
@user_score = []
1.upto(@users_count) do |i|
print "\n Player #{i}, what is your name? ".neon
@user_name << gets.chomp
@user_score[i-1] = 0
put_bar
end
start_game
end
def start_game
#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)
user_turn
end
def pick_number(num)
#randomly pick the bingo board numbers
case num
when 0..4
rand(1..15)
when 5..9
rand(16..30)
when 10..11
rand(31..45)
when 12
" X".red
when 13..14
rand(31.45)
when 15..19
rand(46..60)
when 20..24
rand(61..75)
else
0
end
end
def fill_cards(number)
#fill up each bingo card with the random numbers and put in bingo array
Solution
I think you could improve the readability of the code if you take advantage of objects. You can have at a minimum, classes for
You want to aim for something as simple as this:
Where most of the logic is hidden in the objects. Keep each step simple; for example
This further pushes specifics down to the card level, asking each card to update itself and tell you if any are a winner.
You may or may not do it exactly like this, but the point is you push the logic into lower-level objects so at any part of the code you're at a manageable level of abstraction.
BingoGame, Card, and Player. Possibly Ball too. Having a Card object alone would tuck away a lot of the code related to setting up, checking and outputting cards.You want to aim for something as simple as this:
game = BingoGame.new
players = 10
players.times do
game.add_card(Card.new)
end
until game.winner? do
wait_for_input
ball = game.draw_ball
game.update_cards(ball)
puts game #overridden to_s which prints each card
endWhere most of the logic is hidden in the objects. Keep each step simple; for example
game.update_cards() and game.winner? might be defined in the BingoGame class as:def update_cards(ball)
@cards.each do |card|
card.update(ball)
end
end
def winner?
@cards.any? {|card| card.winner?}
endThis further pushes specifics down to the card level, asking each card to update itself and tell you if any are a winner.
You may or may not do it exactly like this, but the point is you push the logic into lower-level objects so at any part of the code you're at a manageable level of abstraction.
Code Snippets
game = BingoGame.new
players = 10
players.times do
game.add_card(Card.new)
end
until game.winner? do
wait_for_input
ball = game.draw_ball
game.update_cards(ball)
puts game #overridden to_s which prints each card
enddef update_cards(ball)
@cards.each do |card|
card.update(ball)
end
end
def winner?
@cards.any? {|card| card.winner?}
endContext
StackExchange Code Review Q#29958, answer score: 5
Revisions (0)
No revisions yet.