class chainGame {
/**
*
* @param {Array} hands two players hands. e.g. [1,2]
* @param {Array} symbols symbols of the game
*/
constructor(hands, symbols) {
this.hands = hands
this.symbols = symbols
}
/**
* @returns {Object} {
* winnerId: 0, 1, or null
* winnerSymbols: [winnerSymbol, loserSymbol] ; will return [1stPlayerSymbol, 2ndPlayerSymbol] if there is a tie
* }
*/
winner() {
let winnerId = null; //tie is the default value. so only needs to calculate winner
let winnerSymbols = [];
let difference = Math.abs(this.hands[0]-this.hands[1]);
let sortedHands = [];
switch (difference) {
case 1:
winnerId = (this.hands[0] < this.hands[1]) ? 0: 1
sortedHands = (this.hands[0] < this.hands[1]) ? this.hands : this.hands.reverse();
break;
case this.symbols.length -1:
winnerId = (this.hands[0] > this.hands[1]) ? 0 : 1
sortedHands = (this.hands[0] > this.hands[1]) ? this.hands : this.hands.reverse();
break;
default:
sortedHands = this.hands
break;
}
winnerSymbols = [this.symbols[this.hands[0]], this.symbols[this.hands[1]]]
return {
winnerId: winnerId,
winnerSymbols: winnerSymbols,
}
}
}
//global consts
// const symbols = ["rooster", "worm", "stick", "tiger"]
const symbols = ["rock", "scissor", "paper"]
const players = ["Dave", "John"]
const handOfDave = Math.floor(Math.random() * symbols.length)
const handOfJohn = Math.floor(Math.random() * symbols.length)
const shownImageName = [symbols[handOfDave], symbols[handOfJohn]]
let winnerMessage = ""
const currentGame = new chainGame([handOfDave, handOfJohn], symbols)
const result = currentGame.winner()
let theVerb = ""
if (result.winnerId === null) {
winnerMessage = "A tie!"
theVerb = "meets"
} else {
winnerMessage = players[result.winnerId] + " " + "won!"
theVerb = "beats"
}
winnerMessage = `${winnerMessage} ${result.winnerSymbols[0]} ${theVerb} ${result.winnerSymbols[1]}`
/**
*
* @param {Array} hands two players hands. e.g. [1,2]
* @param {Array} symbols symbols of the game
*/
constructor(hands, symbols) {
this.hands = hands
this.symbols = symbols
}
/**
* @returns {Object} {
* winnerId: 0, 1, or null
* winnerSymbols: [winnerSymbol, loserSymbol] ; will return [1stPlayerSymbol, 2ndPlayerSymbol] if there is a tie
* }
*/
winner() {
let winnerId = null; //tie is the default value. so only needs to calculate winner
let winnerSymbols = [];
let difference = Math.abs(this.hands[0]-this.hands[1]);
let sortedHands = [];
switch (difference) {
case 1:
winnerId = (this.hands[0] < this.hands[1]) ? 0: 1
sortedHands = (this.hands[0] < this.hands[1]) ? this.hands : this.hands.reverse();
break;
case this.symbols.length -1:
winnerId = (this.hands[0] > this.hands[1]) ? 0 : 1
sortedHands = (this.hands[0] > this.hands[1]) ? this.hands : this.hands.reverse();
break;
default:
sortedHands = this.hands
break;
}
winnerSymbols = [this.symbols[this.hands[0]], this.symbols[this.hands[1]]]
return {
winnerId: winnerId,
winnerSymbols: winnerSymbols,
}
}
}
//global consts
// const symbols = ["rooster", "worm", "stick", "tiger"]
const symbols = ["rock", "scissor", "paper"]
const players = ["Dave", "John"]
const handOfDave = Math.floor(Math.random() * symbols.length)
const handOfJohn = Math.floor(Math.random() * symbols.length)
const shownImageName = [symbols[handOfDave], symbols[handOfJohn]]
let winnerMessage = ""
const currentGame = new chainGame([handOfDave, handOfJohn], symbols)
const result = currentGame.winner()
let theVerb = ""
if (result.winnerId === null) {
winnerMessage = "A tie!"
theVerb = "meets"
} else {
winnerMessage = players[result.winnerId] + " " + "won!"
theVerb = "beats"
}
winnerMessage = `${winnerMessage} ${result.winnerSymbols[0]} ${theVerb} ${result.winnerSymbols[1]}`