简单的控制台五子棋游戏

之前暑假在看李刚老师的《疯狂JAVA讲义》,课后习题中有五子棋的练习,一直没机会去真正的实践一遍,这次看了这篇文章,有了感悟,与大家共勉

 

 

利用二维数组实现的简单的控制台五子棋游戏,程序中没有添加电脑的AI。

    

  1. import java.io.*;  
  2. import java.util.*;  
  3. public class Gobang   
  4. {  
  5.     //定义一个二维数组来充当棋盘   
  6.     private static String [][] board;  
  7.     //定义棋盘的大小   
  8.     private static final int BOARD_SIZE=15;  
  9.         //创建一个单例对象   
  10.     private static Gobang gb= new Gobang();  
  11.       
  12.     private Gobang(){}  
  13.       
  14.     static int m=0,n=0;//记录下棋步数   
  15.   
  16.     private static void initBoard()  
  17.     {  
  18.         //初始化棋盘   
  19.          board = new String [BOARD_SIZE][BOARD_SIZE];  
  20.         //把没给元素都赋值为"╋",用于在控制台输出棋盘   
  21.         for(int i=0;i<BOARD_SIZE;i++)  
  22.         {  
  23.             for (int j=0;j<BOARD_SIZE ;j++ )  
  24.             {  
  25.                 board[i][j]="╋";  
  26.             }  
  27.         }  
  28.     }  
  29.       
  30.     //在控制台打印棋盘或棋子   
  31.     private static void printBoard()  
  32.     {  
  33.         //打印数组中的每个元素   
  34.         for(int i=0;i<BOARD_SIZE;i++)  
  35.         {  
  36.             for (int j=0;j<BOARD_SIZE ;j++ )  
  37.             {  
  38.                 System.out.print(board[i][j]);  
  39.             }  
  40.             System.out.println();  
  41.         }  
  42.     }  
  43.       
  44.     //检查该点是否有棋子   
  45.     private static boolean checkPiece(int x,int y)  
  46.     {  
  47.         if(x>BOARD_SIZE-1 || y>BOARD_SIZE-1)  
  48.         {  
  49.             System.out.println("您所选择的位置越界!请重新输入!");  
  50.             return false;  
  51.         }  
  52.         else  
  53.             return board[x][y]=="╋" ;  
  54.     }  
  55.     //用户下棋的方法   
  56.     private static void userPlay()   
  57.     {  
  58.         gb.printBoard();  
  59.         System.out.println("请输入您下棋的坐标,应该以x,y的格式:");  
  60.         try  
  61.         {  
  62.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  63.             String inputStr=null;  
  64.             if((inputStr=br.readLine())!=null)  
  65.             {  
  66.                 String [] posStrArr=inputStr.split(",");  
  67.                 int xPos=Integer.parseInt(posStrArr[0]);  
  68.                 int yPos=Integer.parseInt(posStrArr[1]);  
  69.                 if(checkPiece(xPos-1,yPos-1))  
  70.                 {  
  71.                     gb.board[xPos-1][yPos-1]="●";  
  72.                     m++;  
  73.                     n++;  
  74.                 }  
  75.             }  
  76.         }  
  77.         catch (Exception e)  
  78.         {  
  79.             System.out.println(e.getMessage().toString());  
  80.         }  
  81.           
  82.     }  
  83.     //电脑下棋的方法   
  84.     private static void compPlay()  
  85.     {  
  86.         int x=(int)(Math.random()*15);  
  87.         int y=(int)(Math.random()*15);  
  88.         if(checkPiece(x,y))  
  89.         {  
  90.             gb.board[x][y]="○";  
  91.             m++;  
  92.         }  
  93.     }  
  94.     //棋盘扫描的方法   
  95.     private static String scanBoard()  
  96.     {  
  97.         for(int i=0;i<BOARD_SIZE;i++)  
  98.         {  
  99.             for(int j=0;j<BOARD_SIZE;j++)  
  100.             {  
  101.                 //先判断横向   
  102.                 if(j<BOARD_SIZE-5 && board[i][j]=="●" && board[i][j+1]=="●")  
  103.                 {  
  104.                     if(board[i][j+2]=="●" && board[i][j+3]=="●"&& board[i][j+4]=="●")  
  105.                     return "User";  
  106.                 }  
  107.                 else if (j<BOARD_SIZE-5 && board[i][j]=="○" && board[i][j+1]=="○")  
  108.                 {  
  109.                     if(board[i][j+2]=="○" && board[i][j+3]=="○" && board[i][j+4]=="○")  
  110.                     return "Computer";  
  111.                 }  
  112.                 //判断纵向   
  113.                 else if (i<BOARD_SIZE-5 && board[i][j]=="●" && board[i+1][j]=="●")  
  114.                 {  
  115.                     if(board[i+2][j]=="●" && board[i+3][j]=="●" && board[i+4][j]=="●")  
  116.                     return "User";  
  117.                 }  
  118.                 else if (i<BOARD_SIZE-5 && board[i][j]=="○" && board[i+1][j]=="○")  
  119.                 {  
  120.                     if(board[i+2][j]=="○"&& board[i+3][j]=="○" && board[i+4][j]=="○")  
  121.                     return "Computer";  
  122.                 }  
  123.                 //判断斜向   
  124.                 else if (i<BOARD_SIZE-5 && j<BOARD_SIZE-5 && board[i][j]=="●" && board[i+1][j+1]=="●")  
  125.                 {  
  126.                     if(board[i+2][j+2]=="●" && board[i+3][j+3]=="●" && board[i+4][j+4]=="●")  
  127.                     return "User";  
  128.                 }  
  129.                 else if (i<BOARD_SIZE-5 && j<BOARD_SIZE-5&&board[i][j]=="○" && board[i+1][j+1]=="○")  
  130.                 {  
  131.                     if(board[i+2][j+2]=="○"&& board[i+3][j+3]=="○"&& board[i+4][j+4]=="○")  
  132.                     return "Computer";  
  133.                 }  
  134.                 //判断反斜向   
  135.                 else if (i>4 && j>4 && board[i][j]=="●" && board[i-1][j-1]=="●")  
  136.                 {  
  137.                     if(board[i-2][j-2]=="●"&& board[i-3][j-3]=="●"&& board[i-4][j-4]=="●")  
  138.                     return "User";  
  139.                 }  
  140.                 else if (i>4 && j>4&&board[i][j]=="○" && board[i-1][j-1]=="○")  
  141.                 {  
  142.                     if(board[i-2][j-2]=="○"&& board[i-3][j-3]=="○"&& board[i-4][j-4]=="○")  
  143.                     return "Computer";  
  144.                 }  
  145.             }  
  146.         }  
  147.         return "GoOn!";  
  148.     }  
  149.     //人机对弈   
  150.     public static void user2Comp()  
  151.     {  
  152.         if(m%2==0)  
  153.             userPlay();  
  154.         else   
  155.             compPlay();  
  156.         if(n>=5)  
  157.         {  
  158.             if(scanBoard()=="GoOn!")  
  159.                 user2Comp();  
  160.             else if(scanBoard()=="Computer")  
  161.             {  
  162.                 gb.printBoard();  
  163.                 System.out.println("恭喜!恭喜!Computer获胜!");  
  164.             }  
  165.             else if(scanBoard()=="User")  
  166.             {  
  167.                 gb.printBoard();  
  168.                 System.out.println("恭喜!恭喜!User获胜!");  
  169.             }  
  170.             else  
  171.                 System.out.println("出错啦!");  
  172.         }  
  173.         else  
  174.             user2Comp();  
  175.     }  
  176.     public static void main(String[] args)   
  177.     {  
  178.         gb.initBoard();  
  179.         user2Comp();  
  180.     }  
  181. }  

     声明:本代码从李刚老师的《疯狂Java讲义》中得到启示,并部分沿用了原书代码!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值