数据结构与算法Java版---简易五子棋(包含存档、读档功能)

本系列博客源自学习《尚硅谷数据结构与算法》的学习随笔。

在这里插入图片描述

任务描述:在这里插入图片描述
思路分析:
在这里插入图片描述
在这里插入图片描述
实现代码:

package test.learn.algorithm;

import java.io.*;
import java.util.Scanner;

import static javafx.application.Platform.exit;

public class Update_sparseArry {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int [][] arry ;
        System.out.println("游戏开始!你可以选择以下三种模式:");
        System.out.println("1.新建游戏");
        System.out.println("2.读档游戏");
        System.out.println("3.退出");
        int num = sc.nextInt();
        switch (num){
            case 1:
                arry = creatArry();
                arry = work(arry);
                ask(arry);
                break;
            case 2:
                arry = read();
                arry = work(arry);
                ask(arry);
                break;
            case 3:
                System.out.println("退出游戏!");
                exit();
                break;
            default:
                exit();
                break;
        }


    }
    public static void ask(int [][] arry) throws IOException {
        Scanner sc = new Scanner(System.in);

        System.out.println("游戏结束,请问是否存档?1是0否:");
        int choice = sc.nextInt();
        if(choice==1){
            System.out.println("现在开始存档");
            write(arry);
        }
    }
    public static int[][] creatArry(){
        //创建一个原始二维数组,大小为11*11
        int[][] arry = new int[11][11];
        //对二维数组部分赋值
//        arry[1][3] = 1;//1代表黑子
//        arry[2][4] = 2;//2代表蓝子
//        arry[3][5] = 2;//2代表蓝子
        System.out.println("棋盘已创建!");
        return arry;
    }
    public static  void disPlay(int [][] arry){
        for (int[] row : arry) {
            for (int data : row) {
                System.out.print(data+" ");
            }
            System.out.println();
        }
    }
    public static int [][] work(int [][] arry){
        System.out.println("请开始下棋(输入下的行列以及棋子):");
        Scanner sc = new Scanner(System.in);
        int value;
        do{
            System.out.println("请输入行:");
            int row = sc.nextInt();
            System.out.println("请输入列:");
            int col = sc.nextInt();
            System.out.println("请输入棋子(1代表黑子 2代表蓝子 -1代表下棋结束:");
            value = sc.nextInt();
            if(value!=-1)
                arry[row][col] = value;
        }while(value!=-1);
        disPlay(arry);
        return arry;
    }
    public static  void write(int [][] arry) throws IOException {
        //计算二维数组的有效数据的个数
        int count=0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (arry[i][j] != 0)
                    count++;
            }
        }
        //转换成稀疏矩阵
        int[][] arry1 = new int[count + 1][3];
        arry1[0][0] = arry1[0][1] = 11;
        arry1[0][2] = count;
        int num = 1;
        //给稀疏数组赋值
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (arry[i][j] != 0) {
                    arry1[num][0] = i;
                    arry1[num][1] = j;
                    arry1[num][2] = arry[i][j];
                    num++;
                }
            }
        }
        //将其写入文档
        File file = new File("E:\\Idea_File\\1.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file);
        for (int i = 0; i < count + 1; i++) {
            for (int j = 0; j < 3; j++) {
                fw.write(arry1[i][j]+"\t");
            }
            fw.write("\n");

        }
        fw.close();//写入结束
        System.out.println("存档结束!");
    }

    public static int[][] read() throws IOException {
        System.out.println("开始读档.....");
        File file = new File("E:\\Idea_File\\1.txt");
        if(!file.exists()){
            System.out.println("未找到文件!");
        }
        Reader r = new FileReader(file);
        int [][] arry = new int[12][3];//问题所在!
        BufferedReader br = new BufferedReader(r);
            int count = 0;
            String s;
            while((s = br.readLine())!= null) {
                String[] split = s.split("\t");
                for(int i = 0;i<split.length;i++) {
                    arry[count][i] = Integer.parseInt(split[i]);
                }
                count++;
            }
        br.close();
        System.out.println("读档结束!打印读档结果:");
        //2.将稀疏矩阵转换为二维数组
        int row = arry[0][0];//得到二维数组的行数
        int col = arry[0][1];//得到二维数组的列数
        //初始化二维数组
        int[][] arry2 = new int[row][col];
        //遍历稀疏矩阵并为二维数组arry2赋值
        for (int i = 1; i < count; i++) {
            int row1 = arry[i][0];//得到行值
            int col1 = arry[i][1];//得到列值
            int value = arry[i][2];//得到元素值
            arry2[row1][col1] = value;
        }
        //打印新二维数组
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(arry2[i][j] + " ");
            }
            System.out.println();
        }
        return arry2;

    }

}

结果展示:
在这里插入图片描述
在这里插入图片描述
代码交流:
有问题请留言。定期回复!

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值