之前暑假在看李刚老师的《疯狂JAVA讲义》,课后习题中有五子棋的练习,一直没机会去真正的实践一遍,这次看了这篇文章,有了感悟,与大家共勉
利用二维数组实现的简单的控制台五子棋游戏,程序中没有添加电脑的AI。
- import java.io.*;
- import java.util.*;
- public class Gobang
- {
- //定义一个二维数组来充当棋盘
- private static String [][] board;
- //定义棋盘的大小
- private static final int BOARD_SIZE=15;
- //创建一个单例对象
- private static Gobang gb= new Gobang();
- private Gobang(){}
- static int m=0,n=0;//记录下棋步数
- private static 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]="╋";
- }
- }
- }
- //在控制台打印棋盘或棋子
- private static 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.println();
- }
- }
- //检查该点是否有棋子
- private static boolean checkPiece(int x,int y)
- {
- if(x>BOARD_SIZE-1 || y>BOARD_SIZE-1)
- {
- System.out.println("您所选择的位置越界!请重新输入!");
- return false;
- }
- else
- return board[x][y]=="╋" ;
- }
- //用户下棋的方法
- private static void userPlay()
- {
- gb.printBoard();
- System.out.println("请输入您下棋的坐标,应该以x,y的格式:");
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String inputStr=null;
- if((inputStr=br.readLine())!=null)
- {
- String [] posStrArr=inputStr.split(",");
- int xPos=Integer.parseInt(posStrArr[0]);
- int yPos=Integer.parseInt(posStrArr[1]);
- if(checkPiece(xPos-1,yPos-1))
- {
- gb.board[xPos-1][yPos-1]="●";
- m++;
- n++;
- }
- }
- }
- catch (Exception e)
- {
- System.out.println(e.getMessage().toString());
- }
- }
- //电脑下棋的方法
- private static void compPlay()
- {
- int x=(int)(Math.random()*15);
- int y=(int)(Math.random()*15);
- if(checkPiece(x,y))
- {
- gb.board[x][y]="○";
- m++;
- }
- }
- //棋盘扫描的方法
- private static String scanBoard()
- {
- for(int i=0;i<BOARD_SIZE;i++)
- {
- for(int j=0;j<BOARD_SIZE;j++)
- {
- //先判断横向
- if(j<BOARD_SIZE-5 && board[i][j]=="●" && board[i][j+1]=="●")
- {
- if(board[i][j+2]=="●" && board[i][j+3]=="●"&& board[i][j+4]=="●")
- return "User";
- }
- else if (j<BOARD_SIZE-5 && board[i][j]=="○" && board[i][j+1]=="○")
- {
- if(board[i][j+2]=="○" && board[i][j+3]=="○" && board[i][j+4]=="○")
- return "Computer";
- }
- //判断纵向
- else if (i<BOARD_SIZE-5 && board[i][j]=="●" && board[i+1][j]=="●")
- {
- if(board[i+2][j]=="●" && board[i+3][j]=="●" && board[i+4][j]=="●")
- return "User";
- }
- else if (i<BOARD_SIZE-5 && board[i][j]=="○" && board[i+1][j]=="○")
- {
- if(board[i+2][j]=="○"&& board[i+3][j]=="○" && board[i+4][j]=="○")
- return "Computer";
- }
- //判断斜向
- else if (i<BOARD_SIZE-5 && j<BOARD_SIZE-5 && board[i][j]=="●" && board[i+1][j+1]=="●")
- {
- if(board[i+2][j+2]=="●" && board[i+3][j+3]=="●" && board[i+4][j+4]=="●")
- return "User";
- }
- else if (i<BOARD_SIZE-5 && j<BOARD_SIZE-5&&board[i][j]=="○" && board[i+1][j+1]=="○")
- {
- if(board[i+2][j+2]=="○"&& board[i+3][j+3]=="○"&& board[i+4][j+4]=="○")
- return "Computer";
- }
- //判断反斜向
- else if (i>4 && j>4 && board[i][j]=="●" && board[i-1][j-1]=="●")
- {
- if(board[i-2][j-2]=="●"&& board[i-3][j-3]=="●"&& board[i-4][j-4]=="●")
- return "User";
- }
- else if (i>4 && j>4&&board[i][j]=="○" && board[i-1][j-1]=="○")
- {
- if(board[i-2][j-2]=="○"&& board[i-3][j-3]=="○"&& board[i-4][j-4]=="○")
- return "Computer";
- }
- }
- }
- return "GoOn!";
- }
- //人机对弈
- public static void user2Comp()
- {
- if(m%2==0)
- userPlay();
- else
- compPlay();
- if(n>=5)
- {
- if(scanBoard()=="GoOn!")
- user2Comp();
- else if(scanBoard()=="Computer")
- {
- gb.printBoard();
- System.out.println("恭喜!恭喜!Computer获胜!");
- }
- else if(scanBoard()=="User")
- {
- gb.printBoard();
- System.out.println("恭喜!恭喜!User获胜!");
- }
- else
- System.out.println("出错啦!");
- }
- else
- user2Comp();
- }
- public static void main(String[] args)
- {
- gb.initBoard();
- user2Comp();
- }
- }
声明:本代码从李刚老师的《疯狂Java讲义》中得到启示,并部分沿用了原书代码!