1
- // ⌛ Call init function when DOM loaded
2
- document . addEventListener ( "DOMContentLoaded" , init ) ;
3
1
// 🌐 Global Variables
4
2
const cells = document . querySelectorAll ( "td" ) ;
3
+ const instructions = document . querySelector ( "p" )
4
+ // 🤹 Tracking Game States
5
+ let xTurn = true ;
5
6
// 1️⃣ Init Called on document loaded
6
7
const init = ( ) => {
7
8
initCellHandler ( ) ;
8
9
} ;
9
- // 💡 Event Handlers
10
+ // 💡 Init Event Handlers
10
11
const initCellHandler = ( ) => {
11
12
let i = 1 ;
12
13
cells . forEach ( ( cell ) => {
14
+ // debugger
13
15
cell . id = i ++ ;
14
- cell . addEventListener ( "click" , ( e ) => {
15
- console . log ( `${ cell . id } clicked` ) ;
16
- } ) ;
16
+ cell . addEventListener ( "click" , ( ) => cellHandler ( cell ) ) ;
17
+ } ) ;
18
+ } ;
19
+ const resetGame = ( ) => {
20
+ cells . forEach ( ( cell ) => {
21
+ cell . innerHTML = "" ;
17
22
} ) ;
18
- } ;
23
+ instructions . textContent = "Click on the squares to make your mark. Play with a friend! Three in a row, column, or diagonal wins!"
24
+ xTurn = true
25
+ }
26
+ // 🎯 Declare Cell Handler
27
+ const cellHandler = ( cell ) => {
28
+ const markToPlace = xTurn ? "X" : "O" ;
29
+ if ( cell . textContent === "" ) {
30
+ cell . textContent = markToPlace ;
31
+ if ( victoryConditions ( ) ) {
32
+ // Run victoryConditions to check for win
33
+ console . log ( `${ markToPlace } wins!` )
34
+ instructions . textContent = `${ markToPlace } wins! Reset game?` ;
35
+ const resetButton = document . createElement ( "button" )
36
+ resetButton . textContent = "Reset Game"
37
+ instructions . appendChild ( resetButton ) ;
38
+ resetButton . addEventListener ( "click" , resetGame ) ;
39
+ }
40
+ xTurn = ! xTurn ; // Flip xTurn for next players turn
41
+ }
42
+ } ;
43
+ // 🥳 Victory Conditions
44
+ const victoryConditions = ( ) => {
45
+ const horizontalWin = horizontalWinCondition ( xTurn ? "X" : "O" ) ; // Only check the mark of the current player
46
+ const verticalWin = verticalWinCondition ( xTurn ? "X" : "O" ) ; // Only check the mark of the current player
47
+ const diagonalWin = diagonalWinCondition ( xTurn ? "X" : "O" ) ; // Only check the mark of the current player
48
+ return ( horizontalWin || verticalWin || diagonalWin )
49
+ function horizontalWinCondition ( markToCheck = "X" ) {
50
+ let horizontalWinCheck = false ;
51
+ for ( let i = 0 ; i < 3 ; i ++ ) {
52
+ if (
53
+ cells [ i * 3 + 0 ] . textContent === markToCheck && // need to check 0, 3, and 6 (element in array)
54
+ cells [ i * 3 + 1 ] . textContent === markToCheck && // need to check 1, 4, and 7
55
+ cells [ i * 3 + 2 ] . textContent === markToCheck // need to check 2, 5, and 8
56
+ ) {
57
+ horizontalWinCheck = true ;
58
+ return horizontalWinCheck ;
59
+ }
60
+ }
61
+ return horizontalWinCheck ;
62
+ }
63
+ function verticalWinCondition ( markToCheck = "X" ) {
64
+ let verticalWinCheck = false ;
65
+ for ( let i = 0 ; i < 3 ; i ++ ) {
66
+ if (
67
+ cells [ i + 0 ] . textContent === markToCheck && // need to check 0, 1, and 2 (element in array)
68
+ cells [ i + 3 ] . textContent === markToCheck && // need to check 3, 4, and 5
69
+ cells [ i + 6 ] . textContent === markToCheck // need to check 6, 7, and 8
70
+ ) {
71
+ verticalWinCheck = true ;
72
+ return verticalWinCheck ;
73
+ }
74
+ }
75
+ return verticalWinCheck ;
76
+ }
77
+ function diagonalWinCondition ( markToCheck = "X" ) {
78
+ let diagonaWinCheck = false ;
79
+ for ( let i = 0 ; i < 2 ; i ++ ) {
80
+ if (
81
+ cells [ i * 2 ] . textContent === markToCheck && // need to check 0, 2
82
+ cells [ 4 ] . textContent === markToCheck && // need to check 4, 4
83
+ cells [ 8 - i * 2 ] . textContent === markToCheck // need to check 8, 6
84
+ ) {
85
+ diagonaWinCheck = true ;
86
+ return diagonaWinCheck ;
87
+ }
88
+ }
89
+ return diagonaWinCheck ;
90
+ }
91
+ } ;
92
+ // ⌛ Call init function when DOM loaded
93
+ document . addEventListener ( "DOMContentLoaded" , init ) ;
0 commit comments