如何将用户输入的猜测与他们之前的猜测进行比较?
How to compare an user input guess with their previous guess?
我的任务是编写一个基本的猜谜游戏,我已经完成了,但部分任务让我感到困惑。当用户多次输入相同的猜测时,我们被要求创建一个警告。我已经尝试了几种方法来获取以前的用户猜测并将它们与当前的猜测进行比较,但 none 似乎有效。谁能帮我这个?我的 Google 技能似乎让我失望了。
大部分我试过这个:
void guessWarning(int confirmedGuess){
int prevGuess = currentGuess;
int currentGuess = confirmedGuess;
if(prevGuess == currentGuess){
text("Same guess, try again",350,350)
}
}
有多种方法可以解决这个问题。
一个选项是在动态数组中跟踪以前的尝试(参见 ArrayList)。这里有一些代码来说明这个概念:
//create a new list of integers
ArrayList<Integer> guesses = new ArrayList<Integer>();
//in your check function, test if the new value already exists
if(guesses.contains(NEW_GUESS_HERE)){
println("you've already tried this number");
}else{//otherwise add the current guess to keep track of for next time
guesses.add(NEW_GUESS_HERE);
}
另一种选择是使用 HashMap. This is an associative array as opposed to an index based array. This method is more efficient and you can also keep track of how many attempts there were for each value. Be sure to read more on HashMaps:它将在长期 运行 上帮助您,并可能在短期 运行.
上给您的导师留下深刻印象
这里有一个基本的草图来说明这个想法:
//create a new hashmap of integers (key = guess, value = number of times tried)
HashMap<Integer,Integer> guesses = new HashMap<Integer,Integer>();
int answer = '=';
void setup(){}
void draw(){}
void keyPressed(){
guess(keyCode);
println(keyCode);
}
void guess(int newValue){
if(newValue == answer){
println("you guessed it!");
}else{
//check if the value was already recorded
try{
//if there was a value with this key, it's been tried before
int numberOfTries = guesses.get(newValue);
println("you've tried this value",numberOfTries,"times");
//increment the number of times this has beeen attempted
guesses.put(newValue,numberOfTries+1);
}catch(NullPointerException e){
println("it's the first time you try this number, but you haven't guessed it yet");
guesses.put(newValue,1);
}
}
}
一个类似的选项,但使用 JSONObject 会更老套一些。
这个概念是相似的:一个关联数组(尽管键是一个字符串,而不是一个 int),但是你需要先将猜测的数字转换为字符串以对其进行索引:
JSONObject guesses = new JSONObject();
int answer = '=';
void setup(){}
void draw(){}
void keyPressed(){
guess(keyCode);
println(keyCode);
}
void guess(int newValue){
if(newValue == answer){
println("you guessed it!");
}else{
//hacky int to string
String newValueStr = newValue+"";
//check if the value was already recorded
if(guesses.hasKey(newValueStr)){
//if there was a value with this key, it's been tried before
int numberOfTries = guesses.getInt(newValueStr);
println("you've tried this value",numberOfTries,"times");
//increment the number of times this has beeen attempted
guesses.setInt(newValueStr,numberOfTries+1);
}else{
println("it's the first time you try this number, but you haven't guessed it yet");
guesses.setInt(newValueStr,1);
}
}
}
一件好事是您可以 save the guesses to disk, then load 这样程序即使重新启动也可以回忆起以前的猜测。
我将留给您尝试在草图开始时加载数据并在草图存在时保存数据的有趣练习。
我的任务是编写一个基本的猜谜游戏,我已经完成了,但部分任务让我感到困惑。当用户多次输入相同的猜测时,我们被要求创建一个警告。我已经尝试了几种方法来获取以前的用户猜测并将它们与当前的猜测进行比较,但 none 似乎有效。谁能帮我这个?我的 Google 技能似乎让我失望了。
大部分我试过这个:
void guessWarning(int confirmedGuess){
int prevGuess = currentGuess;
int currentGuess = confirmedGuess;
if(prevGuess == currentGuess){
text("Same guess, try again",350,350)
}
}
有多种方法可以解决这个问题。
一个选项是在动态数组中跟踪以前的尝试(参见 ArrayList)。这里有一些代码来说明这个概念:
//create a new list of integers
ArrayList<Integer> guesses = new ArrayList<Integer>();
//in your check function, test if the new value already exists
if(guesses.contains(NEW_GUESS_HERE)){
println("you've already tried this number");
}else{//otherwise add the current guess to keep track of for next time
guesses.add(NEW_GUESS_HERE);
}
另一种选择是使用 HashMap. This is an associative array as opposed to an index based array. This method is more efficient and you can also keep track of how many attempts there were for each value. Be sure to read more on HashMaps:它将在长期 运行 上帮助您,并可能在短期 运行.
上给您的导师留下深刻印象这里有一个基本的草图来说明这个想法:
//create a new hashmap of integers (key = guess, value = number of times tried)
HashMap<Integer,Integer> guesses = new HashMap<Integer,Integer>();
int answer = '=';
void setup(){}
void draw(){}
void keyPressed(){
guess(keyCode);
println(keyCode);
}
void guess(int newValue){
if(newValue == answer){
println("you guessed it!");
}else{
//check if the value was already recorded
try{
//if there was a value with this key, it's been tried before
int numberOfTries = guesses.get(newValue);
println("you've tried this value",numberOfTries,"times");
//increment the number of times this has beeen attempted
guesses.put(newValue,numberOfTries+1);
}catch(NullPointerException e){
println("it's the first time you try this number, but you haven't guessed it yet");
guesses.put(newValue,1);
}
}
}
一个类似的选项,但使用 JSONObject 会更老套一些。 这个概念是相似的:一个关联数组(尽管键是一个字符串,而不是一个 int),但是你需要先将猜测的数字转换为字符串以对其进行索引:
JSONObject guesses = new JSONObject();
int answer = '=';
void setup(){}
void draw(){}
void keyPressed(){
guess(keyCode);
println(keyCode);
}
void guess(int newValue){
if(newValue == answer){
println("you guessed it!");
}else{
//hacky int to string
String newValueStr = newValue+"";
//check if the value was already recorded
if(guesses.hasKey(newValueStr)){
//if there was a value with this key, it's been tried before
int numberOfTries = guesses.getInt(newValueStr);
println("you've tried this value",numberOfTries,"times");
//increment the number of times this has beeen attempted
guesses.setInt(newValueStr,numberOfTries+1);
}else{
println("it's the first time you try this number, but you haven't guessed it yet");
guesses.setInt(newValueStr,1);
}
}
}
一件好事是您可以 save the guesses to disk, then load 这样程序即使重新启动也可以回忆起以前的猜测。 我将留给您尝试在草图开始时加载数据并在草图存在时保存数据的有趣练习。