View Full Version : [TASK 9.1.2] Pair Programming : Ufo13 vs Bonfo
Bene...inziamo questo nuovo task!
9.1.2:
Ogni volta che entrambe le gemme di una gemspair si sono fermate grid deve controllare se ci sono dei bauli presenti all'Interno della griglia. Per ognuno di questi bauli deve controllare se esistono gemme o agglomerati dello stesso colore con almeno un lato a contatto col baule. Se ve ne sono allora queste gemme e agglomerati devono essere cancellate dalla griglia. Se queste gemme sono a loro volta a contatto con altre gemme dello stesso colore allora devono essere cancellate anche queste.
Premesso che i test alla fine saranno sicuramente di +...
Test List:
- Gemma + Baule adiacenti e "droppati" dello stesso tipo si cancellano.
- Gemma + Baule adiacenti, uno dei due non droppato, stesso tipo non si cancellano.
- Gemma + Baule adiacenti non dello stesso tipo non si cancellano.
- 2 Gemme + Baule adiacente solo ad una delle due, stesso tipo, si cancellano tutti.
- 2 Bauli dello stesso tipo adiacenti si cancellano a vicenda.
- 2 Bauli dello stesso tipo droppati ed adiacenti + una gemma adiacente ad uno dei due. Tutto viene cancellato.
- BigGem + Baule, stesso tipo, vengono cancellati.
- La cancellazione avviene solo quando entrambe le gemme in una gemspair sono cadute.
hmmmm mi pare bastino :D
Primo test per Bonfo:
package it.diamonds.tests;
import it.diamonds.Grid;
import it.diamonds.engine.mocks.MockTimer;
import it.diamonds.gems.Gem;
import it.diamonds.gems.GemType;
import junit.framework.TestCase;
import static it.diamonds.gems.GemType.*;
public class TestGemsCrushing extends TestCase
{
private Grid grid;
private MockTimer timer;
public void testGemsAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 3);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 3);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
private void insertAndUpdate(Gem gem, int row, int column)
{
grid.insertGem(row, column, gem);
grid.update(timer, gem);
}
private Gem createGem(GemType gemType)
{
return Gem.create(gemType, 3500);
}
}
Ecco risolto:
public void updateCrushes()
{
removeGemFromGrid(getGemAt(13,3));
removeGemFromGrid(getGemAt(13,2));
}
metodo aggiunto in Grid.
GREEN
Testiamo ora due coppie di diverso tipo:
public void testMoreGemsAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 3);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 2);
insertAndUpdate(createGem(EMERALD), 13, 5);
insertAndUpdate(createGem(EMERALD_CHEST), 13, 6);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
A te...
Build Verde:
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
if (gem == null)
{
continue;
}
removeGemFromGrid(gem);
}
}
}
Nuovo test per Bonfo:
public void testNotCrushingOnDifferentType()
{
insertAndUpdate(createGem(DIAMOND), 13, 3);
insertAndUpdate(createGem(EMERALD_CHEST), 13, 2);
insertAndUpdate(createGem(DIAMOND), 13, 5);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 6);
grid.updateCrushes();
assertEquals("grid must contain two gems", 2, grid.getNumberOfGems());
}
Per soddisfare il test è necessario riuscire a valutare quando un CHEST e un GEM sono dello stesso tipo base.
Dobbiamo aggiungere un metodo per fare questo controllo.
Ecco i test per questo metodo:
public void testGemAndChestTypeEquals()
{
assertSame("The type must be the same type (DIAMOND)",DIAMOND.getBaseType(),DIAMOND_CHEST.getBaseType());
assertSame("The type must be the same type (EMERALD)",EMERALD.getBaseType(),EMERALD_CHEST.getBaseType());
assertSame("The type must be the same type (RUBY)",RUBY.getBaseType(),RUBY_CHEST.getBaseType());
assertSame("The type must be the same type (SAPPHIRE)",SAPPHIRE.getBaseType(),SAPPHIRE_CHEST.getBaseType());
assertSame("The type must be the same type (TOPAZ)",TOPAZ.getBaseType(),TOPAZ_CHEST.getBaseType());
}
public void testGemAndChestTypeNotEquals()
{
assertNotSame("The type must be different",DIAMOND.getBaseType(),EMERALD_CHEST.getBaseType());
}
Vai Ufo ...;)
private static final HashMap<GemType, GemType> chestsBaseTypes = new HashMap<GemType, GemType>();
static
{
chestsBaseTypes.put(EMERALD_CHEST, EMERALD);
chestsBaseTypes.put(RUBY_CHEST, RUBY);
chestsBaseTypes.put(SAPPHIRE_CHEST, SAPPHIRE);
chestsBaseTypes.put(TOPAZ_CHEST, TOPAZ);
chestsBaseTypes.put(DIAMOND_CHEST, DIAMOND);
}
public GemType getBaseType()
{
if (!chest)
{
return this;
}
return chestsBaseTypes.get(this);
}
Ecco fatto, build verde, puoi continuare :)
Ok...ora possiamo finire col test di prima.
Modificato updateCrushes():
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
if (gem == null)
{
continue;
}
Gem nearGem=getGemAt(gem.getCellRow(), gem.getCellColumn() + 1);
if( nearGem!= null)
{
GemType firstGemType = gem.getType().getBaseType();
GemType secondGemType = nearGem.getType().getBaseType();
if(firstGemType == secondGemType)
{
removeGemFromGrid(gem);
removeGemFromGrid(nearGem);
}
}
}
}
Ora viene controllata la corenza di tipo tra due gemme adiacenti
GREEN
fra un po' il prossimo test...
E questo? :D
public void testVerticalGemsAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND_CHEST), 12, 2);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
a te...
Bene...per risolvere questo è stato aggiunto un metodo privato
private boolean removeIfCorrectType(Gem gem,GemType gemType)
{
if( gem != null)
{
GemType thisGemType = gem.getType().getBaseType();
if(thisGemType == gemType)
{
removeGemFromGrid(gem);
return true;
}
}
return false;
}
...altrimenti la complessità ciclotomatica era troppo alta.
Poi è stato così modificato il codice di updateCrushes()
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
boolean toCancel = false;
if (gem == null)
{
continue;
}
GemType thisGemType = gem.getType().getBaseType();
Gem rightGem = getGemAt(gem.getCellRow(), gem.getCellColumn() + 1);
Gem upperGem = getGemAt(gem.getCellRow() - 1, gem.getCellColumn());
toCancel = removeIfCorrectType(upperGem,thisGemType) || removeIfCorrectType(rightGem,thisGemType);
if(toCancel)
{
removeGemFromGrid(gem);
}
}
}
}
GREEN
Nuovo test...
public void testTwoDiamondsNotCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND), 13, 3);
grid.updateCrushes();
assertEquals("grid must contain two gems", 2, grid.getNumberOfGems());
}
...buon divertimento
private boolean tryCrush(Gem gem, Gem otherGem)
{
if (otherGem == null)
{
return false;
}
if (gem.getType().getBaseType() != otherGem.getType().getBaseType())
{
return false;
}
if (!gem.getType().isChest() ^ otherGem.getType().isChest())
{
return false;
}
removeGemFromGrid(gem);
return true;
}
Build verde, preparo nuovo test :)
public void testTwoChestsCrushing()
{
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 2);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 3);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
A te :)
Fantastico risolto con 3 caratteri .... :sofico:
private boolean tryCrush(Gem gem, Gem otherGem)
{
if (otherGem == null)
{
return false;
}
if (gem.getType().getBaseType() != otherGem.getType().getBaseType())
{
return false;
}
if (!gem.getType().isChest() && !otherGem.getType().isChest())
{
return false;
}
removeGemFromGrid(gem);
return true;
}
GREEN
Nuovo test...iniziamoo con le "catene":
public void testTwoDiamondsAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND), 13, 3);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 4);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
avanti :D
Accidenti! 5 righe di test mi han fatto sbattere un poco :)
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
if (gem == null)
{
continue;
}
ArrayList<Gem> crushedGems = new ArrayList<Gem>();
detectCrushes(gem, crushedGems);
while(!crushedGems.isEmpty())
{
Gem crushedGem = crushedGems.get(0);
removeGemFromGrid(crushedGem);
crushedGems.remove(crushedGem);
}
}
}
}
private void detectCrushes(Gem gem, ArrayList<Gem> crushedGems)
{
Gem rightGem = getGemAt(gem.getCellRow(), gem.getCellColumn() + 1);
Gem leftGem = getGemAt(gem.getCellRow(), gem.getCellColumn() - 1);
Gem upGem = getGemAt(gem.getCellRow() - 1, gem.getCellColumn());
Gem downGem = null;
if(gem.getCellRow() < rows-1)
{
downGem = getGemAt(gem.getCellRow() + 1, gem.getCellColumn());
}
tryCrush(gem, rightGem, crushedGems);
tryCrush(gem, upGem, crushedGems);
tryCrush(gem, leftGem, crushedGems);
tryCrush(gem, downGem, crushedGems);
}
private void tryCrush(Gem gem, Gem otherGem, ArrayList<Gem> crushedGems)
{
if (otherGem == null)
{
return;
}
if (gem.getType().getBaseType() != otherGem.getType().getBaseType())
{
return;
}
if (!gem.getType().isChest() && !otherGem.getType().isChest() && crushedGems.isEmpty())
{
return;
}
if (crushedGems.contains(otherGem))
{
return;
}
crushedGems.add(otherGem);
detectCrushes(otherGem, crushedGems);
}
Build verde!
public void testCrushOnLeftBound()
{
insertAndUpdate(createGem(DIAMOND), 13, 0);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 1);
try
{
grid.updateCrushes();
}
catch(ArrayIndexOutOfBoundsException exc)
{
fail("ArrayIndexOutOfBoundsException thrown");
}
}
Tutto tuo :)
Ecco la soluzione:
private void detectCrushes(Gem gem, ArrayList<Gem> crushedGems)
{
Gem rightGem = getGemAt(gem.getCellRow(), gem.getCellColumn() + 1);
Gem leftGem=null;
Gem upGem = getGemAt(gem.getCellRow() - 1, gem.getCellColumn());
Gem downGem = null;
if(gem.getCellRow() < rows-1)
{
downGem = getGemAt(gem.getCellRow() + 1, gem.getCellColumn());
}
if(gem.getCellColumn() > 1)
{
leftGem = getGemAt(gem.getCellRow(), gem.getCellColumn() - 1);
}
tryCrush(gem, rightGem, crushedGems);
tryCrush(gem, upGem, crushedGems);
tryCrush(gem, leftGem, crushedGems);
tryCrush(gem, downGem, crushedGems);
}
GREEN
Ed ecco il prossimo test:
public void testCrushOnRightBound()
{
insertAndUpdate(createGem(DIAMOND), 13, 6);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 7);
try
{
grid.updateCrushes();
}
catch(ArrayIndexOutOfBoundsException exc)
{
fail("ArrayIndexOutOfBoundsException thrown");
}
}
Build verde :) non posto il codice, è = agli altri
public void testCrushOnTopBound()
{
insertAndUpdate(createGem(DIAMOND), 13, 4);
insertAndUpdate(createGem(DIAMOND), 12, 4);
insertAndUpdate(createGem(DIAMOND), 11, 4);
insertAndUpdate(createGem(DIAMOND), 10, 4);
insertAndUpdate(createGem(DIAMOND), 9, 4);
insertAndUpdate(createGem(DIAMOND), 8, 4);
insertAndUpdate(createGem(DIAMOND), 7, 4);
insertAndUpdate(createGem(DIAMOND), 6, 4);
insertAndUpdate(createGem(DIAMOND), 5, 4);
insertAndUpdate(createGem(DIAMOND), 4, 4);
insertAndUpdate(createGem(DIAMOND), 3, 4);
insertAndUpdate(createGem(DIAMOND), 2, 4);
insertAndUpdate(createGem(DIAMOND), 1, 4);
insertAndUpdate(createGem(DIAMOND), 0, 4);
try
{
grid.updateCrushes();
}
catch(ArrayIndexOutOfBoundsException exc)
{
fail("ArrayIndexOutOfBoundsException thrown");
}
}
a te :)
Provate ad indovianre la modifica :D :D
GREEN
Ora il prossimo test...BigGem:
public void testBigGemAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND), 13, 3);
insertAndUpdate(createGem(DIAMOND), 12, 2);
insertAndUpdate(createGem(DIAMOND), 12, 3);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 4);
grid.updateBigGems();
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.numberOfBigGems ());
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
Build verde :)
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
if (gem == null)
{
continue;
}
ArrayList<Gem> crushedGems = new ArrayList<Gem>();
detectCrushes(gem, crushedGems);
while(!crushedGems.isEmpty())
{
Gem crushedGem = crushedGems.get(0);
if(isCellInABigGem(crushedGem.getCellRow(), crushedGem.getCellColumn()))
{
BigGem crushedBigGem = getBigGemAt(crushedGem.getCellRow(), crushedGem.getCellColumn());
bigGems.remove(crushedBigGem);
}
removeGemFromGrid(crushedGem);
crushedGems.remove(crushedGem);
}
}
}
}
a te questo test :)
public void testUpdateOnWaitingState()
{
GridController gridController = GridController.create(Config.createForTesting(), null);
grid = gridController.getGrid();
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 3);
gridController.update(timer);
assertEquals("grid must be empty", 2, grid.getNumberOfGems());
GemsPair gemsPair = gridController.getGemsPair();
gemsPair.getSlaveGem().dropGem();
gridController.update(timer);
gemsPair.getPivotGem().dropGem();
gridController.update(timer);
assertEquals("grid must be empty", 2, grid.getNumberOfGems());
}
public void update(TimerInterface timer)
{
gemsPair.update(timer);
if(!waitState)
{
if(oneGemIsNotFalling())
{
grid.setStrongestGravity();
}
else if(bothGemsAreNotFalling())
{
grid.setNormalGravity();
grid.updateCrushes();
initializeWaitState(timer);
}
}
if(waitState)
{
handleWaitState(timer);
}
grid.updateBigGems();
grid.updateGemAnimations(timer);
}
davvero semplice... task finito?
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.