⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saveandload.java

📁 扫雷的源代码,包结构采用经典的MVC模式,包括模型、控制、视图三部分
💻 JAVA
字号:
/*
 * Created on 2005-5-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package model;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author mqqqvpppm
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class SaveAndLoad {

	public SaveAndLoad(Model model) throws IOException {
		super();
		this.model = model;
		existBegin = true;
		File file = new File("save.dat");
		if(!file.exists()) existBegin = false;
		access = new RandomAccessFile(file, "rw");
	}

	public void save() throws IOException {
		//		access = new RandomAccessFile("save.dat","rw");
		access.seek(MODEL_LOCATION);
		matrix = model.getMatrix();
		access.writeInt(model.getWidth());
		access.writeInt(model.getHeight());
		access.writeInt(model.getMineNumber());
		access.writeInt(model.getType());

		int x = 0;
		int y = 0;
		int width = model.getWidth();
		int height = model.getHeight();
		ModelCellProperty cell;
		for (x = 0; x != width; x++)
			for (y = 0; y != height; y++) {
				cell = matrix[y][x];
				access.writeBoolean(cell.isClickedButton());
				access.writeBoolean(cell.isMark());
				access.writeBoolean(cell.isMarkedMine());
				access.writeBoolean(cell.isMine());
				access.writeBoolean(cell.isNumber());
				access.writeInt(cell.getValue());
			}

		access.writeInt(model.getMarkedMineNumber());
		access.writeInt(model.getTime());
		existBegin = true;
	}

	public void load() throws IOException {
		//		access = new RandomAccessFile("save.dat","r");
		if(!existBegin) throw new IOException();
		access.seek(MODEL_LOCATION);

		int width = access.readInt();
		int height = access.readInt();
		int mineNumber = access.readInt();
		int type = access.readInt();
		model.restart(width, height, mineNumber, type);

		Location location;
		boolean isClicked;

		matrix = model.getMatrix();
		int x = 0;
		int y = 0;
		for (x = 0; x != width; x++)
			for (y = 0; y != height; y++) {
				location = new Location(x, y);
				isClicked = access.readBoolean();

				model.setMark(access.readBoolean(), location);
				model.setMarkedMine(access.readBoolean(), location);
				matrix[y][x].setMine(access.readBoolean());
				matrix[y][x].setNumber(access.readBoolean());
				matrix[y][x].setValue(access.readInt());

				if (isClicked) {
					model.setClickedButton(location);
				}
			}
		model.setMarkedMineNumber(access.readInt());
		model.setTime(access.readInt());
	}

	public int[] loadType() throws IOException {
		if(!existBegin) throw new IOException();
		access.seek(TYPE_LOCATION);
		int[] ret = new int[4];
		ret[INDEX_WIDTH] = access.readInt();
		ret[INDEX_HEIGHT] = access.readInt();
		ret[INDEX_MINENUMBER] = access.readInt();
		ret[INDEX_TYPE] = access.readInt();
		return ret;
	}

	public void saveType() throws IOException {
		access.seek(TYPE_LOCATION);
		access.writeInt(model.getWidth());
		access.writeInt(model.getHeight());
		access.writeInt(model.getMineNumber());
		access.writeInt(model.getType());
	}

	public void loadRecord(String[] names, int[] score) throws IOException {
		if(!existBegin) throw new IOException();
		access.seek(RECORD_LOCATION);
		int i;
		int h;
		int length = names.length;
		char[] name = new char[30];
		int nameLength;
	
		for (i = 0; i != length; i++) {
			
			nameLength = access.readInt();
			if (nameLength > 30)
				return;
			for (h = 0; h != nameLength; h++) {
				name[h] = access.readChar();
			}
			
			names[i] = String.valueOf(name, 0, nameLength);
		}

		for (i = 0; i != length; i++) {
			score[i] = access.readInt();
		}
	}

	public void saveRecord(String[] names, int[] score) throws IOException {
		access.seek(RECORD_LOCATION);
		int i;
		int length = names.length;
		for (i = 0; i != length; i++) {
			if(names[i]==null){
				access.writeInt(11);
				access.writeChars("unknow name");
				continue;
			}
			access.writeInt(names[i].length());
			access.writeChars(names[i]);
		}
		for (i = 0; i != length; i++) {
			access.writeInt(score[i]);
		}
	}

	public void closeStream() throws IOException {
		access.close();
	}

	public static final int INDEX_WIDTH = 0;

	public static final int INDEX_HEIGHT = 1;

	public static final int INDEX_MINENUMBER = 2;

	public static final int INDEX_TYPE = 3;

	private final int TYPE_LOCATION = 0;

	private final int MODEL_LOCATION = 220;

	private final int RECORD_LOCATION = 20;
	
	private boolean existBegin;

	RandomAccessFile access;

	Model model;

	ModelCellProperty[][] matrix;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -