View Full Version : [Task 13.1.2] Ufo vs Bonfo
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
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.
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
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;
}
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());
}
Ecco risolto.
Metodo update in Gem:
public void update(TimerInterface timer)
{
if(!this.getType().isStone())
{
long animationTime = computeAnimationTime(timer);
setCurrentFrame(findAnimationFrame(animationTime));
}
}
GREEN
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... ;)
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));
}
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 ;)
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));
}
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
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());
}
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...
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 :)
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 ;)
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());
}
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());
}
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
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
Perchè la metti visibile a livello di classe ? Non la puoi mettere visibile solo a livello di metodo ?
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:
Sicuramente non è efficiente...ma non mi sembra che ci sia bisogno di preoccuparsi dell'efficienza...
Bhè....finchè possiamo risparmiare con poco facciamolo ;)
Comunque grazie al tuo consiglio ho risolto :D
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.