Java implements simple checkerboard archiving and reading functions

Recently, the archiving of playing Gobang has been studied. The main implementation is to save the Gobang chessboard of the residual game to the local file. When you need to read the game, you can obtain it from the local file and display the original situation of the residual game.

Main ideas

As shown in the above figure, the first table is an 11 * 11 chess game, which can be converted into a two-dimensional array of 11 rows and 11 columns. 1 represents sunspots and 2 represents blueseeds, which can be converted into a two-dimensional array shown in the second table. When saving, considering that most of 0 in the two-dimensional array is unoccupied space, I convert the two-dimensional array into a sparse array with n rows and 3 columns, compress the occupied space and save it to a local file. The first row of the sparse array, row represents 11 rows, col represents 11 columns, and val represents the number of valid data except 0. As shown in the above figure, the number of valid data is 2. Starting from the second row, it indicates the specific positions of sunspots and bluens. For example, the above figure (1, 2, 1) indicates that there is a sunspot in the third column of the second row (the index starts from 0), and (2, 3, 2) indicates that there is a bluen in the fourth column of the third row.

The main idea of reading function is to read out the sparse matrix stored in the text and convert the read data into two-dimensional array, so as to achieve the function of displaying the last chess game.

Main code snippet

package cn.mrlij.array;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.Scanner;

/***
 * 实现棋盘的存档
 * @author dreamer
 *
 */
public class ArrTest {
 public static int[][] newArr(){
 //1.创建一个带棋盘的二维数组
 int arr[][] = new int[11][11];
 //1 => 黑色 2 => 蓝色
 arr[1][2] = 1;
 arr[2][3] = 2;
 System.out.println("原始二维数组~~~");
 for(int[] row : arr) {
  for(int data :row) {
  System.out.printf("%d\t",data);
  }
  System.out.println();
 }
 return arr;
 }
 public static void toXs(int[][] arr) throws IOException{
 //2.转换稀疏数组
 /**
  *  row col val
  * 0 11 11 2
  */
 //统计二维数组中有效数据的个数,遍历二维数组
 int sum = 0;
 for(int i = 0;i<11;i++) {
  for(int j = 0;j<11; j++) {
  if(arr[i][j]!=0) {
   sum++;
  }
  }
 }
 System.out.println("有效数据的个数是====>>>>"+sum);
 //根据二维数组创建二维稀疏数组
 int xsarr[] [] = new int[sum+1][3];
 //给稀疏数组赋值,初始化第一行数据
 xsarr[0][0] = 11;
 xsarr[0][1] = 11;
 xsarr[0][2] = sum;

 //给余下的行赋值
 int count = 0;
 for(int i = 0; i<11;i++) {
  for(int j = 0; j<11;j++) {
  if(arr[i][j]!=0) {
   count++;
   xsarr[count][0] = i;
   xsarr[count][1] = j;
   xsarr[count][2] = arr[i][j];
  }
  }
 }
 //遍历稀疏数组
 System.out.println("稀疏数组是~~~~~");
 for(int i = 0;i<xsarr.length;i++) {
  for(int j = 0;j<3;j++) {
  System.out.printf("%d\t",xsarr[i][j]);
  }
  System.out.println();
 }
 File f = new File("D:/save.txt");
 if(f.exists()) {
  f.createNewFile();
 }
 FileWriter fw =new FileWriter(f);
 for(int i = 0;i<xsarr.length;i++) {
  for(int j = 0;j<3;j++) {
  fw.write(xsarr[i][j]+"\t");
  }
  fw.write("\r\n");
 }
 fw.close();
 }
 public static int[][] getXs() throws IOException{
 File f = new File("D:/save.txt");
 Reader r = new FileReader(f);
 int [][] xsarr = new int[3][3];
 BufferedReader br = new BufferedReader(r);
 if(f.exists()) {

  int flag = 0;
  String s;
  while((s = br.readLine())!=null) {
  String[] split = s.split("\t");
  for(int i = 0;i<split.length;i++) {
   xsarr[flag][i] = Integer.parseInt(split[i]);
  }
  flag++;
  }
 }
 br.close();

 //3.将稀疏数组转换二维数组
 //创建二维数组(行数为11,列数为11)
 int arr2 [][] = new int[11][11];
 //从稀疏数组的第二行开始遍历;
 for(int i = 1; i<xsarr.length;i++) {
  //将遍历的每个值赋值给二维数组
  arr2[xsarr[i][0]][xsarr[i][1]] = xsarr[i][2];
 }
 //转换之后的二维数组是。。
 System.out.println("转换的二维数组。。");
 for(int[] row : arr2) {
  for(int data :row) {
  System.out.printf("%d\t",data);
  }
  System.out.println();
 }
 return arr2;
 }
public static void main(String[] args) throws IOException {
 Scanner sc = new Scanner(system.in);
 while(true) {
 System.out.println("1.创建11*11的棋盘并存档");
 System.out.println("2.读取上次存档");
 System.out.println("3.退出");
 int r = sc.nextInt();
 switch(r) {
 case 1: int arr[][] = newArr();
  System.out.println("棋盘已创建!开始存档!");
  toXs(arr);
  System.out.println("存档完成!");
  break;
 case 2: getXs();
  break;
 case 3: System.out.println("退出!");
  break;
  default:
  break;
  }

 }
}
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>