大神们 急求基于eclipse的java小游戏程序的源码,程序不要多复杂啊。像坦克大战,五子棋,扫雷之类的谢谢

大神们 急求基于eclipse的java小游戏程序的源码,程序不要多复杂啊。像坦克大战,五子棋,扫雷之类的谢谢,第1张


import javautilScanner;

public class Wuziqi {

/

棋盘

/

private final int[][] qipan;

/

步数

/

private int bushu;

/

构造方法,设置棋盘规格

@param x

@param y

/

public Wuziqi(int x, int y) {

if (x < 1 || y < 1) {

Systemoutprintln("棋盘规格应不小于1,使用默认规格");

qipan = new int[9][9];

} else {

qipan = new int[y][x];

}

}

/

游戏开始

/

public void play() {

int[] zuobiao = null;

//如果游戏没有结束

while (!end(zuobiao)) {

//落子,并取得坐标

zuobiao = luozi();

//输出棋盘

out();

}

}

/

输出棋盘和棋子

/

private void out() {

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

for (int j = 0; j < qipan[i]length; j++) {

if (qipan[i][j] == 0) {

Systemoutprint("  +");

}else if (qipan[i][j] == -1) {

Systemoutprint("  白");

}else if (qipan[i][j] == 1) {

Systemoutprint("  黑");

}

}

Systemoutprintln(" ");

}

}

/

落子

/

private int[] luozi() {

int[] zuobiao;

bushu++;

if (bushu % 2 == 1) {

Systemoutprintln("请黑方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = 1;

}else {

Systemoutprintln("请白方落子");

zuobiao = input();

qipan[zuobiao[1]][zuobiao[0]] = -1;

}

return zuobiao;

}

/

输入坐标

@return

/

private int[] input() {

Scanner sc = new Scanner(Systemin);

Systemoutprintln("请输入x轴坐标");

String x = scnext();

Systemoutprintln("请输入y轴坐标");

String y = scnext();

//如果没有通过验证,则再次执行input(),递归算法

if (!validate(x, y)) {

return input();

}

int int_x = IntegervalueOf(x);

int int_y = IntegervalueOf(y);

return new int[] {int_x, int_y};

}

/

校验数据

@param x

@param y

@return

/

private boolean validate(String x, String y) {

Integer int_x = null;

Integer int_y = null;

//异常处理的方式判断字符串是否是一个整数

try {

int_x = IntegervalueOf(x);

int_y = IntegervalueOf(y);

} catch (NumberFormatException e) {

Systemoutprintln("坐标格式错误,坐标应为整数");

return false;

}

if (int_x < 0 || int_y < 0 || int_x >= qipan[0]length || int_y >= qipanlength) {

Systemoutprintln("坐标越界");

return false;

}

if (qipan[int_y][int_x] == 0) {

return true;

} else {

Systemoutprintln("坐标上已有棋子");

}

return false;

};

/

结束条件

@return

/

private boolean end(int[] zuobiao) {

if (zuobiao == null) {

return false;

}

//计数器

//表示棋盘上经过最近落子坐标的4条线上的连续(和最近落子颜色相同的)棋子的个数

//如果某条线上连续的棋子大于等于4(加上最近落子本身,大于等于5),则游戏结束,符合五子棋规则

int[] jieguo = new int[4];

int x = zuobiao[0];

int y = zuobiao[1];

//定义八个方向

final int[][] fangxiang = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};

//最近落子的坐标上的棋子颜色

int number = qipan[y][x];

//搜索最近落子坐标为中心最远4的距离

for (int i = 1; i <= 4; i++) {

//每次搜索不同的距离都搜索八个方向

for (int j = 0; j < fangxianglength; j++) {

//约定如果某个方向为null时,不再搜索这个方向。关键字continue是跳过本次(一次)循环的意思

if (fangxiang[j] == null) {

continue;

}

int mubiao_x = x + i fangxiang[j][0];

int mubiao_y = y + i fangxiang[j][1];

//如果搜索坐标相对于棋盘越界,则不再搜索这个方向

if (mubiao_y >= qipanlength || mubiao_y < 0 || mubiao_x >= qipan[0]length || mubiao_x < 0) {

fangxiang[j] = null;

continue;

}

//如果最近落子坐标上的值等于目标坐标上的值(颜色相同),则计数器上某条线加1

//否则认为这个方向没有棋子或有别的颜色的棋子,不再搜索这个方向

if (number == qipan[mubiao_y][mubiao_x]) {

jieguo[j % 4]++;

}else {

fangxiang[j] = null;

}

}

}

//查看计数器上是否有比3更大的数(查看是否有一方胜出)

for (int i : jieguo) {

if (i > 3) {

Systemoutprintln("游戏结束");

if (bushu % 2 == 1) {

Systemoutprintln("黑方胜");

} else {

Systemoutprintln("白方胜");

}

return true;

}

}

//没有胜出者的情况下,查看棋盘上是否还有空位置,如果有,则游戏可以继续

for (int[] arr : qipan) {

for (int i : arr) {

if (i == 0) {

return false;

}

}

}

//如果没有空位置,则平局

Systemoutprintln("游戏结束,平局");

return true;

}

}

以上就是关于大神们 急求基于eclipse的java小游戏程序的源码,程序不要多复杂啊。像坦克大战,五子棋,扫雷之类的谢谢全部的内容,包括:大神们 急求基于eclipse的java小游戏程序的源码,程序不要多复杂啊。像坦克大战,五子棋,扫雷之类的谢谢、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9480600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存