你的位置:首页 > Java教程

[Java教程]疯狂Java讲义 第一章控制台五子棋(代码分析)


Chessboard.java
1 package com.test; 2 3 public class Chessboard { 4 //定义二维数组作为棋盘 5 private String[][] board; 6 //定义棋盘大小 7 public static final int BOARD_SIZE=22; 8 9 //初始化棋盘10 public void initBoard(){11 board=new String[BOARD_SIZE][BOARD_SIZE];//创建二维数组12 //定义初值,嵌套循环13 for(int i=0;i<BOARD_SIZE;i++){14 for(int j=0;j<BOARD_SIZE;j++){15 board[i][j]="十";16 }17 }18 }19 //test函数的意义不明20 public void test(){21 Object[][] array=new Object[10][10];22 for(int i=0;i<array.length;i++){23 for(int j=0;j<array.length;j++){24 array[i][j]=new Object();25 }26 }27 }28 //打印棋盘29 public void printBoard(){30 for(int i=0;i<BOARD_SIZE;i++){31 for(int j=0;j<BOARD_SIZE;j++){32 System.out.print(board[i][j]);33 }34 System.out.print("\n");35 }36 }37 //放置棋子38 public void setBoard(int posX,int posY,String chessman){39 this.board[posX][posY]=chessman;40 }41 //返回棋盘42 public String [][] getBoard(){43 return this.board;44 }45 46 }

Chessman.java
1 package com.test; 2 3 public enum Chessman { 4 BLACK("●"),WHITE("○"); 5 //设置两个静态棋子属性 6 //等同于public static final Chessman BLACK=new Chessman("●") 7 private String chessman; 8 //设置后可通过.BLACK.chessman进行设置棋子种类 9 private Chessman(String chessman){10 this.chessman=chessman;11 }12 //返回棋子13 public String getChessman(){14 return this.chessman;15 }16 }

 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,则为0121     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