0
0

More than 1 year has passed since last update.

Ruby 麻雀 配牌のシャンテン数をカウントしたい②

Last updated at Posted at 2022-11-12

まだ完成までは到達していませんが、まず一部の目処が立ったので、アウトプットとなります。
その点はご了承ください。

今回の進捗

前回作成した配牌の種類カウンターを用いて、面子の判定を行うコードを作成しました。
もう少し上手い書き方はいくらでもあるかと思いますが、なかなかRubyでコード公開している人いないので、誰か拾い上げてもらえればといった希望も持ってます・・・

コード紹介

j = 0
    while j < 9 do
      if j <= 6
        if mcntr[j] == 2 && (mcntr[j+1] <= 1 && mcntr[j+2] <= 1)
          jantocnt += 1
          mcntr[j] -= 2
        end
        if pcntr[j] == 2 && (pcntr[j+1] <= 1 && pcntr[j+2] <= 1)
          jantocnt += 1
          pcntr[j] -= 2
        end
        if scntr[j] == 2 && (scntr[j+1] <= 1 && scntr[j+2] <= 1)
          jantocnt += 1
          scntr[j] -= 2
        end
        if zcntr[j] == 2
            jantocnt += 1
            zcntr[j] -= 2
        end
      end
      if j >= 7
        if mcntr[j] == 2
          jantocnt += 1
          mcntr[j] -= 2
        end
        if pcntr[j] == 2
          jantocnt += 1
          pcntr[j] -= 2
        end
        if scntr[j] == 2
          jantocnt += 1
          scntr[j] -= 2
        end
        if zcntr[j] == 2
            jantocnt += 1
            zcntr[j] -= 2
        end

      end
      j += 1
    end
  
    # 刻子抜き取り
    k = 0
    while k < 9 do
      if mcntr[k] >= 3
        kotsucnt += 1
        mcntr[k] -= 2
      end
      if pcntr[k] >= 3
        kotsucnt += 1
        pcntr[k] -= 2
      end
      if scntr[k] >= 3
        kotsucnt += 1
        scntr[k] -= 2
      end
      if k < 6
        if zcntr[k] >= 3
          kotsucnt += 1
          zcntr[k] -= 2
        end
      end
      k += 1
    end
  
    # 順子抜き取り
    s = 0
    while s < 7 do
      if mcntr[s] == 1 && mcntr[s+1] == 1 && mcntr[s+2] == 1
        shuntsucnt += 1
        mcntr[s] -= 2
      end
      if pcntr[s] == 1 && pcntr[s+1] == 1 && pcntr[s+2] == 1
        shuntsucnt += 1
        pcntr[s] -= 2
      end
      if scntr[s] == 1 && scntr[s+1] == 1 && scntr[s+2] == 1
        shuntsucnt += 1
        scntr[s] -= 2
      end
      s += 1
    end

    @now_shanten = minishanten - ((kotsucnt * 2) + (shuntsucnt * 2) + jantocnt)
  end

説明

前回作成した部分は割愛します。
どの面子から取得をするかがまずアルゴリズムを理解しなければいけないとのことでいろんな記事を見ましたが足りない頭ではやりたい数式をかろうじて理解できても、コードへの反映は無理と判断したため、麻雀の経験値をもとに雀頭 > 刻子 > 順子 > 塔子 > 余剰 と考えました。
なのでその優先順位に沿って、各牌の種類ごとに面子をカウントしながら以降のカウンターで重複しないように数値も抜き取ってあげるような構図です。

結果と問題点

まず結果の前に塔子をカウントする記述はしていませんので、正確な数値は出てきませんが、、、
以下のように数値は出せるようになりました!e6d05db9078c62782dfdebf01bb93948.png

はい!全然あってません!そりゃそうなんですが、、、
まぁ進歩があったということで、まずは良しとします。
やることによって色々問題点も見えてきて
・塔子ごとの優先順位は?
・一盃口や変則待ちのケアは?
・チートイツや国士無双の方がシャンテン数が近い場合は?
などなど・・・

まとめ

もっとコンパクトにできるでしょ!とか効率悪いとか色々あるかと思いますが
前進あるのみということで、また進んだらアウトプットさせていただきます!

前回

・Ruby 麻雀 配牌のシャンテン数をカウントしたい①
https://qiita.com/tatsuya312/items/1527cff41686ad973d80

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0