|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
[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. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
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 |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Primo test per Bonfo:
Codice:
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);
}
}
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Ecco risolto:
Codice:
public void updateCrushes()
{
removeGemFromGrid(getGemAt(13,3));
removeGemFromGrid(getGemAt(13,2));
}
GREEN |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Testiamo ora due coppie di diverso tipo:
Codice:
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());
}
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Build Verde:
Codice:
public void updateCrushes()
{
for(Gem gemsRow[] : grid)
{
for(Gem gem : gemsRow)
{
if (gem == null)
{
continue;
}
removeGemFromGrid(gem);
}
}
}
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Nuovo test per Bonfo:
Codice:
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());
}
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
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: Codice:
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());
}
Ultima modifica di Bonfo : 27-01-2006 alle 14:34. |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Codice:
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);
}
|
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Ok...ora possiamo finire col test di prima.
Modificato updateCrushes(): Codice:
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);
}
}
}
}
GREEN fra un po' il prossimo test... |
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
E questo?
Codice:
public void testVerticalGemsAndChestCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND_CHEST), 12, 2);
grid.updateCrushes();
assertEquals("grid must be empty", 0, grid.getNumberOfGems());
}
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Bene...per risolvere questo è stato aggiunto un metodo privato
Codice:
private boolean removeIfCorrectType(Gem gem,GemType gemType)
{
if( gem != null)
{
GemType thisGemType = gem.getType().getBaseType();
if(thisGemType == gemType)
{
removeGemFromGrid(gem);
return true;
}
}
return false;
}
Poi è stato così modificato il codice di updateCrushes() Codice:
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 |
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Nuovo test...
Codice:
public void testTwoDiamondsNotCrushing()
{
insertAndUpdate(createGem(DIAMOND), 13, 2);
insertAndUpdate(createGem(DIAMOND), 13, 3);
grid.updateCrushes();
assertEquals("grid must contain two gems", 2, grid.getNumberOfGems());
}
|
|
|
|
|
|
#14 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Codice:
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;
}
|
|
|
|
|
|
#15 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Codice:
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());
}
|
|
|
|
|
|
#16 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Fantastico risolto con 3 caratteri ....
Codice:
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 |
|
|
|
|
|
#17 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Nuovo test...iniziamoo con le "catene":
Codice:
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());
}
|
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Accidenti! 5 righe di test mi han fatto sbattere un poco
Codice:
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);
}
|
|
|
|
|
|
#19 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 1545
|
Codice:
public void testCrushOnLeftBound()
{
insertAndUpdate(createGem(DIAMOND), 13, 0);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 1);
try
{
grid.updateCrushes();
}
catch(ArrayIndexOutOfBoundsException exc)
{
fail("ArrayIndexOutOfBoundsException thrown");
}
}
|
|
|
|
|
|
#20 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Bologna
Messaggi: 1303
|
Ecco la soluzione:
Codice:
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);
}
Ed ecco il prossimo test: Codice:
public void testCrushOnRightBound()
{
insertAndUpdate(createGem(DIAMOND), 13, 6);
insertAndUpdate(createGem(DIAMOND_CHEST), 13, 7);
try
{
grid.updateCrushes();
}
catch(ArrayIndexOutOfBoundsException exc)
{
fail("ArrayIndexOutOfBoundsException thrown");
}
}
Ultima modifica di Bonfo : 27-01-2006 alle 18:15. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:07.



















