ruby 宾果游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 宾果游戏相关的知识,希望对你有一定的参考价值。

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
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
#
#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

以上是关于ruby 宾果游戏的主要内容,如果未能解决你的问题,请参考以下文章

宾果出品。199元。藤原拓浩合作款 !Monogram 花纹 Eclipse涂层帆布!男女兼可用!水桶包 潮包 单肩包

简单的控制台乐透游戏 c# 匹配数组

Ruby‘s Adventrue游戏制作笔记Unity控制ruby移动

Ruby‘s Adventrue游戏制作笔记Unity控制ruby移动

从数组中获取随机数,不重复(使用Timer:)

ruby 这是Ruby中的Guess游戏,只是为了好玩,并将Head First Ruby中的Chap1作为参考:)