diff --git a/src/ch/bfh/sevennotseven/Game.java b/src/ch/bfh/sevennotseven/Game.java index eaf26dc..cbc6738 100644 --- a/src/ch/bfh/sevennotseven/Game.java +++ b/src/ch/bfh/sevennotseven/Game.java @@ -1,19 +1,30 @@ package ch.bfh.sevennotseven; import java.awt.Point; +import java.util.ArrayList; import java.util.List; +import java.util.Random; public class Game { - private int[][] field; + // Constants + static final int numberOfColors = 5; + // Private members + private int[][] field; + private ArrayList nextBlocks; + private int level; + private int score; + private int size; + private Random rand; public Game(){ this(7); } public Game (int size) { - field = new int[size][size]; + this.size = size; + this.reset(); } @@ -32,7 +43,7 @@ public class Game { } public List getNextBlocks(){ - return null; + return nextBlocks; } public int[][] getField(){ @@ -69,7 +80,45 @@ public class Game { } public void reset(){ + // Initialize new blocks + nextBlocks = new ArrayList(); + nextBlocks.add(1); + nextBlocks.add(2); + nextBlocks.add(3); + // Initialize field, level and score + field = new int[size][size]; + level = 1; + score = 0; + + // Initialize random object + rand = new Random(); + + // Populate game field + this.populateField(); } + /* + * Calculates the next game step. + * Add n new blocks to random positions on the field, + * according to the level number. + */ + private void populateField(){ + + // while there are blocks left in nextBlocks + while(nextBlocks.size() > 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 + } + + // add n new colors to nextBlocks according to the level number. + for(int i = 0; i < (level * 3); i++){ + nextBlocks.add(rand.nextInt(numberOfColors)); + } + } + } }