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

 package com.test;

 public class Chessboard {
     //定义二维数组作为棋盘
     private String[][] board;
     //定义棋盘大小
     public static final int BOARD_SIZE=22;

     //初始化棋盘
     public void initBoard(){
         board=new String[BOARD_SIZE][BOARD_SIZE];//创建二维数组
         //定义初值,嵌套循环
         for(int i=0;i<BOARD_SIZE;i++){
             for(int j=0;j<BOARD_SIZE;j++){
                 board[i][j]="十";
             }
         }
     }
     //test函数的意义不明
     public void test(){
         Object[][] array=new Object[10][10];
         for(int i=0;i<array.length;i++){
             for(int j=0;j<array.length;j++){
                 array[i][j]=new Object();
             }
         }
     }
     //打印棋盘
     public void printBoard(){
         for(int i=0;i<BOARD_SIZE;i++){
             for(int j=0;j<BOARD_SIZE;j++){
                 System.out.print(board[i][j]);
             }
             System.out.print("\n");
         }
     }
     //放置棋子
     public void setBoard(int posX,int posY,String chessman){
         this.board[posX][posY]=chessman;
     }
     //返回棋盘
     public String [][] getBoard(){
         return this.board;
     }

 }

Chessboard.java

 package com.test;

 public enum Chessman {
     BLACK("●"),WHITE("○");
     //设置两个静态棋子属性
     //等同于public static final Chessman BLACK=new Chessman("●")
     private String chessman;
     //设置后可通过.BLACK.chessman进行设置棋子种类
     private Chessman(String chessman){
         this.chessman=chessman;
     }
     //返回棋子
     public String getChessman(){
         return this.chessman;
     }
 }

Chessman.java

以上为棋盘类(Chessboard),棋子类(Chessman,枚举类)

最后是游戏类GobangGame.java

 package com.test;

 import java.io.BufferedReader;//输入
 import java.io.InputStreamReader;//输出

 public class GobangGame {
     private final int WIN_COUNT=5;//设置获胜棋子数
     private int posX=0;
     private int posY=0;
     private Chessboard chessboard;//Chessboard类的一个实例

     //将chessboard实例调用
     public GobangGame(Chessboard chessboard){
             this.chessboard=chessboard;
     }
     //接收inputStr里面的x,y值,并赋给posX/Y
     public boolean isValid(String inputStr){
         String[] posStrArr=inputStr.split(",");
         //将字符串分割成字符数组,分割标志为“,”
         try{
             posX=Integer.parseInt(posStrArr[0])-1;
             posY=Integer.parseInt(posStrArr[1])-1;
         }catch(NumberFormatException e){
             chessboard.printBoard();
             System.out.println("pls input position like num,num");
             return false;
         }
         if(posX<0||posX>=Chessboard.BOARD_SIZE||posY<0||posY>=Chessboard.BOARD_SIZE){
             chessboard.printBoard();
             System.out.println("reinput");
             return false;
         }
         //接收chessboard
         String[][] board=chessboard.getBoard();

         if(board[posX][posY]!="十"){
             chessboard.printBoard();
             System.out.println("re input");
             return false;
         }
         return true;
     }
     //游戏主流程函数,配图
     public void start()throws Exception{
         boolean isOver=false;
         chessboard.initBoard();
         chessboard.printBoard();
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         String inputStr=null;
         //若输入不为空,开始循环
         while((inputStr=br.readLine())!=null){
             isOver=false;
             //验证有效(是棋盘坐标)
             if(!isValid(inputStr)){
                 continue;
             }
             //设定棋子
             String chessman=Chessman.BLACK.getChessman();
             chessboard.setBoard(posX, posY, chessman);
             //判定是否胜利
             if(isWon(posX,posY,chessman)){
                 isOver=true;
             }else{
                 int[] computerPosArr=computerDo();
                 chessman=Chessman.WHITE.getChessman();
                 chessboard.setBoard(computerPosArr[0], computerPosArr[1], chessman);
                 if(isWon(computerPosArr[0],computerPosArr[1],chessman)){
                     isOver=true;
                 }
             }
             //判定是否结束
             if(isOver){
                 if(isReplay(chessman)){
                     chessboard.initBoard();
                     chessboard.printBoard();
                     continue;
                 }
                 break;
             }
             //进行下一回合
             chessboard.printBoard();
             System.out.println("pls input the num");
         }
     }
     //是否重新开始游戏
     public boolean isReplay(String chessman)throws Exception{
         chessboard.printBoard();
         String message=chessman.equals(Chessman.BLACK.getChessman())? "you win":"you lose";
         System.out.println(message+"paly again?(Y/N)");
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         if(br.readLine().equals("y")){
             return true;
         }
         return false;
     }
     //生成电脑棋子(随机生成即可,无逻辑)
     public int[] computerDo(){
         int posX=(int)(Math.random()*(Chessboard.BOARD_SIZE-1));
         int posY=(int)(Math.random()*Chessboard.BOARD_SIZE-1);
         String[][] board=chessboard.getBoard();
         //验证电脑下一步棋的坐标没有棋子
         while(board[posX][posY]!="十"){
             posX=(int)(Math.random()*Chessboard.BOARD_SIZE-1);
             posY=(int)(Math.random()*Chessboard.BOARD_SIZE-1);
         }
         int[] result={posX,posY};
         return result;
     }
     //胜利判定(方法为在当前所下棋子周围进行检查,非遍历棋盘法)
     public boolean isWon(int posX,int posY,String ico){
         int startX=0;//直线开始X坐标
         int startY=0;//直线开始Y坐标
         int endX=Chessboard.BOARD_SIZE-1;//直线结束X坐标
         int endY=endX;//直线结束Y坐标
         int sameCount=0;//同条直线上相邻棋子数
         int temp=0;
         //计算棋子的最小X坐标和Y坐标
         temp=posX-WIN_COUNT+1;
         startX=temp<0?0:temp;//若小于0,则为0
         temp=posY-WIN_COUNT+1;
         startY=temp<0?0:temp;
         //计算棋子的最大X坐标和Y坐标
         temp=posX+WIN_COUNT-1;
         endX=temp>Chessboard.BOARD_SIZE-1?Chessboard.BOARD_SIZE-1:temp;
         temp=posY+WIN_COUNT-1;
         endY=temp>Chessboard.BOARD_SIZE-1?Chessboard.BOARD_SIZE-1:temp;
         //从左到右方向计算相同相邻棋子的数目
         String[][] board=chessboard.getBoard();
         //竖直方向
         for(int i=startY;i<endY;i++){
             if(board[posX][i]==ico&&board[posX][i+1]==ico){
                 sameCount++;
             }else if(sameCount!=WIN_COUNT-1){
                 sameCount=0;
             }
         }
         //水平方向
         if(sameCount==0){
             for(int i=startX;i<endX;i++){
                 if(board[i][posY]==ico&&board[i+1][posY]==ico){
                     sameCount++;
                 }else if(sameCount!=WIN_COUNT-1){
                     sameCount=0;
                 }
             }
         }
         if(sameCount==0){
             int j=startY;
             for(int i=startX;i<endX;i++){
                     if(j<endY){
                         if(board[i][j]==ico&&board[i+1][j+1]==ico){
                             sameCount++;
                         }else if(sameCount!=WIN_COUNT-1){
                             sameCount=0;
                         }
                         j++;
                     }
                 }
             }
             return sameCount==WIN_COUNT-1?true:false;
         }

     public static void main(String[] args)throws Exception{
         GobangGame gb=new GobangGame(new Chessboard());//创建实例
         gb.start();//开始程序
     }
 }
     

其中的start()函数写了多个if语句,可读性较差。附上流程图(^…^!手写渣)

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

-END

-^^^^^^^^^^^^^^^^^^

-人世几回伤往事,山形依旧枕寒流。

-2016-09-06

上一篇:Kubernetes 笔记 10 Job 机器人加工厂


下一篇:CS20SI-tensorflow for research笔记: Lecture2