processing/java- 如何检查数组中的值是否不变

processing/java- how to check if values in array dont change

所以我正在制作一个刽子手游戏并生成一个随机单词 theWord 然后我用

制作一个包含单词中所有字符的数组
char theWordChars[] =t heWord.toCharArray();

然后我使用 for 循环检查按下的键是否等于 Word 中的任何字符,并且我创建了一个名为 keyIsFound[] 的布尔数组:

void keyPressed(){
    if(keyCode != 0 || keyCode != UP || keyCode != DOWN || keyCode != LEFT || keyCode != RIGHT){
        lastKey = char(keyCode);
}
    for (int z = 0; z< theWordChars.length; z++) {
        if(lastKey == theWordChars[z]){
keyIsFound[z] = true;
        }
    } 
}

所以现在我要检查的是当按下一个键但数组 keyIsFound 中的值没有变化时,即按下一个错误的字符然后我可以增加我的计数器以显示 body 部分。我该怎么做?接受彻底改变它的想法。

您通常会为此使用 flag - 通常是 boolean:

    // Did we find the key in the word?
    boolean found = false;
    // Look at all of the characters.
    for (int z = 0; z < theWordChars.length; z++) {
        // Did they press this one?
        if (lastKey == theWordChars[z]) {
            // YES! Mark it as found.
            keyIsFound[z] = true;
            // Remember we found one so we don't add a body part.
            found = true;
        }
    }
    if ( !found ) {
        // Not found the key they pressed - add a body part.
    }

您可以检查 indexOf() 字符是否大于等于 0。 如果你把你的char转成String,你可以检查你的单词contains()这个key.

我还注意到您使用了 keyCode,但您可以直接使用 key。 此外,您可以使用字符 class 来确定 key 是否按下 isLetter()

没有任何幻想UI,这是一个使用上述概念的例子:

String keyword = "Whosebug";
int lives = 6;

void setup(){
}
void draw(){
}

void keyPressed(){
  //check if the key pressed is a letter
  if(Character.isLetter(key)){
    //check if the word contains the letter
    if(keyword.contains(""+key)){
      println(key + " is in " + keyword);
      //remove the found letter from the word
      keyword = keyword.replaceAll(""+key,"");
      println("letters left: " + keyword);
      //if all letters have been found, we have a winner
      if(keyword.isEmpty()) {
        println("you win!");
        exit();
      }
    }else{
      lives--;
      if(lives == 0){
        println("game over!");
        exit();
      }
    }
  }
}

更新 为了好玩,这里有一个注释版本,可以在屏幕上绘制一些东西 并且还使用 HashMap and createShape()

//word to solve
String keyword = "Whosebug";
int lettersLeft = keyword.length();
//solved word to remove guessed letters from
String solved;

int lives;
int maxLives;

//what has been guessed so far
String solution;
//a lookup of all the letters already pressed
HashMap<Character,Integer> usedLetters = new HashMap<Character,Integer>();

//game status
String status;
boolean gameOn;

//stick figure drawing
PShape hangman;//the empty group
PShape[] hangmanMembers;//each part


void setup(){
  size(100,100,P2D);
  //intialize the group
  hangman = createShape(GROUP);
  hangman.addChild(createShape(LINE,20,100,20,30));
  hangman.addChild(createShape(LINE,20,30,50,30));
  hangman.addChild(createShape(LINE,50,30,50,35));
  hangmanMembers = new PShape[]{createShape(ELLIPSE,50,35,10,10),//head
                               createShape(LINE    ,55,45,55,50),//neck
                               createShape(LINE    ,55,50,55,70),//body
                               createShape(LINE    ,55,50,40,45),//left arm
                               createShape(LINE    ,55,50,80,45),//right arm
                               createShape(LINE    ,55,70,40,90),//left leg
                               createShape(LINE    ,55,70,60,90)//right leg
                              };
  //some people draw a hangman with no neck, other use more parts, make the number of lives dependant on that
  maxLives = hangmanMembers.length;
  lives = maxLives;//set the number of lives left
  //add the dashes
  solved = ""+keyword;
  solution = "";
  status = "";
  for(int i = 0 ; i < lettersLeft; i++) solution += "_";
  usedLetters.clear();
  gameOn = true;
}
//render
void draw(){
  background(127);
  text(solution,10,15);
  text(status,10,25);
  shape(hangman);
}

void keyPressed(){
  //if the game was not won or lost yet
  if(gameOn){
    //check if the key pressed is a letter
    if(Character.isLetter(key)){
      //check if the letter hasn't been used before
      if(!usedLetters.containsKey(key)){
        //if so, add to the list of used letters
        usedLetters.put(key,keyCode);

        //check if the word contains the letter
        int index = keyword.indexOf(key);
        if(index >= 0){
          println(key + " is in " + keyword);

          //remove the found letter from the word
          solved = solved.replaceAll(""+key,""); 
          println("letters left: " + solved);
          //update text for display
          for(int i = 0 ; i < solution.length(); i++){
            if(keyword.charAt(i) == key){
              solution = solution.substring(0,i)+key+solution.substring(i+1);
            }
          }
          //if all letters have been found, we have a winner
          if(solved.isEmpty()) {
            status = "you win!";
            gameOn = false;
          }
        }else{//wrong letter, lose a part
          int livesDiff = maxLives-lives;//work out the number of lives lost (maximum value - current)
          if(livesDiff < maxLives) {//if there still is a shape to display
            hangman.addChild(hangmanMembers[livesDiff]);
          }
          println(livesDiff);
          lives--;
          if(lives <= 0){
            status = "game over!";
            gameOn = false;
          }
        }
      }else{
        println("you've used " + key + " before");
      }
    }
  }else{
    //reset
    setup();
  }
}