Skip to content

Instantly share code, notes, and snippets.

@jkarnowski
Created June 4, 2015 04:57
Show Gist options
  • Save jkarnowski/3bf41e140bd713a20cf8 to your computer and use it in GitHub Desktop.
Save jkarnowski/3bf41e140bd713a20cf8 to your computer and use it in GitHub Desktop.
# U2.W6: Create a Bingo Scorer (SOLO CHALLENGE)
# I spent 3 hours on this challenge.
# Pseudocode
#initialize the class with a Bingo Board
#method to check for winner, print "BINGO" if the board meets the conditions of one type of winning board
#horizontal board method
#
#if one subarray of the board has 'x' in each slot, BINGO
#vertical method
#if each sub array has one 'x' in the same index position, BINGO
#right-to-left method
#check first sub array for an 'x' in first index position
#second subarray for an 'x' in second index position
#third subarray for 'x' in third index position
#fourth subarray for 'x' in fourth index position and fifth subarray, 'x' in fifth and final index position
#left-to-right
#reverse logic of the right-to-left method
# sample boards
# horizontal = [[47, 44, 71, 8, 88],
# ['x', 'x', 'x', 'x', 'x'],
# [83, 85, 97, 89, 57],
# [25, 31, 96, 68, 51],
# [75, 70, 54, 80, 83]]
# vertical = [[47, 44, 71, 'x', 88],
# [22, 69, 75, 'x', 73],
# [83, 85, 97, 'x', 57],
# [25, 31, 96, 'x', 51],
# [75, 70, 54, 'x', 83]]
# right_to_left = [['x', 44, 71, 8, 88],
# [22, 'x', 75, 65, 73],
# [83, 85, 'x', 89, 57],
# [25, 31, 96, 'x', 51],
# [75, 70, 54, 80, 'x']]
# left_to_right = [[47, 44, 71, 8, 'x'],
# [22, 69, 75, 'x', 73],
# [83, 85, 'x', 89, 57],
# [25, 'x', 96, 68, 51],
# ['x', 70, 54, 80, 83]]
# Initial Solution
class BingoScorer
attr_reader :board
def initialize(board)
@board = board
@bingo = false
end
def print_winning_board
horizontal_win
vertical_win
right_to_left
left_to_right
if @bingo == true
puts "CHAMPION!"
end
end
#print "BINGO" for a nested array with 'x' in each slot
#there must be a better way to do line77
def horizontal_win
board.each do |blob|
if blob == ['x', 'x', 'x', 'x', 'x']
@bingo = true
end#if
end#board.each
end#horizontal_win
#print "WINNER" for a board with 5 vertical x's in same index slot of nested array
def vertical_win #will print all 5 x's
board.each do |row|
row.each do |element, index|
if element == 'x'
@bingo = true
end#if
end #row.each
end #board.each
end #vertical_win
#print "winner!" for diagonal right to left THIS WORKS
def right_to_left
r_index = 0
c_index = 0
board.each_with_index do |row, r_index|
row.each_with_index do |number, c_index|
if number == 'x'
r_index += 1
c_index += 1
end #if
end #row.each
end #board.each
@bingo = true
end #right_to_left
#print "winner!" for diagonal right to left THIS WORKS
def left_to_right
r_index = 0
c_index = 0
board.each_with_index do |row, r_index|
row.each_with_index do |number, c_index|
if number == 'x'
r_index += 1
c_index -= 1
end
end
end
@bingo = true
end#left_to_right
end #class
# Refactored Solution
#print WINNER if each element of a subarray includes 'x' IT WORKS
# def horizontal_win
# @horizontal.each do |subarray|
# if subarray.include?('x')
# print "WINNER"
# end
# end
# end
# DRIVER TESTS GO BELOW THIS LINE
# implement tests for each of the methods here:
#HORIZONTAL
play = BingoScorer.new([[47, 44, 71, 8, 88],
['x', 'x', 'x', 'x', 'x'],
[83, 85, 97, 89, 57],
[25, 31, 96, 68, 51],
[75, 70, 54, 80, 83]])
p play.print_winning_board
#VERTICAL
try_again = BingoScorer.new([
[47, 44, 71, 'x', 88],
[22, 69, 75, 'x', 73],
[83, 85, 97, 'x', 57],
[25, 31, 96, 'x', 51],
[75, 70, 54, 'x', 83]])
p try_again.print_winning_board
#right-to-left
diag = BingoScorer.new([['x', 44, 71, 8, 88],
[22, 'x', 75, 65, 73],
[83, 85, 'x', 89, 57],
[25, 31, 96, 'x', 51],
[75, 70, 54, 80, 'x']])
p diag.print_winning_board
play_again = BingoScorer.new(
[[47, 44, 71, 8, 'x'],
[22, 69, 75, 'x', 73],
[83, 85, 'x', 89, 57],
[25, 'x', 96, 68, 51],
['x', 70, 54, 80, 83]])
p play_again.print_winning_board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment