I'm trying to make a tic-tac-toe game feature in my mobile application using react-native. But when the user won the game, the game didn't stop the round, instead, the game continues until every slot is filled and an error popped out saying that 'this.setState_if_Tie is not a function'.
On the other hand, when the result is a tie, the screen will freeze in white.
Below are some codes that are related to the said problem:
judgeWinner(inputs){ //check the winning situation based on 'const conditions'
return conditions.some(d => d.every(item => inputs.indexOf(item) !== -1));
}
//if result === -1 game continue , result === 0 user won
//if result === 1 bot won , result === 2 no winner
setstate_if_Win(parse_res, parse_result){
if (parse_res && parse_result !== 0){
this.setState({ result: 0}); //User win
}
if (parse_res && parse_result !== 1) {
this.setState({ result: 1}); //Bot win
}
}
setstate_if_Tie = () => {
this.setState({ result: 2 });
}
componentDidUpdate() {
const { userInputs , botInputs , result} = this.state;
const inputs = userInputs.concat(botInputs);
//res is result
if (inputs.length >= 5) {
let res = this.judgeWinner(userInputs); // go to line 95 to check winning result for user
if (res && result !== 0) {
this.setstate_if_Win(res, result); //User win
return;
}
res = this.judgeWinner(botInputs); // go to line 95 to check winning result for bot
if (res && result !== 1) {
this.setstate_if_Win(res, result); //Bot win
return;
}
}
if (inputs.length >= 9 && result !== 2) {
this.setState_if_Tie(); //No winner (Tie)
}
}
Is there any way that I can do to solve this problem? Thank you in advance.
You must named setState_if_Tie instead of setstate_if_Tie:
setstate_if_Tie = () => {
this.setState({ result: 2 });
}