Java 数组游戏 - 游戏逻辑概念
我想创建一个游戏,以左侧 3 个绿点和右侧 3 个红点开始,它们之间有一个空格。残局应该有颜色交换。
游戏规则是,如果一个点旁边有一个空格,它应该能够移动到那里,如果它旁边有相反的颜色 - 它应该能够跳过,当且仅当相反的颜色后面有一个空格颜色。
我的问题位于代码下方。
代码如下:
//import mind.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
public class Drive {
String[] middle = new String[7];
public Drive() {
int player1, player2;
boolean gameEnd, checkempty, checkempty2, enemy, enemy2;
// Gamepieces
String gr,rd,tom;
gr="G"; rd="R"; tom=" ";
// beginning of the game, endgame and the updating array
String[] begin = {gr,gr,gr,tom,rd,rd,rd};
String[] mellan = new String[7];
String[] end = {rd,rd,rd,tom,gr,gr,gr};
Scanner in = new Scanner(System.in);
while (mellan!=end) {
mellan=begin;
for(int i=0; i<mellan.length; i++) {
System.out.print(mellan[i]);
}
System.out.print(" Choose 0-6: ");
int digits = in.nextInt();
//BOOLEAN for game rules!!!
checkempty = mellan[digits+1]==tom;
checkempty2 = mellan[digits-1]==tom;
enemy = (mellan[digits]==gr && mellan[digits+1]==rd && mellan[digits+2]==tom);
enemy2 = (mellan[digits]==rd && mellan[digits-1]==gr && mellan[digits-2]==tom);
if(checkempty) {
mellan[digits+1]=mellan[digits];
mellan[digits]=tom;
} else if (checkempty2) {
mellan[digits-1]=mellan[digits];
mellan[digits]=tom;
} else if (enemy) {
mellan[digits+2]=mellan[digits];
mellan[digits]=tom;
} else if (enemy2) {
mellan[digits-2]=mellan[digits];
mellan[digits]=tom;
}
}
System.out.print("Nicely Done");
}
// Test Drive!
public static void main(String args[]) {
new Drive();
}
}
问题:
现在,它正在构建游戏逻辑。如果这些点不能向后移动,我就会完成任务。但由于它们能够向后移动,因此当代码检查数组外部时,它会给我错误(虽然可以理解)。
我想到的解决方案是使数组更长,并带有微不足道的迹象,以免出现错误。但我想问还有别的办法吗? 因为,按照现在的方式,我无法移动我的第一个和最后一个点中间的数字应该按预期工作!
I want to create a game that starts with 3 green dots on the left and 3 red dots on the right with a space between them. The endgame should have the colors switched sides.
The rules of the game are that, if there is a space next to a dot it should be able to move there, also if an opposite color is next to it - it should be able to jump over iff there is a space behind the opposite color.
My question is located below the code.
Here is the code:
//import mind.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
public class Drive {
String[] middle = new String[7];
public Drive() {
int player1, player2;
boolean gameEnd, checkempty, checkempty2, enemy, enemy2;
// Gamepieces
String gr,rd,tom;
gr="G"; rd="R"; tom=" ";
// beginning of the game, endgame and the updating array
String[] begin = {gr,gr,gr,tom,rd,rd,rd};
String[] mellan = new String[7];
String[] end = {rd,rd,rd,tom,gr,gr,gr};
Scanner in = new Scanner(System.in);
while (mellan!=end) {
mellan=begin;
for(int i=0; i<mellan.length; i++) {
System.out.print(mellan[i]);
}
System.out.print(" Choose 0-6: ");
int digits = in.nextInt();
//BOOLEAN for game rules!!!
checkempty = mellan[digits+1]==tom;
checkempty2 = mellan[digits-1]==tom;
enemy = (mellan[digits]==gr && mellan[digits+1]==rd && mellan[digits+2]==tom);
enemy2 = (mellan[digits]==rd && mellan[digits-1]==gr && mellan[digits-2]==tom);
if(checkempty) {
mellan[digits+1]=mellan[digits];
mellan[digits]=tom;
} else if (checkempty2) {
mellan[digits-1]=mellan[digits];
mellan[digits]=tom;
} else if (enemy) {
mellan[digits+2]=mellan[digits];
mellan[digits]=tom;
} else if (enemy2) {
mellan[digits-2]=mellan[digits];
mellan[digits]=tom;
}
}
System.out.print("Nicely Done");
}
// Test Drive!
public static void main(String args[]) {
new Drive();
}
}
Problem:
Right now, it's making up the game logic. If the dots weren't able to move backwards I would have done the task. But since they are able to move backwards, it gives me the error when the code checks outside the array (understandable though).
The solution on top of my head is to make the array longer with insignificant signs as to not get the error. But I'm asking if there is another way? Because, the way it is now, I CAN'T MOVE my FIRST and LAST dots the middle numbers work as should be though!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当
digits
为 0 或数组的最大索引时,就会出现此问题。当digits
为 0 时,无法检查mellan[digits-1]
;当digits
为 0 时,无法检查mellan[digits+1]
digits
是数组的最大索引。因此,在尝试访问数组之前,您需要检查这些情况。您可以尝试这样的操作:
因为 Java 对此类布尔运算使用短路计算,因此只有当第一部分计算结果为
true
。因此,只有当digits
大于0时,mellan[digits-1]
才会被访问。显然,还需要处理位置向左或向右2个空格,但同样的原则也适用于此。
The issue occurs when
digits
is either 0 or the maximum index for the array. You can't checkmellan[digits-1]
whendigits
is 0, and you can't checkmellan[digits+1]
whendigits
is the maximum index for the array.So, you need to check for these situations before you try to access the array. You could try something like this:
Because Java uses short-circuit evaluation for boolean operations like this, the second part of this expression will only be evaluated if the first part evaluates to
true
. Therefore,mellan[digits-1]
will only be accessed ifdigits
is greater than 0.Obviously, you also need to deal with the position 2 spaces to the left or right, but the same principle could apply there too.
您可以考虑以下其中一项:
1)在两侧垫上 2 个绿色(或红色)点。它们将起到障碍的作用。
2) 每当检查数组的下一个/上一个位置时添加条件。
我会选择第二种方法...
我还建议稍微重构一下代码。您可以将验证跳转(向右或向左)的代码提取到一个单独的方法中,该方法接收方向(+1/-1,最好作为枚举)作为参数。
You may consider one of the following:
1) Pad the two sides with 2 green (or red) dots. They will work as barriers.
2) Add conditions whenever you check the next/previous position of the array.
I would choose the second approach...
I also suggest refactoring the code a little. You can extract the code that validates a jump (either right or left) into a separate method, that receives a direction(+1/-1, better as an enum) as parameter.
只需检查
digits + n
是否大于或等于 0(且小于mellan.length
)。例如:
Simply check if
digits + n
is greater than or equal to 0 (and smaller thanmellan.length
).e.g.: