稀疏数组的应用---五子棋存盘退出和续上盘

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了稀疏数组的应用---五子棋存盘退出和续上盘脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、稀疏数组

稀疏数组的应用---五子棋存盘退出和续上盘

当我们使用二维数组记录棋盘时,会出现很多默认值0,因此会记录很多没有意义的数据,使用稀疏数组会避免这一点

稀疏数组的处理方法是:

  1. 记录数组一共有几行几列,有多少个不同的值。
  2. 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模。

稀疏数组的应用---五子棋存盘退出和续上盘

如上图所示,稀疏数组第一行分别记录原始二维数组的行数、列数和非零元素个数。从第二行开始记录每个非零元素的行数、列数和值。

二、稀疏数组应用实例-->五子棋

2.1 五子棋程序中有存盘退出和续上盘的功能

稀疏数组的应用---五子棋存盘退出和续上盘

2.2 功能实现

package sparse_array;
import java.io.*;

public class SparseArray {
    public static void main(String[] args) {
        //创建一个原始的二维数组11*11
        //0:无子  1:黑子    2:白子
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;

        //输出原始的二维数组
        System.out.PRintln("原始的二维数组:");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%dt", data);
            }
            System.out.println();
        }
        System.out.println();

        //将二维数组-->稀疏数组
        //sum记录非零数字的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("非零数据个数为:");
        System.out.println(sum);

        //创建对应的稀疏数组
        int[][] sparseArr1 = new int[sum + 1][3];
        //给稀疏数组赋值
        sparseArr1[0][0] = 11;
        sparseArr1[0][1] = 11;
        sparseArr1[0][2] = sum;

        //遍历二维数组,将非零的值存放到稀疏数组中
        int count = 0;//用于记录是第几个非零数据
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr1[count][0] = i;
                    sparseArr1[count][1] = j;
                    sparseArr1[count][2] = chessArr1[i][j];
                }
            }
        }
        System.out.println();
        System.out.println("得到的稀疏数组为:");
        for (int[] ints : sparseArr1) {
            System.out.printf("%dt%dt%dn", ints[0], ints[1], ints[2]);
        }
        System.out.println();

        //将稀疏数组导入到sparseArr.txt
        File chessFile = new File("D:\Study\Java_Study\DataStructures\src\sparse_array\SparseArray.txt");
        BufferedWrITer bw = null;

        try {
            bw = new BufferedWriter(new FileWriter(chessFile));

            for (int[] ints : sparseArr1) {
                bw.write(ints[0] + "t" + ints[1] + "t" + ints[2] + "n");
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("数据已写入sparseArray.txt");
        System.out.println();

        //读取sparseArray.txt中的数据
        //将读取到的数据转为稀疏数组
        System.out.println("从sparseArray.txt中读取的数据为:");
        int lineCount = 0;
        String[][] sparseArr2 = new String[11 * 11 + 1][3];
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(chessFile));
            String line;
            while ((line = br.readLine()) != null) {
                lineCount++;
                sparseArr2[lineCount - 1] = line.split("t");
                System.out.println(sparseArr2[lineCount - 1][0] + "t" + sparseArr2[lineCount - 1][1] + "t" + sparseArr2[lineCount - 1][2]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println();

        //将上述稀疏数组-->二维数组
        //1.先读取稀疏数组第一行,根据第一行数据,创建原始的二维数组
        int[][] chessArr2 = new int[Integer.parseint(sparseArr2[0][0])][Integer.parseInt(sparseArr2[0][1])];
        //2.读取稀疏数组后几行的数据,并赋值给原始的二维数组
        for (int i = 1; i <= Integer.parseInt(sparseArr2[0][2]); i++) {
            chessArr2[Integer.parseInt(sparseArr2[i][0])][Integer.parseInt(sparseArr2[i][1])] = Integer.parseInt(sparseArr2[i][2]);
        }
        //恢复后的二维数组
        System.out.println("将读取的稀疏数组恢复为二维数组:");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%dt", data);
            }
            System.out.println();
        }
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的稀疏数组的应用---五子棋存盘退出和续上盘全部内容,希望文章能够帮你解决稀疏数组的应用---五子棋存盘退出和续上盘所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。