麻雀ゲーム1
こんな感じで. Playerクラスは拡張性を与えるために追加してみました.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | class Mahjang
Tu, Num = :t, 14
Hai, Tuhai = [:m, :s, :p, Tu], [:wt, :sh, :et, :nh, :hk, :ht, :tu]
module WisePlayer
def think
unless @tumo
puts [*1..Num].map{|c| format("% 3d", c)}.join(?,) + ' 0=tumo', self
true until (0..Num).include?(i = gets.to_i)
@tumo = true if i.zero?
end
@tumo ? Num : i
end
end
class Player
def initialize
@hai, @ho = [], []
@tumo = nil
end
attr_reader :ho
def to_s
@hai.map{|p| [p.name, p.num]}.
map{|n,m| n != Tu ? " #{n}#{m+1}" : " #{TuHai[m]}"}.join(",")
end
def repai() @hai = @hai.sort_by{|h| [h.name, h.num]} end
def size() @hai.size end
def get(a) @hai << a end
def think() Num end
def set() @ho << @hai.delete_at(think-1) end
end
def initialize
pi = Struct.new("Pi", :name, :num)
@players = Array.new(4){Player.new}
@players[0].extend(WisePlayer)
@hi = Hai.map{|s| Array.new(4){Array.new(9){|i|
pi.new(s,i) unless s == Tu && i >= 7}}}.
flatten.compact.shuffle
init_set
end
def init_set
cycle{@players.last.size >= Num-1}
@players[0].get(@hi.delete_at(3))
end
def play
cycle(true){@hi.size <= Num}
@players.each{|p| puts p}
end
private
def cycle(play=nil)
@players.cycle{|p|
p.get(@hi.shift) if p.size < Num
p.set if play
p.repai
break if yield}
end
end
if __FILE__ == $0
m = Mahjang.new
m.play
end
|
Posted feedbacks - Smalltalk
Squeak Smalltalk で。最低限の動きだけ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | | 人数 全牌 山 全プレイヤ 順番 |
人数 := 4.
全牌 := OrderedCollection new.
#('索' '筒' '萬') do: [:各子 |
#('1' '2' '3' '4' '5' '6' '7' '8' '9') do: [:各数 |
全牌 add: 各数, 各子]].
全牌 addAll: #('白' '發' '中' '東' '南' '西' '北').
全牌 := 全牌, 全牌, 全牌, 全牌.
World findATranscript: nil.
山 := 全牌 shuffled.
全プレイヤ := OrderedCollection new.
人数 timesRepeat: [
全プレイヤ add: ({
#手牌 -> (SortedCollection sortBlock: [:a :b | a reversed < b reversed]).
#河 -> OrderedCollection new} as: Dictionary)].
13 timesRepeat: [
全プレイヤ do: [:プレイヤ |
(プレイヤ at: #手牌) add: 山 removeFirst]].
全プレイヤ do: [:プレイヤ | Transcript cr; show: (プレイヤ at: #手牌) asArray].
順番 := 0.
[山 size > 14] whileTrue: [
| 打手 |
打手 := 全プレイヤ atWrap: (順番 := 順番 + 1).
(打手 at: #手牌) add: 山 removeFirst.
(打手 at: #河) add: (打手 at: #手牌) removeLast].
全プレイヤ do: [:プレイヤ | Transcript cr; show: (プレイヤ at: #河) asArray]
|




saws
#8258()
Rating-9/11=-0.82
麻雀ゲームの部分的な作成がお題になります. 以下のメソッド/関数を組み込んで下さい.
ルールとしては,
と考えていただければ結構です.
鳴き, あがり, 自風, 場風などは考慮していただかなくて結構です.
1 reply [ reply ]