五子棋游戏(五子棋游戏网页版)
myzbx 2025-05-08 20:12 29 浏览
- 完整的游戏功能:
- 黑白双方轮流下棋
- 自动判定胜负
- 支持重新开始游戏
- 美观的界面:
- 标准的15×15棋盘
- 棋盘带有网格线和五个星位点
- 棋子使用渐变效果,看起来更立体
- 状态显示清晰
- 用户友好:
- 实时显示当前回合信息
- 有效的落子位置检测
- 简单直观的操作方式
- 代码特性:
- 使用Canvas绘制棋盘和棋子
- 完整的胜负判定算法
- 响应式的用户交互
- 清晰的代码结构和注释
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>五子棋游戏</title>
<style>
.container {
text-align: center;
margin: 20px auto;
}
#chessboard {
border: 2px solid #8B4513;
margin: 0 auto;
background-color: #DEB887;
}
.status {
margin: 20px;
font-size: 24px;
color: #333;
}
.restart-btn {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 10px;
}
.restart-btn:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<div class="status" id="status">黑方回合</div>
<canvas id="chessboard" width="600" height="600"></canvas>
<br>
<button class="restart-btn" onclick="restartGame()">重新开始</button>
</div>
<script>
const canvas = document.getElementById('chessboard');
const ctx = canvas.getContext('2d');
const statusDiv = document.getElementById('status');
const GRID_SIZE = 40; // 格子大小
const BOARD_SIZE = 15; // 15x15的棋盘
const PIECE_RADIUS = 18; // 棋子半径
let currentPlayer = 1; // 1为黑棋,2为白棋
let gameOver = false;
let board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));
// 初始化棋盘
function initBoard() {
// 绘制棋盘背景
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(GRID_SIZE, GRID_SIZE + i * GRID_SIZE);
ctx.lineTo(GRID_SIZE * BOARD_SIZE, GRID_SIZE + i * GRID_SIZE);
ctx.stroke();
}
// 绘制竖线
for (let i = 0; i < BOARD_SIZE; i++) {
ctx.beginPath();
ctx.moveTo(GRID_SIZE + i * GRID_SIZE, GRID_SIZE);
ctx.lineTo(GRID_SIZE + i * GRID_SIZE, GRID_SIZE * BOARD_SIZE);
ctx.stroke();
}
// 绘制中心点和四个星位
const points = [[7, 7], [3, 3], [3, 11], [11, 3], [11, 11]];
points.forEach(([x, y]) => {
ctx.beginPath();
ctx.arc(GRID_SIZE + x * GRID_SIZE, GRID_SIZE + y * GRID_SIZE, 5, 0, Math.PI * 2);
ctx.fillStyle = '#000';
ctx.fill();
});
}
// 绘制棋子
function drawPiece(row, col, player) {
const x = GRID_SIZE + col * GRID_SIZE;
const y = GRID_SIZE + row * GRID_SIZE;
ctx.beginPath();
ctx.arc(x, y, PIECE_RADIUS, 0, Math.PI * 2);
// 创建渐变
const gradient = ctx.createRadialGradient(
x - 5, y - 5, 1,
x, y, PIECE_RADIUS
);
if (player === 1) { // 黑棋
gradient.addColorStop(0, '#666');
gradient.addColorStop(1, '#000');
} else { // 白棋
gradient.addColorStop(0, '#fff');
gradient.addColorStop(1, '#ccc');
}
ctx.fillStyle = gradient;
ctx.fill();
ctx.strokeStyle = player === 1 ? '#000' : '#ccc';
ctx.stroke();
}
// 检查是否获胜
function checkWin(row, col) {
const directions = [
[[0, 1], [0, -1]], // 水平
[[1, 0], [-1, 0]], // 垂直
[[1, 1], [-1, -1]], // 主对角线
[[1, -1], [-1, 1]] // 副对角线
];
for (let [dir1, dir2] of directions) {
let count = 1;
// 向一个方向查找
for (let i = 1; i < 5; i++) {
const newRow = row + dir1[0] * i;
const newCol = col + dir1[1] * i;
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) break;
if (board[newRow][newCol] !== currentPlayer) break;
count++;
}
// 向相反方向查找
for (let i = 1; i < 5; i++) {
const newRow = row + dir2[0] * i;
const newCol = col + dir2[1] * i;
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) break;
if (board[newRow][newCol] !== currentPlayer) break;
count++;
}
if (count >= 5) return true;
}
return false;
}
// 处理点击事件
canvas.addEventListener('click', (e) => {
if (gameOver) return;
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// 计算落子的行列
const col = Math.round((x - GRID_SIZE) / GRID_SIZE);
const row = Math.round((y - GRID_SIZE) / GRID_SIZE);
// 检查是否在有效范围内
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) return;
// 检查该位置是否已有棋子
if (board[row][col] !== 0) return;
// 放置棋子
board[row][col] = currentPlayer;
drawPiece(row, col, currentPlayer);
// 检查是否获胜
if (checkWin(row, col)) {
statusDiv.textContent = (currentPlayer === 1 ? '黑方' : '白方') + '获胜!';
gameOver = true;
return;
}
// 切换玩家
currentPlayer = currentPlayer === 1 ? 2 : 1;
statusDiv.textContent = (currentPlayer === 1 ? '黑方' : '白方') + '回合';
});
// 重新开始游戏
function restartGame() {
board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(0));
currentPlayer = 1;
gameOver = false;
statusDiv.textContent = '黑方回合';
initBoard();
}
// 初始化游戏
initBoard();
</script>
</body>
</html>
相关推荐
- vue3和web网页直接操作微信小程序云开发数据库
-
我们开发好小程序以后,有时候需要编写一个管理后台网页对数据库进行管理,之前我们只能借助云开发自带的cms网页,但是cms网页设计的比较丑,工作量和代码量也不够,所以我们今天就来带大家实现用vue3编写...
- WebCurl:极致轻量的跨平台 HTTP 请求调试工具
-
在接口开发与测试领域,工具的轻量化、兼容性与安全性往往直接影响工作效率。WebCurl作为一款纯原生、无依赖的网页版API测试与调试工具,凭借极简架构与全场景适配能力,重新定义了接口调试工具的使...
- webapi 全流程_webapi项目
-
C#中的WebAPIMinimalApi没有控制器,普通api有控制器,MinimalApi是直达型,精简了很多中间代码,广泛适用于微服务架构MinimalApi一切都在组控制台应用程序类【Progr...
- Nodejs之MEAN栈开发(四)-- form验证及图片上传
-
这一节增加推荐图书的提交和删除功能,来学习node的form提交以及node的图片上传功能。开始之前需要源码同学可以先在git上fork:https://github.com/stoneniqiu/R...
- CodeSpirit.Amis.AiForm 智能表单使用指南
-
概述AiForm是CodeSpirit.Amis框架的智能表单功能,专为AI驱动的长时间处理任务设计。它自动生成一个多步骤的用户界面,包含表单输入、进度监控、日志显示和结果展示等功能。功能特点...
- 初级、中级、高级前端工程师,对于form表单实现的区别
-
在React项目中使用AntDesign(Antd)的Form组件能快速构建标准化表单,特别适合中后台系统开发。以下是结合Antd的最佳实践和分层实现方案:一、基础用法:快速搭...
- Bun v0.7 大版本发布,与 Vite 牵手来破局?
-
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力。今天给大家带来...
- 「前端」界面可视化开发框架formio.js
-
简介formio是一个前端可视化开发框架,无需写css/js就可以开发一套前端界面。直接在界面内拖拽就可以完成界面的布局及开发。数据交互也是固定的,表单校验也可以配置,功能非常强大。特性使用ES6...
- 小伙给同事爬取数据竟获取不到,竟要使用这种请求方式才能获取?
-
Http请求中FormData和RequestPayload两种参数的区别?AjaxPost请求中常用的两种的形式:formdata和requestpayload一、默认的表单方式...
- WinForm + Win32 API 自定义无边框窗口实战(工业软件必备)
-
前言随着.NET9.0AOT(Ahead-of-Time)的发布,便萌生了用代码测试AOT编译效果的想法,同时评估未来是否为NanUI开发支持AOT编译的新版本。关于NanUI项目,若大家尚未了...
- DeepSeek从入门到精通(11)——网页版、APP、API使用方式比较
-
DeepSeek提供了三种主要的使用方法:网页版、手机APP和API调用。这三种方式各有特点,适用于不同的使用场景。下面从使用方法和主要特点两方面进行比较:一、网页版使用方法:直接通过浏览器访问De...
- 初略Web API Notification 桌面通知
-
HTML5WebNotificationsAPI通知接口用于向用户配置和显示桌面通知弹窗。例如,Email邮件通知、来电提醒、聊天信息提醒或任务提醒等。关于Web开发技术中Notificat...
- 什么是API网关?——驱动数字化转型的“隐形冠军”
-
什么是API网关API网关(APIGateway)是一个服务器,位于应用程序和后端服务之间,提供了一种集中式的方式来管理API的访问。它是系统的入口点,负责接收并处理来自客户端的请求,然后将请求路由...
- .NET 7使用 Entity Framework Core 制作增删改查(CRUD) Web API 教程
-
在本文中,我们将使用EntityFrameworkCore(EFCore)实现一个.NET7WebAPICRUD示例。WebAPI是一个构建HTTP服务的框架,可以从浏览器、移动设备和...
- C# ASP.NET Core Web Api 与 MVC 模式下 body 参数传递,post 参数方式
-
在ASP.NETCore中,WebAPI和MVC模式在处理HTTP请求时,特别是POST请求,使用body参数来传递数据的方式非常相似。两者都使用模型绑定来自动将请求体中的数据映射到C#对象上。下...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 简介 (30)
- HTML 响应式设计 (31)
- HTML URL 编码 (32)
- HTML Web 服务器 (31)
- HTML 表单属性 (32)
- HTML 音频 (31)
- HTML5 支持 (33)
- HTML API (36)
- HTML 总结 (32)
- HTML 全局属性 (32)
- HTML 事件 (31)
- HTML 画布 (32)
- HTTP 方法 (30)
- 键盘快捷键 (30)
- CSS 语法 (35)
- CSS 轮廓宽度 (31)
- CSS 谷歌字体 (33)
- CSS 链接 (31)
- CSS 定位 (31)
- CSS 图片库 (32)
- CSS 图像精灵 (31)
- SVG 文本 (32)
- 时钟启动 (33)
- HTML 游戏 (34)
- JS Loop For (32)