五子棋

本文详细介绍了使用Java实现五子棋游戏的过程,包括棋盘的初始化、棋子的输入、胜负判断等关键环节,通过具体代码展示了游戏逻辑的实现。
package fiveChess;
import java.util.Scanner;
public class FiveChess {
    public static final int  LENGTH=16;
    public static  String[][] chess=new String[LENGTH][LENGTH];
    public static Scanner sc = new Scanner(System.in);
    //打印棋盘
    public static void printChess() {
        //打印横坐标
        for (int k = 0; k < chess.length; k++) {
            if (k == 0) {
                System.out.print("  ");
            } else if (k < 10 && k > 0) {
                System.out.print(k+" ");
            }else{
                System.out.print(k);
            }
        }
        System.out.println();
        for(int i=1;i<chess.length;i++){
            //打印纵坐标
            if (i == 0) {
                System.out.print(" ");
            } else if (i < 10 && i > 0) {
                System.out.print(i+" ");
            }else{
                System.out.print(i);
            }
            for (int j =1; j <chess.length; j++) {
                System.out.print(chess[i][j]);
            }
            System.out.println();
        }
    }
    //输入坐标
    public static void inputChess(int x,int y,int count,String str) {
        if (count % 2 == 0) {
            System.out.println("白棋请输入要下的坐标x和y");
            str="●";
        }
        else{
            System.out.println("黑棋请输入要下的坐标x和y");
            str="⭕";
        }
        x=sc.nextInt();
        y=sc.nextInt();
        if (x < 1 || x > 15 || y < 1 || y > 15) {
            System.out.println("超出棋盘范围,请重新输入坐标 1<=x,y<=15");
        }
        else{
            if (!chess[x][y].equals("十")) {
                System.out.println("此处已被占用,请重新输入坐标1<=x,y<=15");

            }
            else {
                chess[x][y]=str;
                printChess();
                if (CheckWin(x,y)) {
                    System.out.println("胜利");
                    return ;
                }
            }
        }
    }
    //判断胜负
    public static boolean CheckWin(int xIndex, int yIndex) {
        int max = 0;
        int tempXIndex = xIndex;
        int tempYIndex = yIndex;
        // 三维数组记录横向,纵向,左斜,右斜的移动
        int[][][] dir = new int[][][] {
                // 竖着
                { { -1, 0 }, { 1, 0 } },
                // 横向
                { { 0, -1 }, { 0, 1 } },
                // 左斜
                { { -1, -1 }, { 1, 1 } },
                // 右斜
                { { 1, -1 }, { -1, 1 } } };

        for (int i = 0; i < 4; i++) {
            int count = 1;
            //j为0,1分别为棋子的两边方向,比如对于横向的时候,j=0,表示下棋位子的左边,j=1的时候表示右边
            for (int j = 0; j < 2; j++) {
                boolean flag = true;
                /**
                 while语句中为一直向某一个方向遍历
                 有相同颜色的棋子的时候,Count++
                 否则置flag为false,结束该该方向的遍历
                 **/
                while (flag) {
                    tempXIndex = tempXIndex + dir[i][j][0];
                    tempYIndex = tempYIndex + dir[i][j][1];

                    //这里加上棋盘大小的判断,这里我设置的棋盘大小为20 具体可根据实际情况设置 防止越界
                    if (tempXIndex >= 1 && tempXIndex < chess.length && tempYIndex >= 1 && tempYIndex <chess.length) {
                        if ((chess[tempXIndex][tempYIndex] == chess[xIndex][yIndex])) {
                            count++;
                            //System.out.println(count);
                        }
                        else
                            flag = false;
                    }else{
                        flag = false;
                    }

                }
                tempXIndex = xIndex;
                tempYIndex = yIndex;
            }
            if (count >= 5) {
                max = 1;
                break;
            } else
                max = 0;
        }
        if (max == 1)
            return true;
        else
            return false;
    }

    public static void main(String[] args) {

        for(int i=1;i<chess.length;i++){
            for (int j =1; j <chess.length; j++) {
                chess[i][j]="十";
            }
        }
        int x=0;
        int y=0;
        //记录下棋次数
        int count=0;
        String str="";
        System.out.println("下棋开始");
        printChess();
        while (true) {
            inputChess(x,y,count,str);
            count++;
        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值