«前の日記(2003-06-01) 最新 次の日記(2003-06-06)» 編集

fukumoto blog


2003-06-03 [長年日記]

_ [麻雀][PC]comjong.comを更新しよう

さすがにいろんな人から怒られることが多くなってきたのでいいかげん comjong.comの更新をしようかと。

前に書いたソースを見たら、手牌の先読みルーチンを書いてて、あまりの動作の遅さに投げ出したところで止まってました。なので、高速化するべくキャッシュ化をしてみたのでその覚え書き。断片コードだけなんだけど、雰囲気ね。

手牌を解析するときに、

def combination( t )
    max = {}; max.default = 0
 
    s = separate(t)
    return max if s == []
 
    if s.size > 1
      a = []
      s.each{|i| a << combination(i)}
      return merge_combination(a)
    else
      b = s.first
    end
 
    b.shift while b.first == 0
    b.pop while b.last == 0
 
    return @@cache[b].dup if @@cache[b] != nil
 
    c = {}; c.default = 0
    for i in 0...b.size
      # sequence
      if b[i+2] != nil and b[i] >= 1 and b[i+1] >= 1 and b[i+2] >= 1
        t = b.dup; t[i] -= 1; t[i+1] -= 1; t[i+2] -= 1
        c = combination(t)
        c["sets"] += 1;
        c["headed_sets"] = if c["headed_pairs"] == 0 then 0 else c["headed_sets"] + 1 end
        max = max_combination( c, max )
      end
 
      # triplet
      if b[i] >= 3
        t = b.dup; t[i] -= 3
        c = combination(t)
        c["sets"] += 1;
        c["headed_sets"] = if c["headed_pairs"] == 0 then 0 else c["headed_sets"] + 1 end
        max = max_combination( c, max )
      end
      ....

_ な感じにして、今までの結果を最大限に生かしつつ、そこからの差分だけ読むようにしてやればいい感じかも。ちなみに [3, 1, 1, 1, 0, 1] を読ませたときのキャッシュの中身はこんな感じになります (Hash の default は 0 ね) 。

{[3, 1, 1]=>{"headed_pairs"=>1, "sets"=>1, "pairs"=>1, "headed_sets"=>1},
 [2, 1]=>{"headed_pairs"=>1, "pairs"=>1},
 [1, 1, 1, 0, 1]=>{"headed_pairs"=>0, "sets"=>1, "pairs"=>0},
 [1]=>{},
 [1, 1, 1, 1, 0, 1]=>{"headed_pairs"=>0, "sets"=>1, "pairs"=>1},
 [2, 1, 0, 1, 0, 1]=>{"headed_pairs"=>2, "pairs"=>2},
 [2, 0, 1]=>{"headed_pairs"=>1, "pairs"=>1},
 [1, 1]=>{"headed_pairs"=>0, "pairs"=>1},
 [3, 1]=>{"headed_pairs"=>2, "sets"=>1, "pairs"=>0, "headed_sets"=>0},
 [3, 0, 1]=>{"headed_pairs"=>2, "sets"=>1, "pairs"=>0, "headed_sets"=>0},
 [3]=>{"headed_pairs"=>1, "sets"=>1, "pairs"=>0},
 [3, 1, 1, 1, 0, 1]=>{"headed_pairs"=>2, "sets"=>2, "pairs"=>0, "headed_sets"=>1},
 [1, 0, 1, 0, 1]=>{"headed_pairs"=>0, "pairs"=>1},
 [1, 1, 1]=>{"headed_pairs"=>0, "sets"=>1, "pairs"=>0},
 [2, 0, 1, 1, 0, 1]=>{"headed_pairs"=>2, "pairs"=>2},
 [2]=>{"headed_pairs"=>1, "pairs"=>1},
 [1, 1, 0, 1]=>{"headed_pairs"=>0, "pairs"=>1},
 [1, 0, 1]=>{"headed_pairs"=>0, "pairs"=>1}}
[]

ToDo/ToBuy

2003|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|

fukumoto@24d | antenna | comjong.com