程序设计(程序语言种类不限)
现有一大小为10*10的游戏地图(左上坐标{0,0}),程序开始时,兔子默认出现在{0,2}坐标;
编程控制兔子移动,要求:
1 .兔子位置初始化为{0,2};2 .提示用户输入数字,用户每次输入一次后,兔子按照规则移动一次。3 .当用户输入的数字为奇数时,兔子移动3格4 .当用户输入的数字为偶数时, 兔子移动1格;5 .兔子逆时针方向绕地图边沿移动;
现有一大小为10*10的游戏地图(左上坐标{0,0}),程序开始时,兔子默认出现在{0,2}坐标;
编程控制兔子移动,要求:
1 .兔子位置初始化为{0,2};2 .提示用户输入数字,用户每次输入一次后,兔子按照规则移动一次。3 .当用户输入的数字为奇数时,兔子移动3格4 .当用户输入的数字为偶数时, 兔子移动1格;5 .兔子逆时针方向绕地图边沿移动;
Scanner scan = new Scanner(System.in);
int x = 0, y = 2;
while (scan.hasNext()) {
int in = scan.nextInt();
int jump = 3;
if (in % 2 == 0) {
jump = 1;
}
if (x == 0 && y >= jump) {
y -= jump;
}
else if (y == 0 && (x + jump) <= 10) {
x += jump;
}
else if (x == 10 && (y + jump) <= 10) {
y += jump;
}
else if (y == 10 && x >= jump) {
x -= jump;
}
System.out.println("[" + x + "," + y + "]");
}
}
估计我理解错了……
然后就是二维数组判断 是否到边界了 到了 就转呗
//step为步长2或3
void Rubit(int step){
int x=0; //兔子初始横坐标
int y=2; //兔子初始纵坐标
if(x==0){
if((y+step)<10){
y+=step;
}else{
x=y+step-ARRAYMAXSIZE;
y=ARRAYMAXSIZE;
}
}else if(y==9){
if((x+step)<10){
x+=step;
}else{
y=x+step-ARRAYMAXSIZE;
x=ARRAYMAXSIZE;
}
}else if(x==9){
if((y-step>=0)){
y-=step;
}else{
x=ARRAYMAXSIZE-(step-y);
y=ARRAYMINSIZE;
}
}else{
if(x-step>0){
x-=step;
}else{
y+=step-x;
x=ARRAYMINSIZE;
}
}
int[] rubit[]=new int[10][10];
System.out.println("兔子当的位置是"+x+","+y);
}关键部分代码,自测无误,请多指教
else if(y==9){
if((x+step)<10){
x+=step;
}else{
y=ARRAYMAXSIZE-x-step+ARRAYMAXSIZE;
x=ARRAYMAXSIZE;
}
final int size = 10;
final Point startPos = new Point(0, 2);
List<Point> borderPositions = new ArrayList<Point>();
for (int i = size; i-- > 0;) { // Top, from right to left
borderPositions.add(new Point(i, 0));
}
for (int i = 1; i < size; i++) { // Left, from top to bottom
borderPositions.add(new Point(0, i));
}
for (int i = 1; i < size; i++) { // Bottom, from left to right
borderPositions.add(new Point(i, size - 1));
}
for (int i = size - 1; i-- > 1;) { // Right, from bottom to top
borderPositions.add(new Point(size - 1, i));
} Scanner scanner = new Scanner(System.in);
int index = borderPositions.indexOf(startPos);
System.out.println(startPos);
while (scanner.hasNext()) {
int num = scanner.nextInt();
index += (num % 2 == 0) ? 1 : 3;
index %= borderPositions.size();
System.out.println(borderPositions.get(index));
}
}