I have a problem with a project that uses React. It's a tic tac toe game and I am trying to implement the computer player.
When the human player clicks on one square (Field-component) this triggers the updateField() function in the Board-component. It also calls the computerMove() function which in turn calls the minimax() function.
The minimax function is recursivly defined. However, I get the following error message, when the code execution reaches the line where minimax(possibleGame) is called.
[ERROR] react-dom Uncaught TypeError: Cannot read property 'minimax' of undefined
Apparently the function is unknown, which I don't understand because I bind it to the Board scope.
class Field extends React.Component{
render(){
return(
<button className='field' onClick={this.props.clickFunc}>{this.props.value}</button>
);
}
}
class Board extends React.Component{
constructor(){
super();
this.state = {
fields: Array(9).fill(null),
};
this.minimax = this.minimax.bind(this);
this.updateField = this.updateField.bind(this);
this.computerMove = this.computerMove.bind(this);
}
updateField(id){
const fields = this.state.fields.slice();
// only proceed if field hasn't been taken yet
if (fields[id] == null && this.props.winner == null){
fields[id] = this.props.playerSymbol;
this.setState({fields: fields});
this.computerMove(fields);
}
}
// check if somebody won the game
checkWinner(squares){
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
computerMove(fields){
var computerSymbol = (this.props.playerSymbol == "X" ? "O" : "X");
var game = {fields: fields,turn:computerSymbol};
var result = this.minimax(game);
var newBoard = fields.slice();
newBoard[result[1]] = computerSymbol;
this.setState({fields: newBoard});
}
score(game){
// check for winner
var result = this.checkWinner(game.fields);
if (result !== null){
if (result == this.props.playerSymbol){
return -10;
} else{
return 10;
}
}else{
return 0;
}
}
minimax(game) {
var score = this.score(game);
if (score !== 0){
return score;
}
var scores = [];
var moves = [];
game.fields.forEach(function(value,index,arr){
if (value==null){
var newField = arr.slice();
newField[index] = game.turn;
var nextTurn = (game.turn == "X" ? "O" : "X");
var possibleGame = {fields: newField,turn:nextTurn};
var result = this.minimax(possibleGame);
scores.push(result[0]);
moves.push(index);
}
});
if (game.turn == this.props.playerSymbol){
var max_ind = scores.indexOf(Math.max(...scores));
return [scores[max_ind],moves[max_ind]];
} else{
var min_ind = scores.indexOf(Math.min(...scores));
return [scores[min_ind],moves[min_ind]];
}
}
render(){
var fields = this.state.fields;
// check if somebody won
const winner = this.checkWinner(fields);
if (winner){
this.props.declareWinner(winner);
}
return (
<div className="animated fadeInUp board-container">
<div className='row'>
<Field value={fields[0]} clickFunc={() => this.updateField(0)} />
<Field value={fields[1]} clickFunc={() => this.updateField(1)}/>
<Field value={fields[2]} clickFunc={() => this.updateField(2)}/>
</div>
<div className='row'>
<Field value={fields[3]} clickFunc={() => this.updateField(3)} />
<Field value={fields[4]} clickFunc={() => this.updateField(4)}/>
<Field value={fields[5]} clickFunc={() => this.updateField(5)}/>
</div>
<div className='row'>
<Field value={fields[6]} clickFunc={() => this.updateField(6)}/>
<Field value={fields[7]} clickFunc={() => this.updateField(7)}/>
<Field value={fields[8]} clickFunc={() => this.updateField(8)}/>
</div>
</div>
)
}
}
The whole code can be tested here: https://codepen.io/miga89/pen/pPqmra
I also tried to come up with a MWE (https://codepen.io/miga89/pen/XRQBjP) However, this example works for some reason.
Thank you for your help!
game.fields.forEach((value,index,arr) => {