stuivertje85
Gebruiker
- Lid geworden
- 13 jun 2007
- Berichten
- 12
hoi hoi ..
kan iemand me globaal vertellen wat deze code nou doet ??
moet aan het uitleggen aan een student namelijk :
kan iemand me globaal vertellen wat deze code nou doet ??
moet aan het uitleggen aan een student namelijk :
Code:
[B][COLOR="Blue"]package Colorpop;
import graviton.observerPattern.Observable;
import graviton.observerPattern.ObserverManager;
import highscore.HighScoreManager;
import highscore.IHighScoreSubject;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
*
*/
public class ColorpopCanvas extends Canvas
implements IHighScoreSubject,
CommandListener,
Observable {
private int[][] field;
private int score;
private int screenWidth;
private int screenHeight;
private int cellWidth;
private int cellHeight;
private int fieldWidth;
private int fieldHeight;
private int cursorIndexPosX;
private int cursorIndexPosY;
private Vector affectedSlices = new Vector(10);
private Vector CellSet = new Vector(20);
private Random rand = new Random(System.currentTimeMillis());
private final int RED = 0;
private final int GREEN = 1;
private final int BLUE = 2;
private final int PURPLE = 3;
private final int YELLOW = 4;
private final int TURQUOISE = 5;
private final int WHITE = 6;
private final int MARKED = 7;
private boolean initialDraw = true;
private boolean drawWholeScreen = false;
private boolean updateCrosshairsOnly = false;
private boolean gameFinished = false;
private MIDlet Colorpop;
private Command exitCmd;
private Command startCmd;
private Command instructCmd;
private Command backCmd;
private Command aboutCmd;
private Command highScoreCmd;
private Form instructionForm;
private Form aboutForm;
private Form highScoreForm;
private StringItem instructStringItem;
private StringItem aboutStringItem;
private StringItem highScoreStringItem;
private ObserverManager observerManager;
private HighScoreManager highScore;
public ColorpopCanvas(MIDlet pMidlet, int pFieldWidth, int pFieldHeight) {
highScore = HighScoreManager.getInstance(5,false,"Colorpop");
observerManager = new ObserverManager();
observerManager.addObserver(highScore);
final Displayable ColorpopCanvas = this;
// Instructies Scherm
backCmd = new Command("Terug", Command.SCREEN, 1);
instructionForm = new Form("Instructions");
instructStringItem = new StringItem(
"Colorpop Instructies",
"Gebruik pijltje omhoog (of #2), omlaag (of #8), links (of #4) en rechts (of #6) " +
"om het kruisje te verplaatsen tussen de cellen. Zoek voor aaneensluitende cellen (twee of meer) " +
"met dezelfde kleur en druk op de ok knop (of #5) om ze te laten verdwijnen. " +
"Doel van hey spel is zoveel mogelijk cellen te laten verdwijnen.");
instructionForm.append(instructStringItem);
instructionForm.addCommand(backCmd);
instructionForm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
if (c == backCmd) {
updateCrosshairsOnly = false;
CellSet.removeAllElements();
drawWholeScreen = true;
((Colorpop) Colorpop).getDisplay().setCurrent(ColorpopCanvas);
}
}
});
// Info scherm
aboutForm = new Form("Info");
aboutStringItem = new StringItem(
"OVER Colorpop",
"Deze game is geschreven door A.R. El Shershaby (student nr. 194793)" +
"en Y. Ekin (student nr. 196553) studenten aan de Hogeschool van Amsterdam (IIE) " +
"en in opdracht van de HvA.\nMet dank aan M. Serhat Cinar voor advies en gebruik van graviton.");
aboutForm.append(aboutStringItem);
aboutForm.addCommand(backCmd);
aboutForm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
if (c == backCmd) {
updateCrosshairsOnly = false;
CellSet.removeAllElements();
drawWholeScreen = true;
((Colorpop) Colorpop).getDisplay().setCurrent(ColorpopCanvas);
}
}
});
// HighScore Scherm
highScoreForm= new Form("HIGHSCORE");
highScoreForm.addCommand(backCmd);
highScoreForm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
if (c == backCmd) {
score = 0;
gameFinished = false;
initialDraw = true;
repaint();
((Colorpop) Colorpop).getDisplay().setCurrent(ColorpopCanvas);
}
}
});
// Display setten
Colorpop = pMidlet;
startCmd = new Command("Start", Command.SCREEN, 1);
instructCmd = new Command("Instructies", Command.SCREEN, 2);
aboutCmd = new Command("Info", Command.SCREEN, 3);
highScoreCmd = new Command("Highscore", Command.SCREEN, 4);
exitCmd = new Command("Exit", Command.SCREEN, 5);
addCommand(startCmd);
addCommand(instructCmd);
addCommand(aboutCmd);
addCommand(highScoreCmd);
addCommand(exitCmd);
setCommandListener(this);
fieldWidth = pFieldWidth;
fieldHeight = pFieldHeight;
field = new int[pFieldWidth][pFieldHeight];
}
protected void drawCrosshairs(Graphics g) {
int x = cursorIndexPosX * cellWidth;
int y = cursorIndexPosY * cellHeight;
int x1 = x + 2;
int y1 = y + 2;
int x2 = x + cellWidth - 5;
int y2 = y + cellHeight - 6;
// Draw Crosshairs
g.setColor(0, 0, 0);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y1, x1, y2);
}
protected void paint(Graphics g) {
if (initialDraw) {
// grootte van basis scherm elementen (1x)
screenWidth = g.getClipHeight();
screenHeight = g.getClipWidth();
cellWidth = screenWidth / fieldWidth;
cellHeight = screenHeight / fieldHeight;
// Draw blank screen
g.setColor(255, 255, 255);
g.fillRect(0, 0, screenWidth, screenHeight);
// init random cel
for (int i = 0; i < fieldWidth; i++) {
for (int j = 0; j < fieldHeight; j++) {
field[i][j] = getRandomNumber();
}
}
// int [][] field = {{0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,6,6,6,6,6 },
// {0,0,0,0,6,1,1,1,6,0 },
// {0,0,0,0,6,1,0,1,6,0 },
// {0,0,0,0,1,1,0,1,1,1 }};
// this.field = field;
// alle cellen
drawWholeBoard(g);
// crosshairs coordinaten bij 1ste paint() call
cursorIndexPosX = 0;
cursorIndexPosY = 0;
initialDraw = false;
} else if (updateCrosshairsOnly && CellSet.size() > 0) {
// laatst cel aangeraakt
CellBlock crosshairCell = (CellBlock) CellSet.firstElement();
drawField(g, crosshairCell.xval, crosshairCell.yval);
CellSet.removeAllElements();
updateCrosshairsOnly = false;
} else if (gameFinished) {
// Leeg screen
observerManager.notifyObservers(this);
highScoreStringItem = new StringItem("Jouw score is: "+score+" Highscores: ",highScore.getSimpleList());
if(highScoreForm.size() == 0){
highScoreForm.append(highScoreStringItem);
}else{
highScoreForm.set(0,highScoreStringItem);
}
((Colorpop) Colorpop).getDisplay().setCurrent(highScoreForm);
return;
}else if(drawWholeScreen){
g.setColor(255, 255, 255);
g.fillRect(0, 0, screenWidth * 2, screenHeight * 2);
drawWholeBoard(g);
} else {
// board na verdwijnen
for (int i = 0; i < affectedSlices.size(); i++) {
for (int j = 0; j < fieldHeight; j++) {
drawField(g, ((Integer) affectedSlices.elementAt(i)).intValue(), j);
}
}
affectedSlices.removeAllElements();
}
if (!gameFinished)
drawCrosshairs(g);
}
private void drawWholeBoard(Graphics g){
for (int y = 0; y < fieldWidth; y++) {
for (int x = 0; x < fieldHeight; x++) {
drawField(g, x, y);
}
}
}
private void drawField(Graphics g, int ix, int iy) {
// cel kleur
switch (field[iy][ix]) {
case RED:
g.setColor(255, 0, 0);
break;
case GREEN:
g.setColor(0, 255, 0);
break;
case BLUE:
g.setColor(0, 0, 255);
break;
case PURPLE:
g.setColor(255, 0, 255);
break;
case YELLOW:
g.setColor(255, 255, 0);
break;
case TURQUOISE:
g.setColor(0, 255, 255);
break;
case WHITE:
g.setColor(255, 255, 255);
break;
}
int x = cellWidth * ix;
int y = cellHeight *iy;
// cel plaatsen
if (field[iy][ix] == WHITE) {
g.fillRect(x, y, cellWidth, cellHeight);
} else {
g.fillRect(x, y, cellWidth - 3, cellHeight - 4);
g.setColor(0, 0, 0);
g.drawRect(x, y, cellWidth - 3, cellHeight - 4);
}
}
public void keyPressed(int pKeyCode) {
if (gameFinished) {
return;
}
// if
if (pKeyCode != Canvas.KEY_NUM5) {
updateCrosshairsOnly = true;
CellSet.addElement(new CellBlock(cursorIndexPosX, cursorIndexPosY));
}
// debug
if (Canvas.KEY_NUM7 != pKeyCode) {
// Translate native keycodes to standard codes
pKeyCode = getGameAction(pKeyCode);
}
switch (pKeyCode) {
// case -1:
case Canvas.UP:
// case Canvas.KEY_NUM2: // up
if (cursorIndexPosY > 0)
cursorIndexPosY--;
break;
// case -2:
case Canvas.DOWN:
// case Canvas.KEY_NUM8: // down
if (cursorIndexPosY < 9)
cursorIndexPosY++;
break;
// case -3:
case Canvas.LEFT:
// case Canvas.KEY_NUM4: // left
if (cursorIndexPosX > 0)
cursorIndexPosX--;
break;
// case -4:
case Canvas.RIGHT:
// case Canvas.KEY_NUM6: // right
if (cursorIndexPosX < 9)
cursorIndexPosX++;
break;
case Canvas.KEY_NUM7: // debug toetsen
updateCrosshairsOnly = false;
CellSet.removeAllElements();
drawWholeScreen = true;
// drawWholeBoard();
break;
case Canvas.FIRE: // vuur
int selectedColor = field[cursorIndexPosY][cursorIndexPosX];
// WIT? nix doen
if (selectedColor != WHITE) {
// buurcellen met zelfde kleur
gatherColoredNeighbors(new CellBlock(cursorIndexPosX, cursorIndexPosY), selectedColor);
score += (CellSet.size() * CellSet.size());
CellBlock cell;
// alle verdwenen cellen
for (int i = 0; i < CellSet.size(); i++) {
cell = (CellBlock) CellSet.elementAt(i);
field[cell.yval][cell.xval] = MARKED;
}
// cellen vallen
while (CellSet.size() > 0 && (cell = (CellBlock) CellSet.firstElement()) != null) {
affectedSlices.addElement(new Integer(cell.xval));
// mag niet dubbel gebeuren
CellSet.removeElement(cell);
int startposY = cell.yval;
int yPosUp = startposY;
int yPosDown = startposY;
int uppermostY = startposY; // hoogste
int lowermostY = startposY; // laagste
boolean sliceUpWardsInterruped = false;
boolean sliceDownWardsInterruped = false;
while (yPosDown < fieldHeight || yPosUp >= 0) {
// hoogste en laagste aanduiden
// alles verwijderen van celset
if (--yPosUp >= 0 && field[yPosUp][cell.xval] == MARKED && !sliceUpWardsInterruped) {
if (field[yPosUp][cell.xval] != MARKED) {
sliceUpWardsInterruped = true;
} else {
uppermostY = yPosUp;
CellBlock c = new CellBlock(cell.xval, yPosUp);
CellSet.removeElement(c);
}
} else {
sliceUpWardsInterruped = true;
}
if (++yPosDown < fieldHeight && field[yPosDown][cell.xval] == MARKED
&& !sliceDownWardsInterruped) {
if (field[yPosDown][cell.xval] != MARKED) {
sliceDownWardsInterruped = true;
} else {
lowermostY = yPosDown;
CellSet.removeElement(new CellBlock(cell.xval, yPosDown));
}
} else {
sliceDownWardsInterruped = true;
}
if (sliceUpWardsInterruped && sliceDownWardsInterruped) {
break;
}
}
multiArrayMoveSliceY(uppermostY, lowermostY, cell.xval);
}
CellSet.removeAllElements();
// lege naar linkerhoek sturen
for (int i = 0; i < fieldWidth - 1; i++) {
if (field[fieldHeight - 1][i] == WHITE) {
moveSliceLeft(i);
break;
}
}
}
gameFinished = isGameFinished();
}// END SWITCH
// draw nieuwe
repaint();
}
private void moveSliceLeft(int x) {
if (x > fieldWidth) {
return;
}
Vector storage = new Vector(10);
Vector slice;
for (int i = x; i < fieldWidth; i++) {
slice = new Vector(10);
for (int j = 0; j < fieldHeight; j++) {
int tmp = field[j][i];
if (tmp != WHITE) {
slice.addElement(new CellBlock(i, j, field[j][i]));
field[j][i] = WHITE;
} else {
continue;
}
}
if (slice.size() > 0) {
storage.addElement(slice);
}
affectedSlices.addElement(new Integer(i));
}
for (int i = 0; i < storage.size(); i++) {
slice = (Vector) storage.elementAt(i);
for (int j = 0; j < slice.size(); j++) {
CellBlock c = (CellBlock) slice.elementAt(j);
field[c.yval][x + i] = c.color;
}
}
}
protected void showNotify() {
drawWholeScreen = true;
}
private void multiArrayMoveSliceY(int uppermostY, int lowermostY, int xIndex) {
// cellen naar beneden kopieren
int distance = (lowermostY - uppermostY) + 1;
for (int i = lowermostY; i >= 0; i--) {
int sourceField = i - distance;
if (sourceField >= 0) {
field[i][xIndex] = field[sourceField][xIndex];
} else {
field[i][xIndex] = WHITE;
}
}
}
private void printArraySlice(int xIndex) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb.append(field[i][xIndex] + "|");
}
System.out.println(sb);
}
public boolean isGameFinished() {
boolean noMoreContiguousBlocks = true;
for (int i = 0; i < fieldWidth; i++) {
for (int j = 0; j < fieldHeight; j++) {
if (field[j][i] != WHITE && hasNeighbors(new CellBlock(i, j, field[j][i]))) {
return false;
}
}
}
return true;
}
private boolean hasNeighbors(CellBlock pCell) {
Vector v = new Vector(10);
// check for cell with same colour above the current cell
if (pCell.yval > 0 && field[pCell.yval - 1][pCell.xval] == pCell.color) {
return true;
}
// cell met dezelfde kleur eronder?
if (pCell.yval < fieldHeight) {
if (pCell.yval + 1 < fieldHeight) {
if (field[pCell.yval + 1][pCell.xval] == pCell.color) {
return true;
}
}
}
// cel zelfde kleur links?
if (pCell.xval > 0 && field[pCell.yval][pCell.xval - 1] == pCell.color) {
return true;
}
// cel zelfde kleur rechts?
if (pCell.xval < fieldWidth) {
if (pCell.xval + 1 < fieldWidth) {
if (field[pCell.yval][pCell.xval + 1] == pCell.color) {
return true;
}
}
}
return false;
}
// doorlopende ruimtes
private void gatherColoredNeighbors(CellBlock pCell, int pColor) {
Vector v = new Vector(10);
// cel zelfde kleur boven?
if (pCell.yval > 0 && field[pCell.yval - 1][pCell.xval] == pColor) {
CellBlock c = new CellBlock(pCell.xval, pCell.yval - 1);
if (!CellSet.contains(c)) {
v.addElement(c);
CellSet.addElement(c);
}
}
// cel kleur beneden?
if (pCell.yval < fieldHeight) {
if (pCell.yval + 1 < fieldHeight) {
if (field[pCell.yval + 1][pCell.xval] == pColor) {
CellBlock c = new CellBlock(pCell.xval, pCell.yval + 1);
if (!CellSet.contains(c)) {
v.addElement(c);
CellSet.addElement(c);
}
}
}
}
// kleur links?
if (pCell.xval > 0 && field[pCell.yval][pCell.xval - 1] == pColor) {
CellBlock c = new CellBlock(pCell.xval - 1, pCell.yval);
if (!CellSet.contains(c)) {
v.addElement(c);
CellSet.addElement(c);
}
}
// cel rechts?
if (pCell.xval < fieldWidth) {
if (pCell.xval + 1 < fieldWidth) {
if (field[pCell.yval][pCell.xval + 1] == pColor) {
CellBlock c = new CellBlock(pCell.xval + 1, pCell.yval);
if (!CellSet.contains(c)) {
v.addElement(c);
CellSet.addElement(c);
}
}
}
}
// recursive call for all cells of the same colour that were found
for (int i = 0; i < v.size(); i++) {
gatherColoredNeighbors((CellBlock) v.elementAt(i), pColor);
}
// geen buren dus geen verdwijntruucs
if(CellSet.size() == 1){
CellSet.removeAllElements();
}
}
private int getRandomNumber() {
int diff = rand.nextInt();
if (diff < 0)
diff *= -1;
return diff % 6;
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < fieldWidth; i++) {
for (int j = 0; j < fieldHeight; j++) {
sb.append(field[i][j] + " ");
}
sb.append("\n");
}
return sb.toString();
}
// x en y representatie
private class CellBlock {
int xval;
int yval;
int color;
public CellBlock(int x, int y) {
this.xval = x;
this.yval = y;
}
public CellBlock(int x, int y, int color) {
this.xval = x;
this.yval = y;
this.color = color;
}
public boolean equals(Object o) {
if (!(o instanceof CellBlock)) {
return false;
}
if (o == this) {
return true;
}
CellBlock c = (CellBlock) o;
if (c.xval == xval && c.yval == yval) {
return true;
}
return false;
}
public String toString() {
return "X" + xval + "/Y" + yval;
}
}
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
Colorpop b = (Colorpop) Colorpop;
try {
b.destroyApp(false);
} catch (MIDletStateChangeException e) {
throw new RuntimeException(e.getMessage());
}
b.notifyDestroyed();
} else if (c == startCmd) {
score = 0;
gameFinished = false;
initialDraw = true;
repaint();
} else if (c == instructCmd) {
((Colorpop) Colorpop).getDisplay().setCurrent(instructionForm);
} else if (c == aboutCmd) {
((Colorpop) Colorpop).getDisplay().setCurrent(aboutForm);
}else if( c == highScoreCmd){
highScoreStringItem = new StringItem("Jouw huidige score is: "+score+" Highscores: ",highScore.getSimpleList());
if(highScoreForm.size() == 0){
highScoreForm.append(highScoreStringItem);
}else{
highScoreForm.set(0,highScoreStringItem);
}
((Colorpop) Colorpop).getDisplay().setCurrent(highScoreForm);
}
}
public int getScore (){
return score;
}
public String getPlayerName(){
return null;
}
public ObserverManager getObserverManager() {
return observerManager;
}[/COLOR][/B]
Laatst bewerkt door een moderator: