我遇到的面试题:用Java模拟棋子的移动

 

 

           我去面试,公司出的题目。当时我做的时候,只是有点思路,写的并不完整。后来,我就把这个题目做了一下。大部分功能实现了,还有一下细节没有出来好。给大家看看,希望起到抱砖引玉的作用!

 

 

 

       上图为黑白棋盘。用java代码模拟棋盘初始化,白子向上移,向下移,向左移,想右移的过程,成功移动返回值为true,移动失败返回值为false。(规则:白子不能超出棋盘边界;当白子所要移动的位置被黑子占据时,白子不能移动。)提示:用二维数组模拟棋盘。上图黑子的位置用二维数组表示为{{1,1}{2,3}{5,3}{4,4}{7,4}{67}{2,8}{4,8}{7,8}},白子的位置有二维数组表示为{{2,6}}

 

 

代码如下:

import java.util.Scanner;

 

public class Chess {

private static int[][] blackLocation;// 二维数组 blackLocation存放所有黑子的位置

private static int[][] whileLocation;// 二维数组 whileLocation存放所有白子的位置

private static int[][] tempLocation = { { -1, -1 } };// 二维数组tempLocation   存放白子的临时位置

private static boolean result = false;

private static String first;

 

public static void main(String[] args) {

// 第一步:棋盘初始化 ,9个黑子和1个白子分布在棋盘的任意位置。

boolean init = initialize(9, 1);

// 第二步:判断初始化是否成功

if (init) {

// 初始化成功

            ;

} else {

// 初始化失败

if (first.equals("1")) {

// 重新初始化

boolean b = initialize(9, 1);

if (!b) {

System.out.println("重新初始化再次失败,程序自动关闭。");

return;

}

 

} else if (first.equals("2")) {

// 退出

System.out.println("程序正在关闭,请稍后。。。");

return;

} else {

System.out.println("您发出的指令有误,程序自动关闭。请稍后。。。");

return;

}

}

 

// 第三步:发出移动白子的指令

for (int step = 0; step < ++step;) {

System.out.println("白子右移请按1,白子左移请按2,白子上移请按3,白子下移请按4");

Scanner sc = new Scanner(System.in);

String move = sc.next();

if (move.equals("1")) {

result = moveRight();

} else if (move.equals("2")) {

result = moveLeft();

} else if (move.equals("3")) {

result = moveUp();

} else if (move.equals("4")) {

result = moveDown();

} else {

System.out.println("您发出的指令有误,请重新输入指令");

}

 

// 第四步:执行移动白子这个指令的情况

// 成功移动后输出白子的新位置

if (result) {

System.out.println("白子的新位置:[" + whileLocation[0][0] + ","

+ whileLocation[0][1] + "]");

} else {

System.out.println("对不起,您不能移动白子");

}

}

 

}

 

public static boolean initialize(int blackNum, int whileNum) {

blackLocation = createBlack(blackNum);

whileLocation = createWhile(whileNum);

if (blackLocation != null && whileLocation != null) {

// 初始化成功

System.out.println("初始化成功,以下为黑子的位置");

for (int i = 0; i < 9; i++) {

System.out.println("[" + blackLocation[i][0] + ","

+ blackLocation[i][1] + "]");

}

System.out.println("以下为白子的位置");

System.out.println("[" + whileLocation[0][0] + ","

+ whileLocation[0][1] + "]");

 

return true;

} else {

// 初始化失败

System.out.println("初始化失败");

System.out.println("重新初始化,请按1。退出,请按0。");

Scanner sc = new Scanner(System.in);

first = sc.next();

return false;

}

}

 

public static int[][] createBlack(int num) {

/*

 * 参数num表示创建num个黑子

 */

blackLocation = new int[num][2];

for (int i = 0; i < num; i++) {

blackLocation[i][0] = -1;

blackLocation[i][1] = -1;

}

 

for (int i = 0; i < num; i++) {

/*

 * 黑子的位置通过{x,y}决定。 x的取值范围0~9,y的取值范围0~9。

 */

int x = (int) (Math.random() * 10);

int y = (int) (Math.random() * 10);

boolean b = isExisting(x, y);

if (!b) {

blackLocation[i][0] = x;

blackLocation[i][1] = y;

} else {

// 至少两个以上的黑子占据相同的位置

return null;

}

 

}

return blackLocation;

}

 

public static int[][] createWhile(int num) {

/*

 * 参数num表示创建num个黑子

 */

whileLocation = new int[num][2];

for (int i = 0; i < num; i++) {

whileLocation[i][0] = -1;

whileLocation[i][1] = -1;

}

 

for (int i = 0; i < num; i++) {

/*

 * 白子的位置通过{x,y}决定。 x的取值范围0~9,y的取值范围0~9。

 */

int x = (int) (Math.random() * 10);

int y = (int) (Math.random() * 10);

boolean b = isExisting(x, y);

if (!b) {

whileLocation[i][0] = x;

whileLocation[i][1] = y;

} else {

// 白子和黑子占据相同的位置

return null;

}

 

}

return whileLocation;

 

}

 

public static boolean isExisting(int x, int y) {

if (blackLocation != null) {

for (int i = 0; i < blackLocation.length; i++) {

if (blackLocation[i][0] == x && blackLocation[i][1] == y) {

return true;

}

}

}

return false;

}

 

public static boolean moveRight() {

// 白子右移

// 白子的x坐标+1,y坐标不变

while (blackLocation != null && whileLocation != null) {

tempLocation = whileLocation;

 

if ((tempLocation[0][0] + 1) < 0 || (tempLocation[0][0] + 1) > 9

|| isExisting(tempLocation[0][0] + 1, tempLocation[0][1])) {

return false;

} else {

tempLocation[0][0] = tempLocation[0][0] + 1;

tempLocation[0][1] = tempLocation[0][1];

whileLocation = tempLocation;

return true;

}

}

return false;

 

}

 

public static boolean moveLeft() {

// 白子左移

// 白子的x坐标-1,y坐标不变

while (blackLocation != null && whileLocation != null) {

tempLocation = whileLocation;

if ((tempLocation[0][0] - 1) < 0 || (tempLocation[0][0] - 1) > 9

|| isExisting(tempLocation[0][0] - 1, tempLocation[0][1])) {

return false;

} else {

tempLocation[0][0] = tempLocation[0][0] - 1;

tempLocation[0][1] = tempLocation[0][1];

whileLocation = tempLocation;

return true;

}

}

return false;

 

}

 

public static boolean moveUp() {

// 白子上移

// 白子的y坐标-1,x坐标不变

while (blackLocation != null && whileLocation != null) {

tempLocation = whileLocation;

if ((tempLocation[0][1] - 1) < 0 || (tempLocation[0][1] - 1) > 9

|| isExisting(tempLocation[0][1] - 1, tempLocation[0][0])) {

return false;

} else {

tempLocation[0][0] = tempLocation[0][0];

tempLocation[0][1] = tempLocation[0][1] - 1;

whileLocation = tempLocation;

return true;

}

}

return false;

 

}

 

public static boolean moveDown() {

// 白子下移

// 白子的y坐标+1,x坐标不变

while (blackLocation != null && whileLocation != null) {

tempLocation = whileLocation;

if ((tempLocation[0][1] + 1) < 0 || (tempLocation[0][1] + 1) > 9

|| isExisting(tempLocation[0][1] + 1, tempLocation[0][0])) {

return false;

} else {

tempLocation[0][0] = tempLocation[0][0];

tempLocation[0][1] = tempLocation[0][1] + 1;

whileLocation = tempLocation;

return true;

}

}

return false;

 

}

}

 

在控制台的显示信息如下:

第一步:运行代码

 

第二步:按照提示,对白子进行移动的执行情况

 

 

 

 

 

posted @ 2012-11-14 14:13  smile看风云  阅读(648)  评论(0编辑  收藏  举报