井字棋可以通过HTML、CSS和JavaScript的组合来实现。以下是实现井字棋的步骤:
HTML结构
创建一个简单的HTML文件,包含一个3x3的网格,用于放置井字棋的格子。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>井字棋</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<div class="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<div class="status">当前玩家: X</div>
<button class="reset">重新开始</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS样式
为井字棋添加样式,使其看起来像一个标准的3x3网格。
css
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.game-container {
text-align: center;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
font-size: 48px;
cursor: pointer;
}
.status {
font-size: 24px;
margin-bottom: 20px;
}
.reset {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
JavaScript逻辑
添加JavaScript代码,实现井字棋的游戏逻辑,包括玩家切换、胜负判断和重新开始功能。
javascript
const cells = document.querySelectorAll('.cell');
const statusDisplay = document.querySelector('.status');
const resetButton = document.querySelector('.reset');
let currentPlayer = 'X';
let gameState = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function handleCellClick(e) {
const clickedCell = e.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-index'));
if (gameState[clickedCellIndex] !== '' || !gameActive) {
return;
}
gameState[clickedCellIndex] = currentPlayer;
clickedCell.textContent = currentPlayer;
checkResult();
}
function checkResult() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const [a, b, c] = winningConditions[i];
if (gameState[a] === '' || gameState[b] === '' || gameState[c] === '') {
continue;
}
if (gameState[a] === gameState[b] && gameState[b] === gameState[c]) {
roundWon = true;
break;
}
}
if (roundWon) {
statusDisplay.textContent = `玩家 ${currentPlayer} 获胜!`;
gameActive = false;
return;
}
if (!gameState.includes('')) {
statusDisplay.textContent = '平局!';
gameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
statusDisplay.textContent = `当前玩家: ${currentPlayer}`;
}
function resetGame() {
currentPlayer = 'X';
gameState = ['', '', '', '', '', '', '', '', ''];
gameActive = true;
statusDisplay.textContent = `当前玩家: ${currentPlayer}`;
cells.forEach(cell => cell.textContent = '');
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
resetButton.addEventListener('click', resetGame);
功能说明
- 玩家切换:每次点击格子后,玩家会在X和O之间切换。
- 胜负判断:检查是否有玩家连成一条线(横、竖或斜)。
- 平局判断:如果所有格子被填满且没有玩家获胜,则判定为平局。
- 重新开始:点击"重新开始"按钮可以重置游戏状态。
将上述代码分别保存为HTML、CSS和JavaScript文件,并在浏览器中打开HTML文件即可运行井字棋游戏。