JavaScript中的生活游戏-使用表

Game of life in JavaScript - using Table

本文关键字:游戏 的生活 JavaScript      更新时间:2023-09-26

我用JavaScript和HTML表完成了Conway的《人生游戏》的编码
逻辑表中的单元格将被分配唯一的id's,并根据id进行操作(基于4条规则)
你可以在Codepen找到工作代码,或者我把代码放在下面
问题是,它可以很好地处理任何数量的行和9列,如果给定超过9列,它们就不会是唯一的id's,所以它以不希望的方式工作
Query是我可以为整个表分配唯一id的一种方式
代码块tableInitialization是初始化部分。

(function(){
	$(document).ready(function(){
		var column = "", appendRow = "", inc = 1, selectedCells = [], toRemoveClass = [], toAddClass = [], maxValue;
		var tableInitialization = function(noOfRow, noOfColumn){
			for(var row=1; row<=noOfRow; row++){
				for(var col=1; col<=noOfColumn; col++){
					column += "<td  id =" + inc+col + ">  </td>";
				}
				appendRow += "<tr>"+column+"</tr>";
				column= "";
				inc++;
			}
				$(".table").append(appendRow);
		};
		$("#submit").click(function(data){
			var noOfRow = parseInt($("#rowNo").val());
			var noOfColumn = parseInt($("#columnNo").val());
			maxValue = parseInt(noOfRow.toString() + noOfColumn.toString());
			if(isNaN(noOfRow) || isNaN(noOfColumn)){
				alert("Please enter number");
			} else {
				tableInitialization(noOfRow, noOfColumn);
				$("#container").hide();
				$("td").click( function(data){
					selectedCells.push(parseInt(this.id));
					$(this).addClass("valid");
				});
			}	
		});
		
		var checkAgain = function(selectedCells){
			var check = 0, toBeReplaced = [], inArray = [], livingCell;
			var currentNumber = 0;
			var north, northEast, East, southEast, south, southWest, west, northWest;
			for(var i=0; i<selectedCells.length; i++){
				check = 0;
			    currentNumber = parseInt(selectedCells[i]);
			    if($("#"+(currentNumber)).hasClass("valid")){
					livingCell = true;
				} else {
					livingCell = false;
				}
			    if(currentNumber > 0 && currentNumber < maxValue){
				
					/*North*/
					if((currentNumber-10) > 0 && (currentNumber-10) < maxValue){	
						if($("#"+(currentNumber-10)).hasClass("valid")){
							check ++;
						}
					}
					/*North East*/
					if((currentNumber-9) > 0 && (currentNumber-9) < maxValue){	
						if($("#"+(currentNumber-9)).hasClass("valid")){
							check ++;
						}
					}
					/*East*/
					if((currentNumber+1) > 0 && (currentNumber+1) < maxValue){	
						if($("#"+(currentNumber+1)).hasClass("valid")){
							check ++;
						}
					}
					/*South East*/
					if((currentNumber+11) > 0 && (currentNumber+11) < maxValue){	
						if($("#"+(currentNumber+11)).hasClass("valid")){
							check ++;
						}
					}
					/*South*/
					if((currentNumber+10) > 0 && (currentNumber+10) < maxValue){	
						if($("#"+(currentNumber+10)).hasClass("valid")){
							check ++;
						}
					}
					/*South West*/
					if((currentNumber+9) > 0 && (currentNumber+9) < maxValue){	
						if($("#"+(currentNumber+9)).hasClass("valid")){
							check ++;
						}
					}
					/*West*/
					if((currentNumber-1) > 0 && (currentNumber-1) < maxValue){	
						if($("#"+(currentNumber-1)).hasClass("valid")){
							check ++;
						}
					}
					/*North West*/
					if((currentNumber-11) > 0 && (currentNumber-11) < maxValue){	
						if($("#"+(currentNumber-11)).hasClass("valid")){
							check ++;
						}
					}
					if(livingCell){
						if(check === 0 || check === 1 ){
							if(toRemoveClass.indexOf(currentNumber) == -1){
								toRemoveClass.push(currentNumber);
							}
						} 
						if(check == 4 || check == 5 || check == 6 || check == 7 || check == 8 ){
							if(toRemoveClass.indexOf(currentNumber) == -1){
								toRemoveClass.push(currentNumber);
							}
						} 
						if(check == 2 || check == 3){
							if(toAddClass.indexOf(currentNumber) == -1){
								toAddClass.push(currentNumber);
							}
						} 
					} else {
						if(check == 3){
							if(toAddClass.indexOf(currentNumber) == -1){
								toAddClass.push(currentNumber);
							}
						} 
					}
				}
			}
		};
		var gol = function(selectedCells){
			var check = 0, inArray = [];
			 var currentNumber = 0, livingCell;
			for(var i=0; i<selectedCells.length; i++){
					toBeReplaced = [];
					check = 0;
				    currentNumber = parseInt(selectedCells[i]);
				    if($("#"+(currentNumber)).hasClass("valid")){
						livingCell = true;
					} else {
						livingCell = false;
					}
				    
				    if(currentNumber > 0 && currentNumber < maxValue){
					
						/*North*/
						if((currentNumber-10) > 0 && (currentNumber-10) < maxValue){	
							if($("#"+(currentNumber-10)).hasClass("valid")){
								check ++;
							}
						
							if(toBeReplaced.indexOf((currentNumber-10)) == -1){
								toBeReplaced.push(currentNumber-10);
							}
						}
						/*North East*/
						if((currentNumber-9) > 0 && (currentNumber-9) < maxValue){	
							if($("#"+(currentNumber-9)).hasClass("valid")){
								check ++;
							}
						
							if(toBeReplaced.indexOf((currentNumber-9)) == -1){
								toBeReplaced.push(currentNumber-9);
							}
						}
						/*East*/
						if((currentNumber+1) > 0 && (currentNumber+1) < maxValue){	
							if($("#"+(currentNumber+1)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber+1)) == -1){
								toBeReplaced.push(currentNumber+1);
							}
						}
						/*South East*/
						if((currentNumber+11) > 0 && (currentNumber+11) < maxValue){	
							if($("#"+(currentNumber+11)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber+11)) == -1){
								toBeReplaced.push(currentNumber+11);
							}
						}
						/*South*/
						if((currentNumber+10) > 0 && (currentNumber+10) < maxValue){	
							if($("#"+(currentNumber+10)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber+10)) == -1){
								toBeReplaced.push(currentNumber+10);
							}
						}
						/*South West*/
						if((currentNumber+9) > 0 && (currentNumber+9) < maxValue){	
							if($("#"+(currentNumber+9)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber+9)) == -1){
								toBeReplaced.push(currentNumber+9);
							}
						}
						/*West*/
						if((currentNumber-1) > 0 && (currentNumber-1) < maxValue){	
							if($("#"+(currentNumber-1)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber-1)) == -1){
								toBeReplaced.push(currentNumber-1);
							}
						}
						/*North West*/
						if((currentNumber-11) > 0 && (currentNumber-11) < maxValue){	
							if($("#"+(currentNumber-11)).hasClass("valid")){
								check ++;
							}
							if(toBeReplaced.indexOf((currentNumber-11)) == -1){
								toBeReplaced.push(currentNumber-11);
							}
						}
						if(livingCell){
							if(check == 0 || check == 1 ){
								if(toRemoveClass.indexOf(currentNumber) == -1){
									toRemoveClass.push(currentNumber);
								}
							} 
							if(check == 4 || check == 5 || check == 6 || check == 7 || check == 8 ){
								if(toRemoveClass.indexOf(currentNumber) == -1){
									toRemoveClass.push(currentNumber);
								}
							} 
							if(check == 2 || check == 3){
								if(toAddClass.indexOf(currentNumber) == -1){
									toAddClass.push(currentNumber);
								}
							} 
						} else {
							if(check == 3){
								if(toAddClass.indexOf(currentNumber) == -1){
									toAddClass.push(currentNumber);
								}
							} 
						}
					}
				checkAgain(toBeReplaced);
			}
			
			
			for(var i=0; i<toRemoveClass.length; i++){
				$("#"+toRemoveClass[i]).removeClass("valid");
			}
			
			for(var i=0; i<toAddClass.length; i++){
				$("#"+toAddClass[i]).addClass("valid");
			}
			
			toBeReplaced = toAddClass;	
			
			if(toAddClass.length == 0){
				//exit
				return;
			} else {
				setInterval(function(){
					gol($.unique(toBeReplaced));
				},1000);
			}
	
			selectedCells = [];
			toAddClass =[];
			toRemoveClass = [];
	
		};
		start = function(){
			if(selectedCells.length == 0){
				alert("select cell");
			} else {
				gol(selectedCells);
			}
		};
	});
})();
body{
  background: #BBDEFB ;
}
td {
    width: 20px;
    height: 20px;
    background: #eee;
}
table {
    cursor: default;
}
.valid {
    background: #00BFA5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
	<title>Conways Game of Life</title>
	<link rel="stylesheet" type="text/css" href="gameOfLife.css">
</head>
<body>
	<h1><code>Conway's game of life</code></h1>
	<div id="container">
		<h2><code>enter row * columns</code></h2>
		<form>
			<code>row &#9733; column : </code>
			<input id="rowNo" type="text"/> &#9733;
			<input id="columnNo" type="text"/>
		</form>
		<button id="submit"> Submit </button>
	    <br><br>
    </div>
	<table class="table"></table>
	<br><br>
	<button onClick="start()"> start </button>
	<br><br>
	<h2><code> Rules </code></h2>
	<code>1. Any live cell with fewer than two live neighbours dies, 
       as if caused by underpopulation.</code><br>
	<code>2. Any live cell with more than three live neighbours dies, 
	   as if by overcrowding.</code><br>
	<code>3. Any live cell with two or three live neighbours lives 
	   on to the next generation.</code><br>
	<code>4. Any dead cell with exactly three live neighbours becomes 
	   a live cell.</code>
	<script type="text/javascript" src="gameOfLife.js"></script>
</body>
</html>

无需深入挖掘代码。通过使用colrow索引构建ID,因此第一行将获得类似11, 12, 13, 14, 15, ... 110, 111, 112等的内容。如果没有分隔符,第十一行第一元素的ID也将是111。只要您使用一种分隔符,如"_",您的ID就会是唯一的:再次使用1_1, 1_2

for(var row=1; row<=noOfRow; row++){
    for(var col=1; col<=noOfColumn; col++){
        column += "<td id =" + inc+"_"+col + ">  </td>";
        /* you also could add data attributes:
           data-row='""+row+"'" data-col='""+col+"'" 
        */
    }
    appendRow += "<tr>"+column+"</tr>";
    column= "";
    inc++;
}

查看您的代码,我认为您会遇到其他问题,因为有很多代码与"10"有关。例如:if((currentNumber-9) > 0 && (currentNumber-9) < maxValue){-如果您的行数超过9行,这将不起作用。但修复这个问题将是整个游戏的重写版本。