欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java编程实现五子棋人人对战代码示例

程序员文章站 2023-12-13 09:49:40
利用java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋 import java.util.scanner;...

利用java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋

Java编程实现五子棋人人对战代码示例

import java.util.scanner;

public class mainmethod {

	public static char[][] c = new char[10][10];

	public static void main(string[] args) {
		mainmethod mainmethod = new mainmethod();
		mainmethod.init();
		int i;
		boolean color;
		int[] point = new int[2];
		scanner scanner = new scanner(system.in);

		for (i = 1; i < 100; i++) {
			mainmethod.draw();// 绘制棋盘
			system.out.println("请输入坐标,范围为(0,0)到(9,9)");
			int x = scanner.nextint();
			int y = scanner.nextint();
			if (i % 2 == 1)
				color = true;
			else
				color = false;
			point[0] = x;
			point[1] = y;
			if (mainmethod.paint(point, color) == false) {//
				system.out.println("下的位置不合理,请重新开始");
				break;
			}
			if (mainmethod.win() == 1) {
				mainmethod.draw();
				system.out.println("恭喜白棋赢!!");
				break;
			} else if (mainmethod.win() == -1) {
				mainmethod.draw();
				system.out.println("恭喜黑棋赢!!");
				break;
			}

		}

		if (i == 100)
			system.out.println("平局");

	}

	public char[][] init() {// 初始化棋盘
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c.length; j++) {
				c[i][j] = '+';
			}
		}
		return c;
	}

	public boolean paint(int[] point, boolean colr) {// 判断下的位置是否正确,若正确改变棋盘
		boolean b = true;
		int x = point[0];
		int y = point[1];

		if (inboard(point) == false || c[x][y] == 'w' || c[x][y] == 'b' || x < 0) {
			b = false;
			return b;
		}

		else if (colr == true) {// 白棋下
			if (c[x][y] == '+') {
				c[x][y] = 'w';
			}
		}

		else if (colr == false) {// 黑棋下
			if (c[x][y] == '+') {
				c[x][y] = 'b';
			}
		}
		return b;
	}

	public boolean inboard(int[] point) {// 判断point是否在棋盘上
		int x = point[0];
		int y = point[1];
		if (x < 0 || y < 0 || x >= c.length || y >= c.length)
			return false;
		else
			return true;

	}

	public int win() {// 判断输赢,1表示白的赢,-1黑的赢,0暂无输赢
		int whowin = 0;

		int[] point = new int[2];
		outer: while (true) {
			for (int i = 0; i < c.length; i++) {
				for (int j = 0; j < c.length; j++) {

					if (c[i][j] == 'w') {// 遍历数组,找到的第一个白棋
						point[0] = i;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向右数4个的棋盘
							if (c[i][j + 1] == 'w' && c[i][j + 2] == 'w' && c[i][j + 3] == 'w' && c[i][j + 4] == 'w') {
								whowin = 1;
								break outer;
							}
						}

						point[0] = i + 4;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向右,向下数4个的棋盘
							if (c[i + 1][j + 1] == 'w' && c[i + 2][j + 2] == 'w' && c[i + 3][j + 3] == 'w'
									&& c[i + 4][j + 4] == 'w') {
								whowin = 1;
								break outer;
							}
						}

						point[0] = i + 4;
						point[1] = j;

						if (inboard(point)) {// 如果存在向下数4个的棋盘
							if (c[i + 1][j] == 'w' && c[i + 2][j] == 'w' && c[i + 3][j] == 'w' && c[i + 4][j] == 'w') {
								whowin = 1;
								break outer;
							}
						}

						point[0] = i - 4;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向左,向下数4个的棋盘
							if (c[i - 1][j + 1] == 'w' && c[i - 2][j + 2] == 'w' && c[i - 3][j + 3] == 'w'
									&& c[i - 4][j + 4] == 'w') {
								whowin = 1;
								break outer;
							}
						}

					} else if (c[i][j] == 'b') {
						point[0] = i;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向右数4个的棋盘
							if (c[i][j + 1] == 'b' && c[i][j + 2] == 'b' && c[i][j + 3] == 'b' && c[i][j + 4] == 'b') {
								whowin = -1;
								break outer;
							}
						}

						point[0] = i + 4;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向右,向下数4个的棋盘
							if (c[i + 1][j + 1] == 'b' && c[i + 2][j + 2] == 'b' && c[i + 3][j + 3] == 'b'
									&& c[i + 4][j + 4] == 'b') {
								whowin = -1;
								break outer;
							}
						}

						point[0] = i + 4;
						point[1] = j;

						if (inboard(point)) {// 如果存在向下数4个的棋盘
							if (c[i + 1][j] == 'b' && c[i + 2][j] == 'b' && c[i + 3][j] == 'b' && c[i + 4][j] == 'b') {
								whowin = -1;
								break outer;
							}
						}

						point[0] = i - 4;
						point[1] = j + 4;

						if (inboard(point)) {// 如果存在向左,向下数4个的棋盘
							if (c[i - 1][j + 1] == 'b' && c[i - 2][j + 2] == 'b' && c[i - 3][j + 3] == 'b'
									&& c[i - 4][j + 4] == 'b') {
								whowin = -1;
								break outer;
							}
						}

					}

				}
			}
			whowin = 0;
			break outer;

		}
		return whowin;
	}

	public void draw() {// 绘制棋盘
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c.length; j++) {
				system.out.print(c[i][j] + " ");
			}
			system.out.println();
		}
	}

}

运行结果:

+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9)
4
6
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + w + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9)
6
5
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + w + + + 
+ + + + + + + + + + 
+ + + + + b + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
+ + + + + + + + + + 
请输入坐标,范围为(0,0)到(9,9)

谁输谁赢我就不弄出来了,太长,占地。。

总结

以上就是本文关于java编程实现五子棋人人对战代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:

下一篇: