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!!!
-
1Very 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.GhostCat– GhostCat2016年03月13日 00:13:06 +00:00Commented Mar 13, 2016 at 0:13
2 Answers 2
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
The variable
gotItshould probably be an array, otherwise you will only be able to print the same value four times.Before printing the table, the values of
guessandanswerare 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).Important: the class
Testwas 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!
Comments
Software engineering hint: do not put all the code in one single
mainmethod, 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.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.
10 Comments
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/…