用java实现五子棋

package com.gobang;

import java.util.Scanner;

public class Gobang {
	// 要表示棋盘,需要一个二维数组,15行,15列
	private static char[][] chressBoard;
	private static char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	private static Scanner sc = new Scanner(System.in);
	// 初始化棋盘 给二维数组设置初始元素.
	public static void initChreeBoard() {
		chressBoard = new char[15][15];
		for (int i = 0; i < chressBoard.length; i++) {
			for (int j = 0; j < chressBoard[i].length; j++) {
				if (i == 0) {
					if (j == 0) {
						chressBoard[i][j] = '┌';
					} else if (j == 14) {
						chressBoard[i][j] = '┐';
					} else {
						chressBoard[i][j] = '┬';
					}

				} else if (i == 14) {
					if (j == 0) {
						chressBoard[i][j] = '└';
					} else if (j == 14) {
						chressBoard[i][j] = '┘';
					} else {
						chressBoard[i][j] = '┴';
					}
				} else {
					if (j == 0) {
						chressBoard[i][j] = '├';
					} else if (j == 14) {
						chressBoard[i][j] = '┤';
					} else {
						chressBoard[i][j] = '┼';
					}
				}

			}
		}

	}

	// 打印棋盘
	public static void printChressBoard() {
		System.out.print("  ");
		for (int i = 0; i < numbers.length; i++) {
			System.out.print(numbers[i] + " ");
		}
		System.out.println();

		for (int i = 0; i < chressBoard.length; i++) {
			System.out.print(numbers[i] + " ");
			for (int j = 0; j < chressBoard[i].length; j++) {
				if (j == 14) {
					System.out.print(chressBoard[i][j]);
				} else {
					System.out.print(chressBoard[i][j] + "─");
				}
			}
			System.out.println();
		}
	}

	// 判断字符是不是 1~9 A~F
	public static boolean isCharOk(char c) {
		if ((c >= '1' && c <= '9') || (c >= 'A' && c <= 'F')) {
			return true;
		} else {
			return false;
		}
	}

	// 获取落子的位置
	public static char[] getLocation() {
		
		String input = sc.nextLine();
		char c1 = 0;
		char c2 = 0;
		char c3 = 0;
		while (true) {
			if (input.length() == 3) {
				c1 = input.charAt(0);
				c2 = input.charAt(1);
				c3 = input.charAt(2);
				if (isCharOk(c1) && isCharOk(c3) && c2 == ',') {
					break;
				}
			}
			System.out.print("输入有误,请重新输入:");
			input = sc.nextLine();
		}
		char[] location = { c1, c3 };
		return location;
	}

	// 把字符转成 数组下标
	public static int charToInt(char c) {
		int num = 0;
		if (c >= '1' && c <= '9') {
			num = c - '1';
		} else if (c >= 'A' && c <= 'F') {
			num = c - 'A' + 9;
		}
		return num;
	}

	// 是否可以落子
	public static boolean canPutDown(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		if (chressBoard[rowIndex][columnIndex] == '●' || chressBoard[rowIndex][columnIndex] == '○') {
			return false;
		} else {
			return true;
		}
	}

	// 落子
	public static void putDown(char[] location, char c) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		chressBoard[rowIndex][columnIndex] = c;
	}

	// 看当前坐标左边有几个一样的棋子
	public static int leftSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int j = columnIndex - 1;
		while (j >= 0) {
			if (chressBoard[rowIndex][j] == c) {
				count++;
				j--;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标右边有几个一样的棋子
	public static int rightSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int j = columnIndex + 1;
		while (j <= 14) {
			if (chressBoard[rowIndex][j] == c) {
				count++;
				j++;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标上边有几个一样的棋子
	public static int upSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex - 1;
		while (i >= 0) {
			if (chressBoard[i][columnIndex] == c) {
				count++;
				i--;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标下边有几个一样的棋子
	public static int downSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex + 1;
		while (i <= 14) {
			if (chressBoard[i][columnIndex] == c) {
				count++;
				i++;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标左下边有几个一样的棋子
	public static int leftDownSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex + 1;
		int j = columnIndex - 1;
		while (i <= 14 && j >= 0) {
			if (chressBoard[i][j] == c) {
				count++;
				i++;
				j--;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标右上边有几个一样的棋子
	public static int rightUpSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex - 1;
		int j = columnIndex + 1;
		while (i >= 0 && j <= 14) {
			if (chressBoard[i][j] == c) {
				count++;
				i--;
				j++;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标左上边有几个一样的棋子
	public static int leftUpSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex - 1;
		int j = columnIndex - 1;
		while (i >= 0 && j >= 0) {
			if (chressBoard[i][j] == c) {
				count++;
				i--;
				j--;
			} else {
				break;
			}
		}
		return count;
	}

	// 看当前坐标右下边有几个一样的棋子
	public static int rightDownSameCount(char[] location) {
		char c1 = location[0];// 拿到行号 可能1~9 A~F
		char c2 = location[1];// 拿到列号 可能1~9 A~F
		int rowIndex = charToInt(c1);
		int columnIndex = charToInt(c2);
		char c = chressBoard[rowIndex][columnIndex];

		int count = 0;
		int i = rowIndex + 1;
		int j = columnIndex + 1;
		while (i <= 14 && j <= 14) {
			if (chressBoard[i][j] == c) {
				count++;
				i++;
				j++;
			} else {
				break;
			}
		}
		return count;
	}

	// 判断输赢的方法.参数是你下的子的坐标,和子的种类
	// 返回值是赢(true)还是没赢
	public static boolean isWin(char[] location) {
		// 当前下的子算1个子
		int count = 1;
		// 横向向左查看子的个数.
		count += leftSameCount(location);
		// 横向向右查看子的个数
		count += rightSameCount(location);
		// 如果横向大于等于5,说明赢了
		if (count >= 5) {
			return true;
		}

		// 纵向
		count = 1;
		// 纵向向上找有几个一样的子
		count += upSameCount(location);
		// 纵向向下找有几个一样的子
		count += downSameCount(location);
		if (count >= 5) {
			return true;
		}

		// 斜着(左下-右上)
		count = 1;
		count += leftDownSameCount(location);
		count += rightUpSameCount(location);

		if (count >= 5) {
			return true;
		}

		// 斜着(左上-右下)
		count = 1;
		count += leftUpSameCount(location);
		count += rightDownSameCount(location);

		if (count >= 5) {
			return true;
		}

		return false;
	}

	public static void main(String[] args) {
		// 初始化棋盘.把棋盘上的所有点置空.
		initChreeBoard();
		// 打印棋盘
		printChressBoard();
		while (true) {
			// 黑子下棋
			System.out.print("请黑方下棋(输入棋子坐标,例如:7,A):");
			char[] loc = getLocation();
			boolean canPutDown = canPutDown(loc);
			while (canPutDown == false) {
				System.out.print("此处已经有棋子,请重新输入棋子坐标:");
				loc = getLocation();
				canPutDown = canPutDown(loc);
			}
			putDown(loc, '●');
			// 打印棋盘
			printChressBoard();
			// 判断输赢
			if (isWin(loc)) {
				System.out.println("黑方获胜!");
				break;
			}
			System.out.print("请白方下棋(输入棋子坐标,例如:7,A):");
			char[] loc2 = getLocation();
			boolean canPutDown2 = canPutDown(loc2);
			while (canPutDown2 == false) {
				System.out.print("此处已经有棋子,请重新输入棋子坐标:");
				loc2 = getLocation();
				canPutDown2 = canPutDown(loc2);
			}
			putDown(loc2, '○');
			// 打印棋盘
			printChressBoard();
			// 判断输赢
			if (isWin(loc2)) {
				System.out.println("白方获胜!");
				break;
			}
		}

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值