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

📄 gamemap.java

📁 用java写的apple机上的小游戏aquamatic
💻 JAVA
字号:
/** @version 0.0.2 * 	@author mathena * 	init the Container, put all the elements in this; * 	ResourceManager is a inner class of this * 	This is the Model(Skeleton) (MVC) of the whole application. *  04-02-2006 Change the GOAL to an array and add isSuccess(): boolean Ver 0.0.2 *   *  Need record? OK, I will add it: using Commond of Design Pattrens; *  Need Animation? OK; *  Need a timer? *   */package org.orchesta.mathena.aquamatic;import java.awt.Canvas;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.util.Hashtable;public class GameMap{	private GameMap() { 			}			private static GameMap skeleton= new GameMap();	private boolean success;	private String name; 	private int nowx =0 ;	private int nowy =0 ;	private boolean hintstatus = false;			public static GameMap getInstance() {		return skeleton;	}		public static final int BLANK = '.';	public static final int WALL = '#';	public static final int RIGHT = 'R';	public static final int LEFT = 'L';	public static final int UP = 'U';	public static final int DOWN = 'D';	/**	 * 把abilder.png分解一下,放在这里了。	 * 	 */	private Hashtable atomHash = new Hashtable();		private BufferedImage molecular;	/**	 * 布局数组。	 */	private int map[][] = new int[15][15];	/**	 * 答案数组。	 */	private int goal[][];	// store the state of the map; 		//private HashTable();	// Find the compont according the id;		public void setCell(int i, int j, int state) {		map[i][j] = state;	}		public void putAtom(int index, BufferedImage bi)	{		atomHash.put(new Integer(index), bi);	}	private BufferedImage getAtom(int index)	{		return (BufferedImage) atomHash.get(new Integer(index));	}	public BufferedImage getBasicMap(BufferedImage bi) {		Graphics2D g = bi.createGraphics();		Canvas c = new Canvas();		for (int i=0; i<15; i++) {			for (int j =0; j<15; j++) {				g.drawImage(this.getAtom(map[i][j]), j*30, i*30, c);			}		}		return bi;	}				public void move(int fromx, int fromy, int direction) {		// Find the destination;		this.clearhints();		int x = fromx, y=fromy;		switch (direction) {		case 'U': // UP			while (map[x-1][y]==GameMap.BLANK) x--;			break;		case 'D': //DOWN			while (map[x+1][y]==GameMap.BLANK) x++;			break;		case 'L': //LEFT			while (map[x][y-1]==GameMap.BLANK) y--;			break;		case 'R': //RIGHT			while (map[x][y+1]==GameMap.BLANK) y++;			break;		default:			System.out.print("Shoudn't Be Here!");		}		this.nowx = x;		this.nowy = y;			// animation		map[x][y]=map[fromx][fromy];		map[fromx][fromy]= GameMap.BLANK;		if (testSuccess()) return;		if (hashints(x,y)) {showhints(x,y); hintstatus=true;}	}		public boolean isSuccess() {		return success;	}		private boolean testSuccess() {		// Fine the goal pattern in map;		int x = goal.length;		int y = goal[0].length;		int innerx, innery;		int i,j, flag;		char temp;		for (i = 0; i<15-x; i++)			for (j = 0; j<15-y ; j++)			{				flag = 0;				for (innerx=0; innerx<x; innerx++)					for (innery =0; innery< y; innery ++)					{						temp=(char)goal[innerx][innery];						if (temp!=map[i+innerx][j+innery] && Character.isLetterOrDigit(temp)) 							{								flag =1;									break;							}					}				if (flag==0) {success = true; return true;}			}		success = false;		return false;	}		public void setFail() {		success = false;	}		/** 	 * @return	 * 0: Has Hints;	 * 1: Move	 * 2: No hint	 */	public int process(int x, int y)	{		if (map[x][y]==GameMap.LEFT ) { move(x,y+1, 'L'); return 1;}		if (map[x][y]==GameMap.RIGHT) {move(x,y-1, 'R'); return 1;}		if (map[x][y]==GameMap.DOWN) { move(x-1, y, 'D'); return 1;}		if (map[x][y]==GameMap.UP)	{  move(x+1, y, 'U');  return 1;}		if (hashints(x, y)) {clearhints(); showhints(x,y); hintstatus = true; return 0;}			else {clearhints(); hintstatus = false; return 2;}	}				private boolean hashints(int x, int y) {		int temp = map[x][y];		if (temp==GameMap.WALL || temp==GameMap.BLANK || (temp >'A' && temp<'Z')) return false;		if (map[x+1][y]==GameMap.BLANK||map[x-1][y]==GameMap.BLANK				||map[x][y-1]==GameMap.BLANK||map[x][y+1]==GameMap.BLANK) return true;		else return false;	}		private void showhints(int x, int y) {		this.nowx = x;		this.nowy = y;		if (map[x+1][y]==GameMap.BLANK) map[x+1][y]=GameMap.DOWN;  		if (map[x-1][y]==GameMap.BLANK) map[x-1][y]=GameMap.UP; 		if (map[x][y-1]==GameMap.BLANK) map[x][y-1]=GameMap.LEFT;		if (map[x][y+1]==GameMap.BLANK) map[x][y+1]=GameMap.RIGHT; 		}	private void clearhints() {		if (!hintstatus) return;		if (map[nowx+1][nowy]==GameMap.DOWN) map[nowx+1][nowy]=GameMap.BLANK;		if (map[nowx-1][nowy]==GameMap.UP) map[nowx-1][nowy]=GameMap.BLANK;		if (map[nowx][nowy-1]==GameMap.LEFT) map[nowx][nowy-1]=GameMap.BLANK;		if (map[nowx][nowy+1]==GameMap.RIGHT) map[nowx][nowy+1]=GameMap.BLANK;		hintstatus = false;			}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}    /**     * 生成分子式的图片。     * @param map     */	public void setMolecular(int[][] map) {		goal= map;		buildMolecularOnly();	} 	public void buildMolecularOnly(){		int moleLength = goal[0].length;		int moleNumber = goal.length;		BufferedImage molemap = new BufferedImage(moleLength*30+30,moleNumber*30+30,BufferedImage.TYPE_4BYTE_ABGR);		Graphics moleg = molemap.getGraphics();		Canvas ca = new Canvas();		for (int k =0; k< moleNumber; k++)			for (int y = 0; y<goal[k].length; y++)				moleg.drawImage(this.getAtom(goal[k][y]), 30* y, 30*k, new Color(255,255,255, 0) , ca);						this.molecular= molemap;	}	public void buidMolecularOnFelder(int xOffset,int yOffset){		int moleLength = goal[0].length;		int moleNumber = goal.length;		BufferedImage molemap = new BufferedImage(15*30+30,15*30+30,BufferedImage.TYPE_4BYTE_ABGR);		Graphics moleg = molemap.getGraphics();		Canvas ca = new Canvas();		for(int x = 0; x < map.length; x++){			for(int w = 0; w < map[x].length; w++){				int  v = map[x][w];				if( Character.isLetterOrDigit((char)v) )continue;				moleg.drawImage(this.getAtom(v), 30* w, 30*x, new Color(255,255,255, 0) , ca);			}		}		for (int k =0; k< moleNumber; k++){			for (int j = 0; j<goal[k].length; j++){				int v = goal[k][j];				if(v == BLANK)continue;				moleg.drawImage(this.getAtom(v), 30* (j+xOffset), 30*(k+yOffset), new Color(255,255,255, 0) , ca);			}		}						this.molecular= molemap;	}	public BufferedImage getMolecular() {		return molecular;	}		public void output()	{		System.out.println(name);		for (int i = 0; i<15; i++)		{			for (int j = 0; j<15; j++)				System.out.print((char)map[i][j]);			System.out.println("");		}			}}

⌨️ 快捷键说明

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