Skip to content

Commit 53755b1

Browse files
authored
Add files via upload
0 parents  commit 53755b1

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed

index.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=
6+
, initial-scale=1.0">
7+
8+
<link rel="stylesheet" href="style.css">
9+
<title>Game of Life</title>
10+
</head>
11+
12+
<body onload="init()">
13+
<div class="container">
14+
<h1>Game of Life</h1>
15+
<div>
16+
<button class="button" onclick="playGen()">Start</button>
17+
<button class="button" onclick="stopGen()">Stop</button>
18+
<button class="button" onclick="init() , stopGen()">Clear</button>
19+
20+
<span>🐢</span>
21+
<input id="slider" type="range" min="0" max="500" step="100" value="100"
22+
title="speed dial" onclick = "stopGen() , playGen()" onkeydown="stopGen() , playGen()">
23+
<span>🐇 </span>
24+
25+
</div>
26+
27+
<table id="center">
28+
<tbody class="board">
29+
</tbody>
30+
</table>
31+
32+
33+
</div>
34+
<script src="main.js"></script>
35+
</body>
36+
37+
</html>

main.js

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
var gBoard;
2+
var gGridSize = 20;
3+
var slider = document.getElementById("slider");
4+
var gSpeed = 500 - slider.value;
5+
var gameIsOn = false;
6+
var gInterval;
7+
8+
function init() {
9+
gBoard = buildBoard();
10+
renderBoard(gBoard);
11+
12+
initShapeRpentamino();
13+
}
14+
15+
function initShapeRpentamino() {
16+
renderCell(8, 9, "X");
17+
renderCell(9, 9, "X");
18+
renderCell(9, 10, "X");
19+
renderCell(10, 8, "X");
20+
renderCell(10, 9, "X");
21+
gBoard[8][9].isAlive = true;
22+
gBoard[9][9].isAlive = true;
23+
gBoard[9][10].isAlive = true;
24+
gBoard[10][8].isAlive = true;
25+
gBoard[10][9].isAlive = true;
26+
renderBoard(gBoard);
27+
}
28+
29+
function playGen() {
30+
if (gameIsOn) {
31+
clearInterval(gInterval);
32+
}
33+
gameIsOn = true;
34+
gInterval = setInterval(function () {
35+
gSpeed = 500 - slider.value;
36+
gBoard = runGeneration(gBoard);
37+
renderBoard(gBoard);
38+
}, gSpeed);
39+
}
40+
41+
function stopGen() {
42+
clearInterval(gInterval);
43+
gameIsOn = false;
44+
}
45+
46+
function runGeneration(board) {
47+
var duppBoard = JSON.parse(JSON.stringify(board)); //שיבוט הלוח במקום העתקה
48+
for (var i = 0; i < gGridSize; i++) {
49+
for (var j = 0; j < gGridSize; j++) {
50+
var currCell = board[i][j];
51+
var neighbours = checkNeighbours(i, j, board);
52+
if (currCell.isAlive) {
53+
//האם התא חי
54+
if (neighbours < 2 || neighbours > 3) {
55+
duppBoard[i][j].isAlive = false;
56+
} else {
57+
// 2 or 3 neighbours
58+
duppBoard[i][j].isAlive = true;
59+
}
60+
} else {
61+
// האם התא מת
62+
if (neighbours !== 3) {
63+
duppBoard[i][j].isAlive = false;
64+
} else {
65+
duppBoard[i][j].isAlive = true;
66+
}
67+
}
68+
}
69+
}
70+
71+
return duppBoard;
72+
}
73+
74+
function buildBoard() {
75+
// בונה את הלוח במודל
76+
var board = [];
77+
for (var i = 0; i < gGridSize; i++) {
78+
board[i] = [];
79+
for (let j = 0; j < gGridSize; j++) {
80+
board[i][j] = {
81+
neighbours: 0,
82+
isAlive: false,
83+
};
84+
}
85+
}
86+
// console.table(board);
87+
return board;
88+
}
89+
90+
function renderBoard(board) {
91+
// מרנדרת את הלוח בדום
92+
93+
var elBoard = document.querySelector(".board");
94+
var strHTML = "";
95+
for (var i = 0; i < gGridSize; i++) {
96+
strHTML += "<tr>\n";
97+
for (var j = 0; j < gGridSize; j++) {
98+
strHTML += `\t<td class="`;
99+
var currCell = board[i][j];
100+
var cellClass = getClassName(i, j);
101+
if (currCell.isAlive) {
102+
strHTML += "alive ";
103+
}
104+
strHTML += ` cell ${cellClass}" onclick="cellClicked(${i},${j})">
105+
\n${" "} </td>`;
106+
}
107+
strHTML += "\n</tr>\n";
108+
}
109+
elBoard.innerHTML = strHTML;
110+
// console.table(board);
111+
}
112+
113+
function checkNeighbours(cellI, cellJ, board) {
114+
// בודקת האם השכנים הם פצצות
115+
var neighbours = 0;
116+
for (var i = cellI - 1; i <= cellI + 1; i++) {
117+
if (i < 0 || i >= board.length) continue;
118+
for (var j = cellJ - 1; j <= cellJ + 1; j++) {
119+
if (j < 0 || j >= board[i].length) continue;
120+
if (i === cellI && j === cellJ) continue;
121+
if (board[i][j].isAlive) neighbours++;
122+
}
123+
}
124+
//console.log(neighbours);
125+
return neighbours;
126+
}
127+
128+
function getClassName(i, j) {
129+
var cellClass = "cell-" + i + "-" + j;
130+
return cellClass;
131+
}
132+
133+
function cellClicked(i, j) {
134+
if (gBoard[i][j].isAlive) {
135+
gBoard[i][j].isAlive = false;
136+
renderCell(i, j, " ");
137+
} else {
138+
gBoard[i][j].isAlive = true;
139+
renderCell(i, j, "X");
140+
}
141+
}
142+
143+
function renderCell(i, j, value) {
144+
var elCell = document.querySelector(".cell-" + i + "-" + j);
145+
if (value === "X") {
146+
elCell.classList.add("alive");
147+
// elCell.style.backgroundColor = "black";
148+
} else {
149+
elCell.classList.remove("alive");
150+
151+
// elCell.style.backgroundColor = "white";
152+
}
153+
154+
// elCell.innerText = `${value}`;
155+
}

style.css

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
body {
2+
background-color: #77aa9f;
3+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='827' height='413.5' viewBox='0 0 1600 800'%3E%3Cpath fill='%231c4f48' d='M1102.5 734.8c2.5-1.2 24.8-8.6 25.6-7.5.5.7-3.9 23.8-4.6 24.5C1123.3 752.1 1107.5 739.5 1102.5 734.8zM1226.3 229.1c0-.1-4.9-9.4-7-14.2-.1-.3-.3-1.1-.4-1.6-.1-.4-.3-.7-.6-.9-.3-.2-.6-.1-.8.1l-13.1 12.3c0 0 0 0 0 0-.2.2-.3.5-.4.8 0 .3 0 .7.2 1 .1.1 1.4 2.5 2.1 3.6 2.4 3.7 6.5 12.1 6.5 12.2.2.3.4.5.7.6.3 0 .5-.1.7-.3 0 0 1.8-2.5 2.7-3.6 1.5-1.6 3-3.2 4.6-4.7 1.2-1.2 1.6-1.4 2.1-1.6.5-.3 1.1-.5 2.5-1.9C1226.5 230.4 1226.6 229.6 1226.3 229.1zM33 770.3C33 770.3 33 770.3 33 770.3c0-.7-.5-1.2-1.2-1.2-.1 0-.3 0-.4.1-1.6.2-14.3.1-22.2 0-.3 0-.6.1-.9.4-.2.2-.4.5-.4.9 0 .2 0 4.9.1 5.9l.4 13.6c0 .3.2.6.4.9.2.2.5.3.8.3 0 0 .1 0 .1 0 7.3-.7 14.7-.9 22-.6.3 0 .7-.1.9-.3.2-.2.4-.6.4-.9C32.9 783.3 32.9 776.2 33 770.3z'/%3E%3Cpath fill='%23deff26' d='M171.1 383.4c1.3-2.5 14.3-22 15.6-21.6.8.3 11.5 21.2 11.5 22.1C198.1 384.2 177.9 384 171.1 383.4zM596.4 711.8c-.1-.1-6.7-8.2-9.7-12.5-.2-.3-.5-1-.7-1.5-.2-.4-.4-.7-.7-.8-.3-.1-.6 0-.8.3L574 712c0 0 0 0 0 0-.2.2-.2.5-.2.9 0 .3.2.7.4.9.1.1 1.8 2.2 2.8 3.1 3.1 3.1 8.8 10.5 8.9 10.6.2.3.5.4.8.4.3 0 .5-.2.6-.5 0 0 1.2-2.8 2-4.1 1.1-1.9 2.3-3.7 3.5-5.5.9-1.4 1.3-1.7 1.7-2 .5-.4 1-.7 2.1-2.4C596.9 713.1 596.8 712.3 596.4 711.8zM727.5 179.9C727.5 179.9 727.5 179.9 727.5 179.9c.6.2 1.3-.2 1.4-.8 0-.1 0-.2 0-.4.2-1.4 2.8-12.6 4.5-19.5.1-.3 0-.6-.2-.8-.2-.3-.5-.4-.8-.5-.2 0-4.7-1.1-5.7-1.3l-13.4-2.7c-.3-.1-.7 0-.9.2-.2.2-.4.4-.5.6 0 0 0 .1 0 .1-.8 6.5-2.2 13.1-3.9 19.4-.1.3 0 .6.2.9.2.3.5.4.8.5C714.8 176.9 721.7 178.5 727.5 179.9zM728.5 178.1c-.1-.1-.2-.2-.4-.2C728.3 177.9 728.4 178 728.5 178.1z'/%3E%3Cg fill-opacity='0.42' fill='%23FFF'%3E%3Cpath d='M699.6 472.7c-1.5 0-2.8-.8-3.5-2.3-.8-1.9 0-4.2 1.9-5 3.7-1.6 6.8-4.7 8.4-8.5 1.6-3.8 1.7-8.1.2-11.9-.3-.9-.8-1.8-1.2-2.8-.8-1.7-1.8-3.7-2.3-5.9-.9-4.1-.2-8.6 2-12.8 1.7-3.1 4.1-6.1 7.6-9.1 1.6-1.4 4-1.2 5.3.4 1.4 1.6 1.2 4-.4 5.3-2.8 2.5-4.7 4.7-5.9 7-1.4 2.6-1.9 5.3-1.3 7.6.3 1.4 1 2.8 1.7 4.3.5 1.1 1 2.2 1.5 3.3 2.1 5.6 2 12-.3 17.6-2.3 5.5-6.8 10.1-12.3 12.5C700.6 472.6 700.1 472.7 699.6 472.7zM740.4 421.4c1.5-.2 3 .5 3.8 1.9 1.1 1.8.4 4.2-1.4 5.3-3.7 2.1-6.4 5.6-7.6 9.5-1.2 4-.8 8.4 1.1 12.1.4.9 1 1.7 1.6 2.7 1 1.7 2.2 3.5 3 5.7 1.4 4 1.2 8.7-.6 13.2-1.4 3.4-3.5 6.6-6.8 10.1-1.5 1.6-3.9 1.7-5.5.2-1.6-1.4-1.7-3.9-.2-5.4 2.6-2.8 4.3-5.3 5.3-7.7 1.1-2.8 1.3-5.6.5-7.9-.5-1.3-1.3-2.7-2.2-4.1-.6-1-1.3-2.1-1.9-3.2-2.8-5.4-3.4-11.9-1.7-17.8 1.8-5.9 5.8-11 11.2-14C739.4 421.6 739.9 421.4 740.4 421.4zM261.3 590.9c5.7 6.8 9 15.7 9.4 22.4.5 7.3-2.4 16.4-10.2 20.4-3 1.5-6.7 2.2-11.2 2.2-7.9-.1-12.9-2.9-15.4-8.4-2.1-4.7-2.3-11.4 1.8-15.9 3.2-3.5 7.8-4.1 11.2-1.6 1.2.9 1.5 2.7.6 3.9-.9 1.2-2.7 1.5-3.9.6-1.8-1.3-3.6.6-3.8.8-2.4 2.6-2.1 7-.8 9.9 1.5 3.4 4.7 5 10.4 5.1 3.6 0 6.4-.5 8.6-1.6 4.7-2.4 7.7-8.6 7.2-15-.5-7.3-5.3-18.2-13-23.9-4.2-3.1-8.5-4.1-12.9-3.1-3.1.7-6.2 2.4-9.7 5-6.6 5.1-11.7 11.8-14.2 19-2.7 7.7-2.1 15.8 1.9 23.9.7 1.4.1 3.1-1.3 3.7-1.4.7-3.1.1-3.7-1.3-4.6-9.4-5.4-19.2-2.2-28.2 2.9-8.2 8.6-15.9 16.1-21.6 4.1-3.1 8-5.1 11.8-6 6-1.4 12 0 17.5 4C257.6 586.9 259.6 588.8 261.3 590.9z'/%3E%3Ccircle cx='1013.7' cy='153.9' r='7.1'/%3E%3Ccircle cx='1024.3' cy='132.1' r='7.1'/%3E%3Ccircle cx='1037.3' cy='148.9' r='7.1'/%3E%3Cpath d='M1508.7 297.2c-4.8-5.4-9.7-10.8-14.8-16.2 5.6-5.6 11.1-11.5 15.6-18.2 1.2-1.7.7-4.1-1-5.2-1.7-1.2-4.1-.7-5.2 1-4.2 6.2-9.1 11.6-14.5 16.9-4.8-5-9.7-10-14.7-14.9-1.5-1.5-3.9-1.5-5.3 0-1.5 1.5-1.5 3.9 0 5.3 4.9 4.8 9.7 9.8 14.5 14.8-1.1 1.1-2.3 2.2-3.5 3.2-4.1 3.8-8.4 7.8-12.4 12-1.4 1.5-1.4 3.8 0 5.3 0 0 0 0 0 0 1.5 1.4 3.9 1.4 5.3-.1 3.9-4 8.1-7.9 12.1-11.7 1.2-1.1 2.3-2.2 3.5-3.3 4.9 5.3 9.8 10.6 14.6 15.9.1.1.1.1.2.2 1.4 1.4 3.7 1.5 5.2.2C1510 301.2 1510.1 298.8 1508.7 297.2zM327.6 248.6l-.4-2.6c-1.5-11.1-2.2-23.2-2.3-37 0-5.5 0-11.5.2-18.5 0-.7 0-1.5 0-2.3 0-5 0-11.2 3.9-13.5 2.2-1.3 5.1-1 8.5.9 5.7 3.1 13.2 8.7 17.5 14.9 5.5 7.8 7.3 16.9 5 25.7-3.2 12.3-15 31-30 32.1L327.6 248.6zM332.1 179.2c-.2 0-.3 0-.4.1-.1.1-.7.5-1.1 2.7-.3 1.9-.3 4.2-.3 6.3 0 .8 0 1.7 0 2.4-.2 6.9-.2 12.8-.2 18.3.1 12.5.7 23.5 2 33.7 11-2.7 20.4-18.1 23-27.8 1.9-7.2.4-14.8-4.2-21.3l0 0C347 188.1 340 183 335 180.3 333.6 179.5 332.6 179.2 332.1 179.2zM516.3 60.8c-.1 0-.2 0-.4-.1-2.4-.7-4-.9-6.7-.7-.7 0-1.3-.5-1.4-1.2 0-.7.5-1.3 1.2-1.4 3.1-.2 4.9 0 7.6.8.7.2 1.1.9.9 1.6C517.3 60.4 516.8 60.8 516.3 60.8zM506.1 70.5c-.5 0-1-.3-1.2-.8-.8-2.1-1.2-4.3-1.3-6.6 0-.7.5-1.3 1.2-1.3.7 0 1.3.5 1.3 1.2.1 2 .5 3.9 1.1 5.8.2.7-.1 1.4-.8 1.6C506.4 70.5 506.2 70.5 506.1 70.5zM494.1 64.4c-.4 0-.8-.2-1-.5-.4-.6-.3-1.4.2-1.8 1.8-1.4 3.7-2.6 5.8-3.6.6-.3 1.4 0 1.7.6.3.6 0 1.4-.6 1.7-1.9.9-3.7 2-5.3 3.3C494.7 64.3 494.4 64.4 494.1 64.4zM500.5 55.3c-.5 0-.9-.3-1.2-.7-.5-1-1.2-1.9-2.4-3.4-.3-.4-.7-.9-1.1-1.4-.4-.6-.3-1.4.2-1.8.6-.4 1.4-.3 1.8.2.4.5.8 1 1.1 1.4 1.3 1.6 2.1 2.6 2.7 3.9.3.6 0 1.4-.6 1.7C500.9 55.3 500.7 55.3 500.5 55.3zM506.7 55c-.3 0-.5-.1-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2.7-2.7 2.8 0 0 0 0 0 0-1.5 0-2.7-1.2-2.7-2.7-.1-6.7 2.4-12.9 7-18 3.6-4 8.4-7.1 13.7-8.8.5-6.5 3.1-12.9 7.4-17.4 7-7.4 18.2-8.9 27.3-10.1l.7-.1c1.5-.2 2.9.9 3.1 2.3.2 1.5-.9 2.9-2.3 3.1l-.7.1c-8.6 1.2-18.4 2.5-24 8.4-3 3.2-5 7.7-5.7 12.4 7.9-1 17.7 1.3 24.3 5.7 4.3 2.9 7.1 7.8 7.2 12.7.2 4.3-1.7 8.3-5.2 11.1C1335.2 572.4 1332.6 573.4 1329.7 573.4zM1311 546.7c.1 1.5.4 3 .8 4.4 1.7 5.8 8.7 14.2 15.1 16.3 2.8.9 5.1.5 7.2-1.1 2.7-2.1 3.2-4.8 3.1-6.6-.1-3.2-2-6.4-4.8-8.3C1326.7 547.5 1317.7 545.6 1311 546.7z'/%3E%3C/g%3E%3C/svg%3E");
4+
background-attachment: fixed;
5+
}
6+
7+
.container {
8+
color: seashell;
9+
text-align: center;
10+
font-family: 'Ubuntu', sans-serif;
11+
background-color: #1C4F48;
12+
padding: 30px;
13+
padding-top: 5px ;
14+
border-radius: 30px;
15+
width: 500px;
16+
margin: 0 auto;
17+
margin-top: 50px;
18+
}
19+
20+
h1 {
21+
text-align: center;
22+
font-family: 'Ubuntu', sans-serif;
23+
color: #DEFF26;
24+
}
25+
26+
.button {
27+
margin: auto;
28+
left: 50%;
29+
margin-bottom: 10px;
30+
text-align: center;
31+
cursor: pointer;
32+
font-size: 20px;
33+
background-color: rgb(255, 255, 255);
34+
/* border-color: black; */
35+
border-radius: 10%;
36+
border-width: 4px;
37+
}
38+
39+
.cell {
40+
color: black;
41+
background-color: rgb(255, 255, 255);
42+
font-size: 10px;
43+
width: 20px;
44+
height: 16px;
45+
text-align: center;
46+
cursor: pointer;
47+
box-shadow: 1px 1px 1px #bbbbbb;
48+
}
49+
50+
.alive{
51+
background-color: rgb(0, 0, 0);
52+
}
53+
54+
.cell:hover {
55+
background-color: rgba(161, 156, 156, 0.801);
56+
color: rgba(214, 214, 214, 0.726);
57+
}
58+
59+
/* The slider itself */
60+
.slider {
61+
-webkit-appearance: none; /* Override default CSS styles */
62+
appearance: none;
63+
width: 100%; /* Full-width */
64+
height: 25px; /* Specified height */
65+
background: #d3d3d3; /* Grey background */
66+
outline: none; /* Remove outline */
67+
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
68+
-webkit-transition: .2s; /* 0.2 seconds transition on hover */
69+
transition: opacity .2s;
70+
}
71+
72+
/* Mouse-over effects */
73+
.slider:hover {
74+
opacity: 1; /* Fully shown on mouse-over */
75+
}

0 commit comments

Comments
 (0)