JAVA学习--五子棋
来源:互联网 发布:asp连接数据库 教程 编辑:程序博客网 时间:2024/05/17 19:57
import java.util.Scanner;/** * 五子棋 * * @author * */public class Five_In_A_Row { // 15*15的棋盘 static String chessBoard[][] = new String[15][15]; // 输入棋子位置的横纵坐标 static int Line, Row; static Scanner input = new Scanner(System.in); public static void main(String[] args) { init();// 初始化棋盘并输出 play(); } // 棋盘初始化 public static void init() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { chessBoard[i][j] = "*"; } } printChessBoard(); } // 输出棋盘 public static void printChessBoard() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { System.out.print(chessBoard[i][j] + " "); } System.out.println(); } } // 下棋 public static void play() { boolean white = true; while (true) { if (white) { System.out.println("白方下棋子,请输入坐标(如第三行第二列:3 2):"); } else { System.out.println("黑方下棋子,请输入坐标(如第三行第二列:3 2):"); } Line = input.nextInt(); Row = input.nextInt(); // 判断输入的位置是否有效 if (Line <= 15 && Row <= 15 && Line >= 1 && Row >= 1 && chessBoard[Line - 1][Row - 1] == "*") { if (white) { chessBoard[Line - 1][Row - 1] = "白"; printChessBoard(); // 判断四个方位是否胜利 ifHorizonOver(chessBoard, Line, Row, white); ifVerticalOver(chessBoard, Line, Row, white); ifLeftOver(chessBoard, Line, Row, white); ifRightOver(chessBoard, Line, Row, white); white = false; continue; } else { chessBoard[Line - 1][Row - 1] = "黑"; printChessBoard(); // 判断四个方位是否胜利 ifHorizonOver(chessBoard, Line, Row, white); ifVerticalOver(chessBoard, Line, Row, white); ifLeftOver(chessBoard, Line, Row, white); ifRightOver(chessBoard, Line, Row, white); white = true; continue; } } System.out.println("请输入有效的位置!"); } } /** * 判断是否结束比赛 * * @param chessBoard * @param Line * @param Row */ /** * 判断横向是否结束 * * @param chessBoard * @param Line * @param Row * @param white */ public static void ifHorizonOver(String[][] chessBoard, int Line, int Row, boolean white) { if (Row == 1) { // 判断以它为首的横向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2] && chessBoard[Line - 1][Row + 2] == chessBoard[Line - 1][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 2) { // 判断以它为第二个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的横向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2] && chessBoard[Line - 1][Row + 2] == chessBoard[Line - 1][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 3) { // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的横向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2] && chessBoard[Line - 1][Row + 2] == chessBoard[Line - 1][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 4) { // 判断以它为第四个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的横向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2] && chessBoard[Line - 1][Row + 2] == chessBoard[Line - 1][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row >= 5 && Row <= 11) { // 判断以它为第五个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 5] == chessBoard[Line - 1][Row - 4] && chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的横向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2] && chessBoard[Line - 1][Row + 2] == chessBoard[Line - 1][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 12) { // 判断以它为第五个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 5] == chessBoard[Line - 1][Row - 4] && chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1] && chessBoard[Line - 1][Row + 1] == chessBoard[Line - 1][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 13) { // 判断以它为第五个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 5] == chessBoard[Line - 1][Row - 4] && chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row] && chessBoard[Line - 1][Row] == chessBoard[Line - 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 14) { // 判断以它为第五个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 5] == chessBoard[Line - 1][Row - 4] && chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line - 1][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Row == 15) { // 判断以它为第五个的横向连续五个棋子 if (chessBoard[Line - 1][Row - 5] == chessBoard[Line - 1][Row - 4] && chessBoard[Line - 1][Row - 4] == chessBoard[Line - 1][Row - 3] && chessBoard[Line - 1][Row - 3] == chessBoard[Line - 1][Row - 2] && chessBoard[Line - 1][Row - 2] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } } /** * 判断纵向是否结束 * * @param chessBoard * @param Line * @param Row * @param white */ public static void ifVerticalOver(String[][] chessBoard, int Line, int Row, boolean white) { if (Line == 1) { // 判断以它为首的纵向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1] && chessBoard[Line + 2][Row - 1] == chessBoard[Line + 3][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 2) { // 判断以它为第二个的纵向连续五个棋子 if (chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的纵向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1] && chessBoard[Line + 2][Row - 1] == chessBoard[Line + 3][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 3) { // 判断以它为第三个的纵向连续五个棋子 if (chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的纵向连续五个棋子 if (chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的纵向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1] && chessBoard[Line + 2][Row - 1] == chessBoard[Line + 3][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 4) { // 判断以它为第四个的纵向连续五个棋子 if (chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的纵向连续五个棋子 if (chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的纵向连续五个棋子 if (chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的纵向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1] && chessBoard[Line + 2][Row - 1] == chessBoard[Line + 3][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 5 && Line <= 11) { // 判断以它为第五个的纵向连续五个棋子 if (chessBoard[Line - 5][Row - 1] == chessBoard[Line - 4][Row - 1] && chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的纵向连续五个棋子 if (chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的横向连续五个棋子 if (chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的纵向连续五个棋子 if (chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为首的纵向连续五个棋子 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1] && chessBoard[Line + 2][Row - 1] == chessBoard[Line + 3][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 12) { // 判断以它为第五个的纵向连续五个棋子 if (chessBoard[Line - 5][Row - 1] == chessBoard[Line - 4][Row - 1] && chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的纵向连续五个棋子 if (chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的纵向连续五个棋子 if (chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第二个的纵向连续五个棋子 if (chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1] && chessBoard[Line + 1][Row - 1] == chessBoard[Line + 2][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 13) { // 判断以它为第五个的纵向连续五个棋子 if (chessBoard[Line - 5][Row - 1] == chessBoard[Line - 4][Row - 1] && chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的纵向连续五个棋子 if (chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第三个的纵向连续五个棋子 if (chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1] && chessBoard[Line][Row - 1] == chessBoard[Line + 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 14) { // 判断以它为第五个的纵向连续五个棋子 if (chessBoard[Line - 5][Row - 1] == chessBoard[Line - 4][Row - 1] && chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } // 判断以它为第四个的纵向连续五个棋子 if (chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line == 15) { // 判断以它为第五个的纵向连续五个棋子 if (chessBoard[Line - 5][Row - 1] == chessBoard[Line - 4][Row - 1] && chessBoard[Line - 4][Row - 1] == chessBoard[Line - 3][Row - 1] && chessBoard[Line - 3][Row - 1] == chessBoard[Line - 2][Row - 1] && chessBoard[Line - 2][Row - 1] == chessBoard[Line - 1][Row - 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } } /** * 判断左斜是否结束 * * @param chessBoard * @param Line * @param Row * @param white */ public static void ifLeftOver(String[][] chessBoard, int Line, int Row, boolean white) { if (Line >= 1 && Line <= 11 && Row >= 1 && Row <= 11) { // 判断作为左斜第一个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row] && chessBoard[Line][Row] == chessBoard[Line + 1][Row + 1] && chessBoard[Line + 1][Row + 1] == chessBoard[Line + 2][Row + 2] && chessBoard[Line + 2][Row + 2] == chessBoard[Line + 3][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 2 && Line <= 12 && Row >= 2 && Row <= 12) { // 判断作为左斜第二个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row - 2] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row] && chessBoard[Line][Row] == chessBoard[Line + 1][Row + 1] && chessBoard[Line + 1][Row + 1] == chessBoard[Line + 2][Row + 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 3 && Line <= 13 && Row >= 3 && Row <= 13) { // 判断作为左斜第三个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row - 2] && chessBoard[Line - 2][Row - 2] == chessBoard[Line - 3][Row - 3] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row] && chessBoard[Line][Row] == chessBoard[Line + 1][Row + 1]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 4 && Line <= 14 && Row >= 4 && Row <= 14) { // 判断作为左斜第四个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row - 2] && chessBoard[Line - 2][Row - 2] == chessBoard[Line - 3][Row - 3] && chessBoard[Line - 3][Row - 3] == chessBoard[Line - 4][Row - 4] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 5 && Line <= 15 && Row >= 5 && Row <= 15) { // 判断作为左斜第五个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row - 2] && chessBoard[Line - 2][Row - 2] == chessBoard[Line - 3][Row - 3] && chessBoard[Line - 3][Row - 3] == chessBoard[Line - 4][Row - 4] && chessBoard[Line - 4][Row - 4] == chessBoard[Line - 5][Row - 5]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } } /** * 判断右斜是否结束 * * @param chessBoard * @param Line * @param Row * @param white */ public static void ifRightOver(String[][] chessBoard, int Line, int Row, boolean white) { if (Line >= 1 && Line <= 11 && Row >= 5 && Row <= 15) { // 判断作为右斜第一个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 2] && chessBoard[Line][Row - 2] == chessBoard[Line + 1][Row - 3] && chessBoard[Line + 1][Row - 3] == chessBoard[Line + 2][Row - 4] && chessBoard[Line + 2][Row - 4] == chessBoard[Line + 3][Row - 5]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 2 && Line <= 12 && Row >= 4 && Row <= 14) { // 判断作为右斜第二个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 2] && chessBoard[Line][Row - 2] == chessBoard[Line + 1][Row - 3] && chessBoard[Line + 1][Row - 3] == chessBoard[Line + 2][Row - 4]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 3 && Line <= 13 && Row >= 3 && Row <= 13) { // 判断作为右斜第三个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row] && chessBoard[Line - 2][Row] == chessBoard[Line - 3][Row + 1] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 2] && chessBoard[Line][Row - 2] == chessBoard[Line + 1][Row - 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 4 && Line <= 14 && Row >= 2 && Row <= 12) { // 判断作为右斜第四个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row] && chessBoard[Line - 2][Row] == chessBoard[Line - 3][Row + 1] && chessBoard[Line - 3][Row + 1] == chessBoard[Line - 4][Row + 2] && chessBoard[Line - 1][Row - 1] == chessBoard[Line][Row - 2]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } if (Line >= 5 && Line <= 15 && Row >= 1 && Row <= 11) { // 判断作为右斜第五个点的连续五个棋子是否结束 if (chessBoard[Line - 1][Row - 1] == chessBoard[Line - 2][Row] && chessBoard[Line - 2][Row] == chessBoard[Line - 3][Row + 1] && chessBoard[Line - 3][Row + 1] == chessBoard[Line - 4][Row + 2] && chessBoard[Line - 4][Row + 2] == chessBoard[Line - 5][Row + 3]) { String winner = (white == true ? "白方" : "黑方"); System.out.println(winner + "胜利!"); System.exit(0); } } }}
0 0
- JAVA学习--五子棋
- Java控制台五子棋编码学习
- Java入门自绘学习,五子棋
- java学习(4) 五子棋 小程序
- 学习五子棋
- java五子棋
- JAVA五子棋
- java 五子棋
- Java五子棋
- java 五子棋
- java 五子棋
- 五子棋JAVA
- Java五子棋
- Java五子棋
- java五子棋
- java 五子棋
- java 五子棋
- 慕课上学习的五子棋
- leetcode:二叉树之Construct Binary Tree from Inorder and Postorder Traversal
- POJ 3074 DLX精确覆盖求解数独问题
- linux下C语言编程操作MySQL数据库
- chmod 777 修改权限
- Laravel 5.1 事件
- JAVA学习--五子棋
- 开启线程池 每秒联网
- 设计模式6大原则
- [绍棠] iOS 9 NetworkExtension使用
- JAVA学习代码——了解java.io
- xcodebuild & xcrun & xctool 脚本打包
- 初涉项目管理时遇到的问题及解决思路
- 详解Java GC的工作原理+Minor GC、FullGC
- Dubbo分布式服务框架常见问题解答汇总