So I am makeing a bingo game and to solve for all the win conditions I wrote
calWinner = () =>{
let winHor = [[0,1,2,3,4],[5,6,7,8,9],
[10,11,12,13,14],[15,16,17,18,19],[20,21,22,23,24]];
let winVert = this.createWinVert(winHor,5);
let winDiag = this.createWinDiagonal(winHor);
let winDiag2 = this.createWinDiagonal2(winHor);
let winCondtion = [...winHor,...winVert,...winDiag,...winDiag2];
// boardHistory > Per winCondtion
if(winCondtion
.some(arr=> arr
.every(index=>this.state.boardHistory[index] === true))){
this.setState({winner: "Winner"});
}
}
As you can see the 3 functions below have similarities that they all have a for loop and return back an array.
createWinVert= (data,counter)=>{
let arr = [];
for(let z = 0; z < counter; z++){
arr.push(data.map(x=>x[z]));
}
return arr;
}
createWinDiagonal = (data)=>{
let arr = [];
arr.push(data.map((x,index)=>x[index]));
return arr;
}
createWinDiagonal2 = (data)=>{
let arr = [];
let temp = data.length - 1;
arr.push(data.map(x=>x[temp--]));
return arr;
}
The only difference is the way they use the for loop and what they pass to the map function
How could I condense this code?
You can compress all out output into 1 array.
calWinner = () => { let winHor = [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24] ]; let winSomething = this.createWinSomething(winHor, 5); let winCondtion = [...winHor, ...winSomething]; // boardHistory > Per winCondtion if (winCondtion .some(arr => arr .every(index => this.state.boardHistory[index] === true))) { this.setState({ winner: "Winner" }); } } createWinSomething = (data, counter) => { let arr = []; // Vert for (let z = 0; z < counter; z++) { arr.push(data.map(x => x[z])); } // Diag 1 arr.push(data.map((x, index) => x[index])); // Diag 2 let temp = data.length - 1; arr.push(data.map(x => x[temp--])); return arr; }
I would output an object of arrays:
createWin = (data, counter) => {
let winVer, winDia1, winDia2, temp = data.length - 1;
for(let z = 0; z < counter; z++){
winVer = data.map(x=>x[z]);
}
winDia1 = data.map((x,index)=>x[index]));
winDia2 = data.map(x=>x[temp--]));
return {
winVer,
winDia1,
winDia2
}
By the way, as .map()
always returns a new array, you can just assign it straight to the variables.
You can start with reducing lines of code in the 3 functions like below -
let calWinner = () => { let winHor = [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24] ]; let winVert = createWinVert(winHor); let winDiag = createWinDiagonal(winHor); let winDiag2 = createWinDiagonal2(winHor); let winCondtion = [...winHor, ...winVert, ...winDiag, ...winDiag2]; console.log(winCondtion) // boardHistory > Per winCondtion //if(winCondtion //.some(arr=> arr // .every(index=> boardHistory[index] === true))){ //console.log({winner: "Winner"}); //} } let createWinVert = (data) => [data.map(x => x[0])] let createWinDiagonal = (data) => [data.map((x, index) => x[index])] let createWinDiagonal2 = (data) => [data.map((x, i) => x[data.length - i - 1])] calWinner()
And then if you wish to combine these functions then you can do as below
let calWinner = () => { let winHor = [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24] ]; let winVert = createWin(winHor,1); let winDiag = createWin(winHor,0,); let winDiag2 = createWin(winHor,0,true); let winCondtion = [...winHor, ...winVert, ...winDiag, ...winDiag2]; console.log(winCondtion) // boardHistory > Per winCondtion //if(winCondtion //.some(arr=> arr // .every(index=> boardHistory[index] === true))){ //console.log({winner: "Winner"}); //} } let createWin = (d,p,r) => [d.map((x,i) => r ? [...x].reverse()[i] : x[p? 0: i])] calWinner()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.