麻雀ゲーム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 - C#
C#で書いてみました。
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 牌 = System.String;
class 卓 {
static 牌[] 数s = { "一", "二", "三", "四", "伍", "六", "七", "八", "九" };
static 牌[] 字s = { "白", "發", "中", "東", "南", "西", "北" };
static 牌[] 子s = { "索", "筒", "萬" };
int 人数;
public List<家> 家s = new List<家> { };
public List<牌> 山 = new List<牌>();
public List<List<牌>> 河s = new List<List<牌>>();
public 卓(int 人数) {
this.人数 = 人数;
山.AddRange((
from 数 in 数s
from 子 in 子s
from 枚数 in Enumerable.Range(0, 4)
select 子 + 数).ToList());
山.AddRange((
from 字 in 字s
from 枚数 in Enumerable.Range(0, 4)
select 字 + " ").ToList());
Random rand = new Random();
for (int i = 0; i < 山.Count; i++) {
int r1 = rand.Next(山.Count);
int r2 = rand.Next(山.Count);
牌 tmp = 山[r1];
山[r1] = 山[r2];
山[r2] = tmp;
}
for (int i = 0; i < 人数; i++) {
河s.Add(new List<string>());
}
}
public void 配牌() {
for (int i = 0; i < 人数; i++) {
家s.Add(new 家(i));
for (int j = 0; j < 13; j++) {
家s[i].摸打(山);
}
家s[i].手s.Sort();
}
}
public void 局() {
int 番 = 0;
while (山.Count > 14) {
家s[番].摸打(山);
河s[番].Add(家s[番].打牌());
番 = (番 + 1) % 人数;
}
}
}
class 家 {
int id;
string name { get { return "東南西北"[id]+"家"; } }
public 家(int id) {
this.id = id;
}
public List<string> 手s = new List<string>();
public void 摸打(List<string> 山) {
Console.WriteLine("{0}は牌を引きました。{1}", name, 山[0]);
手s.Add(山[0]);
山.RemoveAt(0);
}
public string 打牌() {
var 捨て牌 = 手s[手s.Count-1];
手s.RemoveAt(手s.Count - 1);
Console.WriteLine("{0}は牌を捨てました。{1}", name, 捨て牌);
return 捨て牌;
}
}
class 麻雀 {
static void Main(string[] args) {
Console.Write("プレイヤーは何人?:");
int 人数 = int.Parse(Console.ReadLine());
var 卓 = new 卓(人数);
卓.配牌();
卓.局();
Console.ReadKey();
}
}
|



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