ackage com.xsyu.lip.test3;
import java.util.Scanner;
public class TestGame {
/**
* 现有一大小为10*10的游戏地图(左上坐标{0,0}),程序开始时, 兔子默认出现在{0,2}坐标; 编程控制兔子移动, 要求:
* 1.兔子位置初始化为{0,2};
* 2 .提示用户输入数字,用户每次输入一次后,兔子按照规则移动一次。
* 3 .当用户输入的数字为奇数时,兔子移动3格
* 4 .当用户输入的数字为偶数时, 兔子移动1格;
* 5 .兔子逆时针方向绕地图边沿移动;
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private int x;
private int y;
private int loop;
private int[][] mapCoordinate;
public TestGame() {
mapCoordinate = new int[10][10];
this.x = 0;
this.y = 2;
this.loop = 0;
}
public int[][] getMapCoordinate() {
return mapCoordinate;
}
public void setMapCoordinate(int[][] mapCoordinate) {
this.mapCoordinate = mapCoordinate;
}
public void start(int step){
if(isOdd(step)){
// 判断是否是奇数,然后行走移动3格
isLeft(3);
isDown(3);
isRight(3);
isUp(3);
}else{
// 移动1格
isLeft(1);
isDown(1);
isRight(1);
isUp(1);
}
}
// TODO
public boolean isCastMap(){
return true;
}
//
// left ,down , right, up;
private void isLeft(int step){
if(x==loop&&y>=loop&&y<10-loop){
int temp = y + step;
if(temp>=10-loop){
this.y = 10-loop-1;
this.x = temp -this.y;
}else{
this.y = temp;
}
mapCoordinate[y][x] = 1;
}
}
private void isDown(int step){
if(x>=loop&&x<10-loop&&y==10-loop-1){
int temp = x + step;
if(temp>=10-loop){
this.x = 10-loop-1;
this.y = temp -this.x;
}else{
this.x = temp;
}
mapCoordinate[y][x] = 1;
}
}
private void isRight(int step){
if(x==10-loop-1 && y>=loop && y<10-loop){
int temp = y - step;
if(temp<loop){
this.y = loop;
// 注意这里temp的符号。
this.x = 10-loop-1+temp;
}else{
this.y = temp;
}
mapCoordinate[y][x] = 1;
}
}
private void isUp(int step){
if(x>=loop&&x<10-loop&&y==loop){
int temp = x-step;
if(temp<loop){
this.x = ++loop;
this.y = 10-loop-1+temp;
}else{
this.x = temp;
}
mapCoordinate[y][x] = 1;
}
}
public void show() {
for (int i = 0; i < this.mapCoordinate.length; i++) {
for (int j : mapCoordinate[i]) {
System.out.print(j + "\t");
}
System.out.println();
}
}
// true odd or even false.
private boolean isOdd(int a) {
if (!(a % 2 == 0)) {
return true;
} else
return false;
}
public boolean isPrime(int a) {
boolean b = true;
for(int i = 2;i<a;i++){
if(a%i==0){
b = false;
}
}
return b;
}
public static void main(String[] args) {
TestGame tg = new TestGame();
// tg.show();
Scanner sc = new Scanner(System.in);
for(;;){
System.out.println("请输入数字!");
int step = sc.nextInt();
if(step==100){
break;
}
tg.start(step);
}
tg.show();
}
}
很简单的一道题,可惜啊,我不在............不过,虽然游戏很有挑战,但是我的兴趣不在于此,估计我加入这一行得30岁以后了。