[jetrix-cvs] jetrix/src/java/net/jetrix Field.java,1.2,1.3
Brought to you by:
smanux
From: Emmanuel B. <sm...@us...> - 2005-05-10 02:31:01
|
Update of /cvsroot/jetrix/jetrix/src/java/net/jetrix In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2732 Modified Files: Field.java Log Message: Added persistence and utility methods Index: Field.java =================================================================== RCS file: /cvsroot/jetrix/jetrix/src/java/net/jetrix/Field.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Field.java 11 Jan 2005 00:57:14 -0000 1.2 --- Field.java 10 May 2005 02:30:50 -0000 1.3 *************** *** 1,5 **** /** * Jetrix TetriNET Server ! * Copyright (C) 2001-2003 Emmanuel Bourg * * This program is free software; you can redistribute it and/or --- 1,5 ---- /** * Jetrix TetriNET Server ! * Copyright (C) 2001-2005 Emmanuel Bourg * * This program is free software; you can redistribute it and/or *************** *** 20,26 **** --- 20,30 ---- package net.jetrix; + import static net.jetrix.config.Special.*; + import java.util.logging.*; import java.util.*; + import java.io.*; + import net.jetrix.config.*; import net.jetrix.messages.*; *************** *** 44,62 **** public static final byte BLOCK_RANDOM = '6'; - public static final byte SPECIAL_ADDLINE = 'a'; - public static final byte SPECIAL_CLEARLINE = 'c'; - public static final byte SPECIAL_NUKEFIELD = 'n'; - public static final byte SPECIAL_RANDOMCLEAR = 'r'; - public static final byte SPECIAL_SWITCHFIELD = 's'; - public static final byte SPECIAL_CLEARSPECIAL = 'b'; - public static final byte SPECIAL_GRAVITY = 'g'; - public static final byte SPECIAL_QUAKEFIELD = 'q'; - public static final byte SPECIAL_BLOCKBOMB = 'o'; - private static final byte blocks[] = ! new byte[] { BLOCK_VOID, BLOCK_BLUE, BLOCK_YELLOW, BLOCK_GREEN, BLOCK_PURPLE, BLOCK_RED, SPECIAL_ADDLINE, ! SPECIAL_CLEARLINE, SPECIAL_NUKEFIELD, SPECIAL_RANDOMCLEAR, SPECIAL_SWITCHFIELD, ! SPECIAL_CLEARSPECIAL, SPECIAL_GRAVITY, SPECIAL_QUAKEFIELD, SPECIAL_BLOCKBOMB }; private byte[][] field = new byte[WIDTH][HEIGHT]; --- 48,64 ---- public static final byte BLOCK_RANDOM = '6'; private static final byte blocks[] = ! new byte[] { BLOCK_VOID, BLOCK_BLUE, BLOCK_YELLOW, BLOCK_GREEN, BLOCK_PURPLE, BLOCK_RED, ! (byte) ADDLINE.getLetter(), ! (byte) CLEARLINE.getLetter(), ! (byte) NUKEFIELD.getLetter(), ! (byte) RANDOMCLEAR.getLetter(), ! (byte) SWITCHFIELD.getLetter(), ! (byte) CLEARSPECIAL.getLetter(), ! (byte) GRAVITY.getLetter(), ! (byte) QUAKEFIELD.getLetter(), ! (byte) BLOCKBOMB.getLetter() }; + /** Array of blocks. (0, 0) is the bottom left block, and (12, 22) is the upper right block. */ private byte[][] field = new byte[WIDTH][HEIGHT]; *************** *** 93,96 **** --- 95,181 ---- /** + * Check if the field contains the specified special block. + * + * @param special the special to check + * @since 0.3 + */ + public boolean contains(Special special) + { + for (int i = HEIGHT - 1; i >= 0; i--) + { + for (int j = 0; j < WIDTH; j++) + { + if (field[j][i] == special.getLetter()) + { + return true; + } + } + } + + return false; + } + + /** + * Check if the field contains a special block. + */ + public boolean containsSpecialBlock() + { + for (int i = HEIGHT - 1; i >= 0; i--) + { + for (int j = 0; j < WIDTH; j++) + { + if ('a' <= field[j][i] && field[j][i] <= 'z') + { + return true; + } + } + } + + return false; + } + + /** + * Check if the field has holes (i.e. if a gravity block may have an effect). + * + * @since 0.3 + */ + public boolean hasHoles() + { + for (int i = 1; i <= HEIGHT - 1; i++) + { + for (int j = 0; j < WIDTH; j++) + { + if (field[j][i] != BLOCK_VOID && field[j][i - 1] == BLOCK_VOID) + { + return true; + } + } + } + + return false; + } + + /** + * Return the height of the highest block. + * + * @since 0.3 + */ + public int getHighest() + { + for (int i = HEIGHT - 1; i >= 1; i--) + { + for (int j = 0; j < WIDTH; j++) + { + if (field[j][i] != BLOCK_VOID) + { + return i; + } + } + } + + return 0; + } + + /** * Update the field with the specified FieldMessage. */ *************** *** 121,125 **** } } - } else if (fieldString.length() == WIDTH * HEIGHT) --- 206,209 ---- *************** *** 128,132 **** for (int i = 0; i < fieldString.length(); i++) { ! field[i % WIDTH][HEIGHT - i / WIDTH - 1] = (byte) fieldString.charAt(i); } } --- 212,221 ---- for (int i = 0; i < fieldString.length(); i++) { ! char c = fieldString.charAt(i); ! ! // replace random colored blocks ! c = (c == BLOCK_RANDOM) ? (char) (BLOCK_BLUE + ((int) (Math.random() * 5))) : c; ! ! field[i % WIDTH][HEIGHT - i / WIDTH - 1] = (byte) c; } } *************** *** 159,163 **** /** ! * Return the block at the specified location. * * @param x --- 248,253 ---- /** ! * Return the block at the specified location. (0, 0) is the bottom left ! * block, and (12, 22) is the upper right block. * * @param x *************** *** 182,184 **** --- 272,368 ---- } } + + + /** + * Load the field from the specified file + * + * @since 0.3 + */ + public void load(String file) throws IOException + { + BufferedReader in = null; + + try + { + in = new BufferedReader(new FileReader(file)); + + for (int i = 0; i < HEIGHT; i++) + { + String line = in.readLine(); + + if (line == null || line.length() != WIDTH) + { + throw new IOException("Field format error at line " + i); + } + + for (int j = 0; j < WIDTH; j++) + { + byte block = (byte) line.charAt(j); + + if (isValidBlock(block)) + { + field[j][HEIGHT - i - 1] = block; + } + else + { + throw new IOException("Illegal block '" + block + "' at line " + i + " , column " + j); + } + } + } + } + finally + { + if (in != null) + { + in.close(); + } + } + } + + /** + * Save the field to the specified file. + * + * @since 0.3 + */ + public void save(String file) throws IOException + { + FileOutputStream out = null; + + try + { + out = new FileOutputStream(file); + + for (int i = 0; i < WIDTH; i++) + { + for (int j = 0; j < HEIGHT; j++) + { + out.write(field[i][HEIGHT - j - 1]); + } + out.write('\n'); + } + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + private boolean isValidBlock(byte block) + { + if (block >= '0' && block <= '6') + { + return true; + } + else if (Special.valueOf((char) block) != null) + { + return true; + } + else + { + return false; + } + } } |