Added onclick creation of random blocks. And some Bugfixes.

This commit is contained in:
id101010
2016-06-14 17:09:33 +02:00
parent f7c271b89e
commit 01be52e123
4 changed files with 28 additions and 24 deletions

View File

@@ -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;
}

View File

@@ -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<Integer> 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));
}
}
}

View File

@@ -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());
}
/**