简单五子棋实现

一、定义一个对手Rival类

package com.itwang.gonbang;

public class Rival {
    private String color;//颜色
    private String nickName;//昵称

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

二、定义一个棋盘ChessBoard类

package com.itwang.gonbang;

/**
 * 五子棋棋牌
 */
public class ChessBoard {
    //五子棋的宽度
    public static final Integer BOARD_SIZE = 8;
    public String[][] board;


    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.println();
        }
    }

}

三、定义一个主类Gobang

package com.itwang.gonbang;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * 五子棋的关键算法实现
 */
public class Gobang {
    //棋盘
    private static ChessBoard chessBoard;
    private static Rival rival;//对手1
    private static Rival rival2;//对手2
    private int totalCount;//统计步数

    /**
     * 判断输赢
     * @param chessBoard
     * @param xPost
     * @param yPost
     * @param color
     * @return
     */
    public static boolean isWin(ChessBoard chessBoard,int xPost,int yPost,String color){
        //记录当前连接的点数
        int count = 1;
        int currXPost = 0;
        int currYPost = 0;
        /**
         * 判断水平方向上的胜负
         * 先从xPost左边遍历,然后从xPost右边遍历判断有连续的点
         */
        //向左
        for (currYPost = yPost - 1;currYPost >= 0;currYPost --){
            if ((chessBoard.board[xPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }

        //向右
        for (currYPost = yPost + 1;currYPost < ChessBoard.BOARD_SIZE; currYPost ++){
            if ((chessBoard.board[xPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }
        count = 1;

        /**
         * 判断垂直方向上的胜负
         */
        //向上遍历
        for(currXPost = xPost -1;currXPost >= 0; currXPost --){
            if ((chessBoard.board[currXPost][yPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }
        //向下边遍历
        for (currXPost = xPost + 1;currXPost < ChessBoard.BOARD_SIZE; currXPost ++){
            if ((chessBoard.board[currXPost][yPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }

        count = 1;
        /**
         * 左上到右下遍历
         */
        //左上
        for (currXPost = xPost - 1,currYPost = yPost - 1;currXPost >= 0 && currYPost >= 0;currXPost--,currYPost--){
            if ((chessBoard.board[currXPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }
        //右下
        for (currXPost = xPost + 1,currYPost = yPost + 1;
             currXPost < ChessBoard.BOARD_SIZE && currYPost < ChessBoard.BOARD_SIZE;currXPost++,currYPost++){
            if ((chessBoard.board[currXPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }

        /**
         * 从坐下到右上遍历
         */
        count = 1;
        //左下
        for (currXPost = xPost + 1,currYPost = yPost - 1;
             currXPost < ChessBoard.BOARD_SIZE && currYPost >= 0;currXPost++,currYPost++){
            if ((chessBoard.board[currXPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }
        //右上
        for (currXPost = xPost - 1,currYPost = yPost + 1;
             currXPost >= 0 && currYPost < ChessBoard.BOARD_SIZE;currXPost--,currYPost++){
            if ((chessBoard.board[currXPost][currYPost]).equals(color)){
                count++;
                if (count >= 5){
                    return true;
                }
            }else {
                break;
            }
        }
        return false;
    }

    /**
     * 判断下棋的位置是否有子
     * @param chessBoard
     * @param xPost
     * @param yPost
     * @param color1
     * @param color2
     * @return
     */
    public static boolean isRepeat(ChessBoard chessBoard,int xPost,int yPost,String color1,String color2){
        if ((chessBoard.board[xPost][yPost]).equals(color1) || (chessBoard.board[xPost][yPost]).equals(color2)){
            return false;
        }
        return true;
    }
    public static void main(String[] args) throws IOException {
        chessBoard = new ChessBoard();
        rival2 = new Rival();
        rival = new Rival();
        chessBoard.initBoard();
        chessBoard.printBoard();
        rival.setColor("*");
        rival.setNickName("红方");
        rival2.setColor("=");
        rival2.setNickName("黑方");
        //获取键盘输入,每当在键盘上输入一行内容按回车键,刚输入的内容将被读取到
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String inputStr = null;
        /*System.out.println("红方(1)先下?还是黑方(2)先下");
        Scanner sc = new Scanner(System.in);
        int first = sc.nextInt();*/
        boolean flag = true;
        if (flag){
            System.out.println("请" + rival.getNickName() + "输入坐标: ");
        }else {
            System.out.println("请" + rival2.getNickName() + "输入坐标: ");
        }
        while((inputStr = bufferedReader.readLine()) != null){
            //将用户输入的字符以逗号作为分隔符
            String[] splitStr = inputStr.split(",");
            //将2个字符串转化为用户棋下的坐标
            int xPost = Integer.parseInt(splitStr[0]);
            int yPost = Integer.parseInt(splitStr[1]);

            //把对应的数组元素赋值为'*';这里按照横坐标为x轴,纵坐标为y轴,由于数组与坐标相反
            if (flag){
                if (isRepeat(chessBoard,yPost-1,xPost-1,rival.getColor(),rival2.getColor())){
                    chessBoard.board[yPost - 1][xPost - 1] = rival.getColor();
                    chessBoard.printBoard();
                    flag = false;
                    /**
                     * 判断输赢
                     */
                    if (isWin(chessBoard,yPost - 1,xPost - 1,rival.getColor())){
                        System.out.println(rival.getNickName() + "胜利");
                        break;
                    }
                }else {
                    System.out.println("该地方已经有棋子了,请重试");
                }

            }else{
                if (isRepeat(chessBoard,yPost-1,xPost-1,rival.getColor(),rival2.getColor())){
                    chessBoard.board[yPost - 1][xPost - 1] = rival2.getColor();
                    chessBoard.printBoard();
                    flag = true;
                    /**
                     * 判断输赢
                     */
                    if (isWin(chessBoard,yPost - 1,xPost - 1,rival2.getColor())){
                        System.out.println(rival2.getNickName() + "胜利");
                        break;
                    }
                }else {
                    System.out.println("该地方已经有棋子了,请重试");
                }

            }
            if (flag){
                System.out.println("请" + rival.getNickName() + "输入坐标: ");
            }else {
                System.out.println("请" + rival2.getNickName() + "输入坐标: ");
            }
        }
    }
}
原文地址:https://www.cnblogs.com/ya-qiang/p/9434068.html