日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1.23 实例:五子棋游戏

發布時間:2025/3/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1.23 实例:五子棋游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Scanner; public class Gobang {// 定義棋盤的大小public static int BOARD_SIZE = 15;// 定義一個二維數組來充當棋盤public static String[][] board = new String[BOARD_SIZE][BOARD_SIZE];String black = "●"; // 黑子String white = "○"; // 白子/*** 初始化械盤數組*/public void initBoard() {// 把每個元素賦為"╋",用于在控制臺畫出棋盤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");}}/*** 判斷輸贏的方法*/public static boolean isWin(int x, int y, String color) {if (color.equals("black")) {color = "●";}if (color.equals("white")) {color = "○";}// 橫向for (int i = 0; i < board.length - 5; i++) {if (board[x][i].equals(color) && board[x][i + 1].equals(color) && board[x][i + 2].equals(color)&& board[x][i + 3].equals(color) && board[x][i + 4].equals(color)) {return true;}}// 豎向for (int i = 0; i < board.length - 5; i++) {if (board[i][y].equals(color) && board[i + 1][y].equals(color) && board[i + 2][y].equals(color)&& board[i + 3][y].equals(color) && board[i + 4][y].equals(color)) {return true;}}return false;}/*** 判斷指定坐標是否有棋子*/public static boolean isOk(int x, int y) {if (!board[x - 1][y - 1].equals("╋")) {return false;}return true;}/*** 電腦下棋*/public static String computer(String color) {int x = (int) (Math.random() * 14) + 1; // 生成一個1~14之間的隨機數int y = (int) (Math.random() * 14) + 1; // 生成一個1~14之間的隨機數// 判斷電腦下棋的坐標有無棋子,有棋子則在重新生成隨機數if (isOk(x, y)) {if (color.equals("black")) {board[x][y] = "●";} else if (color.equals("white")) {board[x][y] = "○";}} else {computer(color);}return "x,y";}public static void main(String[] args) throws Exception {Gobang gb = new Gobang();gb.initBoard();gb.printBoard();// 這是用于獲取鍵盤輸入的方法Scanner input = new Scanner(System.in); // 使用Scanner類獲取用戶輸入System.out.println("您想要選擇什么顏色的棋,black或white,請輸入:");String peopleColor = input.next(); // 定義用戶選擇棋子的顏色并返回用戶輸入的字符串// 如果用戶選擇的是白棋,則電腦先下(五子棋中黑棋先下)if (peopleColor.equals("white")) {System.out.println("您選擇的是白棋");computer("black");gb.printBoard();}String inputStr;do {System.out.println("輸入您下棋的坐標,應以x,y的格式:");inputStr = input.next();// 定義數組并賦值坐標x,,yString[] posStrArr = inputStr.split(",");int x = Integer.parseInt(posStrArr[0]);int y = Integer.parseInt(posStrArr[1]);// 如果輸入坐標已有棋子,則重新輸入坐標if (!isOk(x, y)) {System.out.println("此處已有棋子,請換位置!");continue;}// 將上面分隔完以后的字符串轉換成用戶下棋的坐標int xPos = x;int yPos = y;// 定義電腦棋子顏色String comColor = null;// 根據用戶選擇的棋子顏色給對應的數組元素賦值if (peopleColor.equals("black")) {gb.board[xPos - 1][yPos - 1] = "●";comColor = "white";} else if (peopleColor.equals("white")) {gb.board[xPos - 1][yPos - 1] = "○";comColor = "black";}computer(comColor);gb.printBoard();// 判斷輸贏if (isWin(xPos - 1, yPos - 1, peopleColor)) {System.out.println(peopleColor + "獲勝!");break;}if (isWin(xPos - 1, yPos - 1, comColor)) {System.out.println(comColor + "獲勝!");break;}} while (inputStr != null);} }

運行上面程序,可以看到如圖 1 所示的界面。

只判斷了橫、豎是否有5個棋連在一起,從而判定勝負

總結

以上是生活随笔為你收集整理的1.23 实例:五子棋游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。