sokfilesplitter.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 100 行

JAVA
100
字号
package net.sf.pim.game.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 将标准SOK关卡集合(可能带答案,拆解为游戏兼容单文件关卡)
 * 简化规则
 *    在...# ....和下一个空行这间为map
 *    在Solution....和下一个空行之间为demo
 * 可以将多个sok文件简单加在一起,再统一分析 for %i in (*.sok) do type %i >> a.txt
 * @author levin
 */
public class SokFileSplitter {
	int level=10000;
	String source="d:/lzhang/hls/sokoban/levels/a.txt";
	String dest="c:/temp/levels/";
	String line=null;
	String map=null;
	String demo=null;
	int status=0;	//处理文件的小状态机 0 开始map 1开始demo
	BufferedReader br;

	public SokFileSplitter() throws FileNotFoundException {
		super();
		br=new BufferedReader(new FileReader(source));
	}

	public static void main(String[] args) throws Exception {
		new SokFileSplitter().startSplit();
	}

	private void startSplit() throws IOException {
		System.out.println("##start....");
		while((line=br.readLine()) != null){
			System.out.println("#######main line:"+line);
			if(status == 0)
				startMap();
			else if(status == 1)
				startDemo();
			else if(status == 2)
				writeLevel();
		}
		br.close();
	}

	private void writeLevel() throws IOException {
		level ++;
		FileWriter fw=new FileWriter(dest+level+".sok");
		fw.write(map);
		fw.close();
		if(demo != null){
			fw=new FileWriter(dest+level+".soa");
			fw.write(demo);
			fw.close();
		}
		map = null;
		demo = null;
		status =0;
		System.out.println("#### level "+ level +" splited!");
	}

	private void startDemo() throws IOException {
		do{
			System.out.println("#######demo line:"+line);
			if(demo == null && line.startsWith("Solution")){
				demo="";
			}else if(demo != null && !line.trim().isEmpty()){
				demo += line;
			}else if(demo != null && line.trim().isEmpty()){
				status =2;
				break;
			}else if(demo == null && line.indexOf("#") > -1 && line.indexOf("\"") == -1 && line.indexOf(":") == -1 && line.indexOf("<") == -1){
				//无答案的跳过本关,将status=2,则只有关卡,没有答案
				map=line;
				status = 0;
				break;
			}
		}while((line=br.readLine()) != null);
		
	}

	private void startMap() throws IOException {
		do{
			System.out.println("#######map line:"+line);
			if(map == null && line.indexOf("#") > -1 && line.indexOf("\"") == -1 && line.indexOf(":") == -1 && line.indexOf("<") == -1){
				map=line;
			}else if(map != null && line.indexOf("#") > -1 ){
				map += "\n"+line;
			}else if(map != null){
				status =1;
				break;
			}
		}while((line=br.readLine()) != null);
	}
}

⌨️ 快捷键说明

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