【发布时间】:2019-05-06 10:26:37
【问题描述】:
我正在开发一个使用随机数生成器的堆叠箱游戏,但每当一个数字被使用两次时,我都无法让我的程序重置?
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 |
|___|
【问题讨论】:
-
你明白为什么部分代码:switch (number) { case 1: if (number == 1) { box1 = true; } 没有意义?
-
不,抱歉,我是编码新手
-
if ( number == 1 ) -> 除非 number == 1 已经被测试过,否则这段代码永远不会被执行。