Mastermind游戏算法 [英] Mastermind Game Algorithm
问题描述
我必须为作业制作一个Mastermind游戏。我被困在我已经接受用户输入的部分,需要将它们与随机数进行比较。然后我需要将(用户输入)数字放在网格上的正确位置(自下而上)。
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:
- 对通过显示4来显示4,
- 正确但错误的位置,
- 错误显示0
此外,我需要继续询问用户输入,直到他们达到最大尝试次数10或猜对了。
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.
此时我得到的是:
输入4个数字:2342
Enter 4 numbers: 2342
| 2342 | 2342 | 2342 | 2342 |
___________________________ ____
| | | | | == | |
___________________________ ____
| | | | | == | |
___________________________ ____
...
| 2342 | 2342 | 2342 | 2342 |
___________________________ ____
| | | | |==| |
___________________________ ____
| | | | |==| |
___________________________ ____
...
但实际上我需要按以下顺序排列数字:
But I actually need the numbers in the following order:
- | 2 | 3 | 4 | 2 |
- | 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 |
- | 2 | 3 | 4 | 2|==|4444|
如果1个数字错误,2个数字正确且位于正确位置1正确但错误的位置(正确和错误的数字顺序无关紧要。如果正确的数字 2342,但是用户放入 1242 )然后我会这样表现出来:
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 |
- | 2 | 3 | 4 | 2|==|4420|
但请勿在下面的框中获得任何内容。它会一直覆盖X或X处的信息。
but don't get anything in the boxes below. it keeps overwriting the "X" OR the information at the place of the "X'.
任何帮助都会非常感激。
提前谢谢!!!
推荐答案
为了找到你代码中的问题,我把它粘贴在我的编辑器中并尝试编译它。我也重新格式化它以使它更清楚。这就是我得到的
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
_______________________
| | | | |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
| | | | |==| |
_______________________ ____
以下是一些观察结果
-
变量
gotIt
应该是一个数组,否则你只能打印四次相同的值。
The variable
gotIt
should probably be an array, otherwise you will only be able to print the same value four times.
在打印表格之前, guess的值
和回答
设置为空字符串。虽然在游戏开始时它是可以的,但它不会在以后。这应该出现在程序的其他地方(在初始化部分)。
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).
重要:类测试
仅用于测试程序的这一部分,它不会出现在您的最终程序中。对程序的其他部分执行相同的操作(可以重新使用Test类)。
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).
祝你好运!
这篇关于Mastermind游戏算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!