3 回答

TA貢獻1807條經驗 獲得超9個贊
您鏈接的文章為您提供了這種剪刀石頭布的邏輯方法:
泛化 對于 n >= 3 和 n 奇數:
令 d = (n + a - b) % n。然后:
如果 d = 0 => 平局
如果 d % 2 = 1 => a 獲勝
如果 d % 2 = 0 => b 獲勝
你可以這樣實現:
// convert names to numbers
const options = {
rock: 0,
paper: 1,
scissors: 2
}
// length of object
const length = Object.keys(options).length
// user input
const user = options[prompt().toLowerCase()]
// randomly generate input for opponent
const comp = Math.floor(Math.random() * Math.floor(length));
// debug
console.log(user, comp)
// conditions
const win = () => console.log('win')
const lose = () => console.log('lose')
const tie = () => console.log('tie')
// calculate output
const d = (length + user - comp) % length
// d = 0 -> tie
// d % 2 = 1 -> win
// d % 2 = 0 -> lose
d ? d % 2 ? win() : lose() : tie()
這種方法是自動擴展的;您可以在對象中再添加兩個條目,代碼將自動適應它。我將代碼提取到一個函數中,讓它更像一個游戲:
// convert names to numbers
const options = {
rock: 0,
paper: 1,
scissors: 2,
spock: 3,
lizard: 4
}
const condition = (input, comp, condition) => {
switch (condition) {
case 'win':
order = [input, comp]
break
case 'lose':
order = [comp, input]
break
case 'tie':
order = [input]
break
}
console.log(`You chose ${input}, and the opponent chose ${comp},`)
order.length > 1
? console.log(`${order[0]} beats ${order[1]}.`)
: console.log(`${order[0]} cannot beat iself.`)
console.log(`You ${condition}.`)
}
const play = (input, options) => {
input = options[input.toLowerCase()]
var length = Object.keys(options).length
var comp = Math.floor(Math.random() * Math.floor(length))
var d = (length + input - comp) % length
input = Object.keys(options)[input]
comp = Object.keys(options)[comp]
condition(input, comp, d ? d % 2 ? 'win' : 'lose' : 'tie')
}
const input = prompt()
play(input, options)

TA貢獻1829條經驗 獲得超6個贊
我和喬納斯威爾姆斯有同樣的想法,但它讓我有更多時間詳細說明
const win3 = { Rock: { e: [ 'Scissors' ], m: [ 'Rock crushes Scissors'] }
, Paper: { e: [ 'Rock' ], m: ['Paper cover Rock'] }
, Scissors: { e: [ 'Paper'], m: ['Scissors cuts Paper'] }
}
const win5 = { Rock: { e: [ 'Scissors', 'Lizard' ], m: [ 'Rock crushes Scissors', 'Rock crushes Lizard' ] }
, Paper: { e: [ 'Rock' , 'Spock' ], m: [ 'Paper cover Rock', 'Paper disproves Spock' ] }
, Scissors: { e: [ 'Paper' , 'Lizard' ], m: [ 'Scissors cuts Paper', 'Scissors decapitates Lizard' ] }
, Lizard: { e: [ 'Paper' , 'Spock' ], m: [ 'Lizard eats Paper', 'Lizard poisons Spock' ] }
, Spock: { e: [ 'Rock' , 'Scissors' ], m: [ 'Spock vaporizes Rock', 'Spock smashes Scissors' ] }
}
let play = win3 // win5 .. win1000...
, userScore = 0
, computerScore = 0
;
function game ( userChoice, computerChoice )
{
if (userChoice == computerChoice )
{
console.log(' same values , no one score')
return
}
if (play[userChoice].includes(computerChoice) )
{
userScore++
let n = play[userChoice].e.findIndex(e=>e===computerChoice )
console.log( play[userChoice].m[n] )
}
else
{
computerScore++
let n = play[computerChoice].e.findIndex(e=>e===userChoice )
console.log( play[computerChoice].m[n] )
}
}

TA貢獻1856條經驗 獲得超5個贊
您可以將兩個可能選項之間的關系表示為一棵樹:
const winsOver = {
schaar: ["papier"],
steen: ["schaar"],
papier: ["steen"],
};
現在您的比較變得非常簡單:
if(userChoice === computerChoice) {
draw();
} else if(winsOver[userChoice].includes(computerChoice)) {
win();
} else lose();
添加回答
舉報