BINGO卡片(6)

日期:2016.1.26

Create a Bingo Card

B I N G O
         
         
    Free    
         
         

Click here to create a new card

前边的bingo卡片还无法确保每个格子中不出现重复的数,这次呢要纠正这个问题。同时说明数组不必再进行初始化并读取,而是可以在运行时声明和设置他们。

if (!usedNums[newNum]) {
usedNums[newNum] = true;

! 表示“非”,当newNums为没出现的数字时,!usedNums[newNum] 为false,那么将它设置为true,并且记录,然后将其写在卡片上。如果是出现过得数,则什么也不做,会留下空白。

window.onload = initAll;

var usedNums = new Array(76);

function initAll () {
	if (document.getElementById){
		for (var i = 0; i < 24 ; i++) {
			setSquare(i);
		}
	}
	else {
		alert("Sorry,your browser doesn't support this script.")
	}
}

function setSquare(thisSquare){
	var currSquare = "square" + thisSquare;
	
	var colPlace = new Array(0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
	var colBasic = colPlace [thisSquare] * 15;
	var newNum = colBasic + getNewNum() + 1;

	if (!usedNums[newNum]) {
		usedNums[newNum] = true;
		document.getElementById(currSquare).innerHTML = newNum;
	};	
}

function getNewNum() {
	return Math.floor(Math.random() * 15);
}