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

📄 codegen.java

📁 一款基于java 的赛车类游戏 一款基于java 的赛车类游戏
💻 JAVA
字号:
/*
 * Created on 2005-03-10
 * 
 * Copyright (c) 2005 nanoGames. All Rights Reserved.
 *  
 */

//package com.nano.KangooJumper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.*;

/**
 * @author plumkawka CodeGen
 */
public class CodeGen
{

	public int			nCodesCount;
	private String[]	sCode;
	private	String		sName;



	public CodeGen(String name, int codesnum)
	{
		sName = name;
		nCodesCount = codesnum;
		sCode = new String[nCodesCount];

		if (!existsCodeList(name))
		{
			// generate code list
			for (int i = 0; i < nCodesCount; i++)
			{
				boolean exists;
				String newcode;
				char[] tmp = new char[Config.CODES_MAX_CHARS];

				do 
				{				
					// generate 
					for (int a = 0; a < Config.CODES_MAX_CHARS; a++)
					{
						char t = 0;
						if (Math.Rand(0, 100) > 50)
						{
							t = (char) ('A' + (char) Math.Rand(0, ('Z' - 'A') + 1));
						}
						else
						{
							t = (char) ('1' + Math.Rand(0, ('9' - '1') + 1));
						}
						tmp[a] = t;
					}
										
					newcode = new String(tmp);
					
					// check if already exists
					exists = false;
					for (int b = 0; b < (i - 1); b++)
					{
						if (newcode.equals(sCode[b]))
							exists = true;
					}
				} while (exists);
				
				//store new code
				sCode[i] = newcode;
				//Utils.Log(new String(tmp));
			}
			
			// save list to recordstore	
			saveCodeList();
		}
		else
		{
			loadCodeList();
		}
	}

	public void removeCodeList()
	{
		if ((sName == null) || (sName.length() == 0))
		{
			Utils.Log("CodeGen: error removing code list");
			return;
		}
		
		try
		{
			if (existsCodeList(sName) == true)
				RecordStore.deleteRecordStore (sName);
		}
		catch (Exception e)
		{
		}
	}
	
	public	void	saveCodeList()
	{
		if ((sName == null) || (sName.length() == 0))
		{
			Utils.Log("CodeGen: error saving code list");
			return;
		}
		
		try
		{
			ByteArrayOutputStream buf = new ByteArrayOutputStream ();
			DataOutputStream stream = new DataOutputStream (buf);

			for (int i=0; i<nCodesCount; i++)
			{
				stream.writeUTF(sCode[i]);
				Utils.Log("storing: "+sCode[i]);
			}
			
			
			RecordStore rs = RecordStore.openRecordStore (sName, true);
			if (rs.getNumRecords() == 0)
			{ 
				rs.addRecord (buf.toByteArray(), 0, buf.size());
			} 
			else
			{
				rs.setRecord (1,
						  buf.toByteArray(),
						  0,
						  buf.size());
			}
			
			rs.closeRecordStore ();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		Utils.callGc();		
	}

	public	int	checkCode(String code)
	{
		for (int i=0; i<nCodesCount; i++)
		{
			if (code.equals( sCode[i] ))
			{
				return i;
			}
		}
		
		return -1;
	}

	public	String	getCode(int idx)
	{
		String s = "";
		if ((idx >=0) && (idx <nCodesCount))
		{
			s = sCode[idx];
		}

		return s;
	}
	
	
	public void loadCodeList()
	{
		if ((sName == null) || (sName.length() == 0))
		{
			Utils.Log("CodeGen: error loading code list");
			return;
		}
		
		try
		{
			RecordStore rs = RecordStore.openRecordStore (sName, true);
			DataInputStream stream = new DataInputStream (
									 new ByteArrayInputStream (
									 rs.getRecord(Config.CODES_RECORDID))); 

			for (int i=0; i<nCodesCount; i++)
			{
				sCode[i] = stream.readUTF();
				Utils.Log("loading: "+sCode[i]);
			}

			rs.closeRecordStore ();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		Utils.callGc();
	}
	
	public boolean existsCodeList(String name)
	{
		try
		{
			String[] records = RecordStore.listRecordStores();

			// enumarate records
			for (int lp = 0; lp < records.length; lp++)
			{
				if (records[lp].compareTo(name) == 0)
					return true;
			}
		}
		catch (Exception e)
		{
		}
		return false;
	}

}

⌨️ 快捷键说明

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