1. 程式人生 > 實用技巧 >【JAVA例項】五子棋小遊戲

【JAVA例項】五子棋小遊戲

  1 /*
  2     (1)繪製棋盤 - 寫一個成員方法實現
  3 
  4     (2)提示黑方和白方分別下棋並重新繪製棋盤 - 寫一個成員方法實現。
  5 
  6     (3)每當一方下棋後判斷是否獲勝 - 寫一個成員方法實現。
  7 
  8     (4)提示: 採用二維陣列來模擬並描述棋盤,棋盤如下:
  9  */
 10 
 11 package stage1_module2.homework;
 12 
 13 import java.util.Scanner;
 14 import java.util.Arrays;
 15 
 16 public class FiveInARowGame {
17 18 // 1. 繪製一個五子棋棋盤 19 public String[][] drawChessBoard(){ 20 21 String[][] chessBoard = new String[17][17]; 22 23 for(int i = 0; i < chessBoard.length; i++) { 24 for(int j = 0; j < chessBoard.length; j++) { 25 if(i == 0){ 26 chessBoard[i][j] = " " + Long.toHexString(j-1) + " ";
27 } else if(j == 0){ 28 chessBoard[i][j] = Long.toHexString(i-1); 29 } 30 else { 31 chessBoard[i][j] = " + "; 32 } 33 chessBoard[0][0] = " "; 34 System.out.print(chessBoard[i][j] + " ");
35 } 36 System.out.println(); 37 } 38 return chessBoard; 39 40 } 41 42 // 2. 提示黑方白方下棋 43 public void playChess(){ 44 45 boolean flag = true; 46 String side = "black"; 47 48 String[][] chessBoard = drawChessBoard(); 49 50 while ( flag ){ 51 52 System.out.println("請" + side + "落子(按照 行數 列數 的格式), eg: 7 8"); 53 System.out.println("PS:1 <= 行數 <= 16 / 1 <= 列數 <= 16 "); 54 Scanner sc = new Scanner(System.in); 55 56 int row = sc.nextInt(); 57 int column = sc.nextInt(); 58 59 if (chessBoard[row][column].equals(" + ")) { 60 chessBoard[row][column] = side; 61 for (int i = 0; i < chessBoard.length; i++){ 62 for (int j = 0; j < chessBoard[i].length; j++){ 63 System.out.print 64 (chessBoard[i][j]); 65 } 66 System.out.println(); 67 } 68 69 if (judge(chessBoard, side).equals(side + " Winning")){ 70 System.out.println("恭喜" + side + "方贏得勝利"); 71 flag = false; 72 }else{ 73 if (side.equals("black")){ 74 side = " red "; 75 }else{ 76 side = "black"; 77 } 78 } 79 } else{ 80 System.out.println("請重新輸入!"); 81 } 82 83 } 84 } 85 86 // 3. 判斷輸贏 87 public String judge(String[][] chessBoard, String side){ 88 89 // 1.判斷橫向是否連成五子 90 for (int i = 1; i < chessBoard.length; i++) { 91 int count = 0; 92 for (int j = 1; j < chessBoard.length-1 ; j++) { 93 boolean horizontal = chessBoard[i][j].equals(chessBoard[i][j + 1]) 94 && chessBoard[i][j].equals(side); 95 if ( horizontal ) { 96 count++; 97 if (count == 4) { 98 System.out.println(side + " Winning"); 99 return side + " Winning"; 100 } 101 } else { 102 count = 0; 103 } 104 } 105 } 106 107 // 2.判斷縱向是否連成五子 108 for (int j = 1; j < chessBoard.length; j++) { 109 int count = 0; 110 for (int i = 1; i < chessBoard.length-1 ; i++) { 111 // 2.判斷縱向是否連成五子 112 boolean vertical = chessBoard[i][j].equals(chessBoard[i + 1][j]) 113 && chessBoard[i][j].equals(side); 114 if ( vertical ) { 115 count++; 116 if (count == 4) { 117 System.out.println(side + " Winning"); 118 return side + " Winning"; 119 } 120 } else { 121 count = 0; 122 } 123 } 124 } 125 126 // 3.判斷對角線是否連成五子(左上↖️️到右下↘️️) 127 int cnt_1 = 2*(chessBoard.length-1)-9; 128 for (int k = 0; k < cnt_1; k++){ 129 int count = 0; 130 for ( int i = ((k <= (cnt_1/2)) ? 1 : (k-10)) ; 131 i < ( (k <= cnt_1/2) ? k-(cnt_1/2)+chessBoard.length : (cnt_1/2)-k+chessBoard.length) - 1 ; 132 i++){ 133 boolean diagLeftToRight = 134 chessBoard[i][chessBoard.length-k-6+i].equals(chessBoard[i+1][chessBoard.length-k-6+i+1]) 135 && chessBoard[i][chessBoard.length-k-6+i].equals(side); 136 if ( diagLeftToRight ) { 137 count++; 138 if (count == 4) { 139 System.out.println(side + " Winning"); 140 return side + " Winning"; 141 } 142 } else { 143 count = 0; 144 } 145 146 } 147 } 148 149 // 4.判斷對角線是否連成五子(右上↗️到左下↙️) 150 int cnt_2 = 2*(chessBoard.length-1)-9; 151 for (int k = 0; k < cnt_2; k++){ 152 int count = 0; 153 for ( int i = ((k <= (cnt_2/2)) ? 1 : (k-10)) ; 154 i < ( (k <= cnt_2/2) ? k-(cnt_2/2)+chessBoard.length : (cnt_2/2)-k+chessBoard.length) - 1; 155 i++){ 156 boolean diagRightToLeft = 157 chessBoard[i][chessBoard.length+k-11-i].equals(chessBoard[i+1][chessBoard.length+k-11-i-1]) 158 && chessBoard[i][chessBoard.length+k-11-i].equals(side); 159 if ( diagRightToLeft ) { 160 count++; 161 // System.out.print("inner count :" + count + " "); 162 if (count == 4) { 163 System.out.println(side + " Winning"); 164 return side + " Winning"; 165 } 166 } else { 167 count = 0; 168 // System.out.print("count:" + count + " "); 169 } 170 } 171 } 172 173 return "go on"; 174 } 175 }
 1 package stage1_module2.homework;
 2 
 3 public class FiveARowGameTest {
 4 
 5     public static void main(String[] args){
 6 
 7         FiveInARowGame f1 = new FiveInARowGame();
 8         f1.drawChessBoard();
 9         f1.playChess();
10     }
11 }