package pylos.model;

import pylos.exception.PylosError;

public class Model {
	public final int levels;
	public final Ball[] balls;
	private final Player[] players;

	public Model() { this(4); }
	public Model(int levels) {
		int nb_balls = ballsForLevels(levels);
		if (nb_balls % 2 != 0) {
			throw new PylosError("There must be an even number of balls, please choose a correct number of levels (4, 6, ...)");
		}
		this.levels = levels;

		players = new Player[] { new Player(), new Player() };
		balls = new Ball[nb_balls];
		for (int i = 0; i < balls.length/2; i++) {
			balls[i] = new Ball(players[0], 5.5f, 0.8f, i-7);
		}
		for (int i = balls.length/2; i < balls.length; i++) {
			balls[i] = new Ball(players[1], -5.5f, 0.8f, i-7-balls.length/2);
		}
	}

	/** number of balls for a number of levels.
	 * @param n number of levels
	 *
	 * for n = 4: 1 + 4 + 9 + 16
	 * = sum(1,n) n*n
	 * = 1/3n^3 + 1/2n^2 + 1/6n
	 * (by polynomial interpolation)
	 */
	private static int ballsForLevels(int n) {
		return (2*n*n*n + 3*n*n + n)/6;
	}
}

@@ -128,22 +128,81 @@
 alls;%0A%09p
-rivate
+ublic final Player player1;%0A%09public final Player player2;%0A%09public
  final P
@@ -501,17 +501,17 @@
 %09%09player
-s
+1
  = new P
@@ -519,43 +519,86 @@
 ayer
-%5B%5D %7B new Player(), new Player()
+();%0A%09%09player2 = new Player();%0A%09%09players = new Player%5B%5D %7B player1, player2
  %7D;%0A
+%0A
 %09%09ba
@@ -701,12 +701,9 @@
 ayer
-s%5B0%5D
+1
 , 5.
@@ -811,12 +811,9 @@
 ayer
-s%5B1%5D
+2
 , -5

split Player array players into two variables to ease access