一个J2ME地牢游戏的源码 |
| 作者:J2MEDEV 来源:J2MEDEV 发布时间:2006-7-25 9:06:59 |
|
OutputStream baos = new ByteArrayOutputStream(4); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(i); baos.close(); dos.close(); byte[] retArray = baos.toByteArray(); return (retArray); } //-------------------------------------------------------- // integer interpretation illustrated /** * Java appears to treat a byte as being signed when returning it as an * int--this function converts from the signed value to the corresponding * unsigned value. This method is used by nostreamParseInt. */ public static int unsign(int signed) { int retVal = signed; if (retVal < 0) { retVal += 256; } return (retVal); } /** * Takes an array of bytes and returns an int. This version will return the * same value as the method parseInt above. This version is included in * order to illustrate how Java encodes int values in terms of bytes. * * @param data * an array of 1, 2, or 4 bytes. */ public static int nostreamParseInt(byte[] data) { // byte 0 is the high byte which is assumed // to be signed. As we add the lower bytes // one by one, we unsign them because because // a single byte alone is interpreted as signed, // but in an int only the top byte should be signed. // (note that the high byte is the first one in the array) int retVal = data[0]; for (int i = 1; i < data.length; i++) { retVal = retVal << 8; retVal += unsign(data[i]); } return (retVal); } /** * Takes an arbitrary int and returns an array of four bytes. This version * will return the same byte array as the method intToFourBytes above. This * version is included in order to illustrate how Java encodes int values in * terms of bytes. */ public static byte[] nostreamIntToFourBytes(int i) { byte[] fourBytes = new byte[4]; // when you take the byte value of an int, it // only gives you the lowest byte. So we // get all four bytes by taking the lowest // byte four times and moving the whole int // down by one byte between each one. // (note that the high byte is the first one in the array) fourBytes[3] = (new Integer(i)).byteValue(); i = i >> 8; fourBytes[2] = (new Integer(i)).byteValue(); i = i >> 8; fourBytes[1] = (new Integer(i)).byteValue(); i = i >> 8; fourBytes[0] = (new Integer(i)).byteValue(); return (fourBytes); } /** * Takes an int between -32768 and 32767 and returns an array of two bytes. * This does not verify that the argument is of the right size. If the * absolute value of i is too high, it will not be encoded correctly. */ public static byte[] nostreamIntToTwoBytes(int i) { byte[] twoBytes = new byte[2]; // when you take the byte value of an int, it // only gives you the lowest byte. So we // get the lower two bytes by taking the lowest // byte twice and moving the whole int // down by one byte between each one. twoBytes[1] = (new Integer(i)).byteValue(); i = i >> 8; twoBytes[0] = (new Integer(i)).byteValue(); return (twoBytes); } } /** * This class contains the data for the map of the dungeon.. * * @author Carol Hamer */ class BoardDecoder { //-------------------------------------------------------- // fields /** * The coordinates of where the player starts on the map in terms of the * array indices. */ private int[] myPlayerSquare; /** * The coordinates of the goal (crown). */ private int[] myGoalSquare; /** * The coordinates of the doors. the there should be two in a row of each * color, following the same sequence as the keys. */ private int[][] myDoors; /** * The coordinates of the Keys. the there should be of each color, following * the same sequence as the doors. */ private int[][] myKeys; /** * The coordinates of the stone walls of the maze, encoded bit by bit. */ private TiledLayer myLayer; /** * The data in bytes that gives the various boards. This was created using * EncodingUtils... This is a two-dimensional array: Each of the four main * sections corresponds to one of the four possible boards. */ private static byte[][] myData = { { 0, 0, -108, -100, -24, 65, 21, 58, 53, -54, -116, -58, -56, -84, 115, -118, -1, -1, -128, 1, -103, -15, -128, 25, -97, -127, -128, 79, -14, 1, -126, 121, -122, 1, -113, -49, -116, 1, -100, -3, -124, 5, -25, -27, -128, 1, -1, -1 }, { 0, 1, 122, 90, -62, 34, -43, 72, -59, -29, 56, -55, 98, 126, -79, 61, -1, -1, -125, 1, -128, 17, -26, 29, -31, 57, -72, 1, -128, -51, -100, 65, -124, 57, -2, 1, -126, 13, -113, 1, -97, 25, -127, -99, -8, 1, -1, -1 }, { 0, 2, 108, -24, 18, -26, 102, 30, -58, 46, -28, -88, 34, -98, 97, -41, -1, -1, -96, 1, -126, 57, -9, 97, -127, 69, -119, 73, -127, 1, -109, 59, -126, 1, -26, 103, -127, 65, -103, 115, -127, 65, -25, 73, -128, 1, -1, -1 }, { 0, 3, -114, 18, -34, 27, -39, -60, -76, -50, 118, 90, 82, -88, 34, -74, -1, -1, -66, 1, -128, 121, -26, 125, -128, -123, -103, 29, -112, 1, -109, 49, -112, 1, -116, -31, -128, 5, -122, 5, -32, 13, -127, -51, -125, 1, -1, -1 }, }; //-------------------------------------------------------- // initialization /** * Constructor fills data fields by interpreting the data bytes. */ public BoardDecoder(int boardNum) throws Exception { // we start by selecting the two dimensional // array corresponding to the desired board: byte[] data = myData[boardNum]; // The first two bytes give the version number and // the board number, but we ignore them because // they are assumed to be correct. // The third byte of the first array is the first one // we read: it gives the player's starting coordinates: myPlayerSquare = DataConverter.decodeCoords(data[2]); // the next byte gives the coordinates of the crown: myGoalSquare = DataConverter.decodeCoords(data[3]); // the next four bytes give the coordinates of the keys: myKeys = new int[4][]; for (int i = 0; i < myKeys.length; i++) { myKeys[i] = DataConverter.decodeCoords(data[i + 4]); } // the next eight bytes give the coordinates of the doors: myDoors = new int[8][]; for (int i = 0; i < myDoors.length; i++) { myDoors[i] = DataConverter.decodeCoords(data[i + 8]); } // now we create the TiledLayer object that i |
| [] [返回上一页] [打 印] |
|
文章评论 |
