Finishing SaveLevel without saved data
This commit is contained in:
		| @ -38,4 +38,4 @@ level data at the top of the file. | ||||
| ## Known Limitation | ||||
|  | ||||
| 1) by putting the piece size on one byte. We limit the maximum piece size to 15 x 15 (1111 | 1111) | ||||
| I don't think we will ever need a piece larger than 5x5 so this is clearly a feature, not a bug! :-) | ||||
| I don't think we will ever need a piece larger than 5x5 so this is clearly a feature, not a bug! :-) | ||||
|  | ||||
| @ -10,16 +10,6 @@ import java.util.Arrays; | ||||
|  | ||||
| public class BinaryParser implements FileParser { | ||||
|  | ||||
|     @Override | ||||
|     public Map getSavedData(File file) { | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void saveLevelData(File file, Map levelData) throws IOException { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * check if the file is a binary file suited for parsing | ||||
|      * @return true if it is a binary File | ||||
| @ -47,7 +37,7 @@ public class BinaryParser implements FileParser { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Map getLevel(File file) throws IOException { | ||||
|     public Map getLevel(File file, boolean saved_data) throws IOException { | ||||
|         Map ret; | ||||
|  | ||||
|         FileInputStream fileStream = new FileInputStream(file); | ||||
| @ -57,14 +47,35 @@ public class BinaryParser implements FileParser { | ||||
|         ret = new Map(ExtractMapFromLevelData(level_data)); | ||||
|  | ||||
|         ret.addPiece(ExtractPiecesFromLevelData(level_data)); | ||||
|          | ||||
|         if(saved_data) | ||||
|             // TODO: 3/23/23 getSavedDat  | ||||
|  | ||||
|         fileStream.close(); | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void saveLevel(File file, Map levelData) throws FileNotFoundException { | ||||
| //        byte[] data = new byte[]; | ||||
|     public void saveLevel(File file, Map level_data, boolean save_data) throws FileNotFoundException { | ||||
|         int byteSize = getByteSizeForMap(level_data, false); | ||||
|         byte[] data = new byte[byteSize]; | ||||
|         int i = 0; | ||||
|         data[i++] = 'S'; data[i++] = 'M'; data[i++] = 'S'; | ||||
|         data[i++] = (byte) level_data.getWidth(); data[i++] = (byte) level_data.getHeight(); | ||||
|         for(byte b : BuildByteFromMatrix(level_data.getShape())){ | ||||
|             data[i++] = b; | ||||
|         } | ||||
|         data[i++] = (byte) level_data.getPieces().size(); | ||||
|         for (Piece p : level_data.getPieces()) { | ||||
|             data[i++] = Bitwise.NibbleToByte((byte) p.getWidth(), (byte) p.getHeight()); | ||||
|             for(byte b : BuildByteFromMatrix(p.getShape())){ | ||||
|                 data[i++] = b; | ||||
|             } | ||||
|         } | ||||
|         if (save_data){ | ||||
|             // TODO: 3/23/23 Save Data  | ||||
|         } | ||||
|         data[i++] = 'S'; data[i++] = 'M'; data[i++] = 'E'; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @ -126,6 +137,30 @@ public class BinaryParser implements FileParser { | ||||
|         return BuildMatrixFromBytes(map_width, map_height, map_data); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * take a boolean matrix and build an array of byte following the specs of the parser | ||||
|      * @param shape bolean matrix where true are 1 and false are 0 | ||||
|      * @return byte array with each element compiled for file format | ||||
|      */ | ||||
|     private static byte[] BuildByteFromMatrix(boolean[][] shape){ | ||||
|         int width = shape[0].length , height = shape.length; | ||||
|         boolean[] list = new boolean[width * height]; | ||||
|         for (int x = 0; x < shape.length; x++) { | ||||
|             for (int y = 0; y < shape[x].length; y++) { | ||||
|                 list[x * shape[x].length + y] = shape[x][y]; | ||||
|             } | ||||
|         } | ||||
|         byte[] ret = new byte[width * height  / 8 + width * height % 8 == 0 ? 0 : 1 ]; | ||||
|         for (int i = 0; i < ret.length; i++) { | ||||
|             byte b = 0; | ||||
|             for (int b_count = i * 8; b_count < i * 8 + 8; b_count++) { | ||||
|                 b = (byte) ((b << 1) | (list[b_count] ? 1 : 0)); | ||||
|             } | ||||
|             ret[i] = b; | ||||
|         } | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Build a boolean Matrix From a byte array | ||||
|      * Each Byte is composed of 8 bit, each bit is 1 or 0 | ||||
|  | ||||
| @ -24,17 +24,5 @@ public interface FileParser { | ||||
|      * @param file the file where to save | ||||
|      * @param levelData the map to save | ||||
|      */ | ||||
|     void saveLevel(File file, Map levelData) throws FileNotFoundException; | ||||
|  | ||||
|     /** | ||||
|      * Save Map to a file with all it's data | ||||
|      * @param file the file where to save | ||||
|      * @param levelData the map to save | ||||
|      */ | ||||
|     void saveLevelData(File file, Map levelData) throws IOException; | ||||
|  | ||||
|  | ||||
|  | ||||
|     // TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file | ||||
|      | ||||
|     void saveLevel(File file, Map levelData, boolean save_data) throws FileNotFoundException; | ||||
| } | ||||
|  | ||||
| @ -27,4 +27,14 @@ public class Bitwise { | ||||
|         ret[1] = (byte) (in & 15); // apply the mask '00001111' | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Transform 2 byte into 1 with a left part ( 4 bits ) and a right part ( 4 bits) | ||||
|      * @param left first 4 bits | ||||
|      * @param right last 4 bits | ||||
|      * @return concatenated byte | ||||
|      */ | ||||
|     public static byte NibbleToByte(byte left, byte right){ | ||||
|         return (byte) ((left << 4) | right); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user