Java 编写de简单小游戏 -- 五子棋《Five in a Row》

五子棋《Five in a Row》

游戏规则:

游戏中黑棋用1代替,白棋用2表示,棋盘是16*16的大小。在横,竖,左斜以及右斜这四个方向上,只要有相同颜色的连续五个棋子,则获胜。

编写步骤:

(1)创建棋盘,并声明刚开始棋子的落子颜色
(2)从键盘上取出要落子的坐标,以及每次落子都要改变落子的颜色,1+2 = 3,落子颜色改变要在循环内,所以每次要在判断游戏结束之前改变颜色,应将棋子的颜色改为先走棋的相反颜色的棋(黑棋先走,所以应置刚开始棋子的颜色为白棋2)
(3)写判断棋子胜利的方法,将棋子赢有四种情况横,竖,左斜以及右斜,我们可以对每种情况都写出一种方法,最后通过棋子胜利的方法集体调用。
(4)对主方法的代码进行修改,选择合适的循环语句
(5)写打印棋盘的方法
(6)先对横方向上进行测试,完成之后。写判断落子位置有子的代码
(7)以上完成之后,将竖,左斜以及右斜的代码补完

代码实现:

import java.util.Scanner;

public class Game02 {
    public static void main(String[] args) {
        int arrays[][] = new int[16][16];
        int color = 2;
        Scanner sc = new Scanner(System.in);
        //再循环语句之外判断结束的条件,所以将坐标定义在循环体外
        int row = 0;
        int col = 0;
        //需要在有坐标之后判断,所以应先有坐标之后再判断,用do while语句,
        do{
            showGames(arrays);
            color = 3-color;
            System.out.println("请输入要落子位置的行坐标:");
            row = sc.nextInt();
            System.out.println("请输入要落子位置的列坐标:");
            col = sc.nextInt();
            if(arrays[row][col] != 0){
                System.out.println("这里有子,请重新落子:");
                //之前将子的颜色进行了改变,在这里再次进行改变。
                color = 3-color;
                continue;
            }
            arrays[row][col] = color;
        }while(!hasSuccess(arrays,color,row,col));
        showGames(arrays);
        System.out.println("恭喜" +color+ "获胜!");
    }

    public static void showGames(int[][] arrays){
        for(int i = 0;i<arrays.length;i++){
            for(int j = 0;j<arrays[i].length;j++){
                System.out.print(arrays[i][j] +" ");
            }
            System.out.println("");
        }
    }

//    public static boolean hacSuccess(int[][] games,int color,int row,int col) {
//        boolean flag = false;
//        flag = flag || hasHeng(games,color,row,col);
//        flag = flag || hasShu(games,color,row,col);
//        flag = flag || hasZuoXie(games,color,row,col);
//        flag = flag || hasYouXie(games,color,row,col);
//        return flag;
//    }
    public static boolean hasSuccess(int[][] arrays,int color,int row,int col){
        return isHeng(arrays,color,row,col) || isShu(arrays,color,row,col) || isZuoXie(arrays,color,row,col) || isYouXie(arrays,color,row,col);
    }
    //判断在某个方向上连续相同颜色的棋子有多少个?应输入的参数为:棋盘,棋子颜色,棋子的位置(坐标)
    public static boolean isHeng(int[][] arrays,int color,int row,int col){
        //判断棋子的左边之后,还要判断棋子的右边,所以应设一个标志位,将棋子的位置固定
        int tempCol = col;
        int count = 1;//棋子的数量
        while(tempCol>0 && arrays[row][tempCol-1]==color){
            tempCol = tempCol-1;
            count++;
        }
        tempCol = col;
        while(tempCol<arrays[row].length-1 && arrays[row][tempCol+1]==color){
            tempCol = tempCol+1;
            count++;
        }
        return count>=5;
    }
    public static boolean isShu(int[][] arrays,int color,int row,int col){
        int tempRow = row;
        int count = 1;
        while(tempRow>0 && arrays[tempRow-1][col]==color){
            tempRow = tempRow-1;
            count++;
        }
        tempRow = row;
        while (tempRow<arrays.length-1 && arrays[tempRow+1][col]==color){
            tempRow = tempRow+1;
            count++;
        }
        return count>=5;
    }
    public static boolean isZuoXie(int[][] arrays,int color,int row,int col){
        int tempRow = row;
        int tempCol = col;
        int count = 1;
        while(tempRow>0 && tempCol>0 && arrays[tempRow-1][tempCol-1] == color){
            tempRow = tempRow-1;
            tempCol = tempCol-1;
            count++;
        }
        tempRow = row;
        tempCol = col;
        while(tempRow<arrays.length && tempCol<arrays[row].length-1 && arrays[tempRow+1][tempCol+1] == color){
            tempRow = tempRow+1;
            tempCol = tempCol+1;
            count++;
        }
        return count>=5;
    }
    public static boolean isYouXie(int[][] arrays,int color,int row,int col){
        int tempRow = row;
        int tempCol = col;
        int count = 1;
        while(tempRow>0 && tempCol<arrays[row].length-1 && arrays[tempRow-1][tempCol+1] == color){
            tempRow = tempRow-1;
            tempCol = tempCol+1;
            count++;
        }
        tempRow = row;
        tempCol = col;
        while(tempRow<arrays.length-1 && tempCol>0 && arrays[tempRow+1][tempCol-1] == color){
            tempRow = tempRow+1;
            tempCol = tempCol-1;
            count++;
        }
        return count>=5;
    }
}

执行结果:

...
请输入要落子位置的行坐标:
4
请输入要落子位置的列坐标:
5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
请输入要落子位置的行坐标:
5
请输入要落子位置的列坐标:
5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
恭喜1获胜!

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值