程序设计(程序语言种类不限)
现有一大小为10*10的游戏地图(左上坐标{0,0}),程序开始时,兔子默认出现在{0,2}坐标;
编程控制兔子移动,要求:
1 .兔子位置初始化为{0,2};
2 .提示用户输入数字,用户每次输入一次后,兔子按照规则移动一次。
3 .当用户输入的数字为奇数时,兔子移动3格
4 .当用户输入的数字为偶数时, 兔子移动1格;
5 .兔子逆时针方向绕地图边沿移动;
------解决方案--------------------
- Java code
public static void main(String[] args) throws Exception { 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)); } }