以上為棋盤類(Chessboard),棋子類(Chessman,枚舉類)
最後是游戲類GobangGame.java
1 package com.test; 2 3 import java.io.BufferedReader;//輸入 4 import java.io.InputStreamReader;//輸出 5 6 public class GobangGame { 7 private final int WIN_COUNT=5;//設置獲勝棋子數 8 private int posX=0; 9 private int posY=0; 10 private Chessboard chessboard;//Chessboard類的一個實例 11 12 //將chessboard實例調用 13 public GobangGame(Chessboard chessboard){ 14 this.chessboard=chessboard; 15 } 16 //接收inputStr裡面的x,y值,並賦給posX/Y 17 public boolean isValid(String inputStr){ 18 String[] posStrArr=inputStr.split(","); 19 //將字符串分割成字符數組,分割標志為“,” 20 try{ 21 posX=Integer.parseInt(posStrArr[0])-1; 22 posY=Integer.parseInt(posStrArr[1])-1; 23 }catch(NumberFormatException e){ 24 chessboard.printBoard(); 25 System.out.println("pls input position like num,num"); 26 return false; 27 } 28 if(posX<0||posX>=Chessboard.BOARD_SIZE||posY<0||posY>=Chessboard.BOARD_SIZE){ 29 chessboard.printBoard(); 30 System.out.println("reinput"); 31 return false; 32 } 33 //接收chessboard 34 String[][] board=chessboard.getBoard(); 35 36 37 if(board[posX][posY]!="十"){ 38 chessboard.printBoard(); 39 System.out.println("re input"); 40 return false; 41 } 42 return true; 43 } 44 //游戲主流程函數,配圖 45 public void start()throws Exception{ 46 boolean isOver=false; 47 chessboard.initBoard(); 48 chessboard.printBoard(); 49 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 50 String inputStr=null; 51 //若輸入不為空,開始循環 52 while((inputStr=br.readLine())!=null){ 53 isOver=false; 54 //驗證有效(是棋盤坐標) 55 if(!isValid(inputStr)){ 56 continue; 57 } 58 //設定棋子 59 String chessman=Chessman.BLACK.getChessman(); 60 chessboard.setBoard(posX, posY, chessman); 61 //判定是否勝利 62 if(isWon(posX,posY,chessman)){ 63 isOver=true; 64 }else{ 65 int[] computerPosArr=computerDo(); 66 chessman=Chessman.WHITE.getChessman(); 67 chessboard.setBoard(computerPosArr[0], computerPosArr[1], chessman); 68 if(isWon(computerPosArr[0],computerPosArr[1],chessman)){ 69 isOver=true; 70 } 71 } 72 //判定是否結束 73 if(isOver){ 74 if(isReplay(chessman)){ 75 chessboard.initBoard(); 76 chessboard.printBoard(); 77 continue; 78 } 79 break; 80 } 81 //進行下一回合 82 chessboard.printBoard(); 83 System.out.println("pls input the num"); 84 } 85 } 86 //是否重新開始游戲 87 public boolean isReplay(String chessman)throws Exception{ 88 chessboard.printBoard(); 89 String message=chessman.equals(Chessman.BLACK.getChessman())? "you win":"you lose"; 90 System.out.println(message+"paly again?(Y/N)"); 91 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 92 if(br.readLine().equals("y")){ 93 return true; 94 } 95 return false; 96 } 97 //生成電腦棋子(隨機生成即可,無邏輯) 98 public int[] computerDo(){ 99 int posX=(int)(Math.random()*(Chessboard.BOARD_SIZE-1)); 100 int posY=(int)(Math.random()*Chessboard.BOARD_SIZE-1); 101 String[][] board=chessboard.getBoard(); 102 //驗證電腦下一步棋的坐標沒有棋子 103 while(board[posX][posY]!="十"){ 104 posX=(int)(Math.random()*Chessboard.BOARD_SIZE-1); 105 posY=(int)(Math.random()*Chessboard.BOARD_SIZE-1); 106 } 107 int[] result={posX,posY}; 108 return result; 109 } 110 //勝利判定(方法為在當前所下棋子周圍進行檢查,非遍歷棋盤法) 111 public boolean isWon(int posX,int posY,String ico){ 112 int startX=0;//直線開始X坐標 113 int startY=0;//直線開始Y坐標 114 int endX=Chessboard.BOARD_SIZE-1;//直線結束X坐標 115 int endY=endX;//直線結束Y坐標 116 int sameCount=0;//同條直線上相鄰棋子數 117 int temp=0; 118 //計算棋子的最小X坐標和Y坐標 119 temp=posX-WIN_COUNT+1; 120 startX=temp<0?0:temp;//若小於0,則為0 121 temp=posY-WIN_COUNT+1; 122 startY=temp<0?0:temp; 123 //計算棋子的最大X坐標和Y坐標 124 temp=posX+WIN_COUNT-1; 125 endX=temp>Chessboard.BOARD_SIZE-1?Chessboard.BOARD_SIZE-1:temp; 126 temp=posY+WIN_COUNT-1; 127 endY=temp>Chessboard.BOARD_SIZE-1?Chessboard.BOARD_SIZE-1:temp; 128 //從左到右方向計算相同相鄰棋子的數目 129 String[][] board=chessboard.getBoard(); 130 //豎直方向 131 for(int i=startY;i<endY;i++){ 132 if(board[posX][i]==ico&&board[posX][i+1]==ico){ 133 sameCount++; 134 }else if(sameCount!=WIN_COUNT-1){ 135 sameCount=0; 136 } 137 } 138 //水平方向 139 if(sameCount==0){ 140 for(int i=startX;i<endX;i++){ 141 if(board[i][posY]==ico&&board[i+1][posY]==ico){ 142 sameCount++; 143 }else if(sameCount!=WIN_COUNT-1){ 144 sameCount=0; 145 } 146 } 147 } 148 if(sameCount==0){ 149 int j=startY; 150 for(int i=startX;i<endX;i++){ 151 if(j<endY){ 152 if(board[i][j]==ico&&board[i+1][j+1]==ico){ 153 sameCount++; 154 }else if(sameCount!=WIN_COUNT-1){ 155 sameCount=0; 156 } 157 j++; 158 } 159 } 160 } 161 return sameCount==WIN_COUNT-1?true:false; 162 } 163 164 165 public static void main(String[] args)throws Exception{ 166 GobangGame gb=new GobangGame(new Chessboard());//創建實例 167 gb.start();//開始程序 168 } 169 } 170
其中的start()函數寫了多個if語句,可讀性較差。附上流程圖(^…^!手寫渣)
-END
-^^^^^^^^^^^^^^^^^^
-人世幾回傷往事,山形依舊枕寒流。
-2016-09-06