Java语言程序设计第8章8.20四子连

import java.util.Scanner;
public class fourPieces {
    public static void main(String[] args) {
        char[][] checkerboard = initial();
        int[] record = new int[7];
        show(checkerboard);
        while (isInArray(checkerboard, ' ')) {
            input(checkerboard, record, "red");
            input(checkerboard, record, "yellow");
        }
    }

    public static void input(char[][] checkerboard, int[] record, String color) {
        Scanner input = new Scanner(System.in);
        System.out.print("Drop a " + color + " disk at column (0-6): ");
        int n = input.nextInt();
        while (n > 6 || record[n] > 5 ) {
            System.out.print("It's out of range. please enter another number: ");
            n = input.nextInt();
        }
        char letter = color.equals("red") ? 'R' : 'Y';
        checkerboard[5 - record[n]][n] = letter;
        record[n]++;
        show(checkerboard);
        if (isConsecutiveFour(checkerboard, letter)) {
            System.out.println("The " + color + " player won");
            System.exit(0);
        }
        if (!isInArray(checkerboard, ' ')) {
            System.out.println("draw");
            System.exit(0);
        }
    }

    public static boolean isConsecutiveFour(char[][] checkerboard, char ch) {
        for (int i = 0; i < checkerboard.length; i++)
            for (int j = 0; j < checkerboard[i].length; j++)
                if (row(checkerboard, i, j, ch) || column(checkerboard, i, j, ch) ||
                        oblique(checkerboard, i, j, ch) || oblique2(checkerboard, i, j, ch))
                    return true;
        return false;
    }

    public static boolean row(char[][] checkerboard, int i, int j, char ch) {
        for (int count = 0; count < 4; count++)
            if (j + count + 1 > checkerboard[i].length || checkerboard[i][j + count] != ch)
                return false;
        return true;
    }

    public static boolean column(char[][] checkerboard, int i, int j, char ch) {
        for (int count = 0; count < 4; count++)
            if (i + count + 1 > checkerboard.length || checkerboard[i + count][j] != ch)
                return false;
        return true;
    }

    public static boolean oblique(char[][] checkerboard, int i, int j, char ch) {
        for (int count = 0; count < 4; count++)
            if (i + count + 1 > checkerboard.length || j + count + 1 > checkerboard[i].length ||
                    checkerboard[i + count][j + count] != ch)
                return false;
        return true;
    }

    public static boolean oblique2(char[][] checkerboard, int i, int j, char ch) {
        for (int count = 0; count < 4; count++)
            if (i - count < 0 || j + count + 1 > checkerboard[i].length ||
                    checkerboard[i - count][j + count] != ch)
                return false;
        return true;
    }

    public static void show(char[][] checkerboard) {
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print("|");
                System.out.print(checkerboard[i][j]);
            }
            System.out.println("|");
        }
        System.out.println("---------------");
    }

    public static char[][] initial() {
        char[][] checkerboard = new char[6][7];
        for (int i = 0; i < checkerboard.length; i++)
            for (int j = 0; j < checkerboard[0].length; j++)
                checkerboard[i][j] = ' ';
        return checkerboard;
    }

    public static boolean isInArray(char[][] checkerboard, char ch) {
        for (char[] cArray: checkerboard)
            for (char c: cArray)
                if (c == ch)
                    return true;
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋凉78

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值