尝试用 java 脚本制作剪刀石头布游戏
trying to make a rock paper scissors game in java script
当我在其中运行出现这个错误[Uncaught TypeError: rpsdatabase is not a function]
输出应该类似于 [0,1],这意味着人类选择石头而机器人选择纸
我在两个空行之间留下了错误行以便轻松找到它
function rpsGame(yourChoice){
console.log(yourChoice);
var humanChoice, botChoice;
humanChoice = yourChoice.id;
botChoice = numbertochoice(randombot());
console.log('computer choice: ', botChoice)
results = Winner(humanChoice, botChoice)
//messege = finaleMessege(results);
//rpsfrontend(yourChoice.id, botChoice, messege);
}
function randombot() {
return Math.floor(Math.random() * 3)
}
function numbertochoice(number){
return ['rock', 'paper', 'scissors'][number]
}
function Winner(humanChoice, botChoice){
var rpsdatabase = {
'rock': {'scissors': 1, 'paper': 0,'rock': 0.5 },
'paper': {'rock': 1, 'scissors':0, 'paper': 0.5},
'scissors': {'paper': 1, 'rock':0, 'scissors': 0.5},
};
var yourScore = rpsdatabase(humanChoice)(botChoice);
var botScore = rpsdatabase(humanChoice)(yourChoice);
return (yourScore, botScore);
不要使用括号访问对象。这样做
var yourScore = rpsdatabase[humanChoice][botChoice];
var botScore = rpsdatabase[humanChoice][yourChoice];
当我在其中运行出现这个错误[Uncaught TypeError: rpsdatabase is not a function]
输出应该类似于 [0,1],这意味着人类选择石头而机器人选择纸 我在两个空行之间留下了错误行以便轻松找到它
function rpsGame(yourChoice){
console.log(yourChoice);
var humanChoice, botChoice;
humanChoice = yourChoice.id;
botChoice = numbertochoice(randombot());
console.log('computer choice: ', botChoice)
results = Winner(humanChoice, botChoice)
//messege = finaleMessege(results);
//rpsfrontend(yourChoice.id, botChoice, messege);
}
function randombot() {
return Math.floor(Math.random() * 3)
}
function numbertochoice(number){
return ['rock', 'paper', 'scissors'][number]
}
function Winner(humanChoice, botChoice){
var rpsdatabase = {
'rock': {'scissors': 1, 'paper': 0,'rock': 0.5 },
'paper': {'rock': 1, 'scissors':0, 'paper': 0.5},
'scissors': {'paper': 1, 'rock':0, 'scissors': 0.5},
};
var yourScore = rpsdatabase(humanChoice)(botChoice);
var botScore = rpsdatabase(humanChoice)(yourChoice);
return (yourScore, botScore);
不要使用括号访问对象。这样做
var yourScore = rpsdatabase[humanChoice][botChoice];
var botScore = rpsdatabase[humanChoice][yourChoice];