web 五子棋小游戏

news/2024/12/17 16:03:35/

效果图
请添加图片描述

源码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Gomoku</title><link rel="stylesheet" href="./style/css/index.css">
</head><body><div class="container"><table class="chessboard"></table></div><script src="./js/index.js"></script>
</body></html>
.container{border: 3px double rgb(207, 188 , 188);width: 500px;height: 500px;margin: 50px auto;display: flex;justify-content: center;align-items: center;background-color: antiquewhite;
}
.chessboard{width: 92%;height: 92%;border-collapse: collapse;
}
.chessboard td{border: 1px solid black;position: relative;
}.chess{border:1px solid lightgrey;border-radius: 50%;position: absolute;left: -50%;top: -50%;width: 90%;height: 90%;color: lightgrey;font-size: 12px;font-weight: bold;display: flex;justify-content: center;align-items: center;
}.white{background-color: white;
}
.black{background-color: black;}
const $ = selector => document.querySelector(selector)
const $$ = selector => document.querySelectorAll(selector)
const chessboard = $('.chessboard')
let isGameOver = false
let currentPlayer = 'black'
const chessArr = []
const initChessboard = () => {let tableContent = ''for (let i = 0; i < 14; i++) {let row = '<tr>'for (let ii = 0; ii < 14; ii++) {row += `<td data-row="${i}" data-col="${ii}"></td>`}row += '</tr>'tableContent += row}chessboard.innerHTML = tableContent
}
const end = () => {isGameOver = truefor (let i = 0; i < chessArr.length; i++) {const e = chessArr[i];$(`div[data-row="${e.positionY}"][data-col="${e.positionX}"]`).innerHTML = i + 1}
}
const check = () => {// 检查有没有竖着的五子for (const curChess of chessArr) {let chess2, chess3, chess4, chess5console.log(curChess)chess2 = chessArr.some(item => item.positionX === curChess.positionX && item.positionY === curChess.positionY + 1 && item.color === curChess.color)chess3 = chessArr.some(item => item.positionX === curChess.positionX && item.positionY === curChess.positionY + 2 && item.color === curChess.color)chess4 = chessArr.some(item => item.positionX === curChess.positionX && item.positionY === curChess.positionY + 3 && item.color === curChess.color)chess5 = chessArr.some(item => item.positionX === curChess.positionX && item.positionY === curChess.positionY + 4 && item.color === curChess.color)if (chess2 && chess3 && chess4 && chess5) {end()}}// 检查有没有横着的五子for (const curChess of chessArr) {let chess2, chess3, chess4, chess5chess2 = chessArr.some(item => item.positionX === curChess.positionX + 1 && item.positionY === curChess.positionY && item.color === curChess.color)chess3 = chessArr.some(item => item.positionX === curChess.positionX + 2 && item.positionY === curChess.positionY && item.color === curChess.color)chess4 = chessArr.some(item => item.positionX === curChess.positionX + 3 && item.positionY === curChess.positionY && item.color === curChess.color)chess5 = chessArr.some(item => item.positionX === curChess.positionX + 4 && item.positionY === curChess.positionY && item.color === curChess.color)console.log(chess2, chess3, chess4, chess5, chessArr)if (chess2 && chess3 && chess4 && chess5) {end()}}// 检查有没有斜着的五子for (const curChess of chessArr) {let chess2, chess3, chess4, chess5chess2 = chessArr.some(item => item.positionX === curChess.positionX + 1 && item.positionY === curChess.positionY + 1)chess3 = chessArr.some(item => item.positionX === curChess.positionX + 2 && item.positionY === curChess.positionY + 2)chess4 = chessArr.some(item => item.positionX === curChess.positionX + 3 && item.positionY === curChess.positionY + 3)chess5 = chessArr.some(item => item.positionX === curChess.positionX + 4 && item.positionY === curChess.positionY + 4)if (chess2 && chess3 && chess4 && chess5) {end()}}// 检查有没有斜着的五子for (const curChess of chessArr) {let chess2, chess3, chess4, chess5chess2 = chessArr.some(item => item.positionX === curChess.positionX - 1 && item.positionY === curChess.positionY + 1)chess3 = chessArr.some(item => item.positionX === curChess.positionX - 2 && item.positionY === curChess.positionY + 2)chess4 = chessArr.some(item => item.positionX === curChess.positionX - 3 && item.positionY === curChess.positionY + 3)chess5 = chessArr.some(item => item.positionX === curChess.positionX - 4 && item.positionY === curChess.positionY + 4)if (chess2 && chess3 && chess4 && chess5) {end()}}
}
const exists = (row, col) => chessArr.some(item => item.row === row && item.col === col)
const chessDrawing = chessInfo => {if (exists(chessInfo) || isGameOver) return void 0chessArr.push(chessInfo)const newChess = `<div class="chess ${chessInfo.color}" data-row="${chessInfo.positionY}" data-col="${chessInfo.positionX}"></div>`console.log(newChess)if (chessInfo.positionX < 14 && chessInfo.positionY < 14) {$(`td[data-row="${chessInfo.positionY}"][data-col="${chessInfo.positionX}"]`).innerHTML += newChess} else if (chessInfo.positionX === 14 && chessInfo.positionY < 14) {const tdPos = $(`td[data-row="${chessInfo.positionY}"][data-col="13"]`)tdPos.innerHTML += newChesstdPos.lastChild.style.left = '50%'} else if (chessInfo.positionX < 14 && chessInfo.positionY === 14) {const tdPos = $(`td[data-row="13"][data-col="${chessInfo.positionX}"]`)tdPos.innerHTML += newChesstdPos.lastChild.style.top = '50%'} else if (chessInfo.positionX === 14 && chessInfo.positionY === 14) {const tdPos = $(`td[data-row="13"][data-col="13"]`)tdPos.innerHTML += newChesstdPos.lastChild.style.top = '50%'tdPos.lastChild.style.left = '50%'}currentPlayer = currentPlayer === 'black' ? 'white' : 'black'check()
}const bindEvent = () => {chessboard.addEventListener('click', e => {if (e.target.nodeName !== 'TD') return void 0if (isGameOver) return alert('游戏已结束')const temp = Object.assign({}, e.target.dataset)const tdw = chessboard.clientWidth * 0.92 / 14const tdh = chessboard.clientHeight * 0.92 / 14const positionX = e.offsetX > tdw / 2const positionY = e.offsetY > tdh / 2//确定棋子信息const chessInfo = {positionX: positionX ? parseInt(temp.col) + 1 : parseInt(temp.col),positionY: positionY ? parseInt(temp.row) + 1 : parseInt(temp.row),color: currentPlayer}chessDrawing(chessInfo)})
}
const main = () => {//初始化棋盘initChessboard()//绑定对应事件bindEvent()
}main()

--------------------2024-12-15号更新-----------------------

主要更新了js 优化了性能

const $ = selector => document.querySelector(selector);
const $$ = selector => document.querySelectorAll(selector);// 获取棋盘元素
const chessboard = $('.chessboard');// 游戏状态变量
let isGameOver = false;
let currentPlayer = 'black';
const chessArr = [];// 初始化棋盘
const initChessboard = () => {const rows = Array.from({ length: 14 }, (_, rowIndex) =>`<tr>${Array.from({ length: 14 }, (_, colIndex) =>`<td data-row="${rowIndex}" data-col="${colIndex}"></td>`).join('')}</tr>`).join('');chessboard.innerHTML = rows;
};// 结束游戏
const end = () => {isGameOver = true;for (let i = 0; i < chessArr.length; i++) {const e = chessArr[i];const chessElement = $(`div[data-row="${e.positionY}"][data-col="${e.positionX}"]`);// 创建一个新的元素来显示序号const numberElement = document.createElement('span');numberElement.textContent = i + 1;numberElement.classList.add('end-number'); // 添加一个类以便应用样式// 将序号元素添加到棋子中if (!chessElement.querySelector('.end-number')) {chessElement.appendChild(numberElement);}}
};// 检查是否有五子连珠
const check = () => {for (let i = chessArr.length - 1; i >= 0; i--) {const curChess = chessArr[i];const directions = [{ dx: 0, dy: 1 }, // 竖直{ dx: 1, dy: 0 }, // 水平{ dx: 1, dy: 1 }, // 斜对角(右下){ dx: -1, dy: 1 } // 斜对角(左下)];for (const dir of directions) {let count = 1;for (let step = 1; step < 5; step++) {const x = curChess.positionX + dir.dx * step;const y = curChess.positionY + dir.dy * step;const isExist = chessArr.some(item => item.positionX === x && item.positionY === y && item.color === curChess.color)if (x >= 0 && x < 14 && y >= 0 && y < 14 && isExist) {count++;} else {break;}}count >= 5 && end()}}
};// 检查是否存在相同位置的棋子
const exists = (positionX, positionY) =>chessArr.some(item => item.positionX === positionX && item.positionY === positionY);// 绘制棋子
const chessDrawing = ({ positionX, positionY, color }) => {if (exists(positionX, positionY) || isGameOver) return;chessArr.push({ positionX, positionY, color });const newChess = document.createElement('div');newChess.classList.add('chess', color);newChess.setAttribute('data-row', positionY);newChess.setAttribute('data-col', positionX);const td = $(`td[data-row="${positionY}"][data-col="${positionX}"]`);td.appendChild(newChess);currentPlayer = currentPlayer === 'black' ? 'white' : 'black';check();
};// 绑定事件监听器
const bindEvent = () => {chessboard.addEventListener('click', e => {if (e.target.nodeName !== 'TD' || isGameOver) return;const { row, col } = e.target.dataset;const positionX = parseInt(col);const positionY = parseInt(row);// 确定落子位置const offsetX = e.offsetX > (chessboard.clientWidth * 0.92 / 14) / 2;const offsetY = e.offsetY > (chessboard.clientHeight * 0.92 / 14) / 2;const chessInfo = {positionX: offsetX ? positionX + 1 : positionX,positionY: offsetY ? positionY + 1 : positionY,color: currentPlayer};chessDrawing(chessInfo);});
};// 主函数
const main = () => {initChessboard();bindEvent();
};main();

http://www.ppmy.cn/news/1555895.html

相关文章

Repo管理

文章目录 前言Repo介绍清单仓库清单仓库的组成 初始化Repo同步远程仓库Repo实际应用 前言 我们知道&#xff0c;Git是用来管理某一个仓库&#xff0c;那当一个项目用到了多个仓库时&#xff0c;怎么来同步管理这些仓库呢&#xff1f;这个时候就可以引入Repo管理。 Repo介绍 …

实战 | 某院校小程序记录

视频教程在我主页简介里 目录&#xff1a; 前言&#xff1a; 渗透思路 1.绕过前端 2.信息泄露 3.爆破用户账号密码 4.信息泄露2 结束 前言&#xff1a; 遇到一个学校小程序的站点&#xff0c;只在前端登录口做了校验&#xff0c;后端没有任何校验&#xff0c;奇葩弱口令离…

吉利百度发表联合声明:将积极协助极越处理相关事宜

吉利百度发表联合声明&#xff1a;将积极协助极越处理相关事宜 12月13日&#xff0c;吉利和百度发表关于极越汽车的联合声明&#xff0c;作为股东&#xff0c;将积极协助集度管理层妥善处理相关事宜&#xff1a;第一时间解决员工社保缴纳、离职员工问题&#xff1b;维护用户车…

生信技能65 - SRA数据库公共数据自动化下载及SRA批量自动化拆分

根据NCBI Metadata数据表,实现SRA数据库公共数据自动化下载及SRA批量自动化拆分。 1. 程序逻辑 根据SraRunTable.csv自动从公共数据库下载SRA文件 ;模式0(默认)为下载模式,模式1为拆分模式,拆分支持进度显示;提取Metadata关键信息数据,重新写入新的文本文件。2. 运行示…

k8s的污点与容忍度

污点&#xff08;Taint&#xff09;针对节点来说&#xff0c;和节点亲和性正好相对&#xff0c;节点亲和性使Pod被吸引到一类特定的节点&#xff0c;而污点则使节点能够排斥一类特定的Pod。 容忍度&#xff08;Toleration&#xff09;应用于Pod上&#xff0c;它用来允许调度器…

Stable Diffusion Controlnet常用控制类型解析与实战课程 4

本节内容&#xff0c;是stable diffusion Controlnet常用控制类型解析与实战的第四节课程。上节课程&#xff0c;我们陆续讲解了几个与图像风格约束相关的控制类型&#xff0c;本节课程我们再学习一些实用价值较高的控制类型&#xff0c;看一看他们提供了哪些控制思路。 一&…

数据结构与算法——深度优先搜索算法

系列文章目录 文章目录 系列文章目录前言一、代码模板二、题目案例总结 前言 本文所述的深度优先搜索算法主要针对树状结构&#xff0c;其中的树的每个节点都可以包含0到多个子节点。 深度优先&#xff0c;即为先纵向搜索树的枝干&#xff0c;搜索到该枝干的叶子节点后返回&am…

(笔记)lib:no such lib的另一种错误可能:/etc/ld.so.conf没增加

[TOC]((笔记)lib:no such lib的另一种错误可能&#xff1a;/etc/ld.so.conf没增加) 0.需求说明 通过cmakelist去find一个库时&#xff0c;可能导致报错&#xff0c;例如”libsgm.so cannot open“。但明明已经make install了&#xff0c;所以还有一种可能&#xff1a; 共享库…