///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// import processing.phone.*; import processing.sound.*; ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// class Location { private int row; private int column; public Location (int _r, int _c) { setRow(_r); setColumn(_c); } public void setRow(int _r) { row = _r; } public void setColumn(int _c) { column = _c; } public int getRow() { return row; } public int getColumn() { return column; } public boolean equals(int r, int c) { return (row == r) && (column == c); } } ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// class Bubble { public final color[] types = { color(129, 240, 112), // Verde color( 0, 183, 239), // Azul color(255, 121, 121), // Rojo color(255, 211, 79) }; // Amarillo private color type; private boolean selected; private int width; private int height; public Bubble(int width, int height) { setTypeByIndex(random(0, 3)); this.width = width; this.height = height; selected = false; } public color getType() { return type; } public color getColor() { return type; } public void setTypeByIndex(int t) { type = types[t]; } public void setType(color t) { type = t; } public color getGenericType(int i) { return types[i]; } public boolean isSelected() { return selected; } public void setSelected(boolean s) { selected = s; } public void draw(int x, int y, boolean user_selected) { /////////////////////////////////////////// rectMode(CORNER); if(selected) { fill(color(229, 170, 122)); rect(x, y, width, height); } /////////////////////////////////////////// rectMode(CORNER); if(user_selected) { fill(color(234, 0, 117)); rect(x, y, width, height); } /////////////////////////////////////////// if(!selected && !user_selected) { fill(color(0, 0, 0)); rect(x, y, width, height); } /////////////////////////////////////////// fill(getColor()); ellipseMode(CORNER); ellipse(x, y, width, height); /////////////////////////////////////////// if(selected) // again { fill(color(0, 0, 0)); line(x, y, x + width, y + height); line(x + width, y, x, y + height); } /////////////////////////////////////////// } } ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// class Board { private Bubble[][] board; private int rows; private int columns; private int top; private int width; private int height; private int boardWidth; private int boardHeight; private Location selection; private int selectionCount; private int scorePoints; private int scoreExploded; private int scoreShots; private int status; public final int ENABLED = 1; public final int DISABLED = 2; public Board(int rows, int columns, int width, int height) { this.rows = rows; this.columns = columns; this.boardWidth = width; this.boardHeight = height; this.top = 30; this.width = boardWidth / columns; this.height = (boardHeight - top) / rows; initialize(); } private void initialize() { this.status = ENABLED; board = new Bubble[rows][columns]; for(int r=0; r 1) { processSelection(); this.status = this.ENABLED; return this.status; } } } } deselectAll(); this.status = this.DISABLED; return this.status; } private int processSelection() { deselectAll(); int r = selection.getRow(); int c = selection.getColumn(); color type = (board[r][c] != null) ? board[r][c].getType() : color(0, 0, 0); return processSelectionOn(r, c, type); } private int processSelectionOn(int r, int c, color type) { int[] dr = { -1, 0, 1, 0 }; int[] dc = { 0, 1, 0, -1 }; if(r < 0 || c < 0 || r >= rows || c >= columns) return 0; if(board[r][c] == null) return 0; if(board[r][c].getType() != type) return 0; if(board[r][c].isSelected()) return 0; board[r][c].setSelected(true); int count = 1; for(int i=0; i=0; r--) { if(board[r][c] == null) { if(emptyRow == -1) // Undefined emptyRow = r; // else, this is an empty space more, I already have one } else { if(emptyRow != -1) // I have an empty space already detected and I found the first following bubble { board[emptyRow][c] = board[r][c]; board[r][c] = null; r = emptyRow; emptyRow = -1; } // else, I haven't found any holes to fill } } } } private boolean isEmptyColumn(int index) { for(int r=0; r 0) { phone.vibrate(50); } } } ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// void softkeyPressed(String label) { if(label == "New Game") { board.initialize(); redraw(); } } ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// void draw() { if(board != null) { if(board.getStatus() == board.ENABLED) background(0); if(board.getStatus() == board.DISABLED) background(color(245, 228, 156)); } else background(0); if(board != null) board.draw(); } ///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////