Skip to content

Commit bea99e8

Browse files
committed
adding mutations
1 parent 567116f commit bea99e8

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

garden.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ body{
1010
#cellContainer{
1111
display: grid;
1212
grid-template-columns: repeat(3, auto);
13-
width: 700px;
13+
width: 450px;
14+
margin: auto;
1415
}
1516
.cell{
1617
width: 150px;
1718
height: 150px;
1819
border-radius: 15px;
1920
border: 10px solid black;
21+
font-family: "Trebuchet MS";
22+
font-size: 150px;
2023
}

garden.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h1>Garden</h1>
2424
</div>
2525
<div id="list">
2626
<h3><a href=/main/index.html>Home</h3>
27+
<button id="tick">Grow</button>
2728
</div>
2829
</div>
2930
<script src="garden.js"></script>

garden.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
const cells = document.querySelectorAll(".cell"); // cells becomes array with data of each element with id "cell"
2+
const btn = document.querySelector("#tick");
23

34
let plants = ["", "", "", "", "", "", "", "", ""];
45

56
const adjacentPlants = [
67
["1", "3", "4"], //to cell 0
78
["0", "2", "3", "4", "5"], //to cell 1
89
["1", "4", "5"], //to cell 2
10+
["0", "1", "4", "6", "7"], //to cell 3
11+
["0", "1", "2", "3", "5", "6", "7", "8"], //to cell 4
12+
["1", "2", "4", "7", "8"], //to cell 5
13+
["3", "4", "7"], // to cell 6
14+
["3", "4", "5", "6", "8"], //to cell 7
15+
["4", "5", "7"], //to cell 8
916
]
1017
let adjacentPlaceholder = [];
18+
let adjacentAmount = null;
19+
1120

1221
startgame()
1322

@@ -20,20 +29,41 @@ function startgame(){
2029
cell.addEventListener("click", cellClicked); //ignore this cell it is placeholder for certain index in "cells"
2130
});
2231

32+
btn.addEventListener("click", tick())
33+
2334
}
2435

2536
function cellClicked(){
2637

2738
const cellIndex = this.getAttribute("cellIndex");
28-
plants[cellIndex] = "x";
29-
this.textContent = "x";
39+
if(plants[cellIndex] == "x" || this.textContent == "x"){
40+
plants[cellIndex] = "";
41+
this.textContent = "";
42+
}
43+
else {
44+
plants[cellIndex] = "x";
45+
this.textContent = "x";
46+
}
3047
console.log("cellClicked")
31-
32-
updateCell(this, cellIndex);
33-
}
34-
function updateCell(cell, index){
35-
plants[index] = currentPlayer;
36-
cell.textContent = currentPlayer;
3748
}
3849

50+
function tick(){
51+
for(let i = 0; i < plants.length; i++){
52+
let adjacentPlaceholder = adjacentPlants[i]
53+
adjacentAmount = 0
54+
55+
for(let i = 0; i < adjacentPlaceholder.length; i++){
56+
57+
if(plants[adjacentPlaceholder[i]] == "x"){
58+
59+
adjacentAmount++
60+
}
61+
}
62+
63+
if(adjacentAmount >= 2 && plants[i] == "x"){
64+
plants[i] = "o";
65+
this.textContent = "o";
66+
}
67+
}
68+
}
3969

0 commit comments

Comments
 (0)