在我创建的这个程序中,有四种不同颜色的块(红色,绿色,蓝色和黄色。计算机隐藏了三个不同颜色的块。用户然后尝试猜测颜色和顺序猜测三个隐藏块的颜色后,计算机会显示有多少颜色是正确的,有多少颜色位于正确的位置。我遇到的麻烦就是函数checkColoursCorrect()和checkPositionsCorrect(我总是在结果中得到0或者1。我从来没有得到2或3个位置/颜色正确。请告诉我我做错了什么。 这是我的代码:
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Declare Global Variables
var computerBlock1:String; // the computer's first block
var computerBlock2:String; // the computer's second block
var computerBlock3:String; // the computer's third block
function checkGuess(e:MouseEvent):void
{
var userGuess1:String;
var userGuess2:String
var userGuess3:String;
var coloursCorrect:int;
var positionsCorrect:int;
userGuess1 = txtinBlock1.text;
userGuess2 = txtinBlock2.text;
userGuess3 = txtinBlock3.text;
userGuess1 = userGuess1.toUpperCase();
userGuess2 = userGuess2.toUpperCase();
userGuess3 = userGuess3.toUpperCase();
coloursCorrect = checkColoursCorrect(userGuess1,userGuess2,userGuess3);
positionsCorrect = checkPositionsCorrect(userGuess1,userGuess2,userGuess3);
lblOutput.text = "Number of Colours Correct = " + coloursCorrect.toString() + "\r";
lblOutput.text += "Number of Positions Correct = " + positionsCorrect.toString();
}
// g1– the user's guess of the first block
// g2– the user's guess of the second block
// g3– the user's guess of the third block
function checkColoursCorrect(g1:String,g2:String,g3:String):int
{
if(g1 == computerBlock1){
return 1
}
if(g2 == computerBlock2){
return 1
}
if(g3 == computerBlock3){
return 1
}
return 0;
}
// g1– the user's guess of the first block
// g2– the user's guess of the second block
// g3- the user's guess of the third block
function checkPositionsCorrect(g1:String,g2:String,g3:String):int
{
if(g1 == computerBlock1){
return 1
}
if(g2 == computerBlock2){
return 1
}
if(g3 == computerBlock3){
return 1
}
return 0;
}
function newGame(e:MouseEvent):void
{
var block1:int;
var block2:int; // numbers representing hidden block
var block3:int;
// generate three unique numbers between 1 and 4 for the block numbers
block1 = randomWholeNumber(4,1);
block2 = randomWholeNumber(4,1);
block3 = randomWholeNumber(4,1);
// convert block numbers to letters 1-R, 2-G, 3-B, 4-Y
if (block1 == 1) {
computerBlock1 = "R";
} else if (block1 == 2) {
computerBlock1 = "G";
} else if (block1 == 3) {
computerBlock1 = "B";
} else if (block1 == 4) {
computerBlock1 = "Y";
}
if (block2 == 1) {
computerBlock2 = "R";
} else if (block2 == 2) {
computerBlock2 = "G";
} else if (block2 == 3) {
computerBlock2 = "B";
} else if (block2 == 4) {
computerBlock2 = "Y";
}
if (block3 == 1) {
computerBlock3 = "R";
} else if (block3 == 2) {
computerBlock3 = "G";
} else if (block3 == 3) {
computerBlock3 = "B";
} else if (block3 == 4) {
computerBlock3 = "Y";
}
txtinBlock1.text = "";
txtinBlock2.text = "";
txtinBlock3.text = "";
lblOutput.text = "";
}
function randomWholeNumber(highNumber:int,lowNumber:int):int
{
return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}
答案 0 :(得分:0)
我认为你对return
的作用有误解。你的两个方法都只会返回0或1。
我相信你真正想要做的是每次用户猜对话时增加一个值,然后返回该值:
function checkColoursCorrect(g1:String,g2:String,g3:String):int
{
var coloursCorrect:int = 0;
if(g1 == computerBlock1){
coloursCorrect++;
}
if(g2 == computerBlock2){
coloursCorrect++;
}
if(g3 == computerBlock3){
coloursCorrect++;
}
return coloursCorrect;
}