我正在开发一个使用随机数生成器的堆叠盒游戏,但是无论何时两次使用数字我都无法重置我的程序?
1)选择一种实现方式,要么在创建框时跟踪它们,要么直接进入解决方案。
2)while循环控制接下来的三个步骤。
3)挑选一个盒子
4)确定是否可以放置此框
5)放一个盒子
我使用了6个布尔变量,每个变量代表一个框。一个switch语句称为6种方法之一,具体取决于为一个框指定的随机数。这六种方法分析了当前的箱子组合,并确定了要调用的13个箱子组合中的哪一个。
package com.chinus;
i
private static void trace() {
int boxesUsedSoFar = 0;
boolean gameIsDone = false;
int usedOnce;
while (gameIsDone != true) {
int number = (int) ((Math.random() * 6) + 1);
boxesUsedSoFar++;
System.out.println("Current Box Number : " + number);
switch (number) {
case 1:
if (number == 1) {
box1 = true;
}
if (box1 == true && box2 == true && box3 == true) {
combo6();
} else if (box1 == true && box2 == true) {
combo4();
} else if (box1 == true && box3 == true) {
combo7();
} else if (box1 == true) {
combo1();
}
else{
restart();
}
box1=false;
break;
case 2:
if (number == 2) {
box2 = true;
}
if (box1 == true && box2 == true && box3 == true) {
combo6();
} else if (box1 == true && box2 == true) {
combo4();
} else if (box2 == true && box3 == true) {
combo5();
} else if (box2 == true) {
combo2();
}
else{
restart();
}
box2=false;
break;
case 3:
if (number == 3) {
box3 = true;
int count3 = 0;
}
boolean box3used =false;
if (box1 == true && box2 == true && box3 == true) {
combo6();
box3used =true;
} else if (box1 == true && box3 == true) {
combo7();
box3used =true;
} else if (box2 == true && box3 == true) {
combo5();
box3used =true;
}else if (box3 == true) {
combo3();
box3used = true;
}
else {
restart();
}
break;
case 4:
if (number == 4) {
box4 = true;
}
if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
combo12();
} else if (box4 == true && box2 == true & box3 == true && box1 == true) {
combo10();
} else if (box4 == true && box2 == true & box1 == true) {
combo8();
} else {
restart();
}
break;
case 5:
if (number == 5) {
box5 = true;
}
if (box5 == true && box4 == true && box3 == true && box2 == true && box1 == true) {
combo12();
} else if (box5 == true && box2 == true & box3 == true && box1 == true) {
combo11();
} else if (box5 == true && box2 == true & box3 == true) {
combo9();
} else {
restart();
}
break;
case 6:
if (number == 6) {
box6 = true;
}
if (box4 == true && box5 == true) {
combo13();
gameIsDone = true;
} else {
restart();
}
break;
default:
}
System.out.println("Boxes used so far : " + boxesUsedSoFar);
}
}
private static void restart() {
box1 = false;
box2 = false;
box3 = false;
box4 = false;
box5 = false;
box6 = false;
}
}
Actual:
Current Box Number : 2
___ ___
| | | |
| 2 | | 3 |
|___| |___|
Boxes used so far : 49820
Current Box Number : 2
___ ___
| | | |
| 2 | | 3 |
|___| |___|
Expect:Current Box Number : 2
___ ___
| | | |
| 2 | | 3 |
|___| |___|
Boxes used so far : 49820
Current Box Number : 2
___
| |
| 2 |
|___|
答案 0 :(得分:0)
我已经调整了一些内容:
当你说
switch(number)
case 1:
您是在问
if(number == 1)
因此,我们无需检查switch语句中的数字。
在if语句中检查布尔变量时,您无需说
if(boolean == true)
您可以简单地说
if(boolean)
默认情况下,这会检查该值是否为真。
就您代码的逻辑而言,我只展示了与您的问题相关的部分。您想在任何语句的开头检查当前框的状态。在“ case:1”中,我要做的第一件事是检查box1是否为真。如果是这样,我调用了reset方法,并将我们踢出了switch语句。
如果box1为false(表示我们之前没有使用过),则可以实际运行常规逻辑,然后将box1设置为true。我注释掉了某些逻辑的去向,因为我不确定自己要怎么做。
int boxesUsedSoFar = 0;
boolean gameIsDone = false;
while (gameIsDone != true) {
int number = (int) ((Math.random() * 6) + 1);
boxesUsedSoFar++;
System.out.println("Current Box Number : " + number);
switch (number) {
case 1:
if (box1) {
//box1 is false by default, if it was true here, it means we have a duplicate
restart();
break;
}
/*
Your code to call a combo method goes here, repeat for each case.
*/
box1 = true;
break;
case 2:
if (box2) {
restart();
break;
}
box2 = true;
break;
case 3:
if (box3) {
restart();
break;
}
box3 = true;
break;
case 4:
if (box4) {
restart();
break;
}
box4 = true;
break;
case 5:
if (box5) {
restart();
break;
}
box5 = true;
break;
default:
if (box6) {
restart();
break;
}
box6 = true;
break;
}
System.out.println("Boxes used so far : " + boxesUsedSoFar);
}
}
如果这里不清楚,请告诉我。