11const cells = document . querySelectorAll ( ".cell" ) ; // cells becomes array with data of each element with id "cell"
2+ const btn = document . querySelector ( "#tick" ) ;
23
34let plants = [ "" , "" , "" , "" , "" , "" , "" , "" , "" ] ;
45
56const 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]
1017let adjacentPlaceholder = [ ] ;
18+ let adjacentAmount = null ;
19+
1120
1221startgame ( )
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
2536function 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