Last active
August 29, 2015 13:57
-
-
Save bellbind/9761063 to your computer and use it in GitHub Desktop.
[javascript]mahjong calc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<script src="mahjong-calc.js"></script> | |
</head> | |
<body> | |
<h1>Mahjong Calc</h1> | |
<div> | |
Han: <input id="han" type="number" value="3" min="1" max="55" step="1" /> | |
Fu: <input id="fu" type="number" value="30" min="20" max="170" step="10" /> | |
</div> | |
<table border="border"> | |
<thead> | |
<tr> | |
<th>Winner</th><th>Tsumo</th><th>Ron</th> | |
</tr> | |
<thead> | |
<tbody> | |
<tr> | |
<th>Ko</th><td id="ko-tsumo">1000/2000</td><td id="ko-ron">3900</td> | |
</tr> | |
<tr> | |
<th>Oya</th><td id="oya-tsumo">2000</td><td id="oya-ron">5800</td> | |
</tr> | |
<tbody> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener("load", function () { | |
var inputFu = document.getElementById("fu"); | |
var inputHan = document.getElementById("han"); | |
var koTsumo = document.getElementById("ko-tsumo"); | |
var koRon = document.getElementById("ko-ron"); | |
var oyaTsumo = document.getElementById("oya-tsumo"); | |
var oyaRon = document.getElementById("oya-ron"); | |
var evenUp = function (value, base) { | |
return Math.ceil(value / base) * base; | |
}; | |
var scoreData = function (base) { | |
return { | |
tsumo: {ko: evenUp(base, 100), oya: evenUp(base * 2, 100)}, | |
ron: {ko: evenUp(base * 4, 100), oya: evenUp(base * 6, 100)}, | |
}; | |
}; | |
var calcScore = function (han, fu) { | |
var base = evenUp(fu, 10) * Math.pow(2, 2 + han); | |
//console.log([han, fu, base]); | |
if (isNaN(base) || base < 2000) return scoreData(base); | |
if (han < 6) return scoreData(2000); | |
if (han < 8) return scoreData(3000); | |
if (han < 11) return scoreData(4000); | |
if (han < 13) return scoreData(6000); | |
return scoreData(8000); | |
}; | |
var updateScore = function (score) { | |
koTsumo.textContent = score.tsumo.ko + "/" + score.tsumo.oya; | |
koRon.textContent = score.ron.ko; | |
oyaTsumo.textContent = score.tsumo.oya; | |
oyaRon.textContent = score.ron.oya; | |
}; | |
var update = function (ev) { | |
var han = parseInt(inputHan.value); | |
var fu = parseInt(inputFu.value); | |
updateScore(calcScore(han, fu)); | |
}; | |
inputFu.addEventListener("input", update, false); | |
inputHan.addEventListener("input", update, false); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment