Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1.  public GuessResult makeAGuess(MastermindNumber numberOfGuess) {
  2.  
  3.         String secrect = this.secretNumber.toString();
  4.         String guess = numberOfGuess.toString();
  5.  
  6.         int correctpos = 0;
  7.         int incorrectpos = 0;
  8.  
  9.         // iterate the guess to match the secret
  10.         for (int i = 0; i < guess.length(); i++) {
  11.  
  12.             // is the number present in the secret, contains cant handle single chars, so we have to simulate the contains
  13.             if(secrect.indexOf(guess.charAt(i)) > -1) {
  14.  
  15.                 // is it the same position?
  16.                 if (secrect.charAt(i) == guess.charAt(i)) {
  17.                     correctpos++;
  18.                 } // otherwise the number is only present but not on the correct position
  19.                 else {
  20.                     incorrectpos++;
  21.                 }
  22.  
  23.             }
  24.         }
  25.  
  26.         this.guessResult = new GuessResult(correctpos, incorrectpos);
  27.         return this.guessResult;
  28.  
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement