麻雀のアガり判定
https://gyazo.com/7372f74fabe28f969d6039dd01b98bb7.png
#麻雀
Unicode文字を使うべきか🀇🀈🀉🀊🀋🀌🀍🀎🀏
七対子対応してたっけ?
http://www.pitecan.com/Puzzle/Haipai/
実行 (masui.github.io)
実行 (run-p5.web.app)
code:haipai.js
images = [
"https://gyazo.com/ca89fd704029cf33304e38dea3ff3b11.png", // 一萬
"https://gyazo.com/5bb399430694b2433dc40ec5378eec4d.png",
"https://gyazo.com/c3692b9848a7c7453d11276fb8dea38a.png",
"https://gyazo.com/b4af4d9f7bd13364bc6d0379a0466ca5.png",
"https://gyazo.com/2096adf12319d6768dfd930a024d9a4b.png",
"https://gyazo.com/047d855def6794176b200db7a665c034.png",
"https://gyazo.com/555bedbd5830ecd27ff652d8c2ff1c71.png",
"https://gyazo.com/8104d2fe4dd18990372872834b2f4158.png",
"https://gyazo.com/6a789915f92dba76d2a329b64a04d2f9.png" // 九萬
]
function report(machi){
$('body').append($('<p>'))
if(machi == null){
$('body').append($('<span>').css('font-size','60pt').text("天和!"))
}
else {
total = 0
for(var i=0;i<9;i++){
total += machii.length
if(machii.length > 0){
nokori = 0
$('body').append($('<img>').attr('src',imagesi))
$('body').append('<span>' + " を切れば " + '</span>')
for(var j=0;j<machii.length;j++){
nokori += (4 - a[machiij])
$('body').append($('<img>').attr('src',images[machiij]))
}
$('body').append($('<span>').text(" 待ち "))
$('body').append($('<span>').text(" (残り"+nokori+"枚)"))
$('body').append($('<p>'))
}
}
if(total == 0){
$('body').append($('<span>').text("何を切っても聴牌ならずorz"))
}
}
}
function haipai(){
$('body').children().remove()
var pieces = []
for(i=0;i<9;i++){
for(j=0;j<4;j++){
pieces.push(i)
}
}
for(i=0;i<36;i++){ // ランダムに並べかえ
j = Math.floor(Math.random() * (36-i)) + i
tmp = piecesj
piecesj = piecesi
piecesi = tmp
}
pieces = pieces.slice(0,14).sort(); // 14枚取得
$('body').children().remove()
for(i=0;i<14;i++){
$('body').append($('<img>').attr('src',images[piecesi]))
}
return pieces
}
function display(event){ // クリックされると呼ばれる
if(event) event.preventDefault()
if(typeof(show_report) == 'undefined') show_report = false
if(show_report){ // 解析結果を追加表示
report(machi);
show_report = false
}
else { // ランダムな配牌を表示
pieces = haipai()
machi = check(pieces); // 牌nを切ったときの待ち牌がmachinに入る
show_report = true
}
}
function chitoi(){
for(var i=0;i<9;i++){
if(ai != 0 && ai != 2) return false
}
return true
}
function check(pieces){
a = 0, 0, 0, 0, 0, 0, 0, 0, 0;
for(var i=0;i<14;i++){
a[piecesi] += 1
}
if(agari()){ // 既にアガっている
return null
}
else { // いずれかの牌を別のに取り替えたときアガりになるかをチェック
machi = []
for(var i=0;i<9;i++){
machii = []
if(ai > 0){
ai--
for(var j=0;j<9;j++){
if(aj < 4){
aj++
if(agari()){
machii.push(j)
}
aj--
}
}
ai++
}
}
}
return machi
}
function agari(){
var total = 0
for(var i=0;i<9;i++) total += ai
if(total != 14) return false
if(chitoi()) return true
for(var atama=0;atama<9;atama++){ // 頭をはずす
if(aatama >= 2){
aatama -= 2
res = agari2()
aatama += 2
if(res) return true
}
}
return false
}
function agari2(){
var i = 0
for(i=0;i<a.length;i++){
if(ai) break
}
if(i == a.length) return true
if(ai >= 3){ // 刻子チェック
ai -= 3
res = agari2()
ai += 3
if(res) return true
}
if(i+2 <= 8 && ai > 0 && ai+1 > 0 && ai+2 > 0){ // 順子チェック
ai -= 1; ai+1 -= 1; ai+2 -= 1
res = agari2()
ai += 1; ai+1 += 1; ai+2 += 1
if(res) return true
}
return false
}
function setup(){
$(document).on('mousedown', display)
display()
}