Java小实例——模拟五子棋游戏

import java.util.Scanner;

/**
 * 
 * @author Administrator 模拟五子棋游戏
 */
public class Wuziqi {

	private static String[][] chessboard;

	private static Scanner input = new Scanner(System.in);

	private static String player = "";

	private static String chess = " ";

	private static int flag = 0;
	
	public static void main(String[] args) {
		
		boolean result = false;
		init();
		while (true) {
			drawChessboard();
			if (result) {
				break;
			}
			result = playChess();

		}
	}

	/**
	 * 初始化操作
	 */
	public static void init() {

		String boardSize = "";
		String[] boardArray = boardSize.split(",");
		int y = 0;
		int x = 0;

		while (boardArray.length != 2 || y <= 0 || x <= 0) {
			System.out.print("请输入棋盘的大小(y,x):");
			boardSize = input.nextLine();
			boardArray = boardSize.split(",");
			try {
				y = Integer.parseInt(boardArray[0].trim());
				x = Integer.parseInt(boardArray[1].trim());
			} catch (Exception e) {

			}
		}
		y = Integer.parseInt(boardArray[0].trim());
		x = Integer.parseInt(boardArray[1].trim());

		chessboard = new String[y][x];
		for (int i = 0; i < chessboard.length; i++) {
			for (int j = 0; j < chessboard[0].length; j++) {
				chessboard[i][j] = " ";
			}
		}
	}

	/**
	 * 该函数用来模拟画棋盘
	 */
	public static void drawChessboard() {
		for (int i = 0; i < chessboard.length; i++) {
			System.out.print("Y" + (chessboard.length - i - 1) + "| ");
			for (int j = 0; j < chessboard[i].length; j++) {
				System.out.print(chessboard[i][j] + "  ");
			}
			System.out.println();
		}
		System.out.print("   ");

		for (int i = 0; i < chessboard[0].length; i++) {
			System.out.print("—— ");
		}
		System.out.println();
		System.out.print("   ");
		for (int i = 0; i < chessboard[0].length; i++) {
			System.out.print("X" + i + " ");
		}
		System.out.println();
	}

	/**
	 * 该函数用来模拟下棋操作
	 */
	public static boolean playChess() {

		if (flag == 0) {
			player = "黑方";
			flag = 1;
			chess = "●";
		} else {
			player = "白方";
			flag = 0;
			chess = "○";
		}

		String position = "";
		String[] boardArray = position.split(",");
		int y = 0;
		int x = 0;
		boolean bool = false;
		while (boardArray.length != 2 || bool == false) {
			System.out.print("请" + player + "选手输入你下棋的坐标位置(y,x):");
			position = input.nextLine();
			boardArray = position.split(",");
			try {
				y = Integer.parseInt(boardArray[0].trim());
				x = Integer.parseInt(boardArray[1].trim());
			} catch (Exception e) {

			}

			bool = checkPosition(y, x);
		}

		y = Integer.parseInt(boardArray[0].trim());
		x = Integer.parseInt(boardArray[1].trim());
		chessboard[chessboard.length - y - 1][x] = chess;
		boolean gameResult = gameRule(y,x,chess,player);
		
		return gameResult;
	}

	/**
	 * 判断棋子位置是否合法
	 */
	public static boolean checkPosition(int y, int x) {

		if (y < 0 || y >= chessboard.length || x < 0 || x >= chessboard[0].length) {
			System.out.print("棋子位置超出棋盘,落子无效,");
			return false;
		}
		if (!(chessboard[chessboard.length - y - 1][x].equals(" "))) {
			System.out.print("该处已有棋子,落子无效,");
			return false;
		}
		return true;
	}

	/**
	 * 五子棋游戏规则:同一种颜色的棋子,横着,竖着或者斜着先连成5子的一方就获胜
	 */
	public static boolean gameRule(int y, int x, String qizi,String p) {
		String temp = "";
		if (qizi.equals("●")) {
			temp = "●●●●●";
		} else {
			temp = "○○○○○";
		}
		StringBuilder str = new StringBuilder("");
		
		// 判断当前棋子位置是否可以与之前的棋子横着连成5子
		int imin = 0;
		int imax = chessboard[0].length - 1;
		if (x - 4 > 0) {
			imin = x - 4;
		}
		if (x + 4 < imax) {
			imax = x + 4;
		}
		for (int i = imin; i <= imax; i++) {
			str.append(chessboard[chessboard.length - y - 1][i]);
		}
		if (str.toString().indexOf(temp) != -1) {
			System.out.println("游戏结束,恭喜"+p+"赢得游戏!");
			return true;
		}

		// 判断当前棋子位置是否可以与之前的棋子竖着连成5子
		str = new StringBuilder("");
		int jmin = 0;
		int jmax = chessboard.length-1;
		if(chessboard.length - y - 1 - 4 > 0){
			jmin = chessboard.length - y - 1 - 4;
		}
		if(chessboard.length - y - 1 + 4 < jmax ){
			jmax = chessboard.length - y - 1 + 4;
		}
		for (int i = jmin; i <= jmax; i++) {
			str.append(chessboard[i][x]);
		}
		if (str.toString().indexOf(temp) != -1) {
			System.out.println("游戏结束,恭喜"+p+"赢得游戏!");
			return true;
		}
		
		// 判断当前棋子位置是否可以与之前的棋子左斜着连成5子
		str = new StringBuilder("");
		for(int j = jmin, i = imin ; j <= jmax && i <=imax ;j++,i++){
				System.out.println("a"+j+","+i);
				str.append(chessboard[j][i]);
		}
		System.out.println("A"+str.toString());
		if (str.toString().indexOf(temp) != -1) {
			System.out.println("游戏结束,恭喜"+p+"赢得游戏!");
			return true;
		}
		
		// 判断当前棋子位置是否可以与之前的棋子右斜着连成5子
		str = new StringBuilder("");
		for(int j = jmax , i = imin ; j >= jmin && i <=imax ;j--,i++){
			    System.out.println("b"+j+","+i);
				str.append(chessboard[j][i]);
		}
		System.out.println("B"+str.toString());
		if (str.toString().indexOf(temp) != -1) {
			System.out.println("游戏结束,恭喜"+p+"赢得游戏!");
			return true;
		}
		
		return false;
	}
}


Java小实例——模拟五子棋游戏_第1张图片Java小实例——模拟五子棋游戏_第2张图片

你可能感兴趣的:(【Java】)