HTML JavaScript 康威生命游戏

2024-01-09 08:36

本文主要是介绍HTML JavaScript 康威生命游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>康威生命游戏</title><style>body {font-family: Arial, sans-serif;text-align: center;}#game-board {display: inline-block;border: 1px solid black;}.cell {width: 15px;height: 15px;float: left;border:1px solid black;background-color: white;}.cell.alive {background-color: black;}button {margin-top: 10px;}</style>
</head>
<body><h1 style="font-size: 14px;">康威生命游戏 先点击设定初始图形,再点击开始</h1><div id="game-board"></div><button onclick="startGame()" style="width: 50px;">开始</button><button onclick="clearBoard()" style="width: 50px;">清除</button><script>const ROWS = 30;const COLS = 60;let board = [];let nextBoard = [];let gameInterval = null; // 添加一个全局变量来存储定时器的引用  function initializeBoard() {board = [];nextBoard = [];for (let i = 0; i < ROWS; i++) {board[i] = [];nextBoard[i] = [];for (let j = 0; j < COLS; j++) {board[i][j] = false;nextBoard[i][j] = false;}}}function drawBoard() {const gameBoard = document.getElementById('game-board');gameBoard.innerHTML = '';for (let i = 0; i < ROWS; i++) {for (let j = 0; j < COLS; j++) {const cell = document.createElement('div');cell.className = 'cell';if (board[i][j]) {cell.classList.add('alive');}cell.addEventListener('click', () => {board[i][j] = !board[i][j];drawBoard();});gameBoard.appendChild(cell);}const br = document.createElement('br');gameBoard.appendChild(br);}}function countNeighbors(row, col) {let count = 0;for (let i = -1; i <= 1; i++) {for (let j = -1; j <= 1; j++) {if (i === 0 && j === 0) {continue;}const neighborRow = row + i;const neighborCol = col + j;if (neighborRow >= 0 &&neighborRow < ROWS &&neighborCol >= 0 &&neighborCol < COLS &&board[neighborRow][neighborCol]) {count++;}}}return count;}function updateBoard() {for (let i = 0; i < ROWS; i++) {for (let j = 0; j < COLS; j++) {const neighbors = countNeighbors(i, j);if (board[i][j]) {if (neighbors < 2 || neighbors > 3) {nextBoard[i][j] = false;} else {nextBoard[i][j] = true;}} else {if (neighbors === 3) {nextBoard[i][j] = true;}}}}for (let i = 0; i < ROWS; i++) {for (let j = 0; j < COLS; j++) {board[i][j] = nextBoard[i][j];}}}function startGame() {// 在启动新定时器之前,检查并清除可能存在的旧定时器  if (gameInterval !== null) {  clearInterval(gameInterval);  gameInterval = null;  }  // 启动新的定时器并保存在全局变量中  gameInterval = setInterval(() => {  updateBoard();  drawBoard();  }, 300);  }function clearBoard() {// 清除定时器  if (gameInterval !== null) {  clearInterval(gameInterval);  gameInterval = null;  }  initializeBoard();drawBoard();}initializeBoard();drawBoard();</script>
</body>
</html>

在这里插入图片描述

这篇关于HTML JavaScript 康威生命游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/586528

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F