<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Tic Tac Toe</h1>
<div class="container">
<div id="bord" class="game-board x">
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
<div id="message" class="game-message">
<p id="winner">X 赢了!</p>
<button id="restart">重新开始</button>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>
p {
margin: 0;
}
body {
background-color: #f9f2e7;
}
h1 {
text-align: center;
font-size: 60px;
color: #477998;
}
.container {
position: relative;
width: 471px;
height: 471px;
margin: 0 auto;
}
.game-message {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(69, 133, 136, 0.4);
text-align: center;
}
.game-message p {
margin: 180px 0 40px 0;
color: #fff;
font-size: 50px;
}
.game-message button {
color: #517304;
border-color: #517304;
width: 110px;
height: 40px;
font-size: 20px;
cursor: pointer;
}
.game-board {
width: 471px;
height: 471px;
}
.game-board.x .cell:not(.x):not(.o):hover::before {
content: 'X';
color: lightgray;
}
.game-board.o .cell:not(.x):not(.o):hover::before {
content: 'O';
color: lightgray;
}
.row {
display: flex;
}
.row:last-child .cell {
border-bottom: 0;
}
.cell {
flex: 1;
box-sizing: border-box;
width: 157px;
height: 157px;
line-height: 157px;
border-right: 6px solid #546363;
border-bottom: 6px solid #546363;
text-align: center;
cursor: pointer;
font-size: 88px;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, sans-serif;
}
.cell:last-child {
border-right: 0;
}
.cell.x::before {
content: 'X';
color: #01a8c6;
}
.cell.o::before {
content: 'O';
color: #8fbe01;
}
enum Player {
X = 'x',
O = 'o'
}
let winsArr = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
let cells = document.querySelectorAll('.cell')
let gameBord = document.querySelector('#bord')
let message = document.querySelector('#message') as HTMLDivElement
let winner = document.querySelector('#winner') as HTMLParagraphElement
let restart = document.querySelector('#restart') as HTMLButtonElement
let currentPlayer: Player
let steps: number
startGame()
restart.addEventListener('click', startGame)
function startGame() {
message.style.display = 'none'
steps = 0
currentPlayer = Player.X
gameBord.classList.remove(Player.X, Player.O)
gameBord.classList.add(Player.X)
cells.forEach(function (item) {
let cell = item as HTMLDivElement
cell.classList.remove(Player.X, Player.O)
cell.removeEventListener('click', clickCell)
cell.addEventListener('click', clickCell, { once: true })
})
}
function clickCell(event: MouseEvent) {
let target = event.target as HTMLDivElement
target.classList.add(currentPlayer)
steps++
let isWin = checkWin(currentPlayer)
if (isWin) {
message.style.display = 'block'
winner.innerText = currentPlayer + ' 赢了!'
return
}
if (steps === 9) {
message.style.display = 'block'
winner.innerText = '平局'
return
}
currentPlayer = currentPlayer === Player.X ? Player.O : Player.X
gameBord.classList.remove(Player.X, Player.O)
gameBord.classList.add(currentPlayer)
}
function checkWin(player: Player) {
return winsArr.some(function (item) {
let cellIndex1 = item[0]
let cellIndex2 = item[1]
let cellIndex3 = item[2]
if (
hasClass(cells[cellIndex1], player) &&
hasClass(cells[cellIndex2], player) &&
hasClass(cells[cellIndex3], player)
) {
return true
}
return false
})
}
function hasClass(el: Element, name: string) {
return el.classList.contains(name)
}