Created
December 21, 2020 22:52
-
-
Save bradevans/ef02c02113f011d95d57d796d627a647 to your computer and use it in GitHub Desktop.
VwKzqwd
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
const spots = [ | |
['.', '.', '.', '.', '.', '.'], | |
['.', '.', '.', '.', '.', '.'], | |
['.', '.', '.', '.', '.', '.'], | |
['.', '.', '.', '.', '.', '.'], | |
['.', '.', '.', '.', '.', '.'], | |
['.', '.', '.', '.', '.', '.'], | |
]; | |
const board = document.createElement('div'); | |
const messages = document.createElement('div'); | |
messages.id = 'messages'; | |
document.body.append(board, messages); | |
const enemy = ['21', '22', '23']; | |
let hits = 0; | |
function fire(e) { | |
const { target } = e; | |
const hit = target.id.slice(-2); | |
const spot = document.querySelector(`#spot-${hit}`); | |
const msgs = document.querySelector('#messages'); | |
let msg = ''; | |
let viz = ''; | |
if (enemy.includes(hit)) { | |
hits++; | |
msg = hits === 3 ? 'You Win!' : 'Hit!'; | |
viz = 'x'; | |
} else { | |
msg = 'Miss!'; | |
viz = 'o'; | |
} | |
spot.innerHTML = viz; | |
msgs.innerHTML = msg; | |
} | |
spots.forEach((arrs, i) => { | |
const row = document.createElement('div'); | |
board.append(row); | |
arrs.forEach((l, j) => { | |
const spot = document.createElement('div'); | |
spot.id = `spot-${i}${j}`; | |
spot.addEventListener('click', fire); | |
spot.append(l); | |
row.append(spot); | |
}); | |
}); | |
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
div[id^='spot-'] { | |
display: inline-block; | |
width: 20px; | |
height: 20px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment