I have built a simple JS class with functional methods that create a Rock, Paper, Scissors game.
I would be happy to get a non-opinionated review in terms of code being optimal.
class Rps{
constructor(){
this.signs = {1:'rock',2:'paper',3:'scissors'};
this.winMatrix = [
{1:3},
{2:1},
{3:2}
];
}
randomizePlay(){
const keys = Object.keys(this.signs);
return 1 + Math.floor(keys.length * Math.random());
}
play(){
return this.roundResult([this.randomizePlay(),this.randomizePlay()]);
}
roundResult(resultArray) {
let victorious = {};
if (resultArray[0] === resultArray[1]) {
return 'rematch';
}
this.winMatrix.forEach((value, index) => {
const p1Result = resultArray[0];
const p2Result = resultArray[1];
const p1WinMatrix = parseInt(Object.keys(this.winMatrix[index]));
const p2WinMatrix = parseInt(Object.values(this.winMatrix[index]));
if (p1Result === p1WinMatrix && p2Result === p2WinMatrix){
return victorious = {1:p1Result}
}else{
return victorious = {2:p2Result}
}
});
return victorious;
}
}
const theRpsArray = [];
const theRpsAverage = 0;
let rps = new Rps;
rps.play()