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

📄 maplist.java

📁 一个贪吃蛇游戏
💻 JAVA
字号:
/**
 * @(#)MapList.java
 * @A map list of the names of extended maps.
 *
 * @Link Scholes
 * @version 1.00 2008/7/21
 */

package Data;

//Java core packages
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class MapList implements DataInterface
{
	private int temp;
	private String temp1;
	private String temp2;
	private String names[];
	private TreeSet<String> extendedMaps;
	private BufferedReader input;
	private BufferedWriter output;
	private File file;
	private Iterator iterator;
	private Scanner scanner;
	
	//construct a map list based on the file "map.dat"
	public MapList()
	{
		extendedMaps = new TreeSet<String>();
		file = new File("map\\map.dat");
		
		try
		{
			if (file.exists())
			{
				read();
				write();
			}
			else
			{
				create();
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	//get the names of the extended maps in the map list
	public String[] getList()
	{
		names = new String[extendedMaps.size()];
		iterator = extendedMaps.iterator();
		
		for (int i = 0;i < extendedMaps.size();i ++)
		{
			names[i] = (String)iterator.next();
		}
		
		return names;
	}
	
	//add a new map to the map list
	public void addMap(String s)
	{
		extendedMaps.add(s);
		
		try
		{
			write();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	//return whether the map list contains the specified map
	public boolean hasMap(String s)
	{
		iterator = extendedMaps.iterator();
		
		while (iterator.hasNext())
		{
			temp1 = (String)iterator.next();
			
			if (temp1.equalsIgnoreCase(s))
			{
				return true;
			}
		}
		
		return false;
	}
	
	//turn the map list to String
	public String toString()
	{
		iterator = extendedMaps.iterator();
		
		if (iterator.hasNext())
		{
			temp1 = (String)iterator.next();
		}
		else
		{
			temp1 = "";
		}
		
		while (iterator.hasNext())
		{
			temp1 += " " + iterator.next();
		}
		
		return temp1;
	}
	
	//create the file "map.dat" if not existed
	public void create() throws IOException
	{
		output = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("map\\map.dat")))));
		output.close();
	}
	
	//read the contents of the file "map.dat"
	public void read() throws IOException
	{
		temp1 = "";
		input = new BufferedReader(new InputStreamReader(new BufferedInputStream(new GZIPInputStream(new FileInputStream("map\\map.dat")))));
		temp = input.read();
		
		while (temp != -1)
		{
			temp1 += (char)temp;
			temp = input.read();
		}
		
		input.close();
		scanner = new Scanner(temp1);
		
		while (scanner.hasNext())
		{
			temp2 = scanner.next();
			file = new File("map\\extended\\" + temp2 + ".map");
			
			if (file.exists())
			{
				extendedMaps.add(temp2);
			}
		}
	}
	
	//write the contents of the map list to the file "map.dat"
	public void write() throws IOException
	{
		input = new BufferedReader(new StringReader(toString()));
		output = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("map\\map.dat")))));
		temp = input.read();
		
		while (temp != -1)
		{
			output.write(temp);
			temp = input.read();
		}
		
		input.close();
		output.close();
	}
}	//end class MapList

⌨️ 快捷键说明

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