LARK-Bingo

by Soya Natsuki

HTML

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<html>

  <head>

  </head>

  <body>
    <div class="main-container">
      <table id="board">
        <tr>
          <th class="th-corner"></th>
          <th>A</th>
          <th>B</th>
          <th>C</th>
          <th>D</th>
          <th>E</th>
        </tr>
        <tr>
          <th class="th-vertical">1</th>
          <td id="cell-a1" class="cell">
            <span class="board-label">A1</span>
          </td>
          <td id="cell-b1" class="cell">
            <span class="board-label">B1</span>
          </td>
          <td id="cell-c1" class="cell">
            <span class="board-label">C1</span>
          </td>
          <td id="cell-d1" class="cell">
            <span class="board-label">D1</span>
          </td>
          <td id="cell-e1" class="cell">
            <span class="board-label">E1</span>
          </td>
        </tr>
        <tr>
          <th class="th-vertical">2</th>
          <td id="cell-a2" class="cell">
            <span class="board-label">A2</span>
          </td>
          <td id="cell-b2" class="cell">
            <span class="board-label">B2</span>
          </td>
          <td id="cell-c2" class="cell">
            <span class="board-label">C2</span>
          </td>
          <td id="cell-d2" class="cell">
            <span class="board-label">D2</span>
          </td>
          <td id="cell-e2" class="cell">
            <span class="board-label">E2</span>
          </td>
        </tr>
        <tr>
          <th class="th-vertical">3</th>
          <td id="cell-a3" class="cell">
            <span class="board-label">A3</span>
          </td>
          <td id="cell-b3" class="cell">
            <span class="board-label">B3</span>
          </td>
          <td id="cell-c3" class="cell">
            <span...

CSS

body {
  margin: 10px;
  padding: 0px;
}

.main-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}

#board {
  border: 1px solid;
  transform: rotate(45deg);
}

#board th {
  text-align: center;
  vertical-align: middle;
  border: 1px solid;
  background-color: #ddd;
  line-height: 1em;
  height: 30px;
  width: 100px;
}

#board th.th-vertical {
  height: 100px;
  width: 30px;
}

#board th.th-corner {
  width: 30px;
}

#board tr {
  border: 1px solid;
}

#board td {
  position: relative;
  border: 1px solid;
  height: 100px;
  width: 100px;
  transition: all 0.3s;
  transition-timing-function: linear;
}

#board td:hover {
  background-color: #ddd;
  transition: all 0.3s;
  transition-timing-function: linear;
}

#board td.active {
  background-color: #bbb;
}

#board td.bingo {
  background-color: #d46161;
}

.board-label {
  position: absolute;
  top: 0px;
  right: 0px;
  padding: 0 5px;
  border-left: 1px solid;
  border-bottom: 1px solid;
  background-color: #aaa;
}

#tool-box {
  margin-top: 200px;
}

JavaScript

let storedCellState;

const boardMap = [
	['a1', 'b1', 'c1', 'd1', 'e1'],
  ['a2', 'b2', 'c2', 'd2', 'e2'],
  ['a3', 'b3', 'c3', 'd3', 'e3'],
  ['a4', 'b4', 'c4', 'd4', 'e4'],
  ['a5', 'b5', 'c5', 'd5', 'e5']
];

$('.cell').on('click', function() {
	const cellId = $(this).attr('id');
  const trimmedId = cellId.split('-')[1];
  
  saveCellState();
  flipCell(trimmedId);
});

$('.cell').on('dblclick', function() {
	$(this).toggleClass('active');
});

$('#prev').on('click', function() {
	resetCellState();
	restoreCellState();
});

$('#reset').on('click', function() {
	resetCellState();
});

const flipCell = (cellId) => {
	let targetX, targetY;
  let targetArr;

	for(let i = 0; i < 5; i++) {
  	for(let j = 0; j < 5; j++) {
    	if(boardMap[i][j] == cellId) {
      	targetX = i;
        targetY = j;
      }
    }
  }
  
  targetArr = checkTarget(targetX, targetY);
  targetArr.forEach(t => {
  	$(`#cell-${t}`).toggleClass('active');
	});
  
  checkAllBingo();
};

const checkTarget = (x, y) => {
	let tmpArr = [];
  tmpArr.push(boardMap[x][y]);
  
  if((x + 1 >= 0 && x + 1 <= 4) && (y >= 0 && y <= 4)) {
  	tmpArr.push(boardMap[x + 1][y]);
  }
  if((x >= 0 && x <= 4) && (y + 1 >= 0 && y + 1 <= 4)) {
  	tmpArr.push(boardMap[x][y + 1]);
  }
  if((x - 1 >= 0 && x - 1 <= 4) && (y >= 0 && y <= 4)) {
  	tmpArr.push(boardMap[x - 1][y]);
  }
  if((x >= 0 && x <= 4) && (y - 1 >= 0 && y - 1 <= 4)) {
  	tmpArr.push(boardMap[x][y - 1]);
  }
  
  return tmpArr;
};

const checkAllBingo = () => {
	let bingoCells = [];
  
	for(let i = 0; i < 5; i++) {
  	for(let j = 0; j < 5; j++) {
    	bingoCells = bingoCells.concat(checkStraightBingo(i, j));
    }
  }
  
  bingoCells = bingoCells.concat(checkDiagonalBingo());
  
  flipBingo(bingoCells);
};

const checkStraightBingo = (x, y) => {
	const cellState = checkCellState();
  let rowBingo = true, colBingo = true;
  let bingoCells = [];
  
  for(let i = 0; i < 5; i++) {
  	if(cellState[x][i] == 0) {
    	rowBingo = false;
    }
  }
  
 ...