diff --git a/.gitignore b/.gitignore index a031ac2..ae3c172 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1 @@ -*.class - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* /bin/ diff --git a/src/ch/bfh/sevennotseven/FieldCanvas.java b/src/ch/bfh/sevennotseven/FieldCanvas.java index 13b684a..cb07e4a 100644 --- a/src/ch/bfh/sevennotseven/FieldCanvas.java +++ b/src/ch/bfh/sevennotseven/FieldCanvas.java @@ -3,14 +3,19 @@ package ch.bfh.sevennotseven; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; +import java.awt.event.MouseAdapter; -public class FieldCanvas extends Canvas { +public class FieldCanvas extends Canvas{ private int size; private int[][] field; static final Color[] colors = {Color.red,Color.green, Color.blue, Color.yellow,Color.magenta}; + FieldCanvas(){ + + } + public void setSize(int s) { this.size = s; } diff --git a/src/ch/bfh/sevennotseven/Game.java b/src/ch/bfh/sevennotseven/Game.java index ec50923..4564116 100644 --- a/src/ch/bfh/sevennotseven/Game.java +++ b/src/ch/bfh/sevennotseven/Game.java @@ -16,6 +16,7 @@ public class Game { private int level; private int score; private int size; + private int freeBlocks; private Random rand; public Game(){ @@ -23,9 +24,14 @@ public class Game { } public Game (int size) { + /* Initialize variables */ this.size = size; + this.level = 1; + this.score = 0; + this.freeBlocks = size * size; + + rand = new Random(); // Initialize random object this.reset(); - } public boolean isGameOver(){ @@ -39,7 +45,7 @@ public class Game { } public int getLevel(){ - return 0; + return level; } public List getNextBlocks(){ @@ -90,9 +96,6 @@ public class Game { field = new int[size][size]; level = 1; score = 0; - - // Initialize random object - rand = new Random(); // Populate game field this.populateField(); @@ -103,23 +106,23 @@ public class Game { * Add n new blocks to random positions on the field, * according to the level number. */ - private void populateField(){ + public void populateField(){ // while there are blocks left in nextBlocks - while(nextBlocks.size() > 0){ + while((nextBlocks.size() > 0) && (freeBlocks > 0)){ int x = rand.nextInt(size); // get random x position int y = rand.nextInt(size); // get random y position // if the position is free if(field[x][y] == 0){ field[x][y] = nextBlocks.remove(0); // fill with the first element of nextBlocks + freeBlocks--; } - } // add n new colors to nextBlocks according to the level number. for(int i = 0; i < (level * 3); i++){ - nextBlocks.add(rand.nextInt(numberOfColors)); + nextBlocks.add(1 + rand.nextInt(numberOfColors)); } } } diff --git a/src/ch/bfh/sevennotseven/Window.java b/src/ch/bfh/sevennotseven/Window.java index 4ff8d9d..a139488 100644 --- a/src/ch/bfh/sevennotseven/Window.java +++ b/src/ch/bfh/sevennotseven/Window.java @@ -5,6 +5,8 @@ package ch.bfh.sevennotseven; import java.awt.Frame; import java.awt.HeadlessException; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -37,14 +39,20 @@ public class Window extends Frame { field.setSize(7); game = new Game(); + field.addMouseListener(new MouseAdapter(){ + @Override + public void mousePressed(MouseEvent e) { + super.mousePressed(e); + game.populateField(); + field.repaint(); + } + }); this.add(field); this.setSize(400,400); this.setVisible(true); field.setField(game.getField()); - - } /**