80分记牌器,不是自动的。

# 80分记牌器,不是自动的。
#
# a 2 3 4 5 6 7 8 9 0 j q k
# 大怪 Red Joker r
# 小怪 Black Joker b
#
# ♠ / 
# ♥ * 
# ♣ - 
# ♦ +
# NT ?
#
#   北 
# 西  东 
#   南 
#   n
# w   e
#   s
#
# 1.敲入庄家、王的花色和数字
#
# 东庄家,打黑桃2
# e/2
#
# 显示:
# Dealer: E ♠2
# 
#   N    
# W      
#     E  
#   S    
#
# 2.敲入第一个出牌者位置,此圈出掉的牌
#
# 北先出,北红桃A方块A,西红桃2方块2,南红桃5方块10,东红桃3方块4
# n *a+a *2+2 *5+0 *3+4
#
# 前面敲错了!是东家先出的
# ! e *a+a *2+2 *5+0 *3+4
#
# 记到最后显示:
# Dealer: E ♠2
# 
#   N    ♠2♥2  ♠AKQJ0987654  ♥AKQJ  ♣AKQJ ♦AKQJ
# W      ♠2♥2  ♠AKQJ0987654  ♥AKQJ  ♣AKQJ ♦AKQJ
#     E  RR♣2♦2  ♠3  ♥09876543  ♣09876543  ♦0987
#   S    BB♣2♦2  ♠3  ♥09876543  ♣09876543  ♦6543

module ShengjiNerd
  class Game

    def initialize
      welcome()
      loop do
        shorthand(gets())
      end
    end

    def shorthand(input)
      args = input.split(' ')
      case args.size
      when 1 # a new game 庄家花色数字
        new_game(args.first)
      when 5 # a turn 第一个出牌位,p1,p2,p3,p4出的牌
        take_turn(*args)
      when 6 # redo last turn
        # TODO
      else
        welcome()
      end

      show_memory()
    end

    def new_game(input)
      mdealer = match_player(input)
      raise 'dealer required: [NWES]' unless mdealer
      mrank = match_rank(input)
      raise 'rank required: [234567890JQKA]' unless mrank
      msuit = match_suit(input)
      raise 'suit required: [/*-+?]' unless msuit

      @dealer_idx = index_player(mdealer[0]) # 庄家下标
      @rank = mrank[0].upcase # 数字
      @suits = %w[ / * - + ].unshift(msuit[0]).uniq.unshift('?') # 花色 5个:无,主色,3副色(黑桃>红桃>草花>方块)
      table_n = Array.new(9, Array.new) # 北家数字 9堆:怪,主王,3副王,主色,3副色
      table_w = Array.new(9, Array.new)
      table_e = Array.new(9, Array.new)
      table_s = Array.new(9, Array.new)
      @tables = [ table_n, table_w, table_e, table_s ] # 出牌堆:北家,西家,东家,南家
    end

    def take_turn(ps, p1, p2, p3, p4)
      mplayer = match_player(ps)
      raise 'who is the first? [NWES]' unless mplayer
      player_idx = index_player(mplayer[0]) # 出牌人的位置

      [p1, p2, p3, p4].each do |trick|
        suit = nil
        trick.each_char do |ch|
          mfrench = match_french(ch)
          if mfrench # 标花色
            suit = mfrench[0] 
          elsif %w[RB].include?(ch.upcase) # 记大小怪
            @tables[player_idx][0] << ch
          else # 记花色牌
            mrank = match_rank(ch)
            raise 'rank required: [234567890JQKA]' unless mrank
            rank = mrank[0].upcase
            raise "which is the suit of #{rank}? [/*-+]" unless suit
            
            if suit == @suits[1]  # 主王 或 主
              suit_idx = (rank == @rank ? 1 : 5)
            else # 副王 或 副
              suit_idx = (rank == @rank ? 2 : 6) + plus_suit(suit)
            end
            @tables[player_idx][suit_idx] << rank
          end
        end
        player_idx += 1
        player_idx %= 4
      end
    end

    def show_memory
      s = %Q{
Dearler: #{ print_player(@dealer_idx) } #{ print_suit(@suits[1]) }#{ @rank }

  N    #{ print_table(@tables[0]) }
W      #{ print_table(@tables[1]) }
    E  #{ print_table(@tables[2]) }
  S    #{ print_table(@tables[3]) }
}
    end

    # RRBB♠2♥2  ♠AKQ  ♥J09  ♣876  ♦543
    def print_table(table)
      # TODO
    end

    def print_player(player_idx)
      case player_idx
      when 0
        'N'
      when 1
        'W'
      when 2
        'E'
      when 3
        'S'
      end
    end

    def print_suit(suit)
      case suit
      when '?'
        'NT'
      when '/'
        '♠'
      when '*'
        '♥'
      when '-'
        '♣'
      when '+'
        '♦'
      end
    end

    def index_player(player_symbol)
      case dealer_symbol
      when 'N'
        0
      when 'W'
        1
      when 'E'
        2
      when 'S' 
        3
      end
    end

    def plus_suit(suit_symbol)
      case suit_symbol
      when '/'
        0
      when '*'
        1
      when '-'
        2
      when '+' 
        3
      end
    end

    def match_player(str)
      str.upcase.match(/[NWES]/)
    end

    def match_french(str)
      str.match(/[\/\*\-\+]/)
    end

    def match_suit(str)
      str.match(/[\/\*\-\+\?]/)
    end

    def match_rank(str)
      str.upcase.match(/[234567890JQKA]/)
    end

    def welcome
      puts "type 'h' show help"
    end

    def gets
      STDIN.gets
    end
  end
end

你可能感兴趣的:(自动)