Falling Bricks
Informace
Legendární logická hra ve zbrusu novém silně návykovém provedení!
Tento projekt byl vytvořen jako semestrální práce do předmětu
Y36ALG (Algoritmizace).
Popis hry na Wikipedii.
Stáhni a hraj!
Dokumentace
Zdrojové kódy (prohlížení)
- fallingbricks
1: package fallingbricks; 2: import java.awt.*; 3: 4: /** 5: * 6: * @author Ondrej Macoszek 7: */ 8: public class Matrix 9: { 10: /** 11: * Matrix dimension 12: */ 13: public static Dimension dimension = new Dimension (10, 20); 14: 15: /** 16: * Holds information about fields and their status (filled/empty), default all empty 17: */ 18: public boolean[][] fields = new boolean[dimension.height][dimension.width]; 19: 20: /** 21: * Game controller instance 22: */ 23: private Game game; 24: 25: 26: /** 27: * Matrix constructor 28: */ 29: public Matrix() 30: { 31: game = Game.getInstance(); 32: } 33: 34: /** 35: * Write given brick into matrix fields 36: * @param brick 37: */ 38: public void writeBrick(Brick brick) 39: { 40: fields = writeBrick(brick, fields); 41: } 42: 43: /** 44: * Write given brick into given fields and return the result 45: * @param brick 46: * @param fields 47: * @return fields with written brick 48: */ 49: public boolean[][] writeBrick(Brick brick, boolean[][] fields) 50: { 51: for (int h = 0; h < brick.shape.length; h++) 52: { 53: for (int w = 0; w < brick.shape[0].length; w++) 54: { 55: // write if shape field is true 56: if ( 57: (brick.shape[h][w] == true) 58: //TODO why i wrote this? && (brick.position.y + h < fields.length) 59: //TODO why i wrote this? && (brick.position.x + w < fields[h].length) 60: ) 61: { 62: fields[brick.position.y + h][brick.position.x + w] = true; 63: } 64: } 65: } 66: Debug.msg("== Writting brick == "); 67: return fields; 68: } 69: 70: /** 71: * Remove full rows from matrix fields 72: */ 73: public void removeFullRows() 74: { 75: // score counter for current round 76: int score = 0; 77: 78: // determine which rows are empty 79: // and remove the full ones 80: boolean row_status[] = new boolean[dimension.height]; 81: for (int h = dimension.height - 1; h >= 0; h--) 82: { 83: if (isFullOrEmptyRow(fields[h])) 84: { 85: // write score if row is full 86: if(fields[h][0]==true) 87: { 88: score++; 89: } 90: // removing full row 91: fields[h] = new boolean[dimension.width]; 92: // setting row status to empty 93: row_status[h] = false; 94: } 95: else 96: { 97: // row is not empty 98: row_status[h] = true; 99: } 100: } 101: // merge lines empty rows 102: for (int h = dimension.height - 1; h>=0; h--) 103: { 104: if (row_status[h]==false) 105: { 106: int h2 = h; 107: while ((row_status[h2]==false) && (h2>0)) 108: { 109: h2--; 110: } 111: fields[h] = fields[h2]; 112: fields[h2] = new boolean[dimension.width]; 113: row_status[h2] = false; 114: } 115: } 116: if(score > 0) 117: { 118: // give extra points for destroying more rows at single round 119: score = (score > 1) ? score*score : score; 120: // write score 121: game.score.totalScore += score; 122: Debug.msg("== Score: " + game.score.totalScore + " =="); 123: } 124: } 125: 126: /** 127: * Determine if given row has all fields empty or full 128: * @param row 129: * @return boolean status of row 130: */ 131: public boolean isFullOrEmptyRow(boolean[] row) 132: { 133: boolean status = true; 134: for (int w = 0; w<dimension.width; w++) 135: { 136: if (row[w]!=row[0]) 137: { 138: status = false; 139: break; 140: } 141: } 142: return status; 143: } 144: 145: /** 146: * Determine if filled fields has reached the top of matrix 147: * @return boolean 148: */ 149: public boolean reachTop() 150: { 151: for (int i=0; i<dimension.width; i++) 152: { 153: if (fields[0][i] == true) 154: { 155: return true; 156: } 157: } 158: return false; 159: } 160: 161: /** 162: * Render all filled fields in matrix 163: * @param g Graphic enviroment 164: */ 165: public void render(Graphics g) 166: { 167: // Written bricks have gray color 168: g.setColor(Color.GRAY); 169: 170: // Walk all fields and render the filled ones 171: for (int h = 0; h < fields.length; h++) 172: { 173: for (int w = 0; w < fields[h].length; w++) 174: { 175: if ( (fields[h][w]==true) ) { 176: g.fillRect( 177: game.fieldDim.width*w, 178: game.fieldDim.height*h, 179: game.fieldDim.width, 180: game.fieldDim.height 181: ); 182: } 183: } 184: } 185: } 186: } 187:
