java實現簡陋五子棋(雖然bug很多,但會繼續改進)

來源:互聯網
上載者:User

標籤:五子棋   import   小遊戲   電腦   java   

import java.util.InputMismatchException;import java.util.Random;import java.util.Scanner;import javax.swing.JOptionPane;/** * 五子棋小遊戲.可供雙人對戰和人機對戰(人機對戰的機是隨機輸入,未加如入精確演算法)和棋的情況沒有做判斷。 * <p> * 在15*15的棋盤上,獲勝的情況總共有572種。<br> * 在一場五子棋的遊戲中,電腦必須要知道有哪些的獲勝組合,因此我們必須求得獲勝組合的總數。<br> * (1)計算水平方向的獲勝組合數,每一行的獲勝組合是:11種,共15行,所以水平方向的獲勝組合數為:11*15=165. <br> * (2)計算垂直方向的獲勝組合總數,每一列的獲勝組合是:11種,共15列,所以垂直方向的獲勝組合數為:11*15=165.<br> * (3)計算正對角線方向的獲勝組合總數,正對角線上的獲勝組合總數為11+(10+9+...+2+1)*2=121.<br> * (4)計算反對角線方向的獲勝組合總數,反對角線上的獲勝組合總數為11+(10+9+...+2+1)*2=121.<br> * 所以,獲勝的總數有165+165+121+121=572. *  * @author frank * */public class NewGoBang{// 靜態代碼塊static{System.out.println("歡迎來到五子棋世界!");}public final static int size = 15;String[][] board = new String[size][size];boolean flag = true;// 建立一個random隨機對象。Random ran = new Random();/** * 畫出棋盤。"十" */public void initBoard(){for (int x = 0; x < board.length; x++){for (int y = 0; y < board.length; y++){board[x][y] = "十";// ╂ ,╋}}printArray(board);}/** * 判斷棋子是否有五個連續. *  * @param row *            棋子的橫座標 * @param column *            棋子的縱座標 * @param str *            字串數組 * @param c *            字串參數(即是棋子) */public void isFive(int row, int column, String[][] str, String c){// 橫向判斷int m, L;m = 1;L = 1;while ((column + m) < size && str[row][column + m] == c){L++;m++;}m = 1;while ((column - m) >= 0 && str[row][column - m] == c){L++;m++;}showMessage(L, c);// 縱向判斷m = 1;L = 1;while ((row + m) < size && str[row + m][column] == c){L++;m++;}m = 1;while ((row - m) >= 0 && str[row - m][column] == c){L++;m++;}showMessage(L, c);// 斜向判斷Italic(row, column, str, c);}// 斜向判斷public void Italic(int row, int column, String[][] str, String c){int L = 1;// 定義一個指標,超過五個就勝利了。int m = 1;// 斜向判斷while ((row + m) < size && (column + m) < size&& str[row + m][column + m] == c){L++;m++;}m = 1;//m重新置為1;while ((row - m) >= 0 && (column - m) >= 0&& str[row - m][column - m] == c){L++;m++;}showMessage(L, c);// 反斜向判斷m = 1;L = 1;while ((row + m) < size && (column - m) >= 0&& str[row + m][column - m] == c){L++;m++;}m = 1;//m重新置為1;while ((row - m) >= 0 && (column + m) < size&& str[row - m][column + m] == c){L++;m++;}showMessage(L, c);}//判斷是否等於五個public void showMessage(int point, String c){if (point >= 5){System.out.println( "恭喜 " + c + " 棋贏了!");//可以用下面這句彈出一個對話方塊,比較美觀。
//JOptionPane.showMessageDialog(null, "恭喜 " + c + " 棋贏了!");//贏了之後當然得結束程式。System.exit(0);} else{point = 0;}}        /*這裡可以選擇雙人對戰或者是人機對戰。            別太興奮,人機對戰只是隨機輸入的,並沒有加入什麼核心演算法。            後續有機會我會繼續更新的。        */public void show(){System.out.println("雙人對戰請輸入:1,人機對戰請輸入2.");//利用Scanner輸入資料。Scanner sc = new Scanner(System.in);int option = sc.nextInt();//這裡可以選擇雙人對戰或者是人機對戰。switch(option){            //雙人對戰case 1:while (flag){System.out.println("請黑方輸入棋子的座標:(如:1 3)");manInput("●");System.out.println("請白方輸入棋子的座標:(如:1 3)");manInput("○");}//人機對戰case 2:while (flag){System.out.println("請您輸入棋子的座標:(如:1 3)");manInput("●");System.out.println("電腦下子:");computer("○");}//機機對戰,用來快速調試case 3: while (flag){System.out.println("電腦A下子:");computer("●");System.out.println("電腦B下子:");computer("○");}}}//電腦隨機下子。public boolean computer(String c){int row = ran.nextInt(size);int column = ran.nextInt(size);                while(board[row][column] != "十"){row = ran.nextInt(size);column = ran.nextInt(size);}if(board[row][column] == "十"){System.out.println("(" + row + "," + column + ")");board[row][column] = c;printArray(board);isFive(row, column, board, c);}else return false;return true;}//人工下子。輸入座標Coordinateprivate void manInput(String c)// int row,int column{try{ Scanner scan = new Scanner(System.in); int row = scan.nextInt(); int column = scan.nextInt();if (board[row][column] == "十")//row>=0&&column>=0&&row<size&&column<size&&{System.out.println("(" + row + "," + column + ")");board[row][column] = c;// 自選字元‘★‘,‘█‘,‘▓‘,‘‘,‘‘●●●‘╋‘‘"●"‘○printArray(board);isFive(row, column, board, c);} else{
System.out.println("該位置已放有棋子,請放到其他位置!");manInput(c);}} catch (ArrayIndexOutOfBoundsException e){
System.out.println("超出棋盤位置,請重新輸入!");manInput(c);}catch(InputMismatchException e){                //JOptionPane.showMessageDialog(null, "輸入的數不合法,請重新輸入!");System.out.println("輸入的數不合法,請重新輸入!");manInput(c);}                // System.out.println("我是美麗的調試,我無處不在!");}// 列印String類型的二維數組private void printArray(String[][] arr){for (int i = 0; i < arr.length; i++){for (int j = 0; j < arr[i].length; j++){System.out.print(arr[i][j]);}System.out.println("");}}public static void main(String[] args){NewGoBang gobang = new NewGoBang();gobang.initBoard();gobang.show();}}


本文出自 “卡暗哩” 部落格,請務必保留此出處http://kaanli.blog.51cto.com/6744784/1631302

java實現簡陋五子棋(雖然bug很多,但會繼續改進)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.