Skip to content
Snippets Groups Projects
Commit 799120d9 authored by Justin's avatar Justin
Browse files

finished game logic, game should be fully playable

parent f51147d1
No related branches found
No related tags found
No related merge requests found
PicPuzzle/level1.bmp

246 B

PicPuzzle/level2.png

1.21 KiB

PicPuzzle/level3.bmp

822 B

......@@ -10,9 +10,12 @@ import java.io.IOException;
* should show and reacts to input
*/
public class GameLogic implements NonogramEventListener, ActionListener {
private static final String[] LEVEL_EXTENSIONS = {".bmp",".png"};
private Nonogram currrentNonogram;
private NonogramComponent nonogramComponent;
private MainFrame mainFrame;
private int level;
private boolean won;
/**
......@@ -20,12 +23,13 @@ public class GameLogic implements NonogramEventListener, ActionListener {
* @throws IOException
*/
public GameLogic() throws IOException {
level = 1;
won = false;
currrentNonogram = new Nonogram(new File("cross.bmp"));
nonogramComponent = new NonogramComponent(currrentNonogram);
nonogramComponent = new NonogramComponent(new Nonogram(8, 8));
mainFrame = new MainFrame(nonogramComponent);
mainFrame.addNextButtonListener(this);
nonogramComponent.addListener(this);
loadLevel(level);
}
/**
......@@ -39,10 +43,9 @@ public class GameLogic implements NonogramEventListener, ActionListener {
@Override
public void nonogramClicked(int gridX, int gridY, boolean leftButton) {
if (leftButton && currrentNonogram.getPixel(gridX, gridY)) {
if (!won && leftButton && currrentNonogram.getPixel(gridX, gridY)) {
nonogramComponent.setPixel(gridX, gridY, true);
if (nonogramComponent.getBoardState().equals(currrentNonogram)) {
won = true;
mainFrame.setTitleLabel("LEVEL COMPLETED");
mainFrame.setNextButtonEnabled(true);
}
......@@ -51,16 +54,40 @@ public class GameLogic implements NonogramEventListener, ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
currrentNonogram = new Nonogram(new File("test.bmp"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
level++;
if (!loadLevel(level)) {
mainFrame.setTitleLabel("YOU WIN!");
won = true;
mainFrame.setNextButtonEnabled(false);
}
nonogramComponent.setNonogram(currrentNonogram);
mainFrame.pack();
mainFrame.setTitleLabel("Level 2");
mainFrame.setNextButtonEnabled(false);
}
private boolean loadLevel(int level) {
boolean fileLoaded = false;
String levelFileName = "level"+level;
File levelFile = null;
for (int i = 0; i < LEVEL_EXTENSIONS.length; i++) {
try {
levelFile = new File(levelFileName + LEVEL_EXTENSIONS[i]);
currrentNonogram = new Nonogram(levelFile);
fileLoaded = true;
break;
} catch (Exception e) {}
}
if (fileLoaded) {
try {
nonogramComponent.setNonogram(currrentNonogram);
mainFrame.pack();
mainFrame.setTitleLabel("Level " + level);
mainFrame.setNextButtonEnabled(false);
} catch (Exception e) {
System.out.println(e.toString());
fileLoaded = false;
}
}
return fileLoaded;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment