m1.chocobarlite/src/chocobar/combi/ChocoBarLong.java

224 lines
4.8 KiB
Java
Raw Normal View History

2009-05-01 08:07:06 +00:00
package chocobar.combi;
import java.lang.Long;
class ChocoBarLong extends ChocoBar {
/// Pr<50>calcul des puissances.
static private long[] powers;
/// Repr<70>sente la ChocoBar
private long points;
/**
* Constructeur d'une barre de choco.
* Elle a x carres en largeur et y carres en hauteur, et est toute neuve.
**/
private ChocoBarLong()
{
points = 0;
}
/** Constructeur par copie.
*/
private ChocoBarLong(ChocoBarLong old)
{
points = old.points;
}
/** Constructeur par initialisation.
*/
private ChocoBarLong(long bi)
{
points = bi;
}
/** Constructeur par initialisation.
*/
private ChocoBarLong(int c[])
{
points = 0;
for (int i=0; (i<size); i++) {
points += powers[i] * c[size-i-1];
}
// XXX - DEBUG
// System.out.println(
// "ChocoBarLong(" + toLine(c) + ")"
// + " = " + toLine()
// + " = " + toNumber()
// );
// XXX - DEBUG
}
/**
* Affichage de la barre de choco en nombre.
**/
public String toNumber()
{
String s = (new Long(points)).toString();
return s;
}
/**
* Affichage de la barre de choco en ligne.
**/
public String toLine()
{
String s = "";
for (int i=0; (i<size); i++) {
s += get(i) + " ";
}
return s;
}
/**
* Affichage de la barre de choco en ligne.
**/
private static String toLine(int c[])
{
String s = "";
for (int i=0; (i<size); i++) {
s += c[i] + " ";
}
return s;
}
private int get(int x)
{
int i = (size - 1) - x;
long value = (points % powers[i+1]) / powers[i];
return (int) value;
}
/**
* V<EFBFBD>rifie si le carre (x, y) est d<EFBFBD>j<EFBFBD> mang<EFBFBD>.
**/
protected boolean isBroken(int cX, int cY)
{
return ((height - cY) <= get(cX));
}
/**
* Comparaison de barres.
* La conversion entre type de ChocoBar n'est pas impl<EFBFBD>ment<EFBFBD>e
**/
public boolean equals(ChocoBar comp) { return equals((ChocoBarLong) comp); }
public boolean equals(ChocoBarLong comp)
{
return points == comp.points;
}
/** Recherche si l'on peut <EFBFBD>tre un fils de la ChocoBar.
* La conversion entre type de ChocoBar n'est pas impl<EFBFBD>ment<EFBFBD>e
*/
public boolean isNextOf(ChocoBar parent) { return isNextOf((ChocoBarLong) parent); }
public boolean isNextOf(ChocoBarLong parent)
{
// TODO - Il faudrait am<61>liorer cet algo, en fonction des donn<6E>es que
// Long peut nous offrir
int eaten = -1;
for (int i=0; (i<size); i++) {
// Si le parent est plus mang<6E> que le fils, c'est toujours non !
if (parent.get(i) > get(i)) {
return false;
}
if (eaten == -1) {
if (parent.get(i) != get(i)) {
// On a mang<6E> ce carr<72>, les carr<72>s suivants doivent donc <20>tre
// identiquent <20> celui-ci ou <20> celui du p<>re
eaten = get(i);
// On a trait<69> ce carr<72>, on passe au suivant
continue;
}
// Le carr<72> est identique, suivant !
continue;
}
// On a mang<6E> le carr<72> pr<70>c<EFBFBD>dent, ils doivent donc <20>tre
// identiques au point pr<70>c<EFBFBD>dents, ou <20> celui que l'on vient de
// manger
if (parent.get(i) != get(i) && get(i) != eaten) {
return false;
}
}
// Si on arrive ici, c'est que tout c'est bien pass<73>, a-t-on un carr<72>
// mang<6E> ?
return (eaten != -1);
}
/** Usine pour construire une liste de ChocoBar.
* Cette usine contruit une liste exhaustive et ordonn<EFBFBD>e de ChocoBar.
*/
public static ChocoBar[] genChocoBars(int x, int y)
{
// On mets <20> jour les variables statiques de taille
ChocoBarLong.size = x;
ChocoBarLong.height = y;
// Pr<50>calcul des puissances
long pows = 1;
ChocoBarLong.powers = new long[x+1];
for (int i=0; (i<=x); i++) {
ChocoBarLong.powers[i] = pows;
pows *= y + 1;
}
// Calcul du nombre de chocobars diff<66>rentes existantes
int nbChocobars = nbChocoBars(x, y);
// Hop, on r<>serve le tableau de r<>sultat...
ChocoBar[] chocoBars = new ChocoBar[nbChocobars];
// Il est temps de remplir le tableau !
// Cr<43>ation du tableau pour stocker les valeurs
int[] points = new int[x];
boolean fin = false;
int cbIdx = 0;
while (!fin) {
// Affectation
chocoBars[cbIdx] = new ChocoBarLong(points);
cbIdx++;
// Calcul du suivant :
// On incr<63>mente ...
int idx = x-1;
points[idx] ++;
// ... et on g<>re la retenue
boolean overflow = (points[idx] > y);
while (points[idx] > y) {
if (idx == 0) {
fin = true;
break;
}
points[idx-1] ++;
idx--;
}
// On r<>percute la modif vers la droite s'il y a eu overflow
if (overflow) {
for (int i=idx; (i<x); i++) {
points[i] = points[idx];
}
}
}
return chocoBars;
}
public static ChocoBar[] genChocoBars(Integer x, Integer y)
{
return genChocoBars(x.intValue(), y.intValue());
}
// Impl<70>mentation de Vertice
public void setLabel(String l) { }
public String getLabel() { return "null"; }
public void setColor(int c) { }
public int getColor() { return 0; }
}