Having difficulty checking each individual users' hash of randomly
generated numbers in an array, instead checks all
I am creating a bingo game for the ruby console in which, numbers are
randomly put into a hash for each user, comprising of the bingo board, and
each user's hash is put into an array of all "bingo boards". I am having
trouble with checking the right answers against these hashes/boards within
the array, because it checks the winning columns against all users instead
of each individual users board (hash). The first three information/methods
below are used as backup information, whereas the bottom two methods are
where the problem are. How do I switch the methods to check the winning
combinations against one hash in the bingo cards array, and if nothing
restart over on the next hash instead of combining from different cards?
The winning combinations are below
#here are the winning combinations
@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]
]
This method starts a game, and fills up a hash of random bingo numbers
into an array for each user. The array is called @bingo_cards
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)
user_turn
end
this method takes the randomly generated numbers and turns them into bingo
cards (hash) and then puts them in array (@bingo_cards)
def fill_cards(number)
number.times do
@places_keys.each_with_index do |n,i|
@places[n] = pick_number(i)
end
@bingo_cards << @places.dup
end
end
This is where the number is picked each time - @user = 'X', so it replaces
the number with X
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
Here is where I am having the problem. It does properly count the X's, but
for every user. Meaning it only works properly if one person is playing.
If two people are playing it if a user has two X's (meaning the number
came up) in a row in the the top left, and the other user has three x's in
a row in the top right it ends the game - it should only end when a user
has 5 X's, aka the random number is picked 5 times on their board.
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
This method is responsible for checking the game, alongside the method above.
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
Here is a gist for any additional needed information - I commented
everything throughout.
No comments:
Post a Comment