百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

网页五指棋游戏(五指棋单机)

zhezhongyun 2025-07-03 02:24 4 浏览


完整代码如下,大家可以保存到html文件点击打开,就可以看到如上效果
<!DOCTYPE html>

<html lang="zh-CN">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>五子棋游戏</title>

<style>

body {

font-family: 'Microsoft YaHei', sans-serif;

display: flex;

flex-direction: column;

align-items: center;

background-color: #f5f5dc;

margin: 0;

padding: 20px;

}


h1 {

color: #8B4513;

text-shadow: 1px 1px 2px rgba(0,0,0,0.2);

}


#game-container {

position: relative;

margin: 20px 0;

}


#chess-board {

background-color: #DEB887;

border: 2px solid #8B4513;

box-shadow: 0 0 10px rgba(0,0,0,0.3);

}

#status {

margin: 10px 0;

font-size: 18px;

font-weight: bold;

color: #8B4513;

height: 24px;

}

#controls {

margin-top: 10px;

}

button {

background-color: #8B4513;

color: white;

border: none;

padding: 8px 15px;

margin: 0 5px;

border-radius: 4px;

cursor: pointer;

font-size: 14px;

transition: background-color 0.3s;

}

button:hover {

background-color: #A0522D;

}

.piece {

position: absolute;

border-radius: 50%;

pointer-events: none;

}

.black {

background: radial-gradient(circle at 30% 30%, #666, #000);

box-shadow: 1px 1px 2px rgba(0,0,0,0.7);

}

.white {

background: radial-gradient(circle at 30% 30%, #fff, #ccc);

box-shadow: 1px 1px 2px rgba(0,0,0,0.5);

}

.last-move {

box-shadow: 0 0 5px 3px rgba(255, 215, 0, 0.7);

}

</style>

</head>

<body>

<h1>五子棋游戏</h1>

<div id="status">黑方回合</div>

<div id="game-container">

<canvas id="chess-board" width="450" height="450"></canvas>

</div>

<div id="controls">

<button id="restart">重新开始</button>

<button id="undo">悔棋</button>

</div>

<script>

document.addEventListener('DOMContentLoaded', function() {

const canvas = document.getElementById('chess-board');

const ctx = canvas.getContext('2d');

const statusDisplay = document.getElementById('status');

const restartBtn = document.getElementById('restart');

const undoBtn = document.getElementById('undo');

const BOARD_SIZE = 15;

const CELL_SIZE = canvas.width / (BOARD_SIZE + 1);

const PIECE_RADIUS = CELL_SIZE * 0.4;

let board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));

let currentPlayer = 1; // 1: 黑棋, 2: 白棋

let gameOver = false;

let moveHistory = [];

let lastMove = null;

// 初始化棋盘

function initBoard() {

// 清空棋盘

board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));

currentPlayer = 1;

gameOver = false;

moveHistory = [];

lastMove = null;

statusDisplay.textContent = '黑方回合';

// 清除所有棋子DOM元素

document.querySelectorAll('.piece').forEach(el => el.remove());

// 绘制棋盘

ctx.clearRect(0, 0, canvas.width, canvas.height);

ctx.fillStyle = '#DEB887';

ctx.fillRect(0, 0, canvas.width, canvas.height);

// 绘制网格线

ctx.strokeStyle = '#000';

ctx.lineWidth = 1;

for (let i = 0; i < BOARD_SIZE; i++) {

// 横线

ctx.beginPath();

ctx.moveTo(CELL_SIZE, CELL_SIZE * (i + 1));

ctx.lineTo(CELL_SIZE * BOARD_SIZE, CELL_SIZE * (i + 1));

ctx.stroke();

// 竖线

ctx.beginPath();

ctx.moveTo(CELL_SIZE * (i + 1), CELL_SIZE);

ctx.lineTo(CELL_SIZE * (i + 1), CELL_SIZE * BOARD_SIZE);

ctx.stroke();

}

// 绘制五个黑点

const dots = [

[3, 3], [3, 11], [7, 7], [11, 3], [11, 11]

];

ctx.fillStyle = '#000';

dots.forEach(([x, y]) => {

ctx.beginPath();

ctx.arc(

CELL_SIZE * (x + 1),

CELL_SIZE * (y + 1),

CELL_SIZE * 0.1,

0,

Math.PI * 2

);

ctx.fill();

});

}

// 放置棋子

function placePiece(x, y) {

if (gameOver || board[x][y] !== 0) return false;

board[x][y] = currentPlayer;

moveHistory.push({x, y, player: currentPlayer});

lastMove = {x, y};

// 创建棋子DOM元素

const piece = document.createElement('div');

piece.className = `piece ${currentPlayer === 1 ? 'black' : 'white'}`;

if (lastMove && lastMove.x === x && lastMove.y === y) {

piece.classList.add('last-move');

}

piece.style.width = `${PIECE_RADIUS * 2}px`;

piece.style.height = `${PIECE_RADIUS * 2}px`;

piece.style.left = `${CELL_SIZE * (x + 1) - PIECE_RADIUS}px`;

piece.style.top = `${CELL_SIZE * (y + 1) - PIECE_RADIUS}px`;

document.getElementById('game-container').appendChild(piece);

// 检查胜利

if (checkWin(x, y)) {

gameOver = true;

statusDisplay.textContent = `${currentPlayer === 1 ? '黑方' : '白方'}获胜!`;

return true;

}

// 切换玩家

currentPlayer = currentPlayer === 1 ? 2 : 1;

statusDisplay.textContent = `${currentPlayer === 1 ? '黑方' : '白方'}回合`;

return true;

}

// 检查是否获胜

function checkWin(x, y) {

const directions = [

[1, 0], [0, 1], [1, 1], [1, -1] // 横、竖、斜、反斜

];

for (const [dx, dy] of directions) {

let count = 1;

// 正向检查

for (let i = 1; i < 5; i++) {

const nx = x + dx * i;

const ny = y + dy * i;

if (nx < 0 || nx >= BOARD_SIZE || ny < 0 || ny >= BOARD_SIZE || board[nx][ny] !== currentPlayer) {

break;

}

count++;

}

// 反向检查

for (let i = 1; i < 5; i++) {

const nx = x - dx * i;

const ny = y - dy * i;

if (nx < 0 || nx >= BOARD_SIZE || ny < 0 || ny >= BOARD_SIZE || board[nx][ny] !== currentPlayer) {

break;

}

count++;

}

if (count >= 5) return true;

}

return false;

}

// 悔棋

function undoMove() {

if (gameOver || moveHistory.length === 0) return;

const lastMove = moveHistory.pop();

board[lastMove.x][lastMove.y] = 0;

currentPlayer = lastMove.player;

// 移除最后一个棋子DOM

const pieces = document.querySelectorAll('.piece');

if (pieces.length > 0) {

pieces[pieces.length - 1].remove();

}

// 如果有上一个棋子,添加高亮

if (moveHistory.length > 0) {

const prevMove = moveHistory[moveHistory.length - 1];

const prevPieces = document.querySelectorAll('.piece');

prevPieces.forEach(p => p.classList.remove('last-move'));

for (let i = 0; i < prevPieces.length; i++) {

const piece = prevPieces[i];

const pieceX = Math.round((parseInt(piece.style.left) + PIECE_RADIUS) / CELL_SIZE) - 1;

const pieceY = Math.round((parseInt(piece.style.top) + PIECE_RADIUS) / CELL_SIZE) - 1;

if (pieceX === prevMove.x && pieceY === prevMove.y) {

piece.classList.add('last-move');

break;

}

}

}

statusDisplay.textContent = `${currentPlayer === 1 ? '黑方' : '白方'}回合`;

}

// 点击事件处理

canvas.addEventListener('click', function(e) {

if (gameOver) return;

const rect = canvas.getBoundingClientRect();

const x = Math.round((e.clientX - rect.left) / CELL_SIZE) - 1;

const y = Math.round((e.clientY - rect.top) / CELL_SIZE) - 1;

if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE) {

placePiece(x, y);

}

});

// 按钮事件

restartBtn.addEventListener('click', initBoard);

undoBtn.addEventListener('click', undoMove);

// 初始化游戏

initBoard();

});

</script>

</body>

</html>

相关推荐

轻松玩转windows控制台(五):彻底搞懂屏幕缓冲区

写在前面在上一篇文章中,详细地介绍了控制台程序最容易令人困惑和混淆的地方,即什么是控制台窗口,什么是控制台屏幕缓冲区。并通过几个示例程序,演示了如何设置控制台窗口“显示区域”的大小。(轻松玩转wind...

零基础教你学前端——85、高度自适应

这节课,我们学习如何让元素的宽度和高度在容器里自适应。什么是宽高自适应呢?页面里有两个div,开始的时候宽度都是800px,当我们将浏览器窗口的宽度拖动到小于800px的时候,我们发现:上面的...

Grafana可视化平台面板之Gauge仪表和Bar Gauge条形仪表

仪表(Gauge)仪表是一种用于展示单个数值指标并跟踪其随时间变化的图表类型。它通常用于显示系统的实时状态或关键性能指标,如CPU使用率、内存占用率等。仪表通过一个圆形或半圆形的表盘来展示数值,并可以...

网页五指棋游戏(五指棋单机)

完整代码如下,大家可以保存到html文件点击打开,就可以看到如上效果<!DOCTYPEhtml><htmllang="zh-CN"><head>...

2020年度中央机关公开遴选和公开选调公务员报名即将开始

新华社北京10月27日电(记者林晖)记者27日从国家公务员局获悉,2020年度中央机关公开遴选和公开选调公务员报名即将开始,考生可于10月28日8时至11月6日18时期间,登录“2020年度中央机关公...

小程序学习日志7:可滚动的视图区域组件(滚动条)

导读经过这段日子的学习,大家对小程序的了解多了多少呢?我们这段时间学了很多组件的用法,以及这些组件的属性和属性的合法值,图片、文字、块,还了解了不少的样式代码和小程序的基本知识。我们今天来讲一个可滚动...

四叶草SEO:主要SEO作弊方法之隐藏文字

石家庄四叶草SEO小编在这里介绍黑帽,并不意味鼓励大家使用黑帽,恰恰相反,而是因为很多SEOer无意之中就使用了黑帽手法,自己却不知道。对于一个正常的商业网站和大部分个人网站来说,做好内容,正常优化,...

鸿蒙仓颉语言开发实战教程:商城搜索页

大家好,今天要分享的是仓颉语言商城应用的搜索页。搜索页的内容比较多,都有点密集恐惧症了,不过我们可以从上至下将它拆分开来,逐一击破。导航栏搜索页的的最顶部是导航栏,由返回按钮和搜索框两部分组成,比较简...

鸿蒙Next仓颉语言开发实战教程:设置页面

仓颉语言商城应用的页面开发教程接近尾声了,今天要分享的是设置页面:导航栏还是老样式,介绍过很多次了,今天不再赘述。这个页面的内容主要还是介绍List容器的使用。可以看出列表内容分为三组,所以我们要用到...

CSS box-sizing 属性详解(css中box属性有哪些)

box-sizing是CSS的一个非常重要的属性。CSS的box-sizing属性用于控制元素尺寸的计算方式,决定了元素的宽度(width)和高度(height)是否包含内边距(...

68.C# MenuStrip控件(c#基本控件)

摘要MenuStrip控件是在.NETFramework版本2.0中引入的。可以通过MenuStrip控件,轻松创建像MicrosoftOffice中的菜单。MenuStrip...

Qt使用布局管理器实现扩展对话框(qt选择文件路径对话框)

今天跟大家讲讲扩展对话框的实现;扩展对话框效果如下所示:(1)初始界面:(2)单击<More>按钮:(3)再次单击<More>按钮:这节主要讲解用布局管理器方式实现,下节讲解用...

探讨小程序开发的布局(小程序开发部署流程)

谈到小程序布局就不得不谈WXSS(WeiXinStyleSheets)!WXSS是微信小程序专用的样式语言。WXSS与css虽然在尺寸单位和样式导入不同,但是,具有CSS大部分特性。小程序的样式...

图片懒加载的现代 JavaScript 实现,仅需 10 行代码

过去,实现懒加载通常需要监听scroll事件,并结合getBoundingClientRect()等方法计算元素位置,代码不仅繁琐,而且频繁的计算会引发性能问题。现代浏览器提供了Inters...

JS实现轮播图案例(一看就懂,逻辑清晰)

1.功能分析实现如图所示的轮播图,要实现的功能主要有:鼠标经过轮播图模块,左右按钮显示,离开隐藏左右按钮。点击右侧按钮一次,图片下滑一张;点击左侧按钮,图片上滑一张。图片播放的同时,下面小圆圈模块跟...