html制作一个五子棋游戏
时间: 2024-01-20 07:04:07 浏览: 65
抱歉,我是一名语言模型,无法提供图形界面的编程服务。但我可以为您提供一些五子棋游戏的HTML代码参考:
```html
<!DOCTYPE html>
<html>
<head>
<title>五子棋游戏</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
background-color: #F5F5F5;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #333;
margin-top: 50px;
}
#board {
margin: 0 auto;
margin-top: 50px;
width: 500px;
height: 500px;
background-color: #FFC107;
display: flex;
flex-wrap: wrap;
}
.cell {
box-sizing: border-box;
width: 20%;
height: 20%;
border: 2px solid #333;
cursor: pointer;
position: relative;
}
.piece {
width: 80%;
height: 80%;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.black {
background-color: #333;
}
.white {
background-color: #FFF;
border: 2px solid #333;
}
.message {
text-align: center;
color: #333;
font-size: 24px;
font-weight: bold;
margin-top: 50px;
}
.restart {
display: block;
margin: 0 auto;
margin-top: 50px;
padding: 10px 20px;
background-color: #333;
color: #FFF;
border: none;
cursor: pointer;
font-size: 18px;
font-weight: bold;
border-radius: 5px;
}
.restart:hover {
background-color: #FFF;
color: #333;
border: 2px solid #333;
}
</style>
</head>
<body>
<h1>五子棋游戏</h1>
<div id="board"></div>
<div class="message"></div>
<button class="restart">重新开始</button>
<script>
var board = document.getElementById('board');
var message = document.querySelector('.message');
var restart = document.querySelector('.restart');
var player = 1;
var gameover = false;
var boardData = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
];
// 创建棋盘
for (var i = 0; i < 25; i++) {
var cell = document.createElement('div');
cell.classList.add('cell');
cell.setAttribute('data-row', Math.floor(i / 5));
cell.setAttribute('data-col', i % 5);
cell.addEventListener('click', cellClick);
board.appendChild(cell);
}
// 点击棋盘格子
function cellClick() {
if (gameover) {
return;
}
var row = parseInt(this.getAttribute('data-row'));
var col = parseInt(this.getAttribute('data-col'));
if (boardData[row][col] !== 0) {
return;
}
boardData[row][col] = player;
var piece = document.createElement('div');
piece.classList.add('piece');
piece.classList.add(player === 1 ? 'black' : 'white');
this.appendChild(piece);
if (checkWin(row, col)) {
gameover = true;
message.innerHTML = '玩家 ' + player + ' 获胜!';
return;
}
if (checkTie()) {
gameover = true;
message.innerHTML = '平局!';
return;
}
player = player === 1 ? 2 : 1;
}
// 检查是否胜利
function checkWin(row, col) {
var count = 0;
// 水平方向
for (var c = 0; c < 5; c++) {
if (boardData[row][c] === player) {
count++;
} else {
count = 0;
}
if (count === 5) {
return true;
}
}
// 垂直方向
count = 0;
for (var r = 0; r < 5; r++) {
if (boardData[r][col] === player) {
count++;
} else {
count = 0;
}
if (count === 5) {
return true;
}
}
// 左斜方向
count = 0;
var startRow = Math.max(0, row - col);
var startCol = Math.max(0, col - row);
for (var i = 0; i < 5; i++) {
if (startRow + i > 4 || startCol + i > 4) {
break;
}
if (boardData[startRow + i][startCol + i] === player) {
count++;
} else {
count = 0;
}
if (count === 5) {
return true;
}
}
// 右斜方向
count = 0;
var endRow = Math.min(4, row + col);
var endCol = Math.min(4, col + row);
for (var i = 0; i < 5; i++) {
if (endRow - i < 0 || endCol - i < 0) {
break;
}
if (boardData[endRow - i][endCol - i] === player) {
count++;
} else {
count = 0;
}
if (count === 5) {
return true;
}
}
return false;
}
// 检查是否平局
function checkTie() {
for (var r = 0; r < 5; r++) {
for (var c = 0; c < 5; c++) {
if (boardData[r][c] === 0) {
return false;
}
}
}
return true;
}
// 重新开始游戏
restart.addEventListener('click', function() {
boardData = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
];
player = 1;
gameover = false;
message.innerHTML = '';
var pieces = document.querySelectorAll('.piece');
for (var i = 0; i < pieces.length; i++) {
pieces[i].parentNode.removeChild(pieces[i]);
}
});
</script>
</body>
</html>
```
这是一个基本的五子棋游戏,使用HTML、CSS和JavaScript编写。在这个游戏中,玩家可以点击棋盘上的空格子放置棋子,黑白双方轮流下棋,先连成五子的一方获胜,如果棋盘已满且没有任何一方获胜,则为平局。游戏结束后,可以点击“重新开始”按钮重新开始游戏。
阅读全文