1.23 实例:五子棋游戏
生活随笔
收集整理的這篇文章主要介紹了
1.23 实例:五子棋游戏
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import java.util.Scanner;
public class Gobang {// 定義棋盤(pán)的大小public static int BOARD_SIZE = 15;// 定義一個(gè)二維數(shù)組來(lái)充當(dāng)棋盤(pán)public static String[][] board = new String[BOARD_SIZE][BOARD_SIZE];String black = "●"; // 黑子String white = "○"; // 白子/*** 初始化械盤(pán)數(shù)組*/public void initBoard() {// 把每個(gè)元素賦為"╋",用于在控制臺(tái)畫(huà)出棋盤(pán)for (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {board[i][j] = "╋";}}}/*** 在控制臺(tái)輸出棋盤(pán)的方法*/public void printBoard() {// 打印毎個(gè)數(shù)組元素for (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {// 打印數(shù)組元素后不換行System.out.print(board[i][j]);}// 毎打印完一行數(shù)組元素后輸出一個(gè)換行符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;}/*** 判斷指定坐標(biāo)是否有棋子*/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; // 生成一個(gè)1~14之間的隨機(jī)數(shù)int y = (int) (Math.random() * 14) + 1; // 生成一個(gè)1~14之間的隨機(jī)數(shù)// 判斷電腦下棋的坐標(biāo)有無(wú)棋子,有棋子則在重新生成隨機(jī)數(shù)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();// 這是用于獲取鍵盤(pán)輸入的方法Scanner input = new Scanner(System.in); // 使用Scanner類(lèi)獲取用戶輸入System.out.println("您想要選擇什么顏色的棋,black或white,請(qǐng)輸入:");String peopleColor = input.next(); // 定義用戶選擇棋子的顏色并返回用戶輸入的字符串// 如果用戶選擇的是白棋,則電腦先下(五子棋中黑棋先下)if (peopleColor.equals("white")) {System.out.println("您選擇的是白棋");computer("black");gb.printBoard();}String inputStr;do {System.out.println("輸入您下棋的坐標(biāo),應(yīng)以x,y的格式:");inputStr = input.next();// 定義數(shù)組并賦值坐標(biāo)x,,yString[] posStrArr = inputStr.split(",");int x = Integer.parseInt(posStrArr[0]);int y = Integer.parseInt(posStrArr[1]);// 如果輸入坐標(biāo)已有棋子,則重新輸入坐標(biāo)if (!isOk(x, y)) {System.out.println("此處已有棋子,請(qǐng)換位置!");continue;}// 將上面分隔完以后的字符串轉(zhuǎn)換成用戶下棋的坐標(biāo)int xPos = x;int yPos = y;// 定義電腦棋子顏色String comColor = null;// 根據(jù)用戶選擇的棋子顏色給對(duì)應(yīng)的數(shù)組元素賦值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);}
}
運(yùn)行上面程序,可以看到如圖 1 所示的界面。
只判斷了橫、豎是否有5個(gè)棋連在一起,從而判定勝負(fù)
總結(jié)
以上是生活随笔為你收集整理的1.23 实例:五子棋游戏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1.20 实例:数字转人民币读法
- 下一篇: 1.1 对象的概念及面向对象的三个基本特