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!!!