Implemented populate function

This commit is contained in:
id101010
2016-05-27 11:54:48 +02:00
parent 7ffd3cdb12
commit 78085a5aaa

View File

@@ -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<Integer> 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<Integer> getNextBlocks(){
return null;
return nextBlocks;
}
public int[][] getField(){
@@ -69,7 +80,45 @@ public class Game {
}
public void reset(){
// Initialize new blocks
nextBlocks = new ArrayList<Integer>();
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));
}
}
}
}