Skip to content

Instantly share code, notes, and snippets.

@bradevans
Created December 21, 2020 22:52
Show Gist options
  • Save bradevans/ef02c02113f011d95d57d796d627a647 to your computer and use it in GitHub Desktop.
Save bradevans/ef02c02113f011d95d57d796d627a647 to your computer and use it in GitHub Desktop.
VwKzqwd
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);
});
});
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