Java 如何包装像侧滚游戏?
我正在尝试在2d屏幕上创建一个有船的游戏,如果船离开屏幕,我想让它们包装起来Java 如何包装像侧滚游戏?,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我正在尝试在2d屏幕上创建一个有船的游戏,如果船离开屏幕,我想让它们包装起来 public static void placeBoat (char [][] boat,int x, int y ){ for(int row = 0; row < boat.length; row++){ if(row==x){ for(int column = 0; column < boat[row].length; column++){
public static void placeBoat (char [][] boat,int x, int y ){
for(int row = 0; row < boat.length; row++){
if(row==x){
for(int column = 0; column < boat[row].length; column++){
if(column==y){boat [x][y] = '>';
boat [x][y-1] = '=';
boat [x][y-2] = '|';
boat [x][y-3] = '|';
boat [x][y-4] = '=';
boat [x][y-5] = '<';
}
}
}
}
}
publicstaticvoidplaceboat(char[]boat,intx,inty){
对于(int row=0;row';
船[x][y-1]='=';
船[x][y-2]=“|”;
船[x][y-3]=“|”;
船[x][y-4]='=';
船[x][y-5]='如果你想让船零件向左包裹,那么你必须在放置零件之前计算y
位置,而循环不是放置零件的好地方
通常,您的方法根本不需要在网格中循环,因为一次只放置一艘船
整个方法可以替换为基于x
和y
值计算船只位置,然后直接修改网格
public static void placeBoat(char [][] boat, int x, int y){
// the first part is at y
boat [x][y] = '>';
// size is 5 because there 5 more parts to the boat
int[] nextYs = new int[5];
for (int j = 0; j < nextYs.length; j++) {
// next y is at y - 1
int nextY = y - 1;
// next y is to the left
if (nextY >= 0) {
// set y to - 1
y = y - 1;
// next Y is to the right
} else {
// next y and y itself should be set to the last column
nextY = y = boat[x].length - 1;
}
nextYs[j] = nextY;
}
// print the other parts
boat [x][nextYs[0]] = '=';
boat [x][nextYs[1]] = '|';
boat [x][nextYs[2]] = '|';
boat [x][nextYs[3]] = '=';
boat [x][nextYs[4]] = '<';
}
publicstaticvoidplaceboat(char[]boat,intx,inty){
//第一部分在y
船[x][y]='>';
//尺寸是5,因为船上还有5个零件
int[]nextYs=新的int[5];
对于(int j=0;j=0){
//将y设置为-1
y=y-1;
//下一个Y在右边
}否则{
//下一个y和y本身应设置为最后一列
nextY=y=boat[x]。长度-1;
}
nextYs[j]=nextY;
}
//打印其他部分
船[x][nextYs[0]]='=';
船[x][nextYs[1]]='|';
船[x][nextYs[2]]='|';
船[x][nextYs[3]]='=';
boat[x][nextYs[4]='如果希望将boat零件向左包裹,则必须在放置零件之前计算y
位置,而循环不是放置零件的好位置
通常,您的方法根本不需要在网格中循环,因为一次只放置一艘船
整个方法可以替换为基于x
和y
值计算船只位置,然后直接修改网格
public static void placeBoat(char [][] boat, int x, int y){
// the first part is at y
boat [x][y] = '>';
// size is 5 because there 5 more parts to the boat
int[] nextYs = new int[5];
for (int j = 0; j < nextYs.length; j++) {
// next y is at y - 1
int nextY = y - 1;
// next y is to the left
if (nextY >= 0) {
// set y to - 1
y = y - 1;
// next Y is to the right
} else {
// next y and y itself should be set to the last column
nextY = y = boat[x].length - 1;
}
nextYs[j] = nextY;
}
// print the other parts
boat [x][nextYs[0]] = '=';
boat [x][nextYs[1]] = '|';
boat [x][nextYs[2]] = '|';
boat [x][nextYs[3]] = '=';
boat [x][nextYs[4]] = '<';
}
publicstaticvoidplaceboat(char[]boat,intx,inty){
//第一部分在y
船[x][y]='>';
//尺寸是5,因为船上还有5个零件
int[]nextYs=新的int[5];
对于(int j=0;j=0){
//将y设置为-1
y=y-1;
//下一个Y在右边
}否则{
//下一个y和y本身应设置为最后一列
nextY=y=boat[x]。长度-1;
}
nextYs[j]=nextY;
}
//打印其他部分
船[x][nextYs[0]]='=';
船[x][nextYs[1]]='|';
船[x][nextYs[2]]='|';
船[x][nextYs[3]]='=';
boat[x][nextYs[4]='我认为最干净的解决方案是使用模算子
为了简化解决方案,我建议稍微更改功能,而不是将y
作为船的右端位置,现在将是船的左端位置
如果您有一个长度为n=5
的数组,并且要从位置p=4
开始放置3个字符,那么您希望这些字符位于位置4、0、1:
BCxxA
01234
因此,不包装代码将是:
array[p] = 'A' // 4 + 0 = 4
array[p + 1] = 'A' // 4 + 1 = 5 - IndexOutOfBoundsException
array[p + 2] = 'A' // 4 + 2 = 6 - IndexOutOfBoundsException
public static void placeBoat(char[][] boat, int x, int y) {
boat[x][y] = '<';
boat[x][(y + 1) % boat[x].length] = '=';
boat[x][(y + 2) % boat[x].length] = '|';
boat[x][(y + 3) % boat[x].length] = '|';
boat[x][(y + 4) % boat[x].length] = '=';
boat[x][(y + 5) % boat[x].length] = '>';
}
注意,位置4,5,6,模5,给你4,0,1,这正是你需要的:
array[p] = 'A' // 4 + 0 = 4
array[(p + 1) % n] = 'A' // (4 + 1) % 5 = 5 % 5 = 0
array[(p + 2) % n] = 'A' // (4 + 2) % 5 = 6 % 5 = 1
因此,在您的情况下,这将是:
array[p] = 'A' // 4 + 0 = 4
array[p + 1] = 'A' // 4 + 1 = 5 - IndexOutOfBoundsException
array[p + 2] = 'A' // 4 + 2 = 6 - IndexOutOfBoundsException
public static void placeBoat(char[][] boat, int x, int y) {
boat[x][y] = '<';
boat[x][(y + 1) % boat[x].length] = '=';
boat[x][(y + 2) % boat[x].length] = '|';
boat[x][(y + 3) % boat[x].length] = '|';
boat[x][(y + 4) % boat[x].length] = '=';
boat[x][(y + 5) % boat[x].length] = '>';
}
publicstaticvoidplaceboat(char[]boat,intx,inty){
船[x][y]='';
}
我认为最干净的解决方案是使用模运算符
为了简化解决方案,我建议稍微更改功能,而不是将y
作为船的右端位置,现在将是船的左端位置
如果您有一个长度为n=5
的数组,并且要从位置p=4
开始放置3个字符,那么您希望这些字符位于位置4、0、1:
BCxxA
01234
因此,不包装代码将是:
array[p] = 'A' // 4 + 0 = 4
array[p + 1] = 'A' // 4 + 1 = 5 - IndexOutOfBoundsException
array[p + 2] = 'A' // 4 + 2 = 6 - IndexOutOfBoundsException
public static void placeBoat(char[][] boat, int x, int y) {
boat[x][y] = '<';
boat[x][(y + 1) % boat[x].length] = '=';
boat[x][(y + 2) % boat[x].length] = '|';
boat[x][(y + 3) % boat[x].length] = '|';
boat[x][(y + 4) % boat[x].length] = '=';
boat[x][(y + 5) % boat[x].length] = '>';
}
注意,位置4,5,6,模5,给你4,0,1,这正是你需要的:
array[p] = 'A' // 4 + 0 = 4
array[(p + 1) % n] = 'A' // (4 + 1) % 5 = 5 % 5 = 0
array[(p + 2) % n] = 'A' // (4 + 2) % 5 = 6 % 5 = 1
因此,在您的情况下,这将是:
array[p] = 'A' // 4 + 0 = 4
array[p + 1] = 'A' // 4 + 1 = 5 - IndexOutOfBoundsException
array[p + 2] = 'A' // 4 + 2 = 6 - IndexOutOfBoundsException
public static void placeBoat(char[][] boat, int x, int y) {
boat[x][y] = '<';
boat[x][(y + 1) % boat[x].length] = '=';
boat[x][(y + 2) % boat[x].length] = '|';
boat[x][(y + 3) % boat[x].length] = '|';
boat[x][(y + 4) % boat[x].length] = '=';
boat[x][(y + 5) % boat[x].length] = '>';
}
publicstaticvoidplaceboat(char[]boat,intx,inty){
船[x][y]='';
}
你想包装什么:整条船还是它的单个字符?不是整条船而是单个字符,因此如果其中一条船的绳索为0,1,那么第一行中的绳索看起来像这样>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~然后在增加坐标后使用模。看一看:嗯,我不知道我在做什么我正在修改。像这样我会做[x][(y-5)%33]='你想包装什么:整艘船还是它的单个角色?不是整艘船而是单个角色,因此如果其中一艘船的绳索为0,1,那么第一行中的绳索将如下所示>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~然后在增加坐标后使用模。看看:嗯,我对我是什么感到困惑修改。像这样我会做[x][(y-5)%33]='