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

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

创新互联主营涧西网站建设的网络公司,主营网站建设方案,重庆App定制开发,涧西h5小程序制作搭建,涧西网站营销推广欢迎涧西等地区企业咨询

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


新闻名称:Java编程实现五子棋人人对战代码示例
网站URL:http://myzitong.com/article/pipieg.html