Created
September 17, 2016 06:55
-
-
Save miyabisun/281c626ad0d679b1603f52db25fb7d3c to your computer and use it in GitHub Desktop.
minesweeper coffeescript
This file contains hidden or 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
$ -> | |
#以下タイマーの処理 | |
startTime = undefined | |
stopTime = undefined | |
running = false | |
timerId = undefined | |
run =-> | |
if running | |
return | |
running = true | |
if stopTime | |
startTime = startTime + (new Date).getTime() - stopTime | |
if !startTime | |
startTime = (new Date).getTime() | |
timer() | |
return | |
timer =-> | |
document.getElementById('timerText').innerHTML = (((new Date).getTime() - startTime) / 1000).toFixed(2) | |
timerId = setTimeout((-> | |
timer() | |
return | |
), 10) | |
return | |
stop =-> | |
if !running | |
return | |
running = false | |
clearTimeout timerId | |
stopTime = (new Date).getTime() | |
return | |
reset =-> | |
if running | |
return | |
startTime = undefined | |
document.getElementById('timerText').innerHTML = '0.00' | |
return | |
#ここまで | |
###cellClear =-> | |
cell_1 = undefined | |
cell_2 = undefined | |
cell_3 = undefined | |
cell_4 = undefined | |
cell_6 = undefined | |
cell_7 = undefined | |
cell_8 = undefined | |
cell_9 = undefined | |
console.log "セルリセット"### | |
open =-> | |
$('td').click -> | |
###blankElement = $('.blank') | |
blankLeft = blankElement.length### | |
$(@).css | |
'border-style':'inset' | |
'color':'black' | |
if $(@).hasClass('bomb') | |
alert 'GameOver リロードします。' | |
#location.reload() | |
if $(@).hasClass('blank') | |
if $(@).text() == "0" | |
$(@).attr 'class','openZero' | |
else | |
$(@).attr 'class','openBlank' | |
# 以下連鎖して0のマスを開ける処理 | |
openZero = $('.openZero') | |
openZeroLeft = openZero.length # 開いているけど周りのマスを開ける処理をしていない0マスの数 | |
table = document.getElementById('tb') | |
rows = table.tBodies[0].rows | |
height = rows.length-1 | |
width = rows[0].cells.length-1 | |
loop | |
console.log "a" | |
for l in [0...table.rows.length] | |
for m in [0...table.rows[l].cells.length] # cell_数字はテンキー表示 | |
centerCell = table.rows[l].cells[m] | |
centerText = $(centerCell).text() | |
if $(centerCell).hasClass 'openZero' | |
$(centerCell).attr 'class','completedZero' #処理していない0のマスと区別 | |
if l+1 <= height and m-1 >= 0 | |
cell_1 = table.rows[l+1].cells[m-1] | |
$(cell_1).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_1).text() == "0" and $(cell_1).hasClass('blank') | |
$(cell_1).attr 'class','openZero' | |
else if $(cell_1).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_1).attr 'class','openBlank' | |
if l+1 <= height | |
cell_2 =table.rows[l+1].cells[m] | |
$(cell_2).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_2).text() == "0" and $(cell_2).hasClass('blank') | |
$(cell_2).attr 'class','openZero' | |
else if $(cell_2).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_2).attr 'class','openBlank' | |
if l+1 <= height and m+1 <= width | |
cell_3 = table.rows[l+1].cells[m+1] | |
$(cell_3).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_3).text() == "0" and $(cell_3).hasClass('blank') | |
$(cell_3).attr 'class','openZero' | |
else if $(cell_3).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_3).attr 'class','openBlank' | |
if m-1 >= 0 | |
cell_4 = table.rows[l].cells[m-1] | |
$(cell_4).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_4).text() == "0" and $(cell_4).hasClass('blank') | |
$(cell_4).attr 'class','openZero' | |
else if $(cell_4).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_4).attr 'class','openBlank' | |
if m-1 <= width | |
cell_6 = table.rows[l].cells[m+1] | |
$(cell_6).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_6).text() == "0" and $(cell_6).hasClass('blank') | |
$(cell_6).attr 'class','openZero' | |
else if $(cell_6).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_6).attr 'class','openBlank' | |
if l-1 >= 0 and m-1 >= 0 | |
cell_7 = table.rows[l-1].cells[m-1] | |
$(cell_7).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_7).text() == "0" and $(cell_7).hasClass('blank') | |
$(cell_7).attr 'class','openZero' | |
else if $(cell_7).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_7).attr 'class','openBlank' | |
if l-1 >= 0 | |
cell_8 = table.rows[l-1].cells[m] | |
$(cell_8).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_8).text() == "0" and $(cell_8).hasClass('blank') | |
$(cell_8).attr 'class','openZero' | |
else if $(cell_8).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_8).attr 'class','openBlank' | |
if l-1 >= 0 and m+1 <= width | |
cell_9 = table.rows[l-1].cells[m+1] | |
$(cell_9).css | |
'border-style':'inset' | |
'color':'black' | |
if $(cell_9).text() == "0" and $(cell_9).hasClass('blank') | |
$(cell_9).attr 'class','openZero' | |
else if $(cell_9).text() == "0" | |
console.log "もう既に。" | |
else | |
$(cell_9).attr 'class','openBlank' | |
cellClear() | |
cell_1 = null | |
cell_2 = null | |
cell_3 = null | |
cell_4 = null | |
cell_6 = null | |
cell_7 = null | |
cell_8 = null | |
cell_9 = null | |
openZero = $('.openZero') | |
openZeroLeft = openZero.length | |
console.log openZeroLeft | |
blankElement = $('.blank') | |
blankLeft = blankElement.length | |
if blankLeft == 0 | |
alert 'GameClear!!!!!!' | |
stop() | |
break unless openZeroLeft isnt 0 #ここまで0マスの処理 | |
#blankLeft-- | |
blankElement = $('.blank') | |
blankLeft = blankElement.length | |
console.log blankLeft | |
if blankLeft == 0 | |
alert 'GameClear!!!!!!!!!!!!' | |
stop() | |
#location.reload() | |
#右クリックでフラグ(目印)を立てる | |
flag =-> | |
$('td').bind 'contextmenu', -> | |
if $(@).hasClass('openBlank') | |
alert '既に開けています。' | |
else | |
$(@).css | |
background: 'red' | |
#周りの爆弾の数の判定 | |
cellAssign =-> | |
table = document.getElementById('tb') | |
rows = table.tBodies[0].rows | |
height = rows.length-1 | |
width = rows[0].cells.length-1 | |
for l in [0...table.rows.length] | |
blankCount = 0 | |
console.log "------------------" | |
console.log l + "列目です。" | |
for m in [0...table.rows[l].cells.length] # cell_数字はテンキー表示 | |
cellClear() | |
centerCell = table.rows[l].cells[m] | |
console.log "-------" | |
console.log l + "列目の" + m + "個目です。" | |
if $(centerCell).hasClass 'blank' | |
if l+1 <= height and m-1 >= 0 | |
console.log "1ありました。" | |
cell_1 = table.rows[l+1].cells[m-1] | |
if l+1 <= height | |
console.log "2ありました" | |
cell_2 =table.rows[l+1].cells[m] | |
if l+1 <= height and m+1 <= width | |
console.log "3ありました" | |
cell_3 = table.rows[l+1].cells[m+1] | |
if m-1 >= 0 | |
console.log "4ありました" | |
cell_4 = table.rows[l].cells[m-1] | |
if m-1 <= width | |
console.log "6ありました" | |
cell_6 = table.rows[l].cells[m+1] | |
if l-1 >= 0 and m-1 >= 0 | |
console.log "7ありました" | |
cell_7 = table.rows[l-1].cells[m-1] | |
if l-1 >= 0 | |
console.log "8ありました" | |
cell_8 = table.rows[l-1].cells[m] | |
if l-1 >= 0 and m+1 <= width | |
console.log "9ありました" | |
cell_9 = table.rows[l-1].cells[m+1] | |
blankCount = 0 | |
if typeof(cell_1) isnt "undefined" | |
if $(cell_1).hasClass 'bomb' | |
console.log "左下は爆弾" | |
blankCount++ | |
if typeof(cell_2) isnt "undefined" | |
if $(cell_2).hasClass 'bomb' | |
console.log "下は爆弾" | |
blankCount++ | |
if typeof(cell_3) isnt "undefined" | |
if $(cell_3).hasClass 'bomb' | |
console.log "右下は爆弾" | |
blankCount++ | |
if typeof(cell_4) isnt "undefined" | |
if $(cell_4).hasClass 'bomb' | |
console.log "左は爆弾" | |
blankCount++ | |
if typeof(cell_6) isnt "undefined" | |
if $(cell_6).hasClass 'bomb' | |
console.log "右は爆弾" | |
blankCount++ | |
if typeof(cell_7) isnt "undefined" | |
if $(cell_7).hasClass 'bomb' | |
console.log "左上は爆弾" | |
blankCount++ | |
if typeof(cell_8) isnt "undefined" | |
if $(cell_8).hasClass 'bomb' | |
console.log "上は爆弾" | |
blankCount++ | |
if typeof(cell_9) isnt "undefined" | |
if $(cell_9).hasClass 'bomb' | |
console.log "右上は爆弾" | |
blankCount++ | |
if typeof(centerCell) isnt "undefined" | |
$(centerCell).text "#{blankCount}" | |
cellClear() | |
cell_1 = null | |
cell_2 = null | |
cell_3 = null | |
cell_4 = null | |
cell_6 = null | |
cell_7 = null | |
cell_8 = null | |
cell_9 = null | |
#マス目の生成と爆弾の設置 | |
$('#create').click -> | |
vertical = $('#area [name=vertical]').val() | |
horizonal = $('#area [name=horizonal]').val() | |
bomb = $('#area [name=bomb]').val() | |
for i in [0...vertical] | |
rows = $('tbody').append """<tr id ="#{i}tr"></tr>""" | |
for t in [0...horizonal] | |
cells = $("##{i}tr").append """<td class="blank">#{i}-#{t} </td>""" | |
table = document.getElementById('tb') | |
for k in [0...bomb] | |
n = Math.floor Math.random() * vertical | |
o = Math.floor Math.random() * horizonal | |
cell = table.rows[n].cells[o] # セル取得 | |
while $(cell).hasClass 'bomb' | |
n = Math.floor Math.random() * vertical | |
o = Math.floor Math.random() * horizonal | |
cell = table.rows[n].cells[o] | |
$(cell).attr 'class','bomb' | |
$(cell).html '💣' | |
cellAssign() | |
run() | |
open() | |
flag() | |
blankElement = $('.blank') | |
blankLeft = blankElement.length | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment