Java二维数组实现五子棋

五子棋是我们生活中一个经常玩的小游戏,游戏分为黑棋、白棋,游戏开始时,黑棋先落子,白棋随后落子,若某种颜色的棋子在横向、竖向、上斜向(坐标轴中y=x)、下斜向(坐标轴中y=-x)的某个方向形成五个相同的棋子,则这个颜色获胜,游戏结束。那么如何判断某个棋子在某个方向形成五个相同的棋子呢?定义初始化棋盘为0,黑棋为1,白棋为2,假设某点上现在为黑棋,则这个点的数字从0变为1,那么分别比较四个方向的棋子,若数字相同则继续向这个方向比较,若count>=5,则游戏结束。

import java.util.Scanner;
public class Wuziqi {
    public static void main(String[] args) {
        int[][] games = new int[16][16];
        int color = 2;//黑棋为1,白棋为2,黑棋先行,color先为白棋的数
        Scanner scanner = new Scanner(System.in);
        int row = 0;//定义行
        int col = 0;//定义列
        do {
            showGames(games);//每次记录落子完后的棋盘
            color = 3-color;//交换棋子
            System.out.println("请选择落子行");
            row = scanner.nextInt();
            System.out.println("请选择落子列");
            col = scanner.nextInt();
            if(games[row][col] ==0) {
                games[row][col] = color;//落子
            } else {
                color = 3-color;//有子的时候 需要从新下 先改变颜色 进入循环后 变为本身颜色
                System.out.println("这里有子,请重下");
            }
        }while(!isSuccess(games,color,row,col));
        System.out.println("恭喜" + color + "赢了");
    }
    //展现棋盘
    public static void showGames(int[][] games) {
        for(int i = 0; i<games.length; i++) {
            for(int j = 0; j<games[i].length; j++) {
                System.out.print(games[i][j]);
            }
            System.out.println();
        }
    }
    //横向比较
    public static boolean isHeng(int[][] games, int color, int row, int col) {
        int count = 1;
        int tempCol = col;//循环结束后不改变col的值
        while(tempCol>0 && games[row][tempCol-1] == color) {
            count++;
            tempCol = tempCol-1;
        }
        tempCol = col;
        while(tempCol < games[row].length-1 && games[row][tempCol+1] == color) {
            count++;
            tempCol = tempCol+1;
        }
        return count >=5;//同样的颜色大于等于5就赢
    }
    //竖向比较
    public static boolean isShu(int[][] games, int color, int row, int col) {
        int tempRow = row;//循环结束后不改变row的值
        int count = 1;
        while(tempRow >0 && games[tempRow-1][col] == color) {
            count++;
            tempRow = tempRow-1;
        }
        tempRow = row;
        while(tempRow < games.length-1 && games[tempRow+1][col] == color) {
            count++;
            tempRow = tempRow+1;
        }
        return count >=5;
    }
    //Y=X比较
    public static boolean isShangxie(int[][] games, int color, int row, int col){
        int tempRow = row;
        int tempCol = col;
        int count = 1;
        while (tempRow >0 && tempCol < games.length-1 && games[tempRow-1][col+1] == color ){
            count++;
            tempRow = tempRow -1;
            tempCol = tempCol +1;
        }
        tempRow = row;
        tempCol = col;
        while (tempRow <games.length-1 && tempCol >0 && games[tempRow+1][col-1] == color){
            count++;
            tempRow = tempRow +1;
            tempCol = tempCol -1;
        }
        return count >=5;
    }
    //Y=-X比较
    public static boolean isXiaxie(int[][] games, int color, int row, int col){
        int tempRow = row;
        int tempCol = col;
        int count = 1;
        while(tempRow < games.length-1 && tempCol < games.length-1 && games[row+1][col+1]==color){
            count++;
            tempRow = tempRow +1;
            tempCol = tempCol +1;
        }
        tempRow = row;
        tempCol = col;
        while(tempRow > 0 && tempCol > 0 && games[row-1][col-1]==color){
            count++;
            tempRow = tempRow -1;
            tempCol = tempCol -1;
        }
        return count >=5;
    }
    //判断输赢
    public static boolean isSuccess(int[][] games, int color, int row, int col) {
        boolean flag = false;
        flag = flag || isHeng(games,color,row,col);
        flag = flag || isShu(games,color,row,col);
        flag = flag || isShangxie(games,color,row,col);
        flag = flag || isXiaxie(games, color, row, col);
        return flag;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值