Results 1 to 11 of 11
  1. #1
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default how to make mastermind game

    hi there
    i was just wondereing if some on could help me create a game.
    basically the computer needs to come up with 4 random letters from ABCDEF.
    the player will only get 6 six changes to guess the code.
    i also need to show how many letter that they got in the right place or approximate place

    i have a basic understanding of java so i knw that im gonna need to use an array and loop but i dont knw how to set it up.
    plz help:D

  2. #2
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default

    so far ive got

    public class Practice {

    private String generateSecretCode() {

    ArrayList<String> = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    String firstLetter = lettersList.remove((int)(Math.random() * 6));
    String secondLetter = lettersList.remove((int)(Math.random() * 5));
    String thirdLetter = lettersList.remove((int)(Math.random() * 4));
    String fourthLetter = lettersList.remove((int)(Math.random() * 3));
    String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    return secretCode;
    }

    private String getGuess() {
    }

    private int getExactMatches(String secretCode, String guess) {
    }

    private int getApproxMatches(String secretCode, String guess) { }

    public void start() {

    int numberOfTurns = 6;
    int guessNumber = 1;
    System.out.println("WELCOME");
    String secretCode = generateSecretCode();
    System.out.println("The secret code is: " + secretCode);
    while (numberOfTurns > 0) {
    System.out.print("\n" + (guessNumber++) + ". " + "Enter your guess (ABCDEF): ");
    String guess = getGuess();
    int exactMatches = getExactMatches(secretCode, guess);
    int approxMatches = getApproxMatches(secretCode, guess);
    System.out.println("Exact matches: " + exactMatches);
    System.out.println("Approximate matches: " + approxMatches);
    numberOfTurns--;
    if (numberOfTurns == 0) {
    System.out.println("Bad luck. You lose! The code was: " + secretCode);
    }

    if (exactMatches > 3) {
    System.out.println("Congratulations. You won!");
    numberOfTurns *= 0;
    }
    }


    }

    which gies errors like :

    java:5: not a statement
    ArrayList<String> = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );

    java:5: ';' expected
    ArrayList<String> = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );

    reached end of file while parsing


    also can you show me how to add loops

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    ArrayList<String> is a type...
    Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } ); return an List object..., so you need a object variable name...

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    From what you've got so far, my main recommendation would be to avoid having separate variables 'firstLetter', 'secondLetter' etc. There's nothing special about each letter position: they're just four of the same thing. So use an array to represent that. That'll make it much easier when you need to do "things with every letter", such as cycling through them to print them out, check if they've been guessed etc.

  5. #5
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default

    i dont understand what you mean can you show me an example

  6. #6
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default Whats

    whats an object variable name

  7. #7
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default hi

    Quote Originally Posted by neilcoffey View Post
    From what you've got so far, my main recommendation would be to avoid having separate variables 'firstLetter', 'secondLetter' etc. There's nothing special about each letter position: they're just four of the same thing. So use an array to represent that. That'll make it much easier when you need to do "things with every letter", such as cycling through them to print them out, check if they've been guessed etc.
    i dunno how to use the array to represent the letters. can you plz show with an example

  8. #8
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    ArrayList<String> is a type...
    Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } ); return an List object..., so you need a object variable name...
    whats an object variable name

  9. #9
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    On the line where you do
    Java Code:
    ArrayList<String> = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    should be
    Java Code:
    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    So you can later do
    Java Code:
    varName.get(1); //gets the second object in the ArrayList which is called varname

  10. #10
    javabeginer is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Mr.Beans View Post
    On the line where you do
    Java Code:
    ArrayList<String> = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    should be
    Java Code:
    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    So you can later do
    Java Code:
    varName.get(1); //gets the second object in the ArrayList which is called varname

    what does this error mean
    Practice.java:5: ';' expected
    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" })
    ^
    Practice.java:49: reached end of file while parsing
    };
    ^
    2 errors

    heres my code

    public class Practice {

    private String generateSecretCode() {

    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" })

    String firstLetter = lettersList.remove((int)(Math.random() * 6));
    String secondLetter = lettersList.remove((int)(Math.random() * 5));
    String thirdLetter = lettersList.remove((int)(Math.random() * 4));
    String fourthLetter = lettersList.remove((int)(Math.random() * 3));
    String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    return secretCode;
    }

    private String getGuess() {
    }

    private int getExactMatches(String secretCode, String guess) {
    }

    private int getApproxMatches(String secretCode, String guess) { }

    public void start() {

    int numberOfTurns = 6;
    int guessNumber = 1;
    System.out.println("WELCOME");
    String secretCode = generateSecretCode();
    varName.get(1);
    System.out.println("The secret code is: " + secretCode);
    while (numberOfTurns > 0) {
    System.out.print("\n" + (guessNumber++) + ". " + "Enter your guess (ABCDEF): ");
    String guess = getGuess();
    int exactMatches = getExactMatches(secretCode, guess);
    int approxMatches = getApproxMatches(secretCode, guess);
    System.out.println("Exact matches: " + exactMatches);
    System.out.println("Approximate matches: " + approxMatches);
    numberOfTurns--;
    if (numberOfTurns == 0) {
    System.out.println("Bad luck. You lose! The code was: " + secretCode);
    }

    if (exactMatches > 3) {
    System.out.println("Congratulations. You won!");
    numberOfTurns *= 0;
    }


    }

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Practice.java:5: ';' expected
    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" })
    You have to be kidding, right? It's telling you that it's expecting a ";" and it's telling you on what line it's expecting it... I'll give you a hint... what goes at the end of almost all the lines in a Java program?
    Practice.java:49: reached end of file while parsing
    };
    hhhmmm... look for a missing closing bracket or a bracket that has a ";" after it. As posted, your program is missing at least one closing brackets at the end.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 12:33 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  4. Tic Tac Game
    By loggen in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 07:36 AM
  5. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •