Skip to content

Commit ab158d2

Browse files
committed
test 3
1 parent 5ddf1e5 commit ab158d2

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

garden.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body>
1111
<div id="gameContainer">
1212
<h1>Garden</h1>
13-
<div id="farmPlots"></div>
13+
<div id="farmPlots">
1414
<div cellId="0" class="cell"></div>
1515
<div cellId="1" class="cell"></div>
1616
<div cellId="2" class="cell"></div>
@@ -21,9 +21,9 @@ <h1>Garden</h1>
2121
<div cellId="7" class="cell"></div>
2222
<div cellId="8" class="cell"></div>
2323
</div>
24+
<button id="tick">Grow</button>
2425
<div id="list">
2526
<h3><a href="https://ilikegrape.netlify.app">Home</h3>
26-
<button id="tick">Grow</button>
2727
</div>
2828
</div>
2929
<script src="garden.js"></script>

garden.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
const cells = document.querySelectorAll(".cell");
22
const btn = document.querySelector("#tick");
3-
43
let plants = ["", "", "", "", "", "", "", "", ""];
5-
4+
let adjacentArray = [
5+
["0","0"], //to cell 0
6+
["0","0"], //to cell 1
7+
["0","0"], //to cell 2
8+
["0","0"], //to cell 3
9+
["0","0"], //to cell 4
10+
["0","0"], //to cell 5
11+
["0","0"], //to cell 6
12+
["0","0"], //to cell 7
13+
["0","0"], //to cell 8
14+
//[#number of x, #number of o]
15+
];
616
const adjacentPlants = [
717
["1", "3", "4"], //to cell 0
818
["0", "2", "3", "4", "5"], //to cell 1
@@ -13,7 +23,12 @@ const adjacentPlants = [
1323
["3", "4", "7"], // to cell 6
1424
["3", "4", "5", "6", "8"], //to cell 7
1525
["4", "5", "7"], //to cell 8
16-
]
26+
];
27+
let adjacentPlantsPlaceholder;
28+
let adjacentArrayPlaceholder;
29+
let amountPlaceholder;
30+
31+
1732

1833
function startgame(){
1934
cells.forEach(function(cell) {
@@ -23,39 +38,42 @@ function startgame(){
2338
btn.addEventListener("click", tick)
2439
}
2540

41+
42+
2643
function cellClicked(){
2744
const cellIndex = this.getAttribute("cellIndex");
28-
if(plants[cellIndex] == "x" || this.textContent == "x"){
29-
plants[cellIndex] = "";
30-
this.textContent = "";
31-
}
32-
else {
33-
plants[cellIndex] = "x";
45+
if(plants[cellIndex] == "" && this.textContent == ""){
46+
plants[cellIndex] = "x"; //changing box you clicked on
3447
this.textContent = "x";
48+
let adjacentPlantsPlaceholder = adjacentPlants[cellIndex] //preping for next step
49+
50+
for(let i = 0; i < adjacentPlantsPlaceholder.length; i++){ //updating adjacent plants
51+
let adjacentArrayPlaceholder = adjacentArray[adjacentPlantsPlaceholder[i]]//setting placeholder to each cell touching cell clicked
52+
adjacentArrayPlaceholder[0]++ //changing amount of x's by 1
53+
adjacentArray[adjacentPlantsPlaceholder[i]] = adjacentArrayPlaceholder //updating real array
54+
}
3555
}
56+
57+
58+
3659
console.log("cellClicked")
3760
}
3861

3962
function tick(){
40-
for(let i = 0; i < plants.length; i++){
41-
let adjacentPlaceholder = adjacentPlants[i]
42-
let adjacentAmount = 0
43-
44-
for(let j = 0; j < adjacentPlaceholder.length; j++){
45-
46-
if(plants[adjacentPlaceholder[j]] == "x"){
47-
adjacentAmount++
48-
}
49-
}
63+
cells.forEach(function(cell){
64+
const cellIndex = cell.getAttribute("cellIndex");
65+
let adjacentArrayPlaceholder = adjacentArray[cellIndex]
66+
if(adjacentArrayPlaceholder.contains("x")){
67+
cell.textContent = "o"
68+
plants[cellIndex] = "o"
69+
let adjacentPlantsPlaceholder = adjacentPlants[cellIndex] //preping for next step
5070

51-
if(adjacentAmount >= 2 && plants[i]!= "x" && plants[i]!= "o"){
52-
plants[i] = "o";
53-
cells[i].textContent = "o";
71+
for(let i = 0; i < adjacentPlantsPlaceholder.length; i++){ //updating adjacent plants
72+
let adjacentArrayPlaceholder = adjacentArray[adjacentPlantsPlaceholder[i]]//setting placeholder to each cell touching cell clicked
73+
adjacentArrayPlaceholder[1]++ //changing amount of o's by 1
74+
adjacentArray[adjacentPlantsPlaceholder[i]] = adjacentArrayPlaceholder //updating real array
75+
}
5476
}
55-
}
77+
})
5678
}
57-
58-
59-
60-
6179
startgame()

0 commit comments

Comments
 (0)