title: Mastermind game in java
body: <pre><code>package edu.blastermind.model;
  
  import java.util.Random;
  
  /**
   * Text-based version of the Mastermind game. The hints are simpler in that you
   * know the position of correct pegs (but you know nothing at all about
   * correctly-colored pegs in wrong positions).
   * 
   * @author
   * 
   */
  public class BlasterMindGame {
  
      private static final int NUM_PEGS = 5;
      private String secret;
  
      /**
       * Creates a new game with a randomized secret consisting of some
       * combination of the letters: A, B, C, D, and/or E
       */
      public BlasterMindGame() {
          Random rng = new Random();
  
          this.secret = "ABCDE";
          // TODO 7: for-loop to create the secret string
          StringBuffer guess = new StringBuffer("");
          for (int i = 0; i &lt; NUM_PEGS - 1; i++) {
              char currentChar = guess.charAt(i);
              String cs = currentChar + "";
              currentChar = (char) ((char) (currentChar));
              Character A = guess.charAt(0);
              Character B = guess.charAt(1);
              Character C = guess.charAt(2);
              Character D = guess.charAt(3);
              Character E = guess.charAt(4);
              if (cs.matches("0")) {
                  guess.append(secret.charAt(A));
              }else if (cs.matches("1")) {
                  guess.append(secret.charAt(B));
              }else if (cs.matches("2")) {
                  guess.append(secret.charAt(C));
              }else if (cs.matches("3")) {
                  guess.append(secret.charAt(D));
              }else if (cs.matches("4")) {
                  guess.append(secret.charAt(E));
              }
          }
      }
  
      /**
       * Tries to guess the secret.
       * 
       * @param guess
       *            a 5-character string made up of only A, B, C, D, or E. Must be
       *            exactly 5 characters long.
       * 
       * @return a 5-character string made up of the characters - and O,
       *         indicating: '-' for an incorrect match in that position 'O' for a
       *         correct match in that position
       */
      public String getEasyHint(String guess) {
  
          String hint = "";
  
          return hint;
      }
  
      /**
       * Lets us know if we've guessed the secret or not.
       * 
       * @param guess
       *            our guess (must be exactly 5 characters long)
       * @return true if correct, false otherwise.
       */
      public boolean isGuessCorrect(String guess) {
  
          return this.secret.equals(guess);
      }
  }
  package edu.westga.blastermind.controllers;
  
  import java.util.Scanner;
  
  import edu.westga.blastermind.model.BlasterMindGame;
  
  public class BlasterMind {
  
      public static void main(String[] args) {
  
          BlasterMindGame game = new BlasterMindGame();
          Scanner kb = new Scanner(System.in);
          int turns = 1;
  
          while(true){
          System.out.println("Enter a guess:");
          String guess = kb.next();
          if (!game.isGuessCorrect(guess)){
              System.out.println("You guessed incorrect!");
              String input = kb.next();
              turns++;
          } else if  (game.isGuessCorrect(guess)) break;
          }
          // TODO 6: write the game simulation loop
  
  
          System.out.printf("You won in %d turns\n", turns);
      }
  
  }
  </code></pre>
  
  <p>Hey everyone I am writing the mastermind game in java which is a string-guessing game.  The secret in this game is not a number, but a 5-character string made up of the capital letters A, B, C, D, and E. When I run the main method I get this: </p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
      at java.lang.StringBuffer.charAt(Unknown Source)
      at edu.westga.blastermind.model.BlasterMindGame.&lt;init&gt;(BlasterMindGame.java:29)
      at edu.westga.blastermind.controllers.BlasterMind.main(BlasterMind.java:11)
  </code></pre>
  
  <p>The while loop is suppose to: •runs as long as the guess is incorrect
  in the body: </p>
  
  <ul>
  <li>tells the user the guess was incorrect</li>
  <li>gets the next guess from the Scanner</li>
  <li><p>increments the number of turns
  Which I think I have this right(Please let me know if I do not) and the for loop is suppose to:</p></li>
  <li><p>iterates from 0 to the number of pegs (note the static constant NUM_PEGS)</p></li>
  <li>for each iteration: 
  • gets a random number between 0 and NUM_PEGS-1 (see the nextInt(int n) method of Random for help here)
  <ul>
  <li>if the number is 0, append 'A' to this.secret</li>
  <li>if the number is 1, append 'B' to this.secret</li>
  <li>etc. for 2, 3, 4 and C, D, E, respectively</li>
  </ul></li>
  </ul>
  
  <p>For some reason when I run the program I get the error stated above and I am not sure how to change it to get it working properly.  Any help is appreciated.  Thanks in advance.</p>