一个简单的控制台五子棋小游戏

import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*五子棋游戏
*/
public class Playgame{
	private final int WIN_COUNT=5;
	//坐标
	private int posX=0,posY=0;
	//棋盘
	private Chessboard chessboard;
	

	/**
	*构造器
	*/
	public Playgame(Chessboard chessboard){
		this.chessboard=chessboard;
	}
	/**
	*检查输入是否为符合要求的坐标,并给posX和posY赋值
	*@param inputStr 输入的字符串
	*@return 字符串合法返回true,反则返回false
	*/
	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("请以(数字,数字)的格式输入:");
			return false;
		}
		if(posX<0||posX>=Chessboard.BOARD_SIZE||
				posY<0||posY>=Chessboard.BOARD_SIZE){
			chessboard.printBoard();
			System.out.println("坐标应在1~"+Chessboard.BOARD_SIZE);
			return false;
		}
		String[][] board = chessboard.getBoard();
		if(board[posX][posY]!="十"){
			chessboard.printBoard();
			System.out.println("此地以有棋子,请重新输入:");
			return false;
		}
		return true;
	}
	
	/**
	*开始游戏
	*/
	public void start()throws Exception{
		chessboard.initBoard();
		chessboard.printBoard();
		String chessman;
		int i=0;
		//从键盘获取输入
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inputStr=null;
		System.out.println("请输入您下棋的坐标,应以x,y的格式输入:");
		while((inputStr=br.readLine())!=null){
			if(!isValid(inputStr))continue;
			//黑子先行
			if(++i%2==1)chessman=Chessman.BLACK.getChessman();
			else chessman=Chessman.WHITE.getChessman();
			chessboard.setBoard(posX,posY,chessman);
			chessboard.printBoard();
			//判断是否获胜
			if(isWon(posX,posY,chessman))break;
			System.out.println("请输入您下棋的坐标,应以x,y的格式输入:");
		}
	}
	/**
	*判断是否五子一线了
	*/
	public boolean isWon(int posX,int posY,String ico){
		int startX,startY,endX,endY,temp;
		int sameCount=0;
		//根据落子坐标找判断范围,一个十和一个叉
		startX=(temp=posX-4)<0?0:temp;
		endX=(temp=posX+4)>Chessboard.BOARD_SIZE - 1 ?Chessboard.BOARD_SIZE - 1
					:temp;
		startY=(temp=posY-4)<0?0:temp;
		endY=(temp=posY+4)>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)sameCount++;
			else if(sameCount!=5)sameCount=0;
		}
		if(sameCount==0){
			for(int i=startX;i<=endX;i++){
				if(board[i][posY]==ico)sameCount++;
				else if(sameCount!=5)sameCount = 0;
			}
		}
		if(sameCount==0){
			for(int i=startX,j=startY;i<=endX;i++,j++){
				if(board[i][j]==ico)sameCount++;
				else if(sameCount!=5)sameCount=0;
			}
		}
		if(sameCount==0){
			for(int i=startX,j=endY;i<=endX;i++,j--){
				if(board[i][j]==ico)sameCount++;
				else if(sameCount!=5)sameCount=0;
			}
		}
		if(sameCount==5){System.out.println("恭喜您获得胜利!");return true;}
		else return false;
	}
	
	public static void main(String[] args)throws Exception{
	
		var gb=new Playgame(new Chessboard());
		gb.start();
	}

}
/**
*棋子枚举类
*/
public enum Chessman{
	BLACK("●"), WHITE("○");
	private String chessman;
	

	private Chessman(String chessman){
		this.chessman=chessman;
	}	
	/**
	*@return String chessman
	*/
	public String getChessman(){
		return this.chessman;
	}	

}
/**
*跟棋盘相关
*/
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]="十";
			}
		}
	}
	/**
	*打印初始化棋盘
	*/
	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;
	}
	/**
	 * 返回棋盘
	 * 
	 * @return 返回棋盘
	 */
	public String[][] getBoard(){
		return this.board;
	}

}

你可能感兴趣的:(一个简单的控制台五子棋小游戏)