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

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

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


完整代码如下,大家可以保存到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>

相关推荐

Python入门学习记录之一:变量_python怎么用变量

写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...

python变量命名规则——来自小白的总结

python是一个动态编译类编程语言,所以程序在运行前不需要如C语言的先行编译动作,因此也只有在程序运行过程中才能发现程序的问题。基于此,python的变量就有一定的命名规范。python作为当前热门...

Python入门学习教程:第 2 章 变量与数据类型

2.1什么是变量?在编程中,变量就像一个存放数据的容器,它可以存储各种信息,并且这些信息可以被读取和修改。想象一下,变量就如同我们生活中的盒子,你可以把东西放进去,也可以随时拿出来看看,甚至可以换成...

绘制学术论文中的“三线表”具体指导

在科研过程中,大家用到最多的可能就是“三线表”。“三线表”,一般主要由三条横线构成,当然在变量名栏里也可以拆分单元格,出现更多的线。更重要的是,“三线表”也是一种数据记录规范,以“三线表”形式记录的数...

Python基础语法知识--变量和数据类型

学习Python中的变量和数据类型至关重要,因为它们构成了Python编程的基石。以下是帮助您了解Python中的变量和数据类型的分步指南:1.变量:变量在Python中用于存储数据值。它们充...

一文搞懂 Python 中的所有标点符号

反引号`无任何作用。传说Python3中它被移除是因为和单引号字符'太相似。波浪号~(按位取反符号)~被称为取反或补码运算符。它放在我们想要取反的对象前面。如果放在一个整数n...

Python变量类型和运算符_python中变量的含义

别再被小名词坑哭了:Python新手常犯的那些隐蔽错误,我用同事的真实bug拆给你看我记得有一次和同事张姐一起追查一个看似随机崩溃的脚本,最后发现罪魁祸首竟然是她把变量命名成了list。说实话...

从零开始:深入剖析 Spring Boot3 中配置文件的加载顺序

在当今的互联网软件开发领域,SpringBoot无疑是最为热门和广泛应用的框架之一。它以其强大的功能、便捷的开发体验,极大地提升了开发效率,成为众多开发者构建Web应用程序的首选。而在Spr...

Python中下划线 ‘_’ 的用法,你知道几种

Python中下划线()是一个有特殊含义和用途的符号,它可以用来表示以下几种情况:1在解释器中,下划线(_)表示上一个表达式的值,可以用来进行快速计算或测试。例如:>>>2+...

解锁Shell编程:变量_shell $变量

引言:开启Shell编程大门Shell作为用户与Linux内核之间的桥梁,为我们提供了强大的命令行交互方式。它不仅能执行简单的文件操作、进程管理,还能通过编写脚本实现复杂的自动化任务。无论是...

一文学会Python的变量命名规则!_python的变量命名有哪些要求

目录1.变量的命名原则3.内置函数尽量不要做变量4.删除变量和垃圾回收机制5.结语1.变量的命名原则①由英文字母、_(下划线)、或中文开头②变量名称只能由英文字母、数字、下画线或中文字所组成。③英文字...

更可靠的Rust-语法篇-区分语句/表达式,略览if/loop/while/for

src/main.rs://函数定义fnadd(a:i32,b:i32)->i32{a+b//末尾表达式}fnmain(){leta:i3...

C++第五课:变量的命名规则_c++中变量的命名规则

变量的命名不是想怎么起就怎么起的,而是有一套固定的规则的。具体规则:1.名字要合法:变量名必须是由字母、数字或下划线组成。例如:a,a1,a_1。2.开头不能是数字。例如:可以a1,但不能起1a。3....

Rust编程-核心篇-不安全编程_rust安全性

Unsafe的必要性Rust的所有权系统和类型系统为我们提供了强大的安全保障,但在某些情况下,我们需要突破这些限制来:与C代码交互实现底层系统编程优化性能关键代码实现某些编译器无法验证的安全操作Rus...

探秘 Python 内存管理:背后的神奇机制

在编程的世界里,内存管理就如同幕后的精密操控者,确保程序的高效运行。Python作为一种广泛使用的编程语言,其内存管理机制既巧妙又复杂,为开发者们提供了便利的同时,也展现了强大的底层控制能力。一、P...