Java实例(一)五子棋

因为我刚开始系统性地学习java,所以程序写的很简陋,这实例也是在培训时由老师教导着写的,当然有部分功能是我自己写的,写的很初级,勉强够看。


这个博客我个人只是拿来用作记录我学习java的过程,所以基本上是不回去看评论的。
 

以下是源代码:

import java.util.Scanner;

public class wuziqi {
	public static void main(String[] args) {

		char[] bianhao = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		char[][] chess = new char[15][15];
		int sum = 0, count = 0;
		int i = 0, j = 0;
		for (i = 0; i < chess.length; i++) {
			for (j = 0; j < chess[i].length; j++) {
				chess[i][j] = '+';
			}
		}
		chess[3][3] = chess[3][7] = chess[3][11] = '#';
		chess[7][3] = chess[7][7] = chess[7][11] = '#';
		chess[11][3] = chess[11][7] = chess[11][11] = '#';

		// qipan[3][4]=qipan[3][5]=qipan[3][6]=qipan[3][7]=qipan[3][8]='0';
		while (true) {
			System.out.print("  ");
			for (int k = 0; k < bianhao.length; k++) {
				System.out.print(bianhao[k] + " ");
			}
			System.out.println();
			for (i = 0; i < chess.length; i++) {
				System.out.print(bianhao[i] + " ");
				for (j = 0; j < chess[i].length; j++) {
					System.out.print(chess[i][j] + " ");
				}
				System.out.println();
			}

			Scanner scanner = new Scanner(System.in);// 创建屏幕输入对象
			System.out.print("请输入Y坐标:");
			int y = scanner.nextInt();
			if (y > 14 || y < 0) {
				System.out.println("请输入正确的Y坐标(0~14)");
				scanner.nextLine();
				scanner.nextLine();
				continue;
			}
			System.out.print("请输入X坐标:");
			int x = scanner.nextInt();
			if (y > 14 || y < 0) {
				System.out.println("请输入正确的X坐标(0~14)");
				scanner.nextLine();
				scanner.nextLine();
				continue;
			}
			if (chess[y][x] == '@' || chess[y][x] == '0') {
				System.out.println("该坐标已经有子,请重新下子!");
				scanner.nextLine();
				scanner.nextLine();
				continue;
			}
			// 棋子颜色不用判断
			count++;
			if (count % 2 == 1) {
				chess[y][x] = '@';
			} else {
				chess[y][x] = 'O';
			} // count判断下棋顺序
			int x1 = x;
			int y1 = y;
			while (true) {
				x1++;
				if (x1 > 14) {
					break;
				}
				char value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			x1 = x;
			y1 = y;
			while (true) {
				x1--;
				if (x1 < 0) {
					break;
				}
				int value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			if (sum >= 4) {
				System.out.println("该方胜利!");
				break;
			}

			sum = 0;
			x1 = x;
			y1 = y;

			while (true) {
				y1++;
				if (y1 > 14) {
					break;
				}
				char value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			x1 = x;
			y1 = y;
			while (true) {
				y1--;
				if (y1 < 0) {
					break;
				}
				int value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			if (sum >= 4) {
				System.out.println("该方胜利!");
				break;
			}

			sum = 0;
			x1 = x;
			y1 = y;

			while (true) {
				y1++;
				x1--;
				if (y1 > 14 || x1 < 0) {
					break;
				}
				char value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			x1 = x;
			y1 = y;
			while (true) {
				y1--;
				x1++;
				if (y1 < 0 || x1 > 14) {
					break;
				}
				int value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			if (sum >= 4) {
				System.out.println("该方胜利!");
				break;
			}

			sum = 0;
			x1 = x;
			y1 = y;

			while (true) {
				y1++;
				x1++;
				if (y1 > 14 || x1 > 14) {
					break;
				}
				char value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}
			x1 = x;
			y1 = y;
			while (true) {
				y1--;
				x1--;
				if (y1 < 0 || x1 < 0) {
					break;
				}
				int value = chess[y1][x1];
				if (value == chess[y][x]) {
					sum++;
				} else {
					break;
				}
			}

			if (sum >= 4) {
				System.out.println("该方胜利!");
				break;
			}
		}
	}
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值