Java實現模擬五子棋遊戲
阿新 • • 發佈:2019-01-08
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; } }