
1.棋盤類
/** * 棋盤 * */public class Chessboard { // 定義一個二維數組來充當棋盤 PRivate String[][] board; // 定義棋盤的大小 public final int BOARD_SIZE = 15; /** * 初始化棋盤 * * @return void */ public void initBoard() { board = new String[BOARD_SIZE][BOARD_SIZE]; // 把每個元素賦值為“十”,用于控制臺輸出棋盤 for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "十"; } } } /** * 在控制臺輸出棋盤 */ public void printBoard() { // 打印每個數組元素 for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j]);// 打印后不換行 } System.out.print("/n");// 每打印完一行數組元素就換行一次 } } /** * 給棋盤位置賦值 * * @param posX * X坐標 * @param posY * Y坐標 * @param chessman * 棋子 */ public void setBoard(int posX, int posY, String chessman) { board[posX][posY] = chessman; } /** * 返回棋盤 * * @return 返回棋盤 */ public String[][] getBoard() { return board; } public int getBoardSize() { return BOARD_SIZE; }}2.棋手類----你和電腦public class ChessMan { static final String blackChessman="●"; static final String whiteChessman="○"; /** * @return String 黑棋 */ public static String getBlackChessman() { return blackChessman; } /** * @return String 白棋 */ public static String getWhiteChessman() { return whiteChessman; }}3.五子棋的具體實現類import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Random;import java.util.Scanner;/** * 五子棋游戲類 * */public class GobangGame { private final int WIN_COUNT = 5;// 定義達到贏條件的棋子數目 private int posX = 0;// 定義用戶輸入的X坐標 private int posY = 0;// 定義用戶輸入的X坐標 private Chessboard chessboard;// 定義棋盤 /** * 構造器,初始化棋盤和棋子屬性 * * @param chessboard * 棋盤類 */ public GobangGame(Chessboard chessboard) { this.chessboard = chessboard; } /** * 檢查輸入是否合法。 * * @param inputStr * 由控制臺輸入的字符串。 * @return 字符串合法返回true,反則返回false。 */ public boolean isValid(String inputStr) { // 將用戶輸入的字符串以逗號(,)作為分隔,分隔成兩個字符串 String[] posStrArr = inputStr.split(","); try { posX = Integer.parseInt(posStrArr[0]) - 1;//用戶棋子的X坐標 posY = Integer.parseInt(posStrArr[1]) - 1;//用戶棋子的Y坐標 } catch (NumberFormatException e) { chessboard.printBoard(); System.out.println("請以(數字,數字)的格式輸入:"); return false; } // 檢查輸入數值是否在范圍之內 if (posX < 0 || posX >= chessboard.getBoardSize() || posY < 0 || posY >= chessboard.getBoardSize() ) { chessboard.printBoard(); System.out.println("X與Y坐標只能大于等于1,與小于等于" + chessboard.getBoardSize() + ",請重新輸入:"); return false; } // 檢查輸入的位置是否已經有棋子 String[][] board = chessboard.getBoard(); if (board[posX][posY] != "十") { chessboard.printBoard(); System.out.println("此位置已經有棋子,請重新輸入:"); return false; } return true; } /** * 開始下棋 */ public void start() throws Exception { chessboard.initBoard(); chessboard.printBoard(); Scanner scan = new Scanner(System.in); boolean isOver = false;// true為游戲結束 while(!isOver){ System.out.print("請輸入棋子的位置:"); String inputStr = scan.nextLine(); //isOver = false; if (!isValid(inputStr)) { continue;// 如果不合法,要求重新輸入,再繼續 } String chessman =ChessMan.getBlackChessman(); chessboard.setBoard(posX, posY,chessman); // 判斷用戶是否贏了 if (isWon(posX, posY,chessman)) { isOver = true; } else { // 計算機隨機選擇位置坐標 int[] computerPosArr = computerDo(); chessman = ChessMan.getWhiteChessman(); chessboard.setBoard(computerPosArr[0], computerPosArr[1],chessman); // 判斷計算機是否贏了 if (isWon(computerPosArr[0], computerPosArr[1], chessman)) { isOver = true; } } // 如果產生勝者,詢問用戶是否繼續游戲 if (isOver) { // 如果繼續,重新初始化棋盤,繼續游戲 if (isReplay(chessman)) { isOver = false; chessboard.initBoard(); chessboard.printBoard(); continue; } // 如果不繼續,退出程序 break; } chessboard.printBoard(); } } /** * 是否重新開始下棋。 * * @param chessman * "●"為用戶,"○"為計算機。 * @return 開始返回true,反則返回false。 */ public boolean isReplay(String chessman) throws Exception { chessboard.printBoard(); String playerWin = "恭喜您,您贏了,"; String computerWin = "很遺憾,您輸了,"; String message = chessman.equals(ChessMan.getBlackChessman()) ? playerWin:computerWin ; System.out.println(message + "再下一局?(y/n)"); Scanner scan = new Scanner(System.in); if (scan.next().equals("y")) { // 開始新一局 return true; } return false; } /** * 計算機隨機下棋 */ public int[] computerDo() { Random random = new Random(); int posX = random.nextInt(chessboard.getBoardSize()-1); int posY = random.nextInt(chessboard.getBoardSize()-1); String[][] board = chessboard.getBoard(); while (board[posX][posY] != "十" && board[posX][posY]!=ChessMan.getBlackChessman()&& board[posX][posY]!=ChessMan.getWhiteChessman()) { posX = random.nextInt(chessboard.getBoardSize()-1); posY = random.nextInt(chessboard.getBoardSize()-1); } int[] result = { posX, posY }; return result; } /** * 判斷輸贏 * * @param posX * 棋子的X坐標。 * @param posY * 棋子的Y坐標 * @param ico * 棋子類型 * @return 如果有五顆相鄰棋子連成一條直接,返回真,否則相反。 */ public boolean isWon(int posX, int posY, String ico) { int startX = 0;// 直線起點的X最小坐標 int startY = 0;// 直線起點Y最小坐標 int endX = chessboard.getBoardSize()-1;// 直線結束X最大坐標 int endY = chessboard.getBoardSize()-1;// 直線結束Y最大坐標 int sameCount = 0;// 同條直線上相鄰棋子累積數 int temp = 0; // 計算起點的最小X坐標與Y坐標 temp = posX - WIN_COUNT + 1; startX = (temp<0)? 0:temp; temp = posY - WIN_COUNT + 1; startY = (temp<0)?0:temp; // 計算終點的最大X坐標與Y坐標 temp = posX + WIN_COUNT - 1; endX = temp > (chessboard.getBoardSize()-1)?(chessboard.getBoardSize()-1):temp; temp = posY + WIN_COUNT - 1; endY = temp >(chessboard.getBoardSize()-1)?(chessboard.getBoardSize()-1):temp; // 計算一行中相同棋子的數目 String[][] board = chessboard.getBoard(); for (int i = startY; i < endY; i++) { if (board[posX][i] == ico && board[posX][i + 1] == ico) { sameCount++; } else if (sameCount < WIN_COUNT - 1) { sameCount = 0; } } // 計算一行中相同棋子的數目 if (sameCount == 0) { for (int i = startX; i < endX; i++) { if (board[i][posY] == ico && board[i + 1][posY] == ico) { sameCount++; } else if (sameCount < WIN_COUNT - 1) { sameCount = 0; } } } // 從左上到右下計算相同棋子的數目 if (sameCount == 0) { int j = startY; for (int i = startX; i < endX; i++) { if (j < endY) { if (board[i][j] == ico && board[i + 1][j + 1] == ico) { sameCount++; } else if (sameCount < WIN_COUNT - 1) { sameCount = 0; } j++; } } } if (sameCount == 0) { // 從右上到左下計算相同棋子的數目 int j = startX; for (int i = endY; i > startY; i--) { if (j < endY) { if (board[i][j] == ico && board[i-1][j+1] == ico) { sameCount++; } else if (sameCount < WIN_COUNT - 1) { sameCount = 0; } j++; } } } return sameCount >= (WIN_COUNT-1)?true:false; }}4.控制類import java.util.Scanner;public class Main { public static void main(String[] args) throws Exception { GobangGame gb = new GobangGame(new Chessboard()); gb.start(); }}源代碼
新聞熱點
疑難解答