栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号