React Tutorial Winner Calculation - Explained
by nh_codes
HTML
<img src="https://reactjs.org/static/tictac-numbers-685df774da6da48f451356f33f4be8b2-be875.png">
JavaScript
squares = Array(9).fill(null); //array of length 9; each of the 9 elements are set to null >> [null, null, ..., null]
squares [0] = 'X'; //simulate player puting X in box 0 (see image in results)
squares [1] = 'X'; //putting X in box 1
squares [2] = "X"; //and in box 3
// the squares array is now ['X', 'X', 'X', null, null, ..., null]
function calculateWinner(squares) {
// the lines array below is for every winning combination
// i.e. there is a winner if there is the same letter (i.e 'X') in boxes 0, 1, and 2 (the first combination below)
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];
// create a new array with the same values as each winning combo. i.e. when i = 0 the new array of [a, b, c] is [0, 1, 2]
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
// the if statement checks if the there are 3 of the same letters for the winning combos
// i.e. for winning combo [0, 1, 2] it looks up the values in the squares array -> squares[0], squares[1], squares[2]
// since we've set the array so that index 0, 1, 2 of squares is 'X' then the if statement should evaluate to true.
// here's how it breaks down for the first winning combo [0, 1, 2] in lines
// since a is 0, then squares[a] is squares[0] and the value of squares [0] is 'X'
// squares[a] is true since it has a value, the value is 'X'
// squares[a] === squares[b] is true because the value of squares[b] (i.e. squares[1]) is 'X', the same as squares[a]
// squares[a] === squares[c] is true because squares[c] (i.e. squares[2] is 'X'), the same as squares [a]
// since all three parts in the if are true the function returns the value of squares[a], which is 'X' and thus the player 'X'
}
// call the calculateWinner(squares) to...