Created
March 28, 2016 08:27
-
-
Save shaun-sweet/b54852679aa800c06e40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BingoBoard | |
def initialize(board) | |
#initialized the game instance and maps the array to a hash to model a bingo board. | |
@bingo_board = board | |
maperino | |
end | |
def call | |
# Calls a new letter/number combo! | |
# @called_letter = "B" | |
# @called_numbers = 22 | |
@called_letter = @letters.keys.sample | |
@called_numbers = rand(1..100) | |
print "Now calling... #{@called_letter}#{@called_numbers}\n" | |
sleep(1) | |
check | |
mark | |
show | |
end | |
def inspect | |
["Bingo!", "Did you win yet?", "Feeling lucky?", "DADDY NEEDS A NEW MAC BOOK AMETUER!!!"].sample | |
end | |
def show | |
# Shows the latest version of the board with all 'X' marked off. | |
@bingo_board.each do |x| | |
print "#{x} \n" | |
end | |
inspect | |
end | |
private | |
def check | |
#checks if the letter/number combo found with the .call method exists on your board! Cross your fingers :) | |
if @called_letter == nil | |
raise ArgumentError.new("You need to call a number before you can check it!") | |
end | |
if @letters["#{@called_letter}"].include?(@called_numbers) | |
@index = @letters[@called_letter].index(@called_numbers) | |
return true | |
end | |
end | |
def mark | |
#Checks if a value exists on your board, if so replaces it with an X | |
if check == true | |
@bingo_board[@index] = @bingo_board[@index].map do |num| | |
if num == @called_numbers | |
'X' | |
else | |
num | |
end | |
end | |
maperino | |
end | |
end | |
def maperino | |
# This is a helper function to model the bingo board in a way that allows you to assign certain array indexes to a hash that represents the bingo letters at the top of the board. | |
#***This needs to be called anytime you make changes to @bingo_board in order to remodel it.*** | |
@letters = {"B" => [], "I" => [], "N" => [], "G" => [], "O" => []} | |
@bingo_board.each do |x| | |
@letters["B"] << x[0] | |
@letters["I"] << x[1] | |
@letters["N"] << x[2] | |
@letters["G"] << x[3] | |
@letters["O"] << x[4] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment