#==============================================================================
# ■ Poker_Game vol.3.36
#------------------------------------------------------------------------------
# ※『Game_Party_Coin』(必須)
# ※『Window_digits_number』(必須)
# ※『Coin_Game_Base』(必須)より下に挿入
# 11/09/10
#==============================================================================
module HIDE_POKER
  #==============================================================================
  # ■ デバッグ(開発者用)
  #==============================================================================
  DEBUG = false                    # [true/false]
  DEBUG_CARD = {}                  # {0=>1,1=>15,2=>29,3=>43,4=>56}
  #==============================================================================
  # ■ ゲーム設定
  #==============================================================================
  FIVE_JOKER = false               # ジョーカーを5枚使うフラグ
  SPEED = 2                        # カードのめくり速度(大=遅)
  #==============================================================================
  # ■ グラフィック設定 ※Windowskinsフォルダ内のファイル名を指定
  #==============================================================================
  BACK = "poker_background"        # ポーカーの背景画像
  CARD_S = "card0"                 # カード画像(スペード)
  CARD_D = "card1"                 # カード画像(ダイヤ)
  CARD_C = "card2"                 # カード画像(クラブ)
  CARD_H = "card3"                 # カード画像(ハート)
  CARD_J = "card4"                 # カード画像(ジョーカー/表/影)
  SELECT = "poker_back"            # カーソル画像
  POKER_HIT = "poker_hit"          # 当たり効果画像
  #==============================================================================
  # ■ 選択肢とヘルプの設定
  #==============================================================================
  GAME_COMS = ["Start", "End"]
  GAME_HELPS = ["ゲームを開始します。", "ゲームを終了します。"]
  CHANGE_COMS = ["Draw","Raise","Fold"]
  CHANGE_HELPS = ["現在のカードで勝負します。",
  "賭け金を倍に増やします。",
  "賭け金の半額を払い、ゲームを降ります。"]
  CARD_COMS = ["Card1","Card2","Card3","Card4","Card5"]
  CARD_HELPS = ["Card1を交換します。",
  "Card2を交換します。",
  "Card3を交換します。",
  "Card4を交換します。",
  "Card5を交換します。"]
  #==============================================================================
  # ■ 音の設定 ※ファイル名を指定 ※Audioフォルダ内 #()のファイル名
  #==============================================================================
  BGM = "019-Field02"              # ポーカー実行中のBGM(BGM)
  CHENGE_SE = "046-Book01"         # カードめくりの効果音(SE)
  DECISION_SE = "002-System02"     # 決定キーの効果音(SE)
  CANCEL_SE = "003-System03"       # キャンセルキーの効果音(SE)
  BUZZER_SE = "004-System04"       # ブザー音の効果音(SE)
  HIT_ME_MAX = "009-Fanfare03"     # 大当りの効果音(ME)
  HIT_ME = "007-Fanfare01"         # 当りの効果音(ME)
  
  ME_MAX_TIME = 16                 # 大当りの待ち時間(HIT_ME_MAXの演奏時間(秒))
  ME_TIME = 4                      # 当りの待ち時間(HIT_MEの演奏時間(秒))
  #==============================================================================
  # ■ ポーカータイプの設定(COIN,RATE,ME_RATE_MAX)
  # COIN[n] = value　※n=タイプ番号($game_temp.poker_type から参照される)
  #==============================================================================
  COIN = [] # value=消費コイン数(賭けるコイン枚数)&レイズで上乗せするコイン数
  COIN[0] = 300 #300枚賭け
  COIN[1] = 30
  COIN[2] = 3
  #--------------------------------------------------------------------------
  # NAME[n] = [a,b,c,d,e,f,g,h,i,j]
  # n=タイプ番号($game_temp.poker_type から参照される)
  # a,b,c,d,e...=役の名前
  #--------------------------------------------------------------------------
  NAME = []
  NAME[0] = ["ロイヤル.Ｓ.Ｆ","５カード","Ｓ.Ｆ","４カード","フルハウス",
  "フラッシュ","ストレート","３カード","２ペア","ジョーカー"]
  NAME[1] = ["ロイヤル.Ｓ.Ｆ","５カード","Ｓ.Ｆ","４カード","フルハウス",
  "フラッシュ","ストレート","３カード","２ペア","ジョーカー"]
  NAME[2] = ["ロイヤル.Ｓ.Ｆ","５カード","Ｓ.Ｆ","４カード","フルハウス",
  "フラッシュ","ストレート","３カード","２ペア","ジョーカー"]
  #--------------------------------------------------------------------------
  # RATE[n] = [a,b,c,d,e,f,g,h,i,j]
  # n=タイプ番号($game_temp.poker_type から参照される)
  # a=NAME[n][a] の役が揃った場合の獲得コイン数(ロイヤル.Ｓ.Ｆ)
  # b=NAME[n][b] の役が揃った場合の獲得コイン数(５カード)
  # c,d,e...
  #--------------------------------------------------------------------------
  RATE = [] # □value=獲得コイン数(役が揃う)
  RATE[0] = [30000,15000,9000,6000,3000,1800,1200,900,600,300]
  RATE[1] = [3000,1500,900,600,300,180,120,90,60,30]
  RATE[2] = [300,150,90,60,30,18,12,9,6,3]
  # □大当りの効果音(HIT_ME_MAX)を演奏する獲得コイン数(n以上)
  ME_RATE_MAX = []
  ME_RATE_MAX[0] = 6000
  ME_RATE_MAX[1] = 600
  ME_RATE_MAX[2] = 60
  #==============================================================================
  # ■ NAME/RATE の文字設定
  #==============================================================================
  A_COLOR = Color.new(255,255,255,255)     # 通常色
  B_COLOR = Color.new(40,40,40,255)        # 当り色
  #--------------------------------------------------------------------------
  # NAME の文字設定
  #--------------------------------------------------------------------------
  N_SIZE = 20                              # 文字サイズ
  N_BOLD = false                           # 太文字フラグ[true/false]
  N_ITALIC = false                         # 斜め文字フラグ[true/false]
  N_SHADOW = true                          # 影文字フラグ[true/false]
  N_ROUND = false                          # 囲み文字フラグ[true/false]
  N_NAME = ["ＭＳ ゴシック"]               # フォント名
  #--------------------------------------------------------------------------
  # RATE の文字設定
  #--------------------------------------------------------------------------
  R_SIZE = 22                              # 文字サイズ
  R_BOLD = false                           # 太文字フラグ[true/false]
  R_ITALIC = false                         # 斜め文字フラグ[true/false]
  R_SHADOW = true                          # 影文字フラグ[true/false]
  R_ROUND = false                          # 囲み文字フラグ[true/false]
  R_NAME = ["Arial","ＭＳ ゴシック"]       # フォント名
end
#==============================================================================
# ■ Window_Poker_hit
#==============================================================================
class Window_Poker_hit
  include HIDE_POKER
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    @poker_hit = Sprite.new
    @poker_hit.bitmap = Bitmap.new(640,240)
    @poker_hit.opacity = 15
    @count_on = false
    @count = 0
  end
  #--------------------------------------------------------------------------
  # ■ 当りの描画
  #--------------------------------------------------------------------------
  def refresh(pos)
    name = HIDE_POKER::NAME[$game_temp.poker_type][pos]
    if pos == 10
      return
    end
    bx = 32 ; by = 28
    if pos > 4
      pos -= 4
      bx = 336 ; by = 0
    end
    @count_on = true
    pic = RPG::Cache.windowskin(HIDE_POKER::POKER_HIT)
    # □当たりの出たレートを点滅
    @poker_hit.bitmap.blt(bx,by+28*pos-4,pic,Rect.new(0,0,320,32))
  end
  #--------------------------------------------------------------------------
  # ■ Bitmapの消去
  #--------------------------------------------------------------------------
  def reset
    if @poker_hit.bitmap != nil
      @poker_hit.bitmap.clear
      @poker_hit.opacity = 10
      @count_on = false
    end
  end
  #--------------------------------------------------------------------------
  # ■ 更新
  #--------------------------------------------------------------------------
  def update
    if @count_on
      if @poker_hit.opacity > 10
        @poker_hit.opacity -= 10
      else
        @count_on = false
      end
    else
      if @poker_hit.opacity < 250
        @poker_hit.opacity += 10
      else
        @count_on = true
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 透明度を返す(Bitmapの点滅)
  #--------------------------------------------------------------------------
  def opacity?
    return @poker_hit.opacity
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  def dispose
    if @poker_hit.bitmap != nil
      @poker_hit.bitmap.dispose
    end
    @poker_hit.dispose
  end
end  
#==============================================================================
# ■ Window_Poker_top
#==============================================================================
class Window_Poker_top
  attr_accessor :rate
  include HIDE_POKER
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    @poker_top = Sprite.new
    @rate1 = HIDE_POKER::RATE[$game_temp.poker_type]
    @rate2 = HIDE_POKER::RATE[$game_temp.poker_type].collect{|a| a*3/2}
    @bitmap1 = make_rate(@rate1)
    @bitmap2 = make_rate(@rate2)
    refresh(1)
  end
  #--------------------------------------------------------------------------
  # ■ ポーカー役のBitmapを作成
  #--------------------------------------------------------------------------
  def make_rate(rate,bx = 0,by = 24)
    name = HIDE_POKER::NAME[$game_temp.poker_type]
    bitmap = Bitmap.new(640,240)
    #…………………………………………………………………………………………………
    # ■ 当りパターンの描画
    #…………………………………………………………………………………………………
    fonts = Copy_Font.new(HIDE_POKER::N_SIZE,HIDE_POKER::N_BOLD,
    HIDE_POKER::N_ITALIC,HIDE_POKER::N_SHADOW,HIDE_POKER::N_ROUND,
    HIDE_POKER::N_NAME,HIDE_POKER::A_COLOR,HIDE_POKER::B_COLOR)
    
    hit_fonts = Copy_Font.new(HIDE_POKER::R_SIZE,HIDE_POKER::R_BOLD,
    HIDE_POKER::R_ITALIC,HIDE_POKER::R_SHADOW,HIDE_POKER::R_ROUND,
    HIDE_POKER::R_NAME,HIDE_POKER::A_COLOR,HIDE_POKER::B_COLOR)
    for i in 0...rate.size
      if i > 4
        bx = 320-16
        by = 24 - (28*5)
      end
      # □ポーカー役の描画
      bitmap.draw_cache_text(32+bx+8,by+28*i,160,32,name[i].to_s,0,fonts)
      # □当りレートの描画
      bitmap.draw_cache_text(128+bx+8,by+28*i,120,32,rate[i].to_s,2,hit_fonts)
    end
    return bitmap
  end
  #--------------------------------------------------------------------------
  # ■ 更新
  #--------------------------------------------------------------------------
  def refresh(type)
    if type == 1
      @poker_top.bitmap = @bitmap1
      @rate = @rate1
    else
      @poker_top.bitmap = @bitmap2
      @rate = @rate2
    end
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  def dispose
    if @bitmap1 != nil
      @bitmap1.dispose
    end
    if @bitmap2 != nil
      @bitmap2.dispose
    end
    @poker_top.dispose
  end
end
#==============================================================================
# ■ Window_Poker
#==============================================================================
class Window_Poker
  include HIDE_POKER
  attr_accessor :card
  attr_accessor :cards
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    card_reset
    card_clear
    @first_draw = false
    @old_card = []
    @card_windows = []
    @viewports = []
    # ■ポーカーのカードを作成
    for i in 0...5
      x = 32*(i+1)+96*i-16
      y = 192
      @viewports[i] = Viewport.new(x,y,96,136)
      @card_windows[i] = Sprite.new(@viewports[i])
      @card_windows[i].bitmap = Bitmap.new(96,136*16)
      @card_windows[i].viewport.z = 100 + 2
    end
    @cards = [0,14,28,42]
    card_select?({0=>8,1=>8,2=>8,3=>8,4=>8})
    # □カードの更新
    make_card?(@card,[8]*5)
  end
  #--------------------------------------------------------------------------
  # ■ カードの選択(既に引いたカードを除外)
  #--------------------------------------------------------------------------
  def card_select?(nums)
    # □フラッシュ確率UP(5ジョーカーの場合)
    if @first_draw && HIDE_POKER::FIVE_JOKER
      @max_mark = rand(14) + @mark.index(@mark.max)*14
    end
    for i in nums.keys
      if nums[i] == 8
        @card[i] = nil
      end
      while @card[i] == nil
        c = rand(57)
        if ! @cards.include?(c)
          @cards.push(c)
          @card[i] = c
        # □フラッシュ確率UP(5ジョーカーの場合)
        elsif @max_mark != nil && HIDE_POKER::FIVE_JOKER
          unless @cards.include?(@max_mark)
            @cards.push(@max_mark)
            @card[i] = @max_mark
          end
        end
        if @cards.size >= 57
          break
        end
      end
    end
    # □最初のドローでは、マークジョーカーは除外する
    unless @first_draw
      @max_mark = nil
      if HIDE_POKER::FIVE_JOKER
        @cards = [@card[0],@card[1],@card[2],@card[3],@card[4]]
      end
      # □デバッグ処理
      if HIDE_POKER::DEBUG_CARD != {}
        @card = HIDE_POKER::DEBUG_CARD.dup
        for i in @card.values
          @cards.push(i)
        end
      end
      @first_draw = true
    end
    # □カードの描画
    draw_card?(@card)
  end
  #--------------------------------------------------------------------------
  # ■ カードの初期化(ポーカーの終了)
  #--------------------------------------------------------------------------
  def card_reset
    @card = {0=>nil,1=>nil,2=>nil,3=>nil,4=>nil}
    # □最初のドローでは、マークジョーカーは除外する
    @first_draw = false
    @cards = [0,14,28,42]
  end
  #--------------------------------------------------------------------------
  # ■ カードの初期化(カード交換)
  #--------------------------------------------------------------------------
  def card_clear
    @numbers = []
    @mark = [0]*5
    @datas = []
  end
  #--------------------------------------------------------------------------
  # ■ 当り役の取得
  #--------------------------------------------------------------------------
  def card_hit?
    #…………………………………………………………………………………………………
    # ■手持ちのカードをグループ化
    #…………………………………………………………………………………………………
    items = @numbers.uniq
    group = [0]*items.size
    joker = 0
    @numbers.each{ |a| if a == 0 ; joker += 1 ; end }
    for i in 0...items.size
      for z in @numbers
        if z == items[i] && z != 0
          group[i] += 1
        end
      end
    end
    #…………………………………………………………………………………………………
    # ■(3,4,5)カード/(1,2)ペアの判定
    #…………………………………………………………………………………………………
    n_card = 0 ; pair = 0
    for i in group
      if i > n_card
        n_card = i
      end
      if i == 2
        pair += 1
      end
    end
    #…………………………………………………………………………………………………
    # ■フラッシュの判定
    #…………………………………………………………………………………………………
    flash = 0
    for i in 0..3
      if @mark[i] > flash
        flash = @mark[i]
      end
    end
    #…………………………………………………………………………………………………
    # ■ストレートの判定
    #…………………………………………………………………………………………………
    straight = 0
    items.sort!
    items.delete_if{ |a| a == 0 }
    n = @datas.index(items[0])
    num = 1
    for s in (n+1)...(n+5)
      if @datas[n] + num == @datas[s]
        straight += 1
      end
      num += 1
    end
    if straight >= 2 && joker > 0
      straight += joker
    end
    #…………………………………………………………………………………………………
    # ■ロイヤル・ストレートの判定(Aエースを含んでいるのが条件)
    #…………………………………………………………………………………………………
    royal = 0 ; num = 1 ; n = 9
    if @numbers.include?(1)
      for s in (n+1)...(n+5)
        if 9 + num == @datas[s]
          royal += 1
        end
        num += 1
      end
    end
    if royal >= 2 && joker > 0
      royal += joker
    end
    #…………………………………………………………………………………………………
    # ■ジョーカーを含む場合
    #…………………………………………………………………………………………………
    if joker > 0
      # □1ペアを3カードに変更
      if pair == 1 && n_card != 3
        n_card = 2 + joker
        pair = 0
      # □2ペアをフルハウスに変更
      elsif pair == 2
        n_card = 3
        pair = 1
      # □(3,4,5)カードに変更
      else
        n_card += joker
      end
    end
    #…………………………………………………………………………………………………
    # ■ロイヤル・ストレート・フラッシュの判定(フラッシュを含んでいるのが条件)
    #…………………………………………………………………………………………………
    if royal == 4 && flash == 5
      return 0
    elsif royal == 4
      # □フラッシュではない場合、ストレートに変更
      straight = 4
    end
    #…………………………………………………………………………………………………
    # ■ポーカー役の強い順で判別
    #…………………………………………………………………………………………………
    hand = 10
    if n_card == 5
      # □5カード(ワイルドカードを含んでいるのが条件)
      if @mark[4] == 1
        case joker
        when 1,5
          # □5カード
          hand = 1
        when 2,4
          # □4カード
          hand = 3
        when 3
          # □フルハウス
          hand = 4
        end
      else
        case joker
        when 1,4
          # □4カード
          hand = 3
        when 2,3
          # □フルハウス
          hand = 4
        end
      end
    elsif n_card == 4
      if @mark[4] == 1
        # □4カード(ワイルドカードを含んで場合)
        hand = 3
      else
        case joker
        when 0,1,3
          # □4カード
          hand = 3
        when 2
          # □フルハウス(ジョーカー2枚)
          hand = 4
        end
      end
    elsif n_card == 3
      # □ジョーカー2枚のストレート.フラッシュ(フルハウスより優先)
      if flash == 5 && straight == 4
        hand = 2
      # □フルハウス
      elsif pair == 1
        hand = 4
      # □ジョーカー2枚のストレート(3カードより優先)
      elsif straight == 4
        hand = 6
      # □3カード
      else
        hand = 7
      end
    elsif pair == 2
      # □2ペア
      hand = 8
    elsif pair == 1
      # □1ペア(役として不成立)
      hand = 10
    else
      # □ストレート.フラッシュ
      if flash == 5 && straight == 4
        hand = 2
      # □フラッシュ
      elsif flash == 5
        hand = 5
      # □ストレート
      elsif straight >= 4
        hand = 6
      # □ジョーカー1枚を含む1ペア(役として成立)
      elsif joker == 1
        hand = 9
      else
        # □0ペア(役として不成立)
        hand = 10
      end
    end
    # □デバッグ処理
    if HIDE_POKER::DEBUG
      p debug = ("F=" + flash.to_s + ",S=" + straight.to_s + ",P=" + 
      pair.to_s + ",C=" + n_card.to_s + ",J=" + joker.to_s + ",R=" + royal.to_s)
    end
    return hand
  end
  #--------------------------------------------------------------------------
  # ■ カードの取得
  #--------------------------------------------------------------------------
  def check_card?(i)
    case @card[i]
    when 0..13 # スペード
      pic = RPG::Cache.windowskin(HIDE_POKER::CARD_S)
      num = @card[i]
      @mark[0] += 1
    when 14..27 # ダイヤ
      pic = RPG::Cache.windowskin(HIDE_POKER::CARD_D)
      num = @card[i]-14
      @mark[1] += 1
    when 28..41 # クラブ
      pic = RPG::Cache.windowskin(HIDE_POKER::CARD_C)
      num = @card[i]-28
      @mark[2] += 1
    when 42..55 # ハート
      pic = RPG::Cache.windowskin(HIDE_POKER::CARD_H)
      num = @card[i]-42
      @mark[3] += 1
    when 56 # ジョーカー(ワイルドカード)
      pic = RPG::Cache.windowskin(HIDE_POKER::CARD_J)
      num = 0
      @mark[4] = 1
      @mark[0] += 1 ; @mark[1] += 1 ;@mark[2] += 1 ;@mark[3] += 1
    end
    @datas[num] = num
    @numbers.push(num)
    return [pic,num]
  end
  #--------------------------------------------------------------------------
  # ■ カードの描画
  #--------------------------------------------------------------------------
  def draw_card?(card)
    @card = card
    card_clear
    # □カードの描画
    for i in 0...5
      # □マークの取得
      check = check_card?(i)
      pic = check[0]
      num = check[1]
      case num
      when 0..4
        rx = 96*num
        ry = 136*0
      when 5..9
        rx = 96*(num-5)
        ry = 136*1
      when 10..13
        rx = 96*(num-10)
        ry = 136*2
      end
      # □カードの描画の再描画
      if @card[i] != @old_card[i]
        @card_windows[i].bitmap.clear
        @old_card[i] = @card[i]
        # □カードの描画
        shadow = RPG::Cache.windowskin(HIDE_POKER::CARD_J)
        @card_windows[i].bitmap.blt(0,0,pic,Rect.new(rx,ry,96,136))
        for n in 1..3
          rect = Rect.new(12*n,136*n,96-n*24,136)
          @card_windows[i].bitmap.stretch_blt(rect,pic,Rect.new(rx,ry,96,136))
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*2,0,96,136))
        end
        rect = Rect.new(47,136*4,2,136)
        @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*4,0,96,136))
        for n in 1..3
          rect = Rect.new(12*(4-n),136*(n+4),96-(4-n)*24,136)
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96,0,96,136))
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*3,0,96,136))
        end
        @card_windows[i].bitmap.blt(0,136*8,shadow,Rect.new(96,0,96,136))
        for n in 1..3
          rect = Rect.new(12*n,136*(n+8),96-n*24,136)
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96,0,96,136))
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*2,0,96,136))
        end
        rect = Rect.new(47,136*12,2,136)
        @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*4,0,96,136))
        for n in 1..3
          rect = Rect.new(12*(4-n),136*(n+12),96-(4-n)*24,136)
          @card_windows[i].bitmap.stretch_blt(rect,pic,Rect.new(rx,ry,96,136))
          @card_windows[i].bitmap.stretch_blt(rect,shadow,Rect.new(96*3,0,96,136))
        end
        @card_windows[i].bitmap.blt(0,136*16,pic,Rect.new(rx,ry,96,136))
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ カードの更新
  #--------------------------------------------------------------------------
  def make_card?(card,type = [8]*5)
    @card = card
    for i in 0...5
      case type[i]
      when 16
        @card_windows[i].oy = 0
      else
        @card_windows[i].oy = 136*type[i]
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 解放
  #--------------------------------------------------------------------------
  def dispose
    @viewports.each{ |a| a.dispose }
    @card_windows.each{ |a| a.bitmap.dispose }
    @card_windows.each{ |a| a.dispose }
  end
end
#==============================================================================
# ■ Scene_Poker
#==============================================================================
class Scene_Poker
  include HIDE_POKER
  #--------------------------------------------------------------------------
  # ■ オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize
    game_reset?
    # □マップ BGM を記憶し、BGM を停止
    $game_temp.map_bgm = $game_system.playing_bgm
    $game_system.bgm_stop
    # □ポーカー BGM を演奏
    $game_system.bgm_play(RPG::AudioFile.new(HIDE_POKER::BGM))
  end
  #--------------------------------------------------------------------------
  # ■ ポーカーの初期化
  #--------------------------------------------------------------------------
  def game_reset?
    @card = {0=>true,1=>true,2=>true,3=>true,4=>true}
    @values = {0=>16,1=>16,2=>16,3=>16,4=>16}
    @card_close = true
    @card_open = true
    @se_play = false
    @poker_step = 0
    @speed = HIDE_POKER::SPEED
    @raise = false
    @fold = false
    # □カードの初期状態(裏向き=8)
    @card_count = 8*@speed
    # □カードのアニメーションフラグをOFF
    @move = false
    # □ゲーム実行中フラグをOFF
    @playing = false
  end
  #--------------------------------------------------------------------------
  # ■ ウィンドウの初期化
  #--------------------------------------------------------------------------
  def window_reset?
    @card_select_window.active = false
    @card_select_window.index = -1
    @card_select_window.visible = false
    # □カード選択色を変更
    @card_select_window.colors = [false]*5
    @card_select_window.refresh
    @card_exchange_window.active = false
    @card_exchange_window.index = -1
    @card_exchange_window.visible = false
    # □カード選択色を変更
    @card_exchange_window.colors = [false]*5
    @card_exchange_window.refresh
    # □ヘルプの更新
    @game_select_window.reset?
    @game_select_window.active = true
    @game_select_window.index = 0
    @game_select_window.visible = true
    @playing = false
    # □当たり報酬を戻す(レイズ)
    @poker_window_top.refresh(1)
  end
  #--------------------------------------------------------------------------
  # ■ メイン処理
  #--------------------------------------------------------------------------
  def main
    # ヘルプウィンドウを作成
    @help_window = Window_Help.new
    @help_window.opacity = 0
    @help_window.windowskin = Bitmap.new(1,1)
    @help_window.y = 480-64
    # □ポーカーウィンドウ背景を作成
    @poker_window_back = Window_Coin_Game_back.new(HIDE_POKER::BACK,"Ante",HIDE_POKER::COIN[$game_temp.poker_type],408,406)
    # □当たり演出ウィンドウを作成
    @poker_window_under = Window_Poker_hit.new
    # □ポーカーウィンドウを作成
    @poker_window = Window_Poker.new
    # □通常色/影色を設定
    n_color = Color.new(255,255,255,255)
    s_color = Color.new(0,0,0,200)
    fonts = Copy_Font.new(26,true,false,true,false,["Arial","ＭＳ ゴシック"],n_color,s_color)
    # □コイン数値ウィンドウを作成
    @coin_window = Window_digits_number.new(440,415,100,$game_party.coin,10,10,fonts)
    # □ポーカーの配当リストを作成
    @poker_window_top = Window_Poker_top.new
    fonts = Copy_Font.new(22,true,false,true,false,["Georgia","ＭＳ ゴシック"],n_color,s_color)
    help_fonts = Copy_Font.new(16,false,false,false,false,["ＭＳ ゴシック"],n_color,s_color)
    pic = HIDE_POKER::SELECT
    # □ゲーム開始選択ウィンドウを作成
    com = HIDE_POKER::GAME_COMS
    helps = HIDE_POKER::GAME_HELPS
    @game_select_window = Window_Picture_Select.new(0,384,128*2,64,com,pic,fonts,helps,help_fonts)
    @game_select_window.help_window = @help_window
    @game_select_window.index = 0
    @game_select_window.active = true
    @game_select_window.update_help
    @game_select_window.visible = true
    # □カード交換ウィンドウを作成
    com = HIDE_POKER::CARD_COMS
    helps = HIDE_POKER::CARD_HELPS
    @card_select_window = Window_Picture_Select.new(0,320,128*5,64,com,pic,fonts,helps,help_fonts)
    @card_select_window.help_window = @help_window
    @card_select_window.index = -1
    # □カード交換の開始ウィンドウを作成
    com = HIDE_POKER::CHANGE_COMS
    helps = HIDE_POKER::CHANGE_HELPS
    @card_exchange_window = Window_Picture_Select.new(0,352,128*3,64,com,pic,fonts,helps,help_fonts)
    @card_exchange_window.help_window = @help_window
    # □選択肢の文字色を初期化
    exchange_colors?
    # トランジション実行
    Graphics.transition
    # メインループ
    loop do
      # ゲーム画面を更新
      Graphics.update
      # 入力情報を更新
      Input.update
      # フレーム更新
      update
      # 画面が切り替わったらループを中断
      if $scene != self
        break
      end
    end
    # トランジション準備
    Graphics.freeze
    # ウィンドウを解放
    @poker_window_back.dispose
    @poker_window.dispose
    @coin_window.dispose
    @poker_window_top.dispose
    @card_select_window.dispose
    @game_select_window.dispose
    @card_exchange_window.dispose
    @help_window.dispose
    @poker_window_under.dispose
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新(ゲーム開始)
  #--------------------------------------------------------------------------
  def update_select
    #…………………………………………………………………………………………………
    # ■ C ボタンが押された場合
    #…………………………………………………………………………………………………
    if Input.trigger?(Input::C)
      unless @move
        case @game_select_window.index
        when 0 #□ゲーム開始
          if @poker_step == 8
            @poker_step = 9
          else
            if $game_party.coin >= HIDE_POKER::COIN[$game_temp.poker_type]
              # □決定SEを演奏
              $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::DECISION_SE))
            end
            @poker_step = 1
          end
        when 1 #□ゲーム終了
          # □決定SEを演奏
          $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::DECISION_SE))
          poker_end
        end
      end
      return
    end
    #…………………………………………………………………………………………………
    # ■ B ボタンが押された場合
    #…………………………………………………………………………………………………
    if Input.trigger?(Input::B)
      # □キャンセルSEを演奏
      $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::CANCEL_SE))
      poker_end
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新(交換するカードの選択)
  #--------------------------------------------------------------------------
  def update_card
    unless @move
      #…………………………………………………………………………………………………
      # ■ C ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::C)
        @card = {0=>false,1=>false,2=>false,3=>false,4=>false}
        if @values[@card_select_window.index] == 16 && @card_close == false
          @card_close = true
        end
        if @values[@card_select_window.index] == 8 && @card_close
          @card_close = false
        end
        #…………………………………………………………………………………………………
        # ■ 表から裏に変わるアニメーションを実行
        #…………………………………………………………………………………………………
        if @card_close == false
          @card_close = true
          @card[@card_select_window.index] = true
          @card_count = @speed * 8
          @card_open = true
          # □カード選択色を変更
          @card_select_window.colors[@card_select_window.index] = false
        #…………………………………………………………………………………………………
        # ■ 裏から表に変わるアニメーションを実行
        #…………………………………………………………………………………………………
        else
          @card_close = false
          @card[@card_select_window.index] = true
          @card_count = 0
          @card_open = false
          # □カード選択色を変更
          @card_select_window.colors[@card_select_window.index] = true
        end
        @card_select_window.refresh
        @poker_step = 4
        return
      end
      #…………………………………………………………………………………………………
      # ■ 方向ボタンの下が押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::DOWN)
        $game_system.se_play($data_system.cursor_se)
        @card_select_window.index = -1
        @card_select_window.active = false
        # □ヘルプの更新
        @card_exchange_window.reset?
        @card_exchange_window.index = 0
        @card_exchange_window.active = true
        return
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新(カード交換の開始)
  #--------------------------------------------------------------------------
  def update_exchange
    unless @move
      #…………………………………………………………………………………………………
      # ■ C ボタンが押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::C)
        case @card_exchange_window.index
        when 0
          # □決定SEを演奏
          $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::DECISION_SE))
          # □カードの交換を実行
          @poker_window.card_select?(@values)
          @card_close = true
          for i in @values.keys
            if @values[i] == 8
              @card[i] = true
            else
              @card[i] = false
            end
          end
          @card_count = @speed * 8
          @card_open = true
          @poker_step = 5
        when 1
          # □レイズの実行(賭け金を変更)
          unless @raise
            # □決定SEを演奏
            $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::DECISION_SE))
            # □コイン(賭け金)を減らす
            if $game_party.coin >= HIDE_POKER::COIN[$game_temp.poker_type]
              $game_party.lose_coin(HIDE_POKER::COIN[$game_temp.poker_type])
            end
            # □カード選択色を変更
            @raise = true
            @fold = true
            exchange_colors?(true)
            # □当たり報酬を2倍に変更
            @poker_window_top.refresh(2)
          else
            # ブザー SE を演奏
            $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::BUZZER_SE))
          end
        when 2
          # □フォルドの実行(ゲームを降りる)
          unless @fold
            # □決定SEを演奏
            $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::DECISION_SE))
            # □コインを増やす(賭け金の半分)
            $game_party.gain_coin((HIDE_POKER::COIN[$game_temp.poker_type]/2))
            # □カード選択色を変更
            exchange_colors?
            @card_close = false
            for i in @values.keys
              if @values[i] == 8
                @card[i] = false
              else
                @card[i] = true
              end
            end
            @card_count = 0
            @card_open = false
            @poker_step = 12
          else
            # ブザー SE を演奏
            $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::BUZZER_SE))
          end
        end
        return
      end
      #…………………………………………………………………………………………………
      # ■ 方向ボタンの上が押された場合
      #…………………………………………………………………………………………………
      if Input.trigger?(Input::UP)
        $game_system.se_play($data_system.cursor_se)
        @card_exchange_window.index = -1
        @card_exchange_window.active = false
        # □ヘルプの更新
        @card_select_window.reset?
        @card_select_window.index = 0
        @card_select_window.active = true
        return
      end
    end
  end
  #--------------------------------------------------------------------------
  # ■ 選択肢の色更新
  #--------------------------------------------------------------------------
  def exchange_colors?(on = $game_party.coin < HIDE_POKER::COIN[$game_temp.poker_type])
    # □カード選択色を変更
    @raise = on
    @game_select_window.colors[0] = $game_party.coin < HIDE_POKER::COIN[$game_temp.poker_type]
    @game_select_window.refresh
    @card_exchange_window.colors[1] = @raise
    @card_exchange_window.colors[2] = @fold
    @card_exchange_window.refresh
  end
  #--------------------------------------------------------------------------
  # ■ 当たりの処理
  #--------------------------------------------------------------------------
  def poker_hit
    @move = true
    @game_select_window.active = false
    hit = @poker_window.card_hit?
    #…………………………………………………………………………………………………
    # ■ 当りの場合の効果音を演奏(ME)
    #…………………………………………………………………………………………………
    gain = 0
    unless hit == 10
      # □ハズレの場合、待ち時間無し
      gain = @poker_window_top.rate[hit]
    end
    #…………………………………………………………………………………………………
    # ■ 所持コインを増やす
    #…………………………………………………………………………………………………
    $game_party.gain_coin(gain)
    @poker_window_under.reset
    @poker_window_under.refresh(hit)
    time_up = 0
    if gain > 0
      # □大当たりの場合
      if gain >= HIDE_POKER::ME_RATE_MAX[$game_temp.poker_type]
        $game_system.me_play(RPG::AudioFile.new(HIDE_POKER::HIT_ME_MAX))
        # □次のゲーム開始まで待つ(MEの演奏時間)
        time_up = HIDE_POKER::ME_MAX_TIME
      else
        $game_system.me_play(RPG::AudioFile.new(HIDE_POKER::HIT_ME))
        # □次のゲーム開始まで待つ(MEの演奏時間)
        time_up = HIDE_POKER::ME_TIME
      end
    end
    #…………………………………………………………………………………………………
    # ■ 次の開始まで待つ
    #…………………………………………………………………………………………………
    time = 0
    while time < time_up
      # □ゲーム画面を更新
      Graphics.update
      # □回転数値の更新(所持コイン)
      @coin_window.update($game_party.coin)
      # □当りの演出を更新
      @poker_window_under.update
      if @poker_window_under.opacity? <= 10
        time += 1
      end
    end
    # □選択色を変更
    exchange_colors?
    @poker_window_under.reset
    #@move = false
    @poker_step = 7
  end
  #--------------------------------------------------------------------------
  # ■ ゲームの終了
  #--------------------------------------------------------------------------
  def poker_end
    unless @playing
      # □ポーカー開始前の BGM に戻す
      $game_system.bgm_play($game_temp.map_bgm)
      # マップ画面に切り替え
      $scene = Scene_Map.new
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ フレーム更新
  #--------------------------------------------------------------------------
  def update
    #…………………………………………………………………………………………………
    # ■ 回転数値の更新(所持コイン)
    #…………………………………………………………………………………………………
    @coin_window.update($game_party.coin)
    #…………………………………………………………………………………………………
    # ■ ゲーム開始ウィンドウの更新
    #…………………………………………………………………………………………………
    if @card_select_window.active
      @card_select_window.update
      update_card
    end
    #…………………………………………………………………………………………………
    # ■ カード選択ウィンドウの更新
    #…………………………………………………………………………………………………
    if @game_select_window.active
      @game_select_window.update
      update_select
    end
    #…………………………………………………………………………………………………
    # ■ カード決定ウィンドウの更新
    #…………………………………………………………………………………………………
    if @card_exchange_window.active
      @card_exchange_window.update
      update_exchange
    end
    #…………………………………………………………………………………………………
    # ■ ゲームの流れ
    #…………………………………………………………………………………………………
    case @poker_step
    when 1
      # □コインを減らす(賭け金)
      if $game_party.coin >= HIDE_POKER::COIN[$game_temp.poker_type]
        $game_party.lose_coin(HIDE_POKER::COIN[$game_temp.poker_type])
        # □カード選択色を変更
        exchange_colors?
        @playing = true
        @game_select_window.active = false
        @poker_step = 2
      else
        # ブザー SE を演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::BUZZER_SE))
        @poker_step = 0
        @playing = false
        return
      end
    when 2
      # □ポーカーの開始
      if card_anim?(@card_close,@card)
        # □裏向きカード5枚全て表向きに変更
        @card = {0=>false,1=>false,2=>false,3=>false,4=>false}
        @game_select_window.active = false
        @game_select_window.index = -1
        @game_select_window.visible = false
        # □カード交換の処理フラグをON
        @card_select_window.active = true
        @card_select_window.index = 0
        @card_select_window.visible = true
        @card_exchange_window.visible = true
        @poker_step = 3
      end
      return
    when 3
      # □交換するカードの選択待ち
      return
    when 4
      # □交換するカードの向きを変更(個別)
      if card_anim?(@card_close,@card)
        @poker_step = -1
      end
      return
    when 5
      # □カードアニメの実行(裏向きカードを全て表向きに変更)
      if card_anim?(@card_close,@card)
        @poker_step = 6
      end
      return
    when 6
      unless @move
        # □ポーカー役の判定
        poker_hit
      end
      return
    when 7
      # □ウインドウの初期化
      @move = false
      window_reset?
      @poker_step = 8
      return
    when 8
      # □ゲームの開始待ち(連続ゲーム)
      return
    when 9
      # □アニメ変数の初期化
      animation_reset?
      # □コイン数のチェック
      if $game_party.coin >= HIDE_POKER::COIN[$game_temp.poker_type]
        @playing = true
        @game_select_window.active = false
        @poker_step = 10
      else
        # ブザー SE を演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::BUZZER_SE))
        @poker_step = 0
        @playing = false
        return
      end
      return
    when 10
      # □カードアニメの実行
      if card_anim?(@card_close,@card)
        @poker_step = 11
      end
      return
    when 11
      unless @move
        # □ポーカーの初期化(ゲーム開始)
        poker_reset?
        @poker_step = 1
      end
      return
    when 12
      # □カードアニメの実行(表向きカードを全て裏向きに変更)
      if card_anim?(@card_close,@card)
        # □ウインドウの初期化
        window_reset?
        # □アニメ変数の初期化
        animation_reset?
        # □ポーカーの初期化(ゲーム開始)
        poker_reset?
        @poker_step = 0
      end
      return
    end
  end
  #--------------------------------------------------------------------------
  # ■ アニメ変数の初期化
  #--------------------------------------------------------------------------
  def animation_reset?
    @card = {0=>true,1=>true,2=>true,3=>true,4=>true}
    @card_close = false
    @card_count = 0
    @card_open = false
  end
  #--------------------------------------------------------------------------
  # ■ ポーカーの初期化
  #--------------------------------------------------------------------------
  def poker_reset?
    @poker_window.card_clear
    @poker_window.card_reset
    @poker_window.card_select?({0=>8,1=>8,2=>8,3=>8,4=>8})
    game_reset?
  end
  #--------------------------------------------------------------------------
  # ■ カードのアニメーションを実行
  #--------------------------------------------------------------------------
  def card_anim?(close,num)
    #…………………………………………………………………………………………………
    # ■ 裏から表に変わるアニメーションを実行
    #…………………………………………………………………………………………………
    if close
      if @move == false && @se_play == false && 
        @card != {0=>false,1=>false,2=>false,3=>false,4=>false}
        # □カードめくりSEを演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::CHENGE_SE))
        @se_play = true
      end
      card_pos = @speed * 16
      stop_pos = 8
      open = true
    #…………………………………………………………………………………………………
    # ■ 表から裏に変わるアニメーションを実行
    #…………………………………………………………………………………………………
    else
      card_se = false
      if @move == false && @se_play == false
        # □カードめくりSEを演奏
        $game_system.se_play(RPG::AudioFile.new(HIDE_POKER::CHENGE_SE))
        @se_play = true
      end
      card_pos = @speed * 8
      stop_pos = 0
      open = false
    end
    #…………………………………………………………………………………………………
    # ■ アニメーションを実行
    #…………………………………………………………………………………………………
    if @card_open == open
      if @card_count != card_pos
        @card_count += 1
        if @card_count % @speed == 0
          for i in num.keys
            if num[i] == true
              @values[i] = @card_count/@speed
            end
          end
          @move = true
          @poker_window.make_card?(@poker_window.card,@values)
        end
      elsif @card_count >= card_pos
        @move = false
        @se_play = false
        return true
      end
    end
    return false
  end
end