7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby 麻雀の配牌を作成する

Posted at

RUbyで麻雀アプリを作成するために勉強中

まずは簡単なところから作成します。
麻雀牌34種136枚から14枚をランダムに抜き取って表示します。

コード紹介

majons_controller.rb
class MajonsController < ApplicationController
  def index
    majon_pais = ["m1", "m1", "m1", "m1", "m2", "m2", "m2", "m2", "m3", "m3", "m3", "m3", "m4", "m4", "m4", "m4", "m5", "m5", "m5", "m5", "m6", "m6", "m6", "m6", "m7", "m7", "m7", "m7", "m8", "m8", "m8", "m8", "m9", "m9", "m9", "m9",
      "p1", "p1", "p1", "p1", "p2", "p2", "p2", "p2", "p3", "p3", "p3", "p3", "p4", "p4", "p4", "p4", "p5", "p5", "p5", "p5", "p6", "p6", "p6", "p6", "p7", "p7", "p7", "p7", "p8", "p8", "p8", "p8", "p9", "p9", "p9", "p9", 
      "s1", "s1", "s1", "s1", "s2", "s2", "s2", "s2", "s3", "s3", "s3", "s3", "s4", "s4", "s4", "s4", "s5", "s5", "s5", "s5", "s6", "s6", "s6", "s6", "s7", "s7", "s7", "s7", "s8", "s8", "s8", "s8", "s9", "s9", "s9", "s9", 
      "z1", "z1", "z1", "z1", "z2", "z2", "z2", "z2", "z3", "z3", "z3", "z3", "z4", "z4", "z4", "z4", "z5", "z5", "z5", "z5", "z6", "z6", "z6", "z6", "z7", "z7", "z7", "z7"]
    haipai = majon_pais.sample(14)
    @ripai = haipai.sort
  end
end

haipaiの変数にsampleメソッドで14個ランダムにデータを抜き取るようにする。
@ripaiにhaipaiにsortメソッドを用いて数値順に並び替えられるようにする。
各データの名称は表示する画像に合わせているが、英字(例:萬子=m)からスタートした名称にすることによってソートできるようにしています。

index.html.erb
<% @ripai.each do |pai| %>
  <%= image_tag "#{pai}.gif", class:"#" %>
<% end %>

結果

各牌の画像をasset/imagesに配置すると
image.png

こんな感じで表示できるようになりました!!
(天和できないかなと思い何回かリロードしましたが、、、)

コントローラーに配列を直に置いたり、シャンテン数とかもまだ表示できるようにしていませんが
それはこれから作成していきます!

まとめ

簡単な内容で申し訳ないですが、似たようなこと考えている人いればぜひ参考にしてください!
また何かアドバイス等あればお願いします!

7
4
6

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?