11const cells = document . querySelectorAll ( ".cell" ) ;
22const btn = document . querySelector ( "#tick" ) ;
3-
43let 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+ ] ;
616const 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
1833function startgame ( ) {
1934 cells . forEach ( function ( cell ) {
@@ -23,39 +38,42 @@ function startgame(){
2338 btn . addEventListener ( "click" , tick )
2439}
2540
41+
42+
2643function 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
3962function 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-
6179startgame ( )
0 commit comments