井字棋游戏(HTML+CSS+JavaScript)

ops/2024/10/8 17:04:35/
htmledit_views">

  🌏个人博客主页:心.c

前言:这两天在写植物大战僵尸,写不动了,现在和大家分享一下之前我写的一个很简单的小html" title=游戏>游戏井字棋,这个没有AI,可以两个人一起玩,如果大家觉得我哪里写的有一些问题,还希望积极改正,欢迎大家留言

🔥🔥🔥专题文章:JavaScript小html" title=游戏>游戏

😽感谢大家的点赞👍收藏⭐️评论✍您的一键三连是我更新的动力 💓 


目录

页面效果: 

 相关技能实现:

创建空数组:

 获得大盒子和小盒子对象:

 给大盒子添加事件监听:

 判断小格子是否被填满:

判断是否有一方成功:

 判断数组中存在的数的个数:

 更新页面:

源代码:

HTML

CSS

 JavaScript:


页面效果: 

关于应用就是不可重复添加,而且两次点击的表情是不一样的,每个表情代表一方,出现三连的那一方就赢了,每一个小方格是不可重复添加的,如果每个格子都填满了就平局成功或平局都会出现alert提示,然后页面清空,清空之后可以继续玩

 相关技能实现:

创建空数组:

空数组用来遍历(通过其值是1是2或是null)来放置我们的表情 

javascript">  const arr = Array(9).fill(null)
 获得大盒子和小盒子对象:
javascript"> let box = this.document.querySelector('.box')let small_boxes = this.document.querySelectorAll('.small')
 给大盒子添加事件监听:

给我们的大盒子添加点击事件,冒泡到子级,通过1和2的个数,来添加1或2,谁少添加谁,优先添加1,如果1和2的个数有一个大于3,可能出现一方胜利的情况,就添加winner方法进行判断,如果有一方胜利,就返回这一方,然后页面进行清空(通过返回的1还是2进行判断是谁胜利了)如果一直没有胜利,直到小方格都被填写完,算两方平局,页面就会被清空,就可以重新进行添加了

javascript">box.addEventListener('click', function (e) {if (e.target.tagName === 'DIV') {let id = +e.target.dataset.idif (getCount(arr, 1) <= getCount(arr, 2)) {if (arr[id - 1] === null) {arr[id - 1] = 1console.log(arr)render()} else {alert('请在空白处添加')}} else {if (arr[id - 1] === null) {arr[id - 1] = 2console.log(arr)render()} else {alert('请在空白处添加')}}//判断是否被填满if (allSet()) {let time0 = setTimeout(function () {alert('平局')arr.fill(null)small_boxes.forEach(function (small_box) {console.log(small_box)small_box.innerHTML = ''; // 清空每个文本框});clearTimeout(time0)}, 300)}//判断是否有一方赢if (getCount(arr, 1) >= 3 || getCount(arr, 2) >= 3) {let time = setTimeout(function () {let win = winner()if (win != -1) {if (win === 1) {alert('笑脸成功')} else if (win === 2) {alert('哭脸成功')}arr.fill(null)small_boxes.forEach(function (small_box) {console.log(small_box)small_box.innerHTML = ''; // 清空每个文本框});clearTimeout(time)}}, 400)}}})
 判断小格子是否被填满:

遍历数组arr的每个值,如果该值有一个为null,就返回false,如果都不为false,最后返回true 

javascript">function allSet() {for (let i of arr) {if (i == null) {return false}}return true}
判断是否有一方成功:

 这个html" title=游戏>游戏虽然是3×3方格,但是是用一维数组存储的,如果下面有一对(三个)下标的值相等,就返回其中一个下标的值

javascript">//判断是否成功function winner() {const winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8],[0, 3, 6], [1, 4, 7], [2, 5, 8],[0, 4, 8], [2, 4, 6]];for (let combo of winningCombinations) {if (arr[combo[0]] && arr[combo[0]] === arr[combo[1]] && arr[combo[1]] === arr[combo[2]]) {return arr[combo[0]];}}return -1;}
 判断数组中存在的数的个数:

这个方法就是为了判断1和2的个数 

javascript">//返回某个数存在的个数function getCount(arr, value) {return arr.filter(item => item === value).length;}
 更新页面:

遍历数组向每个小方格添加内容,如果为1,添加笑脸,如果为2,添加哭脸,如果为null,什么也不添加,数组和方格是一一对应的 

javascript"> function render() {small_boxes.forEach(function (small_box) {small_box.innerHTML = ''; // 清空每个文本框});for (let i = 0; i < 9; i++) {let smal = document.querySelector(`[data-id="${i + 1}"]`)if (arr[i] === 1) {smal.innerHTML = '&#xe68b;'}else if (arr[i] === 2) {smal.innerHTML = '&#xe68e;'} else {smal.innerHTML = ''}}}

源代码:

HTML:
html"><!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="./iconfont/iconfont.css"><link rel="stylesheet" href="./game.css">
</head><body><div class="box wrapper"><div class="small iconfont" data-id="1"></div><div class="small iconfont" data-id="2"></div><div class="small iconfont" data-id="3"></div><div class="small iconfont" data-id="4"></div><div class="small iconfont" data-id="5"></div><div class="small iconfont" data-id="6"></div><div class="small iconfont" data-id="7"></div><div class="small iconfont" data-id="8"></div><div class="small iconfont" data-id="9"></div></div><div class="text"><h1>----井字棋----</h1></div><script src="./game.js"></script>
</body></html>
CSS:
css">:root {--bgc: rgb(223, 225, 248);
}.wrapper {margin: auto;
}* {margin: 0;height: 0;box-sizing: border-box;
}.body {user-select: none;background-color: #f6faff;
}.box {user-select: none;margin-top: 20px;display: flex;flex-wrap: wrap;width: 570px;height: 570px;border: 12px solid var(--bgc);border-radius: 40px;background-color: #ffffff;
}.small {font-size: 120px;color: rgb(183, 190, 227);line-height: 182px;text-align: center;user-select: none;width: 182px;height: 182px;cursor: pointer;
}.small:nth-child(1),
.small:nth-child(2),
.small:nth-child(4),
.small:nth-child(5) {border-right: 12px solid var(--bgc);border-bottom: 12px solid var(--bgc);
}.small:nth-child(3),
.small:nth-child(6) {border-bottom: 12px solid var(--bgc);
}.small:nth-child(7),
.small:nth-child(8) {border-right: 12px solid var(--bgc);
}.text {text-align: center;color: var(--bgc);
}
 JavaScript:
javascript">window.addEventListener('load', function () {const arr = Array(9).fill(null)let box = this.document.querySelector('.box')let small_boxes = this.document.querySelectorAll('.small')box.addEventListener('click', function (e) {if (e.target.tagName === 'DIV') {let id = +e.target.dataset.idif (getCount(arr, 1) <= getCount(arr, 2)) {if (arr[id - 1] === null) {arr[id - 1] = 1console.log(arr)render()} else {alert('请在空白处添加')}} else {if (arr[id - 1] === null) {arr[id - 1] = 2console.log(arr)render()} else {alert('请在空白处添加')}}//判断是否被填满if (allSet()) {let time0 = setTimeout(function () {alert('平局')arr.fill(null)small_boxes.forEach(function (small_box) {console.log(small_box)small_box.innerHTML = ''; // 清空每个文本框});clearTimeout(time0)}, 300)}//判断是否有一方赢if (getCount(arr, 1) >= 3 || getCount(arr, 2) >= 3) {let time = setTimeout(function () {let win = winner()if (win != -1) {if (win === 1) {alert('笑脸成功')} else if (win === 2) {alert('哭脸成功')}arr.fill(null)small_boxes.forEach(function (small_box) {console.log(small_box)small_box.innerHTML = ''; // 清空每个文本框});clearTimeout(time)}}, 400)}}})function allSet() {for (let i of arr) {if (i == null) {return false}}return true}function render() {small_boxes.forEach(function (small_box) {small_box.innerHTML = ''; // 清空每个文本框});for (let i = 0; i < 9; i++) {let smal = document.querySelector(`[data-id="${i + 1}"]`)if (arr[i] === 1) {smal.innerHTML = '&#xe68b;'}else if (arr[i] === 2) {smal.innerHTML = '&#xe68e;'} else {smal.innerHTML = ''}}}//判断是否成功function winner() {const winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8],[0, 3, 6], [1, 4, 7], [2, 5, 8],[0, 4, 8], [2, 4, 6]];for (let combo of winningCombinations) {if (arr[combo[0]] && arr[combo[0]] === arr[combo[1]] && arr[combo[1]] === arr[combo[2]]) {return arr[combo[0]];}}return -1;}//返回某个数存在的个数function getCount(arr, value) {return arr.filter(item => item === value).length;}}
)

 到这里就讲完了,感谢大家的观看!!!


http://www.ppmy.cn/ops/95085.html

相关文章

【大模型部署及其应用 】使用 Llama 3 开源和 Elastic 构建 RAG

使用 Llama 3 开源和 Elastic 构建 RAG 本博客将介绍使用两种方法实现 RAG。 Elastic、Llamaindex、Llama 3(8B)版本使用 Ollama 在本地运行。 Elastic、Langchain、ELSER v2、Llama 3(8B)版本使用 Ollama 在本地运行。 笔记本可从此GitHub位置获取。 在开始之前,让我…

LVS 、DR模式

lvs --环境 主机名IP地址功能web1192.168.1.17 rs web2192.168.1.18realservenat 内&#xff1a;192.168.1.16 外&#xff1a;192.168.1.102 directorserver,ntpdns192.168.1.12dns --web1、web2 yum -y install nginxecho "xx" > /usr/share/nginx/html/index.…

Android12 SystemUI QS面板新增截屏功能

问题:Android12 中SystemUI版本,QS下拉快捷面板式没有截屏功能的。 需求:客户要求在项目中实现下拉快捷面板具备一键截屏功能 目前自己只针对Android12 mtk/RK平台实践过,接触的全志平台暂未实验验证。 文章目录 前言一、实际实现效果二、修改点1.新增文件2.修改文件三、基…

2024爱分析·AI Agent开发管理平台市场厂商评估报告:火山引擎

01研究范围定义 大模型浪潮席卷全球&#xff0c;AI Agent作为这股浪潮中的新星正在取代Copilot&#xff0c;成为大模型应用的主流形态之一&#xff0c;以其惊人的速度和影响力重塑科技和商业的版图。 AI Agent是指以大模型为驱动&#xff0c;具有自主理解感知、规划、记忆和使…

十九、中介者模式

文章目录 1 基本介绍2 案例2.1 Developer 抽象类2.2 FrontendDeveloper 类2.3 BackendDeveloper 类2.4 Mediator 接口2.5 ProjectManager 类2.6 Client 类2.7 Client 类的运行结果2.8 总结 3 各角色之间的关系3.1 角色3.1.1 Colleague ( 同事 )3.1.2 ConcreteColleague ( 具体的…

物理网卡MAC修改器v3.0-直接修改网卡内部硬件MAC地址,重装系统不变!

直接在操作系统里就能修改网卡硬件mac地址&#xff0c;刷新网卡mac序列号硬件码机器码&#xff0c;电脑主板集成网卡&#xff0c;pcie网卡&#xff0c;usb有线网卡&#xff0c;usb无线网卡&#xff0c;英特尔网卡&#xff0c;瑞昱网卡全支持&#xff01; 一键修改mac&#xff0…

System V IPC奥秘:解锁共享内存、消息队列与信号量的高效通信之路

&#x1f351;个人主页&#xff1a;Jupiter. &#x1f680; 所属专栏&#xff1a;Linux从入门到进阶 欢迎大家点赞收藏评论&#x1f60a; 目录 &#x1f351;system V共享内存 &#x1f352;共享内存的原理共享内存数据结构查看和删除共享内存资源的命令 &#x1f33b;共享内存…

基因组学系列4:参考转录本数据库MANE

1. 参考转录本数据库MANE简介 为了促进临床参照的一致性&#xff0c;美国国家生物技术信息中心( NCBI)和欧洲分子生物学实验室-欧洲生物信息学研究所(EMBL-EBI)合作发布了参考转录本数据库MANE&#xff08;Matched Annotation from the NCBI and EMBL-EBI&#xff09;&#xf…