Last active
January 3, 2017 01:40
-
-
Save carolineartz/c79a6d1983ed7c1752e6 to your computer and use it in GitHub Desktop.
BINGO GAME
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
# | |
#Bingo Checker Module | |
# | |
module BingoWinChecker | |
BINGO = ['X', 'X', 'X', 'X', 'X'] | |
class << self | |
attr_accessor :bingo_board | |
end | |
def self.win?(bingo_board) | |
self.bingo_board = bingo_board | |
(in_row? || in_column? || in_descending_diagonal? || in_ascending_diagonal?).tap { |winner| puts "BINGO!" if winner } | |
end | |
private | |
module_function | |
def in_row? | |
bingo_board.any? {|row| row == BINGO} | |
end | |
def in_column? | |
bingo_board.transpose.any? {|column| column == BINGO} | |
end | |
def in_descending_diagonal? | |
bingo_board.each_with_index.map {|row, index| row[index]} == BINGO | |
end | |
def in_ascending_diagonal? | |
bingo_board.each_with_index.map {|row, index| row[-1 - index]} == BINGO | |
end | |
end |
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
require 'forwardable' | |
require 'terminal-table' | |
# | |
# Bingo Card Class: A Player's Bingo Board | |
# | |
LETTERS = %w(B I N G O) | |
class BingoCard | |
attr_accessor :bingo_board | |
attr_reader :name | |
extend Forwardable | |
def initialize(player_name) | |
@bingo_board = (1..75).each_slice(15).each_with_object([]) do |range, board| | |
board << range.sample(5) | |
end | |
@name = player_name | |
end | |
def_delegators :@bingo_board, :[], :any?, :transpose, :each_with_index, :map, :map! | |
def to_s | |
table = Terminal::Table.new :headings => LETTERS, :rows => bingo_board.transpose | |
"#{name}'s Card:\n" << table.to_s + "\n\n" | |
end | |
end |
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
require_relative '1bingo_win_checker' | |
require_relative '2bingo_card' | |
# | |
# Bingo Game Class: Game Play Driving Class | |
# Initialized with [array of] BingoCard object(s) | |
# | |
class BingoGame | |
def initialize(player_cards) | |
@cards = [*player_cards] | |
@pool = LETTERS.zip((1..75).each_slice(15)).to_h | |
end | |
def play | |
round = 0 | |
winners = [] | |
loop do | |
puts self | |
call.tap do |letter_number| | |
puts "CALL: #{letter_number}\n\n" | |
update_cards(*letter_number) | |
end | |
round += 1 | |
@cards.each_with_object(winners) { |card, champs | champs << card if BingoWinChecker.win?(card) } | |
break unless winners.empty? | |
end | |
puts "After #{round} rounds we have a winner!" | |
winners.each {|winner| puts winner} | |
end | |
def to_s | |
@cards.each_with_object("") { |card, string| string << card.to_s } | |
end | |
private | |
def call | |
puts "POOL: #{@pool}" | |
letter = LETTERS.sample | |
@pool[letter].length > 0 ? Array[letter, @pool[letter].shuffle!.pop] : call | |
end | |
def update_cards(call_letter, call_number) | |
@cards.map do |card| | |
card[LETTERS.index(call_letter)].map! {|square| square == call_number ? 'X' : square} | |
end | |
end | |
end | |
my_card = BingoCard.new("Caroline") | |
your_card = BingoCard.new("Player2") | |
player_cards = [my_card, your_card] | |
game = BingoGame.new(player_cards) | |
game.play |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, im interested in using this, can you please tell me how to make it work on tampermonkey?
email me @ [email protected]