帮我用html css js 写一个五子棋对战
时间: 2023-11-21 14:06:24 浏览: 42
好的,我可以给你提供一个简单的五子棋对战的示例代码,但需要注意的是,这个示例只是一个基础的版本,还有许多改进的空间。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>五子棋对战</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin-top: 30px;
}
.cell {
width: 40px;
height: 40px;
border: 1px solid #000;
background-color: #fff;
box-sizing: border-box;
cursor: pointer;
}
.black {
background-color: #000;
}
.white {
background-color: #fff;
border-color: #000;
}
.message {
margin-top: 20px;
text-align: center;
font-size: 20px;
}
button {
margin-top: 20px;
font-size: 16px;
padding: 10px 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container"></div>
<div class="message"></div>
<button>重新开始</button>
<script src="app.js"></script>
</body>
</html>
```
CSS 代码:
```css
body {
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin-top: 30px;
}
.cell {
width: 40px;
height: 40px;
border: 1px solid #000;
background-color: #fff;
box-sizing: border-box;
cursor: pointer;
}
.black {
background-color: #000;
}
.white {
background-color: #fff;
border-color: #000;
}
.message {
margin-top: 20px;
text-align: center;
font-size: 20px;
}
button {
margin-top: 20px;
font-size: 16px;
padding: 10px 20px;
cursor: pointer;
}
```
JS 代码:
```javascript
const container = document.querySelector('.container');
const message = document.querySelector('.message');
const restartBtn = document.querySelector('button');
const ROWS = 15;
const COLS = 15;
const CELL_SIZE = 40;
let board = [];
let isBlack = true;
let gameOver = false;
function initBoard() {
for (let i = 0; i < ROWS; i++) {
board[i] = [];
for (let j = 0; j < COLS; j++) {
board[i][j] = '';
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', handleClick);
container.appendChild(cell);
}
}
}
function handleClick(event) {
if (gameOver) return;
const row = parseInt(event.target.dataset.row);
const col = parseInt(event.target.dataset.col);
if (board[row][col] !== '') return;
const color = isBlack ? 'black' : 'white';
event.target.classList.add(color);
board[row][col] = color;
if (checkWin(row, col, color)) {
message.textContent = color === 'black' ? '黑方胜利!' : '白方胜利!';
gameOver = true;
} else {
isBlack = !isBlack;
message.textContent = isBlack ? '黑方落子' : '白方落子';
}
}
function checkWin(row, col, color) {
let count = 1;
let i = 1;
while (row + i < ROWS && board[row + i][col] === color) {
count++;
i++;
}
i = 1;
while (row - i >= 0 && board[row - i][col] === color) {
count++;
i++;
}
if (count >= 5) return true;
count = 1;
i = 1;
while (col + i < COLS && board[row][col + i] === color) {
count++;
i++;
}
i = 1;
while (col - i >= 0 && board[row][col - i] === color) {
count++;
i++;
}
if (count >= 5) return true;
count = 1;
i = 1;
while (row + i < ROWS && col + i < COLS && board[row + i][col + i] === color) {
count++;
i++;
}
i = 1;
while (row - i >= 0 && col - i >= 0 && board[row - i][col - i] === color) {
count++;
i++;
}
if (count >= 5) return true;
count = 1;
i = 1;
while (row + i < ROWS && col - i >= 0 && board[row + i][col - i] === color) {
count++;
i++;
}
i = 1;
while (row - i >= 0 && col + i < COLS && board[row - i][col + i] === color) {
count++;
i++;
}
if (count >= 5) return true;
return false;
}
function restart() {
board = [];
isBlack = true;
gameOver = false;
message.textContent = '黑方落子';
container.innerHTML = '';
initBoard();
}
initBoard();
restartBtn.addEventListener('click', restart);
```
这个示例代码使用了 HTML、CSS 和 JavaScript 来实现一个简单的五子棋对战,包括了棋盘的绘制、点击事件的处理、胜负判断、重新开始等功能。你可以根据自己的需求进行修改和完善。