JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div id="board" class="container col-sm-12">
</div>
CSS
#board {
margin: auto;
}
.card-back {
background-color: #2EC4B6 !important;
}
.tile {
height: 0;
padding-bottom: 25%;
width: 25%;
margin: 5px;
border-radius: 10px;
}
JavaScript
var jsBoard = [];
var gamePieces = 12;
// create all the matched hexidecimal tiles
// to do: why the fuck am I only sometimes generating 10 colors?
var generateTiles = function() {
for (var i = 0; i <= gamePieces / 2 - 1; i++) {
var firstColor = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
var secondColor = firstColor;
jsBoard.push(firstColor, secondColor);
}
}
generateTiles();
// shuffle those bitches
function shuffle(jsBoard) {
var currentIndex = jsBoard.length,
temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = jsBoard[currentIndex];
jsBoard[currentIndex] = jsBoard[randomIndex];
jsBoard[randomIndex] = temporaryValue;
}
}
shuffle(jsBoard);
var showTile = function() {
var tile = document.getElementsByClassName('tile');
var tilesChosen = 0;
console.log(this.style.backgroundColor);
console.log(this);
if (tilesChosen < 2) {
this.classList.remove('card-back');
tilesChosen = tilesChosen + 1;
console.log('under 2');
console.log(tilesChosen);
}
else if (tilesChosen == 2) {
tile.removeEventListener('click', function() {
console.log('hit the max');
})
}
};
// grab dom board and append divs with classes and background colors from jsBoard
var generateBoard = function() {
var domBoard = document.getElementById('board');
for (var i = 0; i <= jsBoard.length - 1; i++) {
var tile = document.createElement('div');
tile.className += 'card-back tile col-xs-3'
tile.style.backgroundColor = jsBoard[i];
tile.addEventListener('click', showTile);
domBoard.append(tile);
console.log(tile.style.backgroundColor);
}
}
generateBoard();