Skip to content

Commit c1d17f9

Browse files
committed
adding
1 parent 7f24fc5 commit c1d17f9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tic-tac-toe/src/App.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,13 @@ function App() {
1919
];
2020

2121
const checkWins = (array) => {
22-
let didWin = false;
23-
winnings.forEach((item) => {
24-
if (
25-
item[0] === array[0] &&
26-
item[1] === array[1] &&
27-
item[2] === array[2]
28-
) {
29-
didWin = true;
30-
alert("win");
31-
}
32-
});
33-
return didWin;
22+
return winnings.some((item) => {
23+
return item.every((index) => array.includes(index))
24+
})
3425
};
3526

3627
const handleClick = (i) => {
28+
if (xInput.includes(i) || oInput.includes(i)) return;
3729
setXTurn(!xTurn);
3830

3931
if (xTurn) {
@@ -42,13 +34,26 @@ function App() {
4234

4335
if (tempX.length >= 3) {
4436
let didWin = checkWins(tempX);
37+
console.log('didWin', didWin)
38+
39+
if(didWin){
40+
alert('win')
41+
setXInput([])
42+
setOInput([])
43+
}
4544
}
4645
} else {
4746
let tempO = [...oInput, i];
4847
setOInput(tempO);
4948

5049
if (tempO.length >= 3) {
5150
let didWin = checkWins(tempO);
51+
console.log('didWin', didWin)
52+
if(didWin){
53+
alert('win')
54+
setXInput([])
55+
setOInput([])
56+
}
5257
}
5358
}
5459
};

0 commit comments

Comments
 (0)