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

📄 saveandload.java

📁 扫雷完整的程序
💻 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());
		access.writeBoolean(model.isMenuMarkSelected());
		access.writeInt(model.getMarkedMineNumber());
		access.writeInt(model.getTime());

		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());
			}

		existBegin = true;
	}

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

		int width = access.readInt();
		int height = access.readInt();
		int mineNumber = access.readInt();
		int type = access.readInt();
		
		boolean zz = access.readBoolean();
System.out.println("asdf"+zz);
		model.setMenuMarkSelected(zz);
		
		model.restart(width, height, mineNumber, type);
		//let the first click without the mine and number sitting
		model.setFirstClicking(false);
				
		int markedMineNumber = access.readInt();
		model.setMarkedMineNumber(markedMineNumber);		
		model.setTime(access.readInt());
		Location location;
		Location gameOverLocation = null;
		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) {
					if (model.isMine(location)) {//to sure save the game over
												 // case rightly
						gameOverLocation = location;
					} else {
						model.setClickedButton(location);
					}
				}
			}
		model.setMarkedMineNumber(markedMineNumber);
		if (gameOverLocation != null) {
			model.setClickedButton(gameOverLocation);
		}

	}

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

	public boolean loadIsMenuMarkSelected() throws IOException {
		if (!existBegin)
			throw new IOException();
		access.seek(TYPE_LOCATION);
		for (int i = 0; i != 4; i++) {
			access.readInt();
		}		
		return access.readBoolean();		
	}

	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());
		access.writeBoolean(model.isMenuMarkSelected());
	}

	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 = 300;

	private final int RECORD_LOCATION = 40;

	private boolean existBegin;

	RandomAccessFile access;

	Model model;

	ModelCellProperty[][] matrix;
}

⌨️ 快捷键说明

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