PDA

View Full Version : [Task 13.1.2] Ufo vs Bonfo


Bonfo
22-03-2006, 14:47
13.1.2:
Cambiare il codice che crea le stone in modo che il frame da mostrare durante la caduta sia dipendente dalla riga in cui è previsto che la pietra andra a collidere.
Per sapere quale frame usare seguite la tabella:
Riga Frame
13-12 2
11-10 3
9-7 4
6-4 5
3-0 6

Ufo13
22-03-2006, 15:02
Test List:

- Per tutte le fasce dove va a cadere la Stone deve essere settato il frame corretto.
- Una Stone contiene 8 frame.
- I frame della Stone sono posizionati correttamente.
- Il frame corrente deve poter essere settato in base al valore che deve essere visualizzato.

Bonfo
22-03-2006, 15:03
Partiamo col primo test...mooolto facile:

public void testStoneNumberOfFrames()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);
assertEquals("Stone must have 8 frames", 8, stone.getNumberOfFrames());
}


Partiamo....!!!! :D

Ufo13
22-03-2006, 15:37
Green


public Gem createStone(GemType gemType)
{
Gem newStone = Gem.createStone(gemType);

return setupGemAnimationAndSound(newStone);
}



public static Gem createStone(GemType type)
{
Gem newStone = new Gem(type, 0);
newStone.numberOfFramesInTexture = 8;

return newStone;
}

Ufo13
22-03-2006, 16:53
Nuovo test:

public void testStoneIsNotAnimated()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);

grid.insertGem(13, 2, stone);

grid.updateGemAnimations(timer);
int startingFrame = stone.getCurrentFrame();

timer.advance(config.getInteger("GemAnimationUpdateRate")+1);
grid.updateGemAnimations(timer);

assertEquals("Stone must not be animated", startingFrame, stone.getCurrentFrame());
}

Bonfo
22-03-2006, 16:53
Ecco risolto.
Metodo update in Gem:

public void update(TimerInterface timer)
{
if(!this.getType().isStone())
{
long animationTime = computeAnimationTime(timer);

setCurrentFrame(findAnimationFrame(animationTime));
}
}


GREEN

Bonfo
22-03-2006, 17:00
Ecco il prossimo test:

public void testStoneFrameInSecondPortion()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);

insertAndUpdate(stone, 0, 4);
assertEquals("Stone must using the second frame", 1, stone.getCurrentFrame());
}


a te la palla... ;)

Ufo13
22-03-2006, 19:08
commento un secondo il tuo test...

Aggiungo il seguente test in TestGrid:


public void testColumnHeightIsZero()
{
assertEquals("Column height must be 0", 0, grid.getHeightOfColumn(0));
}


con QuickFix passa da solo :)

allora ti propongo questo che fallisce:

public void testColumnHeightIsOne()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
assertEquals("Column height must be 1", 1, grid.getHeightOfColumn(2));
}

Bonfo
23-03-2006, 00:05
Risolto!
Aggiunto il metodo in grid.


public int getHeightOfColumn(int column)
{
int value = 0;
for(int i = grid.length - 1; i >= 0; i--)
{
Gem gem = grid[i][column];
if(gem == null)
{
continue;
}
value++;
}
return value;
}


GREEN

Di nuovo a te ;)

Ufo13
23-03-2006, 11:10
Nuovo test :D


public void testColumnHeightWithHoles()
{
Gem floatingGem = createGem(DIAMOND);
floatingGem.drop();
insertAndUpdate(floatingGem, 4, 2);

assertEquals("Column height must be 10", 10, grid.getHeightOfColumn(2));
}

Bonfo
23-03-2006, 11:33
Ecco il codice relativo a questo test.
Gl'indici mi hanno fatto un po' impazzire..ma alla fine ho vinto io ;)


public int getHeightOfColumn(int column)
{
int i = 0;

while(i < grid.length && grid[i][column] == null)
{
i++;

}

return grid.length - i ;
}


..non è rimasto molto dell'originale :D

GREEN

Ufo13
23-03-2006, 12:52
A questo punto mi permetto di fare un piccolo cambiamento nel test postato da Bonfo:


public void testStoneFrameInSecondPortion()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);

grid.insertStoneIntoColumn(stone);
assertEquals("Stone must using the second frame", 1, stone.getCurrentFrame());
}

Ufo13
23-03-2006, 13:08
Aggiunto test:


public void testSetAndGetCurrentFrame()
{
Gem gem = createGem();
gem.createAnimationSequence(0);

gem.setCurrentFrame(1);
assertEquals(1, gem.getCurrentFrame());

gem.setCurrentFrame(2);
assertEquals(2, gem.getCurrentFrame());
}


Passa da solo :)


public void insertStoneIntoColumn(Gem stone)
{
stone.setCurrentFrame(1);
}


Il test di Bonfo ora passa.

Preparo nuovo test...

Ufo13
23-03-2006, 13:20
public void testStoneFrameInSecondSegment()
{
GemFactory gemFactory = GemFactory.createForTesting(null);

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 11, 4);

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);
grid.insertStoneIntoColumn(stone, 4);

assertEquals("Stone must be using the second frame", 1, stone.getCurrentFrame());
}


A te :)

Bonfo
23-03-2006, 13:54
Fatto ;)


public void insertStoneIntoColumn(Gem stone, int column)
{
if(getHeightOfColumn(column)<=1)
{
stone.setCurrentFrame(0);
}
else
{
stone.setCurrentFrame(1);
}
}


GREEN

Ed ecco subito il prossimo test

public void testStoneFrameInThirdSegment()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 11, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 10, 4);

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);
grid.insertStoneIntoColumn(stone, 4);

assertEquals("Stone must be using the third frame", 2, stone.getCurrentFrame());
}


buon lavoro ;)

Bonfo
23-03-2006, 20:00
Ufo ha un po' di problemi ... impegni improvvisi ;)
Vado avanti io :D

SOLUZIONE:

public void insertStoneIntoColumn(Gem stone, int column)
{
if(getHeightOfColumn(column)<=1)
{
stone.setCurrentFrame(0);
}
else if(getHeightOfColumn(column)<=3)
{
stone.setCurrentFrame(1);
}
else
{
stone.setCurrentFrame(2);
}
}


GREEN

Ecco il prossimo test:

public void testStoneFrameInFourthSegment()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 11, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 10, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 9, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 8, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 7, 4);

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);
grid.insertStoneIntoColumn(stone, 4);

assertEquals("Stone must be using the fourth frame", 3, stone.getCurrentFrame());
}

Bonfo
23-03-2006, 20:09
SOLUZIONE:

public void insertStoneIntoColumn(Gem stone, int column)
{
if(getHeightOfColumn(column)<=1)
{
stone.setCurrentFrame(0);
}
else if(getHeightOfColumn(column)<=3)
{
stone.setCurrentFrame(1);
}
else if(getHeightOfColumn(column)<=6)
{
stone.setCurrentFrame(2);
}
else
{
stone.setCurrentFrame(3);
}
}


GREEN

Ecco il prossimo test:

public void testStoneFrameInFifthSegment()
{
int[] randomSequence = {0};
GemFactory gemFactory = GemFactory.createForTesting(new MockRandomGenerator(randomSequence));

insertAndUpdate(gemFactory.create(GemType.EMERALD), 13, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 12, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 11, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 10, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 9, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 8, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 7, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 6, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 5, 4);
insertAndUpdate(gemFactory.create(GemType.EMERALD), 4, 4);

Gem stone = gemFactory.createStone(GemType.EMERALD_STONE);
grid.insertStoneIntoColumn(stone, 4);

assertEquals("Stone must be using the fifth frame", 4, stone.getCurrentFrame());
}

Bonfo
23-03-2006, 20:10
Ecco la soluzion all'ultimo test.

public void insertStoneIntoColumn(Gem stone, int column)
{
if(getHeightOfColumn(column)<=1)
{
stone.setCurrentFrame(0);
}
else if(getHeightOfColumn(column)<=3)
{
stone.setCurrentFrame(1);
}
else if(getHeightOfColumn(column)<=6)
{
stone.setCurrentFrame(2);
}
else if(getHeightOfColumn(column)<=9)
{
stone.setCurrentFrame(3);
}
else
{
stone.setCurrentFrame(4);
}
}


GREEN

Bonfo
23-03-2006, 20:54
Siccome il metodo così fa abbastanza schifo, ecco un piccolo refactoring basato su una idea di Ufo.



private static final int[] STONES_FRAMES_BASED_ON_ROW = {0,0,1,1,2,2,2,3,3,3,4,4,4,4};

public void insertStoneIntoColumn(Gem stone, int column)
{
stone.setCurrentFrame(STONES_FRAMES_BASED_ON_ROW[getHeightOfColumn(column)]);
}



Funziona perfettamente...ma non il checkstyle non mi fa passare il
private static final...quindi niente commit :cry:

TASK COMPLETATO

cionci
23-03-2006, 20:57
Perchè la metti visibile a livello di classe ? Non la puoi mettere visibile solo a livello di metodo ?

Bonfo
24-03-2006, 00:54
non so se ho capito bene...ma come dici tu ogni volta che viene invocato il metodo il vettore viene riallocato.
Non mi sembra molto efficente come cosa..o no?? :wtf: :mbe:

cionci
24-03-2006, 01:15
Sicuramente non è efficiente...ma non mi sembra che ci sia bisogno di preoccuparsi dell'efficienza...

Bonfo
24-03-2006, 01:39
Bhè....finchè possiamo risparmiare con poco facciamolo ;)
Comunque grazie al tuo consiglio ho risolto :D