비전공자 개발일기

Tic Tae Toc 본문

Javascript

Tic Tae Toc

HiroDaegu 2022. 7. 9. 00:54
728x90
SMALL

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>TIC TAC TOE</title>
  <link rel="stylesheet" href="style.css">
  <script defer src="main.js"></script>
</head>
<body>
  <div class="header">
    <h1>Tic Tac Toe</h1>
  </div>
  <div class="box">
    <div class="row">
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
    </div>
    <div class="row">
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
    </div>
    <div class="row">
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
      <input class="btn" type="text" readonly>
    </div>
  </div>
  <p class="result">Player X Turn</p>
  <div class="reset">
    <button id="reset">Reset</button>
  </div>
</body>
</html>
@import url("https://fonts.googleapis.com/css2?family=Radio+Canada:wght@400;500;600;700&display=swap");
* {
  padding: 0;
  margin: 0;
  font-family: "Radio Canada", sans-serif;
}
body {
  background-color: white;
}
.header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 15px;
}
.header h1 {
  text-align: center;
}
.box {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  margin: 35px;
}
.row {
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn {
  border: 1px solid black;
  border-radius: 5px;
  height: 75px;
  margin: 2px;
  width: 75px;
  text-align: center;
  font-size: 50px;
  font-weight: 500;
}
.btn:hover {
  cursor: pointer;
  background-color: rgb(240, 240, 240);
}
.btn:disabled {
  color: black;
  cursor: no-drop;
}
.result {
  text-align: center;
}
.reset {
  text-align: center;
}
#reset {
  border: 1px solid black;
  cursor: pointer;
  color: black;
  background-color: white;
  border-radius: 5px;
  padding: 5px 20px;
  font-size: 17px;
  margin: 35px;
  transition: all 0.1s ease-in-out;
}
#reset:hover {
  color: white;
  background-color: black;
}
let cells = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let result = document.querySelector('.result');
let btns = document.querySelectorAll('.btn');
let conditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
];
const ticTacToe = (element, index) => {
    element.value = currentPlayer;
    element.disabled = true;
    cells[index] = currentPlayer;
    currentPlayer = currentPlayer == 'X' ? 'O' : 'X';
    result.innerHTML = `Player ${currentPlayer} Turn`;
    for (let i = 0; i < conditions.length; i++) {
        let condition = conditions[i];
        let a = cells[condition[0]];
        let b = cells[condition[1]];
        let c = cells[condition[2]];
        if (a == '' || b == '' || c == '') {
            continue;
        }
        if ((a == b) && (b == c)) {
            result.innerHTML = `Player ${a} Won 🎉`;
            btns.forEach((btn) => btn.disabled = true);
        }
    }
};
function reset() {
    cells = ['', '', '', '', '', '', '', '', ''];
    btns.forEach((btn) => {
        btn.value = '';
    });
    currentPlayer = 'X';
    result.innerHTML = `Player X Turn`;
    btns.forEach((btn) => btn.disabled = false);
};
document.querySelector('#reset').addEventListener('click', reset);
btns.forEach((btn, i) => {
    btn.addEventListener('click', () => ticTacToe(btn, i));
});
728x90
LIST

'Javascript' 카테고리의 다른 글

ColorFul Rain  (0) 2022.07.31
Notification Bell  (0) 2022.07.27
Chatbot  (0) 2022.07.08
QR code Generator  (0) 2022.07.07
Heart Animation Effects  (0) 2022.07.06