Java控制台五子棋

前言

说明

控制台五子棋是在java控制台实现的五子棋对战游戏,可玩性并不高,实现这个小游戏主要联系面向对象编程和五子棋核心逻辑实现。电脑也没有实现人工智能。

特点

不能用鼠标操作,只能用键盘输入下棋位置。

Java可以用AWT,Swing技术实现可视化,目前还没用。

输入输出

输入用(x,y)格式输入要要下棋的坐标。

输入用字符串“+”来表示棋格,“@”表示黑棋,”O”表示白棋。

规则

黑棋先行,一般来说是用户。

棋盘大小一般为15*15,可以自己定义

白棋或黑棋先为横,竖,斜5连为赢家

棋盘下满还无赢家为和棋。

面向对象设计类

棋盘类ChessBorad

属性

二维字符串数组

棋盘大小

行为

初始化棋盘,将二维数组全部初始化为“+”

打印棋盘和棋子,用来输出

Get棋盘,后面会用到。

根据棋盘位置和棋子set旗子

棋子枚举类

因为棋子只有两种类别,并且为字符串类型,设置为枚举类型,后面需要引用的时候直接调用,逻辑清楚

游戏类

属性

一个棋盘

用户当前输入的坐标

赢得连珠大小

行为

1判断用户输入的坐标是否合法,第一步判断输入格式是否正确,第二部判断输入大小是否在棋盘范围内,第三步判断输入位置是否有棋子。每种情况都满足返回true

2判断用户是否赢得胜利,五子棋游戏的核心。分为四种情况,即横竖斜,每种情况从当前输入的位置向两个方向计数判断,效率相对扫描棋盘高。

3判断是否为和棋

4.电脑是通过随机生成坐标坐,需要判断随机生成的坐标是否有棋子。

5判断当和棋或者一方胜利时是否重新开始游戏。

游戏过程

流程图

 

代码实现

ChessBorad类

public class ChessBoard {
    private String[][] chessBoard;
    public final int SIZE = 20;
    
    public void initBoard(){
        chessBoard = new String[SIZE][SIZE];
        for(int i = 0;i < SIZE;i++){
            for(int j = 0;j < SIZE;j++){
                chessBoard[i][j] =  "+";
            }
        }
    }
    public void printBoard(){
        for(int i = 0;i < SIZE;i++){
            for(int j = 0; j < SIZE;j++){
                System.out.print(chessBoard[i][j]);
            }
            System.out.println();
        }
    }
    public String[][] getChessBoard(){
        return chessBoard;
    }
    public void setChessBoard(int x,int y,String str){
        chessBoard[x][y] = str;
    }
}

ChessMan类

public enum ChessMan {
    BLOCK("@"),WHITE("O");
    private String s;
    private ChessMan(String str){
        s = str;
    }
    public String getS(){
        return s;
    }
}

GoBangGame类

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class GobangGame {
    private ChessBoard chessBoard;
    private int posX;
    private int posY;
    private final int WON_SIZE = 5;

    public boolean isValid(String str){
        String[]input = str.split(",");
        try{
            posX = Integer.parseInt(input[0]) - 1;
            posY = Integer.parseInt(input[1]) - 1;
        }catch(NumberFormatException ex){
            chessBoard.printBoard();
            System.out.println("输入格式不对,请输入(x,y)类型,重新输入:");
            return false;
        }
        if(posX < 0 || posX >= chessBoard.SIZE || posY < 0 || posY >= chessBoard.SIZE)return false;

        String[][] s = chessBoard.getChessBoard();
        if(s[posX][posY] != "+")return false;
        return true;
    }
    public boolean isPeace(){
        String[][] str = chessBoard.getChessBoard();
        for(int i=0; i < chessBoard.SIZE;i++){
            for(int j = 0; j < chessBoard.SIZE;j++){
                if(str[i][j] == "+")return false;
            }
        }
        return true;
    }
    public boolean isWon(int X,int Y,String str){
        String[][] s = chessBoard.getChessBoard();
        int wonCount = 1;
        int x = X;
        int y = Y-1;
        //判断横线
        while(y >= 0){
            if(s[x][y].equals(str))
            {
                wonCount++;
                y--;
            }
            else break;
        }
        y = Y + 1;
        while(y < chessBoard.SIZE){
            if(s[x][y].equals(str)){
                wonCount++;
                y++;
            }
            else break;
        }
        if(wonCount >= WON_SIZE)return true;
        //判断竖线
        x = X - 1;
        y = Y;
        wonCount = 1;
        while(x >= 0){
            if(s[x][y].equals(str)){
                wonCount++;
                x--;
            }
            else break;
        }
        x = Y + 1;
        while(x < chessBoard.SIZE){
            if(s[x][y].equals(str)){
                wonCount++;
                x++;
            }
            else break;
        }
        if(wonCount >= 5)return true;
        //判断斜线1
        x = X - 1;
        y = Y - 1;
        wonCount = 1;
        while(x >=0 && y>=0){
            if(s[x][y].equals(str)){
                wonCount++;
                x--;
                y--;
            }
            else break;
        }
        x = X + 1;
        y = Y + 1;
        while(x < WON_SIZE && y < WON_SIZE){
            if(s[x][y].equals(str)) {
                wonCount++;
                x++;
                y++;
            }
            else break;
        }
        if(wonCount >= 5)return true;

        //判断斜线2
        x = X - 1;
        y = Y + 1;
        wonCount = 1;
        while(x >= 0 && y < WON_SIZE){
            if(s[x][y].equals(str)){
                wonCount++;
                x--;
                y++;
            }
            else break;
        }
        x = X + 1;
        y = Y - 1;
        while(x < WON_SIZE && y >= 0){
            if(s[x][y].equals(str)){
                wonCount++;
                x++;
                y--;
            }
            else break;
        }
        if(wonCount >= 5)return true;
        return false;
    }
    public int[] computerDo(){
        //System.out.println("djlfdf");
        String[][] s = chessBoard.getChessBoard();
        int x = (int)(Math.random() * chessBoard.SIZE);
        int y = (int)(Math.random() * chessBoard.SIZE);
        while(!s[x][y].equals("+")){
            x = (int)(Math.random() * chessBoard.SIZE);
            y = (int)(Math.random() * chessBoard.SIZE);
        }
        int[] a = new int[2];
        a[0] = x;
        a[1] = y;
        //System.out.println("djlfdf");
        return a;
    }
    public boolean isReplay(String str) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if(ChessMan.BLOCK.getS().equals(str))System.out.println("黑棋胜出,输入y继续游戏");
        else if(ChessMan.WHITE.getS().equals(str))System.out.println("白棋胜出,输入y继续游戏");
        else System.out.println("和棋,输入y继续游戏");
        String s = br.readLine();
        if(s.equals("y"))return true;
        return false;
    }
    public void game() throws Exception{
        boolean isOver = false;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        chessBoard = new ChessBoard();
        chessBoard.initBoard();
        chessBoard.printBoard();
        System.out.println("请输入黑棋坐标:");
        String str;
        while((str = br.readLine()) != null){
            if(!isValid(str)){
                //System.out.println("is here 1");
                continue;
            }
            //System.out.println("djlfdf");
            String chess = ChessMan.BLOCK.getS();
            chessBoard.setChessBoard(posX,posY,ChessMan.BLOCK.getS());
            if(isWon(posX,posY,ChessMan.BLOCK.getS()))isOver = true;
            //if(isPeace())isOver = true;
            else {
                //System.out.println("djlfdf");
                chess = ChessMan.WHITE.getS();
                int[] s = computerDo();
                //System.out.println("djlfdf");
                chessBoard.setChessBoard(s[0], s[1], ChessMan.WHITE.getS());
                //System.out.println("djlfdf");
                if (isWon(s[0], s[1], ChessMan.WHITE.getS())) isOver = true;
                if (isPeace()) {
                    isOver = true;
                    chess = null;
                }
                //System.out.println("djlfdf");
            }
            //System.out.println("djlfdf");
            if(isOver){
                    if(isReplay(chess)){
                        isOver = false;
                        chessBoard.initBoard();
                        chessBoard.printBoard();
                        continue;
                    }
                    else break;
                }
                chessBoard.printBoard();
                System.out.println("请输入黑棋坐标:");
        }
    }

    public static void main(String[] args) {
        GobangGame g = new GobangGame();
        try{
            g.game();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值