Skip to content

Instantly share code, notes, and snippets.

@koko1000ban
Created April 5, 2010 08:02
Show Gist options
  • Save koko1000ban/356133 to your computer and use it in GitHub Desktop.
Save koko1000ban/356133 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#まーじゃんのやつ
#http://www.itmedia.co.jp/enterprise/articles/1004/03/news002.html
class Hai
DEBUG = false
def initialize(s)
@table = build_table(s)
end
def get_pai
build_pai(@table).each do |k, v|
table = table_to_string(v).split(//)
if table.size == 2
if table[0] == table[1] ||
table[0].succ == table[1]
puts show(k,v)
end
else
puts show(k,v)
end
end
end
private
def show(a, b)
"#{a.map{|x| "(#{x})" }.to_s}[#{table_to_string(b)}]"
end
def build_pai(table)
@ret = {}
hoge(table)
@ret
end
def hoge(table, success=[])
log "残り:#{table_to_string(table)} 現在:#{success.inspect}"
if success.length >= 3
#役が3こできたので残りから構成
#頭を探す
log "残り: #{table_to_string(table)}"
atama = table.select { |k,v| v == 2 }.map{|k,v| k}
goals = success.sort
case atama.size
when 0
log "頭ないよ"
#候補をまた探してあれば、残り1個がまち(頭のまち)
candidates = get_candidates(table)
candidates.each do |k,v|
@ret[(goals + [k]).sort] = v
end
when 1
log "頭1個あるよ"
#残り2個がまち
copy = table.dup
copy[atama.first] -= 2
@ret[(goals + [atama.first.to_s * 2]).sort] = copy
when 2
log "頭2個アルよ"
#両方が待ち(頭、あたまの)
copy1 = table.dup
copy1[atama[0]] -= 2
@ret[(goals + [atama[0].to_s * 2]).sort] = copy1
copy2 = table.dup
copy2[atama[1]] -= 2
@ret[(goals + [atama[1].to_s * 2]).sort] = copy2
end
else
candidates = get_candidates(table)
unless candidates.empty?
candidates.each do |yaku, rest|
hoge(rest,success + [yaku])
end
end
end
end
def get_candidates(table)
candidates = {}
#順子
table.keys.sort.each do |x|
if table[x] != 0 && table[x+1] != 0 && table[x+2] != 0
tmp = x.to_s + (x+1).to_s + (x+2).to_s
copy = table.dup
copy[x] -= 1
copy[x+1] -= 1
copy[x+2] -= 1
candidates[tmp] = copy
end
end
#刻子
table.each do |key, value|
if value == 3
copy = table.dup
copy[key] -= 3
candidates[key.to_s * 3] = copy
end
end
candidates
end
def build_table(s)
s.split(//).inject(Hash.new(0)){|accum, x| accum[x.to_i] += 1; accum }
end
def table_to_string(table)
s = ""
table.keys.sort.each do |k|
s << k.to_s * table[k]
end
s
end
def log(m)
puts m if DEBUG
end
end
if __FILE__ == $0
%w{1112224588899 1122335556799 1112223335559 1223344888999 1112345678999}.each do |t|
Hai.new(t).get_pai
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment