在Java中比较一个游戏的数组
我想知道是否有人能帮我,我有一个游戏,电脑会随机生成一个4位数的代码,每个数字从0到5不等。然后用户猜出这段代码,计算机返回一个数组,我将其打印成字符串。数组中的每一个数字在正确的位置上都应该包含一个6,猜测中的每一个数字在正确的位置上都应该包含一个7,猜测中的每一个数字在正确的位置上都应该包含一个7,最后,对于任何完全不正确的数字都应该包含一个5 例如,如果代码为:0143在Java中比较一个游戏的数组,java,arrays,Java,Arrays,我想知道是否有人能帮我,我有一个游戏,电脑会随机生成一个4位数的代码,每个数字从0到5不等。然后用户猜出这段代码,计算机返回一个数组,我将其打印成字符串。数组中的每一个数字在正确的位置上都应该包含一个6,猜测中的每一个数字在正确的位置上都应该包含一个7,猜测中的每一个数字在正确的位置上都应该包含一个7,最后,对于任何完全不正确的数字都应该包含一个5 例如,如果代码为:0143 用户的猜测是:0451 数组应为:6775 这是因为0是完全正确的,1和4在代码中,但猜错了位置,而5是完全错误的。此外
用户的猜测是:0451
数组应为:6775 这是因为0是完全正确的,1和4在代码中,但猜错了位置,而5是完全错误的。此外,每个数字都位于阵列的单独部分中 这就是我到目前为止所做的:
for (int i = 0; i < 4; i++) {
if (combo[i] == guess[i]) {
Pegs[peg] = 6;
peg++;
}
}
for (int i = 0; i < 4; i++) {
for (int x = 0; x < 4; x++) {
if (guess[i] == combo[x] && guess[i] != combo[i]) {
Pegs[peg] = 7;
peg++;
}
}
}
for (int i = 0; i < 4; i++) {
if (Pegs[i] == 0) {
Pegs[i] = 5;
}
}
for(inti=0;i<4;i++){
if(combo[i]==guess[i]){
Pegs[peg]=6;
peg++;
}
}
对于(int i=0;i<4;i++){
对于(intx=0;x<4;x++){
如果(猜[i]==combo[x]&&guess[i]!=combo[i]){
Pegs[peg]=7;
peg++;
}
}
}
对于(int i=0;i<4;i++){
如果(Pegs[i]==0){
销钉[i]=5;
}
}
数组guess存储用户的猜测,数组组合是正确的代码,Pegs[]是存储比较数组的位置,int peg只表示在数组中存储值的位置。问题在于,它并不总是将数组正确返回到它要执行的操作。要比较数组,您只需要第一个循环,只有当相同位置的数字不相等时,才应执行内部循环:
for(int i = 0; i < 4; i++) {
Pegs[i]=5;
if(combo[i] == guess[i]) {
Pegs[i] = 6;
guess[i]= -1;
}
}
for(int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(combo[i] == guess[j]) {
Pegs[i]=7;
guess[j]= -1;
break;
}
}
}
...
for(int i=0;i<4;i++){
销钉[i]=5;
if(组合[i]==猜测[i]){
Pegs[i]=6;
猜测[i]=-1;
}
}
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
if(combo[i]==guess[j]){
Pegs[i]=7;
猜测[j]=-1;
打破
}
}
}
...
要比较数组,您只需要第一个循环,只有当同一位置的数字不相等时,才应执行内循环:
for(int i = 0; i < 4; i++) {
Pegs[i]=5;
if(combo[i] == guess[i]) {
Pegs[i] = 6;
guess[i]= -1;
}
}
for(int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(combo[i] == guess[j]) {
Pegs[i]=7;
guess[j]= -1;
break;
}
}
}
...
for(int i=0;i<4;i++){
销钉[i]=5;
if(组合[i]==猜测[i]){
Pegs[i]=6;
猜测[i]=-1;
}
}
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){
if(combo[i]==guess[j]){
Pegs[i]=7;
猜测[j]=-1;
打破
}
}
}
...
使用其他方法,如果出现以下情况,您将遇到麻烦:
int[] combot = new int[] {0, 1, 1, 3};
int[] guess = new int[] {0, 4, 5, 1};
因为你会得到一个不正确的[6,7,7,5]
,而不是正确的[6,7,5,5]
,因为你会把你最后的猜测1
计算成两倍
通过使用两个标志数组,一个用于精确匹配,另一个用于单个标志数组的错位猜测,您可以获得更好、更准确的结果:
**已编辑**我将其还原回去,因为我发现对于组合=[0,1,1,3]
和猜测=[3,0,1,5]
,它给出了错误的响应
public int[] computeGuess(int[] combo, int[] guess) {
int[] result = new int[4];
Arrays.fill(result, 5); // incorrect values for all!
boolean[] exactMatches = new boolean[4]; // all initially set to false
boolean[] misplaced = new boolean[4];
for (int i = 0; i < 4; i++) {
if (combo[i] == guess[i]) {
exactMatches[i] = true;
misplaced[i] = false; // make sure we don't use this as misplaced
} else {
for (int j = 0; j < 4; j++) {
if (i != j && combo[i] == guess[j] && !exactMatches[j] && !misplaced[j]) {
misplaced[j] = true;
break;
}
}
}
}
int i = 0;
for (boolean b : exactMatches) {
if (b) {
result[i++] = 6;
}
}
for (boolean b : misplaced) {
if (b) {
result[i++] = 7;
}
}
return result;
}
public int[]计算(int[]组合,int[]猜测){
int[]结果=新的int[4];
Arrays.fill(result,5);//所有的值都不正确!
boolean[]exactMatches=new boolean[4];//所有初始设置为false
布尔[]错位=新布尔[4];
对于(int i=0;i<4;i++){
if(组合[i]==猜测[i]){
精确匹配[i]=真;
misplaced[i]=false;//确保我们不会将此用作misplaced
}否则{
对于(int j=0;j<4;j++){
如果(i!=j&&combo[i]==guess[j]&&!exactMatches[j]&&!misplaced[j]){
错位[j]=真;
打破
}
}
}
}
int i=0;
for(布尔b:exactMatches){
如果(b){
结果[i++]=6;
}
}
for(布尔值b:错位){
如果(b){
结果[i++]=7;
}
}
返回结果;
}
使用其他方法,如果出现以下情况,您将遇到麻烦:
int[] combot = new int[] {0, 1, 1, 3};
int[] guess = new int[] {0, 4, 5, 1};
因为你会得到一个不正确的[6,7,7,5]
,而不是正确的[6,7,5,5]
,因为你会把你最后的猜测1
计算成两倍
通过使用两个标志数组,一个用于精确匹配,另一个用于单个标志数组的错位猜测,您可以获得更好、更准确的结果:
**已编辑**我将其还原回去,因为我发现对于组合=[0,1,1,3]
和猜测=[3,0,1,5]
,它给出了错误的响应
public int[] computeGuess(int[] combo, int[] guess) {
int[] result = new int[4];
Arrays.fill(result, 5); // incorrect values for all!
boolean[] exactMatches = new boolean[4]; // all initially set to false
boolean[] misplaced = new boolean[4];
for (int i = 0; i < 4; i++) {
if (combo[i] == guess[i]) {
exactMatches[i] = true;
misplaced[i] = false; // make sure we don't use this as misplaced
} else {
for (int j = 0; j < 4; j++) {
if (i != j && combo[i] == guess[j] && !exactMatches[j] && !misplaced[j]) {
misplaced[j] = true;
break;
}
}
}
}
int i = 0;
for (boolean b : exactMatches) {
if (b) {
result[i++] = 6;
}
}
for (boolean b : misplaced) {
if (b) {
result[i++] = 7;
}
}
return result;
}
public int[]计算(int[]组合,int[]猜测){
int[]结果=新的int[4];
Arrays.fill(result,5);//所有的值都不正确!
boolean[]exactMatches=new boolean[4];//所有初始设置为false
布尔[]错位=新布尔[4];
对于(int i=0;i<4;i++){
if(组合[i]==猜测[i]){
精确匹配[i]=真;
misplaced[i]=false;//确保我们不会将此用作misplaced
}否则{
对于(int j=0;j<4;j++){
如果(i!=j&&combo[i]==guess[j]&&!exactMatches[j]&&!misplaced[j]){
错位[j]=真;
打破
}
}
}
}
int i=0;
for(布尔b:exactMatches){
如果(b){
结果[i++]=6;
}
}
for(布尔值b:错位){
如果(b){
结果[i++]=7;
}
}
返回结果;
}
“阵列应为:6775”。是6757,对吗?试着做一个SCCEE,给我们看一些输出。“阵列应该是:6775”。这是6757,对吗?试着做一个SCCEE并向我们展示一些输出。只有在没有其他匹配项的情况下分配5
会使程序变得不必要的复杂。您仍然有一个peg