JavaScript中的生活游戏 - 使用表格

时间:2015-03-30 05:59:41

标签: javascript jquery html conways-game-of-life

我在JavaScript和HTML表格中完成了Conway的生命游戏编码 表格中的逻辑单元格将分配唯一id's并基于id操作(基于4条规则)进行。
您可以在Codepen找到工作代码,或者我已将代码放在下面 事情是它适用于任意数量的行和9列,如果给出超过9列,它们将不是唯一的id's,因此它以不期望的方式工作。
查询这是我可以为整个表格分配唯一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);

			}
		};

	});
})();
&#13;
body{
  background: #BBDEFB ;
}
td {
    width: 20px;
    height: 20px;
    background: #eee;
}
table {
    cursor: default;
}
.valid {
    background: #00BFA5;
}
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

没有深入挖掘您的代码。您可以使用 col row 索引构建ID,这样您就可以获得第一行11, 12, 13, 14, 15, ... 110, 111, 112等内容。如果没有分隔符,第11行第一个元素的ID也将是111。一旦你使用了一种分隔符,如&#39; _&#39;您的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++;
}

查看您的代码,我认为您会遇到其他问题,因为有很多与&#34; 10&#34;相关的代码。例如:if((currentNumber-9) > 0 && (currentNumber-9) < maxValue){ - 如果您有超过9行,这将无法工作。但修复此问题将是完整游戏的重写版本。