1+ const cellElements = document . querySelectorAll ( '[data-cell]' ) ;
2+ const board = document . getElementById ( "board" ) ;
3+ const winningMessageDisplay = document . getElementById ( "winning-msg" ) ;
4+ const winningMessageText = document . getElementById ( "displaymsg" ) ;
5+ const restatBtn = document . getElementById ( "restartBtn" ) ;
6+
7+ const X_CLASS = "x" ;
8+ const CIRCLE_CLASS = "circle" ;
9+ let circleTurn = "" ; //undefined halema 0 first
10+ const WINNING_COMBINATIONS = [
11+ [ 0 , 1 , 2 ] ,
12+ [ 3 , 4 , 5 ] ,
13+ [ 6 , 7 , 8 ] ,
14+ [ 0 , 3 , 6 ] ,
15+ [ 1 , 4 , 7 ] ,
16+ [ 2 , 5 , 8 ] ,
17+ [ 0 , 4 , 8 ] ,
18+ [ 2 , 4 , 6 ]
19+ ] ;
20+
21+ startGame ( ) ;
22+
23+ restatBtn . addEventListener ( "click" , startGame ) ;
24+
25+ function startGame ( ) {
26+ circleTurn = false ;
27+ cellElements . forEach ( cell => {
28+ //reset everthing after the restart button is clicked
29+ cell . classList . remove ( X_CLASS ) ;
30+ cell . classList . remove ( CIRCLE_CLASS ) ;
31+ cell . removeEventListener ( "click" , handleClick ) ;
32+
33+ // ekchtoti kunai cell click bhaisakesi tyo cell feri click garda value return garda
34+ // only once the click event is fired for a cell
35+ cell . addEventListener ( 'click' , handleClick , { once : true } ) ;
36+ } ) ;
37+
38+ setBoardHover ( ) ;
39+ winningMessageDisplay . classList . remove ( 'show' ) ;
40+ }
41+
42+ function handleClick ( e ) {
43+ // console.log("clicked");
44+
45+ const cell = e . target ;
46+ const currentClass = circleTurn ? CIRCLE_CLASS : X_CLASS ;
47+ // console.log(currentClass)
48+ //placeMark
49+ placeMark ( cell , currentClass ) ;
50+
51+ //check for win
52+ if ( checkWin ( currentClass ) ) {
53+ // console.log("winner");
54+ endGame ( false ) ; //no draw case
55+ }
56+ else if ( checkDraw ( ) ) {
57+ endGame ( true ) ;
58+ }
59+ else {
60+ // Switch Truns
61+ swapTurns ( ) ;
62+ setBoardHover ( ) ;
63+ }
64+
65+
66+ // Check for Draw
67+ function endGame ( draw ) {
68+ if ( draw ) {
69+ winningMessageText . innerHTML = `DRAW!!` ;
70+ }
71+ else {
72+ winningMessageText . innerHTML = `
73+ ${ circleTurn ? "O's" : "X's" } Wins!` ;
74+ }
75+
76+ winningMessageDisplay . classList . add ( 'show' ) ;
77+ }
78+
79+
80+ }
81+
82+ function placeMark ( cell , currentClass ) {
83+ cell . classList . add ( currentClass ) ;
84+ }
85+
86+ function swapTurns ( ) {
87+ circleTurn = ! circleTurn ;
88+ }
89+
90+ function setBoardHover ( ) {
91+
92+ board . classList . remove ( X_CLASS ) ;
93+ board . classList . remove ( CIRCLE_CLASS ) ;
94+
95+ if ( circleTurn ) {
96+ board . classList . add ( CIRCLE_CLASS ) ;
97+ }
98+ else {
99+ board . classList . add ( X_CLASS ) ;
100+ }
101+ }
102+
103+ function checkWin ( currentClass ) {
104+ // .some returns true if any of the comb is true
105+ return WINNING_COMBINATIONS . some ( combination => {
106+ return combination . every ( index => { //harek cell ko lagi check
107+ return cellElements [ index ] . classList . contains ( currentClass ) ; // same class xa ki nai check
108+ } ) ;
109+ } ) ;
110+ }
111+
112+ function checkDraw ( ) {
113+ return [ ...cellElements ] . every ( cell => { //destructring of array
114+ return cell . classList . contains ( X_CLASS ) ||
115+ cell . classList . contains ( CIRCLE_CLASS ) ;
116+ } )
117+ }
0 commit comments