3

I have to make a Mastermind Game for an assignment. I am stuck on the part where I have taken user input and need to compare them with the random numbers. Then I need to put the (user input) numbers in correct spots on the grid (bottom up).

Also, I have to show if the number is:

  • right and in right position by showing 4,
  • right but wrong position by showing 2,
  • wrong by showing 0

Further, I need to keep asking for user input until they have reached the maximum tries of 10 or have guessed the right answer.


Here is my code

...
 public static void main(String[] args) {
 PlayMasterMind.computerNum();
 PlayMasterMind.printBoard();
 PlayMasterMind.userInput();
 PlayMasterMind.compare();
 }
 public void printBoard(){
 System.out.println(" _______________________");
 System.out.println("| " + gotIt + " | " + gotIt + " | " + gotIt + " | " + gotIt + " | ");
 System.out.println(" _______________________ ____");
 for (j = 0; j < 10; j++) {
 for (int k = 0; k < 4; k++) {
 guess[j][k] = " ";
 answer[k] = " ";
 }
 System.out.println("| " + guess[j][0] + "| " + guess[j][1] + "| " + guess[j][2] + "| "
 + guess[j][3] + "|==|" + answer[0] + answer[1] + answer[2] + answer[3]
 + "|");
 System.out.println(" _______________________ ____");
 } 
}
 }

I am having a problem in the above section, where I need to put user input in the grid from bottom up.

At this point what I get is that:

Enter 4 numbers: 2342


| 2342 | 2342 | 2342 | 2342 | ___________________________ ____ | | | | |==| | ___________________________ ____ | | | | |==| | ___________________________ ____
...

But I actually need the numbers in the following order:

  • | 2 | 3 | 4 | 2|

and if these are the right numbers. I would like to output them the following way:

  • | 2 | 3 | 4 | 2|==|4444|

if 1 number is wrong, 2 numbers are right and at right position and 1 right but wrong position (right and wrong numbers order does NOT matter. Say if the right number is 2342, but user puts in 1242) then I would to show it this way:

  • | 2 | 3 | 4 | 2|==|4420|

but don't get anything in the boxes below. it keeps overwriting the "X" OR the information at the place of the "X'.

Any help would be much appreciated.

Thank you in advance!!!

asked Mar 12, 2016 at 23:45
1
  • 1
    Very first hint: do not stuff every bit of code into one large method. Such code is almost impossible to debug; or to understand 1 day after you wrote. To the contrary: better try to create many many small methods; that have good names; and that only do one single thing. Commented Mar 13, 2016 at 0:13

2 Answers 2

1

In order to find the problems in your code, I pasted it in my editor and tried to compile it. Also I reformatted it to make clearer. That is what I got

class Test73 {
 public static final int MAX_GUESSES = 10;
 public static final int NB_COLUMNS= 4;
 private String guess[][];
 private String answer[];
 private String gotIt;
 public static void main(String[] args) {
 Test73 t = new Test73();
 }
 public Test73() {
 gotIt = " "; // should probably be an array? 
 guess = new String[MAX_GUESSES][NB_COLUMNS];
 answer = new String[NB_COLUMNS];
 printBoard();
 }
 public void printBoard() {
 System.out.println(" _______________________");
 System.out.println( // assumes gotIt to be of length 1
 "| " + gotIt +
 " | " + gotIt +
 " | " + gotIt +
 " | " + gotIt +
 " | ");
 System.out.println(" _______________________ ____");
 for (int j = 0; j < MAX_GUESSES; j++) {
 for (int k = 0; k < NB_COLUMNS; k++) {
 guess[j][k] = " ";
 answer[k] = " ";
 }
 System.out.println(
 "| " + guess[j][0] +
 "| " + guess[j][1] +
 "| " + guess[j][2] +
 "| " + guess[j][3] +
 "|==|" + answer[0] +
 answer[1] +
 answer[2] +
 answer[3] + "|");
 System.out.println(" _______________________ ____");
 }
 }
}

I compiled it and ran it. That is what I got

 _______________________
| | | | | 
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____
| | | | |==| |
 _______________________ ____

Here are a few observations

  1. The variable gotIt should probably be an array, otherwise you will only be able to print the same value four times.

  2. Before printing the table, the values of guess and answer are set to empty strings. Although it is OK at the beginning of the game, it won't be later on. That should appear elsewhere in your program (in the initialization section).

  3. Important: the class Test was only created for testing this part of the program, it will not appear in your final program. Do the same thing for the other parts of your program (you may re-use the Test class).

Good luck!

answered Mar 13, 2016 at 14:07
Sign up to request clarification or add additional context in comments.

Comments

1
  1. Software engineering hint: do not put all the code in one single main method, create methods for specific tasks (ex. ask user choice, print board, compute answer, etc). You will then be able to debug these individual tasks one at a time.

  2. When generating random number, since Math.random() generates a floating point number 0.0 <= r < 1.0 (exclusive), the entire expression (int) (Math.random() * nums.length) will generate an integer from 0 to 7. Add one to make it in the range 1-8.

answered Mar 13, 2016 at 0:04

10 Comments

I am getting the numbers from 1-8 now but how can i compare them more efficiently. and how to pass the user input values to the spaces in the grid accordingly
In order for other people to be able to help, specify which of the methods do not seem to work. Tell what it is supposed to do, and what it does. The only thing I was able to see when you have the condition if (i == j), wouldn't that be if (guess[i] == answer[j]) or something similar? Of course, separating the code in different methods was not meant to solve the problem, but would allow better isolating where the problem comes from. Have a look at "debugging techniques", for example stackoverflow.com/questions/17630527/…
Edit your OP with this new information (your last two comments). Greater visibility = increase chances of getting answer.
Edit your original post with this new information (your last two comments). By writing what the problem is (in the original question, instead of in a comment) will increase the chances of someone looking at it and help you find the problem.
Have a look at stackoverflow.com/help/mcve It explains what the comments you received were trying to tell.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.