【www.gdgbn.com--微信】
js智力小游戏
<script language="JavaScript">
var size=10
var moves = 0
var off = size*2
var on = 0
var current = null
function doOver() {
if ((event.srcElement.tagName=="TD") && (current!=event.srcElement)) {
if (current!=null)
current.style.backgroundColor = current._background
event.srcElement._background = event.srcElement.style.backgroundColor
event.srcElement.style.backgroundColor = "lightgrey"
current = event.srcElement
}
}
function setColor(el) {
if ((el._background=="") || (el._background==null)) {
el.style.backgroundColor = "yellow"
el._background = "yellow"
} else {
el.style.backgroundColor = ""
el._background = ""
}
}
function countLights() {
off = 0; on = 0
for (var x=0; x < size; x++)
for (var y=0; y < size; y++) {
var p = board.rows[x].cells[y]
if (p._background=="yellow")
on++
else
off ++
}
document.all.on.innerText = on
if (off!=0)
document.all.off.innerText = off
else
document.all.off.innerText = "你赢了!"
return (off==0)
}
function doClick() {
setColor(current)
var cellIdx = current.cellIndex
var rowIdx = current.parentElement.rowIndex
if (rowIdx>0)
setColor(board.rows[rowIdx-1].cells[cellIdx])
if (rowIdx
if (cellIdx>0)
setColor(board.rows[rowIdx].cells[cellIdx-1])
if (cellIdx
moves++
document.all.moves.innerText = moves
win = countLights()
if (win) {
board.onclick = null
board.onmouseover = null
current.style.background = "yellow"
}
}
function buildBoard() {
var str = ""
for (var x=0; x < size; x++) {
str+=""
for (var y=0; y < size; y++) {
str+=" "
}
str+=""
}
str+=""
return str
}
function newGame() {
size = document.all.gameSize.value
if (size<3)
size=3
if (size>15)
size=15
document.all.gameSize.value = size
document.all.board.outerHTML = buildBoard()
moves=0
document.all.moves.innerText = moves
countLights()
}
</script>
在方格上点击,你可以将下面的方格全部填成黄色吗?
快开动你智慧脑筋,看看有什么技巧与规律。
移动:
0
灯灭:
25
灯亮:
0
大小:
<script language="JavaScript">
document.write(buildBoard())
</script>