Skip to content

Instantly share code, notes, and snippets.

@discoNeko
Created July 7, 2016 09:19
Show Gist options
  • Save discoNeko/3bfb8f7a5306c45be10b919602df1cb3 to your computer and use it in GitHub Desktop.
Save discoNeko/3bfb8f7a5306c45be10b919602df1cb3 to your computer and use it in GitHub Desktop.
(function(){
var w = 800, h = 600;
var requestId;
var deck = [];
var hand = function(){
this.num = [];
for(var i = 0; i < 9; i++)
this.num[i] = 0;
}
var phand = [];
for(var i = 0; i < 4; i++)
phand[i] = new hand();
var bingo_str = [];
var view;
var canvas = document.getElementById('canvas');
canvas.addEventListener("click", onClick, false);
canvas.addEventListener('mousemove', onMove, false);
var ctx = canvas.getContext('2d');
init();
requestId = window.requestAnimationFrame(renderTitle);
function init(){
view = 0;
setInitDeck();
distributeHand();
for(var i = 0; i < 25; i++)
bingo_str[i] = setBingoStr(i);
}
function setInitDeck(){
var tmp_deck = [];
for(var i = 0; i < 100; i++){
tmp_deck[i] = true;
}
for(var i = 0; i < 4; i++)
deck[i] = 0;
var cnt = 4;
while(cnt<100){
var rnd = Math.floor(Math.random()*96) + 4;
if(tmp_deck[rnd]){
tmp_deck[rnd] = false;
deck[cnt] = Math.floor(rnd/4);
cnt++;
}
}
//var ccnt = [];
//for(var i = 0; i < 25; i++){
// ccnt[i] = 0;
//}
for(var i = 0; i < 100; i++){
//ccnt[deck[i]]++;
console.log("deck["+i+"] = "+deck[i]);
}
//for(var i = 0; i < 25; i++){
// console.log(ccnt[i]);
//}
}
function distributeHand(){
for(var i = 0; i < 9; i++){
var n = i*4;
phand[0].num[i] = deck[n];
phand[1].num[i] = deck[n+1];
phand[2].num[i] = deck[n+2];
phand[3].num[i] = deck[n+3];
}
for(var i = 0; i < 4; i++){
sortHand(i);
var s = "player["+i+"]";
for(var j = 0; j < 9; j++){
s += " "+phand[i].num[j];
}
console.log(s);
}
}
function sortHand(p){
phand[p].num.sort(function(a,b){return a - b;});
}
function setBingoStr(num){
/*
if(num==12){
num = 0;
}else{
num = Math.floor(Math.random()*24) + 1;
}
*/
return num == 12 ? 0 : Math.floor(Math.random()*24) + 1;
}
function renderTitle(){
//var str = "SampleText";
//var margin = w - 20*str.length;
ctx.fillStyle = '#aaa';
ctx.fillRect(0,0,w,h);
ctx.font= 'bold 25px Meiryo';
ctx.strokeStyle = '#333';
ctx.lineWidth = 6;
ctx.lineJoin = 'round';
ctx.fillStyle = '#fff';
//ctx.strokeText(str,margin/2,455,510);
//ctx.fillText(str,margin/2,455);
drawBoard();
requestId = window.requestAnimationFrame(renderTitle);
}
function drawBoard(){
//main board
ctx.fillStyle = '#333';
ctx.fillRect(10,10,580,580);
//hand
drawHand();
//discard
drawDiscard();
//bingo board
ctx.fillStyle = '#396';
ctx.fillRect(150,150,300,300);
//bingo mass
drawBingoMass();
}
function drawHand(){
ctx.fillStyle = '#aa6';
ctx.fillRect(100,510,400,50);
ctx.fillRect(40,100,50,400);
ctx.fillRect(100,40,400,50);
ctx.fillRect(510,100,50,400);
drawHandStr();
}
function drawHandStr(){
var x = 55, y = 75, r = 42;
var dx = [60,0,60,470];
var dy = [470,60,0,60];
ctx.font= 'bold 25px Meiryo';
ctx.fillStyle = '#ddd';
for(var i = 0; i < 4; i++){
for(var j = 0; j < 9; j++){
if(i%2==0){
ctx.fillText(phand[i].num[j], x+dx[i]+j*r, y+dy[i]);
}else{
ctx.fillText(phand[i].num[j], x+dx[i], y+dy[i]+j*r);
}
}
}
}
function drawDiscard(){
ctx.fillStyle = '#a6a';
ctx.fillRect(150,460,300,40);
ctx.fillRect(100,150,40,300);
ctx.fillRect(150,100,300,40);
ctx.fillRect(460,150,40,300);
}
function drawBingoStr(i,j){
var num = i*5+j;
var ax = 7, ay = 33;
ctx.strokeText(bingo_str[num],155+i*60+ax,155+j*60+ay);
ctx.fillText(bingo_str[num],155+i*60+ax,155+j*60+ay);
}
function drawBingoMass(){
ctx.fillStyle = '#6da';
ctx.font= 'bold 25px Meiryo';
for(var i = 0; i < 5; i++){
for(var j = 0; j < 5; j++){
ctx.fillRect(155+i*60,155+j*60,50,50);
drawBingoStr(i,j);
}
}
//hit bingo Mass
drawHitBingoMass();
}
function drawHitBingoMass(){
var bingo_hit = [];
for(var i = 0; i < 9; i++){
for(var j = 0; j < 25; j++){
if(phand[view].num[i] == bingo_str[j])
bingo_hit[j] = true;
}
}
ctx.globalAlpha = 0.5;
ctx.fillStyle = '#00f';
for(var i = 0; i < 25; i++){
if(bingo_hit[i]){
var x = Math.floor(i/5);
var y = i%5;
ctx.fillRect(155+60*x,155+y*60,50,50);
}
}
ctx.globalAlpha = 1.0;
}
function onClick(e){
var rect = e.target.getBoundingClientRect();
var x = Math.round(e.clientX - rect.left);
var y = Math.round(e.clientY - rect.top);
//console.log("click "+x+" "+y);
}
function onMove(e){
var rect = e.target.getBoundingClientRect();
var x = Math.round(e.clientX - rect.left);
var y = Math.round(e.clientY - rect.top);
//console.log(x+" "+y);
}
document.onkeydown = function (e){
var key = e.keyCode;
//console.log(key);
switch(key){
case 65 : view = 1; break;//a
case 83 : view = 0; break;//s
case 68 : view = 3; break;//d
case 87 : view = 2; break;//w
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment