Skip to content

Instantly share code, notes, and snippets.

@fallengiants
Created January 11, 2013 20:55
Show Gist options
  • Select an option

  • Save fallengiants/52cb53bd5aa4f449906a to your computer and use it in GitHub Desktop.

Select an option

Save fallengiants/52cb53bd5aa4f449906a to your computer and use it in GitHub Desktop.
module PL
module Pragma
module Poker
# testing a theory, don't mind me
def self.new_deck
cards = "chsd".split(//).collect {|x| ['A','2','3','4','5','6','7','8','9','10','J','Q','K'].collect {|y| y + x}}.flatten
13.times { cards.each_index {|x| y = rand(cards.length - 1); cards[x], cards[y] = cards[y], cards[x]}}
cards
end
# pass this five cards and see wat do
def self.score_hand(*cards)
conv = {'A' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,'7' => 7,'8' => 8,'9' => 9,'10' => 10,'J' => 11,'Q' => 12,'K' => 13}
mags, suits = [], []
cards.dup.collect {|c| mags << conv[c[0..-2]]; suits << c.last}
mags = mags.sort
suits = suits.sort
oak = PL::Pragma::Poker.of_a_kind(mags)
return :royal_flush if mags == [1,10,11,12,13] && suits.uniq.length == 1
return :straight_flush if PL::Pragma::Poker.is_straight?(mags) && suits.uniq.length == 1
return :four_of_a_kind if oak.include?(4)
return :full_house if oak.select {|x| x.nonzero?}.sort == [2, 3]
return :flush if suits.uniq.length == 1
return :straight if PL::Pragma::Poker.is_straight?(mags)
return :three_of_a_kind if oak.include?(3)
return :two_pair if oak.select {|x| x == 2} == [2, 2]
return :pair unless oak.select {|x| x == 2}.empty?
return :nothing
end
def self.of_a_kind(ary)
ct = [0] * 14
ary.each {|x| ct[x.to_i] += 1}
ct
end
def self.is_straight?(ary)
a = ary.dup.sort
return true if ary == [1,10,11,12,13]
rv = nil
ary.each do |c|
return false unless rv.nil? || c == rv + 1
rv = c
end
return true
end
def self.test_poker_hand
retval = [self.new_deck[0..4]]
retval << self.score_hand(*retval[0])
retval
end
end
module PokerHelpers
def new_hand
@request.session[:poker] = Mash.new([])
@request.session[:poker][:hand] = []
@request.session[:poker][:deck] = PL::Pragma::Poker.new_deck
@request.session[:poker][:can_draw] = true
draw_hand
end
def draw_hand
while @request.session[:poker][:hand].length < 5
@request.session[:poker][:hand] << @request.session[:poker][:deck].pop
end
@request.session[:poker][:hand]
end
# discard cards at positions 0..4
def discard_and_redraw(*cards)
@request.session[:poker][:can_draw] = false
@request.session[:poker][:hand].each_with_index do |x,i|
if cards.include?(x)
@request.session[:poker][:hand][i] = @request.session[:poker][:deck].pop
end
end
@request.session[:poker][:hand].reverse!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment