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:
Further, I need to keep asking for user input until they have reached the maximum tries of 10 or have guessed the right answer.
...
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:
and if these are the right numbers. I would like to output them the following way:
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:
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!!!
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 gotIt should probably be an array, otherwise you will only be able to print the same value four times.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With