/** * @pre * @post verifie le bon fonctionnement de la methode size */ public void testSize() { // creation de la liste DList l=new DList(); assertEquals("Une liste vide " + "devrait avoir une taille de zéro",0,l.size()); // ajout d'un premier element l.add(new Double(2.3)); assertEquals("Une liste d'un élément " + "devrait avoir une taille de 1",1,l.size()); // ajout d'un second element l.add(new Double(2.4)); assertEquals("Une liste de deux éléments " + "devrait avoir une taille de deux",2,l.size()); // retrait d'un element l.remove(new Double(2.3)); assertEquals("Une liste d'un élément " + "devrait avoir une taille de 1",1,l.size()); // retrait d'un second element l.remove(new Double(2.4)); assertEquals("Une liste vide " + "devrait avoir une taille de zéro",0,l.size()); }
/** * @pre * @post verification du bon fonctionnement de la methode remove: * vérification de l'échec de cette méthode quand il le faut. */ public void testEchecRemove(){ // Creation de la liste DList l=new DList(); // Retrait d'un element d'une liste vide assertEquals("Le retrait d'un element ne se trouvant pas dans la liste" + " devriat renvoyer false",false,l.remove(new Double(2.3))); // Ajout de trois elements a la liste l.add(new Double(2.3)); l.add(new Double(2.4)); l.add(new Double(2.4)); // Retrait de ces trois elements l.remove(new Double(2.3)); assertEquals("Le retrait d'un element ne se trouvant plus dans la liste " + "devrait renvoyer false",false,l.remove(new Double(2.3))); assertEquals("Le retrait d'un element ne se trouvant pas dans la liste " + "devriat renvoyer false",false,l.remove(new Double(2.2))); l.remove(new Double(2.4)); assertTrue("Vous n'avez pas corrigé l'erreur"+ " contenue dans la classe.",false == l.remove(new Double(2.4))); }
/** * Searches if the length of the winning network is larger or equal to 6 * * @param dlist is the possible winning network * @return true if the network has enough length and false if not */ boolean lengthMatch(DList dlist) throws InvalidNodeException { if (dlist.length() > 0) { DListNode currentNode = (DListNode) dlist.front(); for (int i = 0; i < dlist.length(); i++) { DList currentList = (DList) currentNode.item(); if (currentList.length() >= 6) { return true; } currentNode = (DListNode) currentNode.next(); } } return false; }
/** * Searches if a network has passed through a chip without turning a corner * * @param list is a possible network * @return true if the network has turned corner when passing through a chip false if it hasn't */ private boolean isTurnCorner(DList list) throws InvalidNodeException { if (list.length() < 3) { return true; } else { Chip currChip = (Chip) list.front().item(); Chip fatherChip = (Chip) list.front().next().item(); Chip grandFatherChip = (Chip) list.front().next().next().item(); if (slope(currChip, fatherChip) == slope(fatherChip, grandFatherChip)) { return false; } else { return true; } } }
/** * @pre - * @post verifie le bon fonctionnement de la methode contains sur plusieurs * exemples d'utilisation de la liste de Double: vérification de la * réussite de la méthode quand il le faut. */ public void testReussiteContains() { // Creation de la liste DList l=new DList(); // Ajout de trois Double l.add(new Double(2.3)); l.add(new Double(2.4)); l.add(new Double(2.4)); // Verification sur base des elements ajoutes assertEquals("La verification de la presence d'un element present une fois dans la liste devrait renvoyer true",true,l.contains(new Double(2.3))); assertEquals("La verification de la presence d'un element present deux fois dans la liste devrait renvoyer true",true,l.contains(new Double(2.4))); }
/** * @pre * @post verification du bon fonctionnement de la methode remove: * vérification de la réussite de cette méthode quand il le faut. */ public void testReussiteRemove() { // Creation de la liste DList l=new DList(); // ajout de trois elements a la liste l.add(new Double(2.3)); l.add(new Double(2.4)); l.add(new Double(2.4)); // retrait de ces trois elements assertEquals("Le retrait d'un element se trouvant dans la liste " + "devrait renvoyer true",true,l.remove(new Double(2.3))); assertEquals("Le retrait d'un element se trouvant deux fois dans la liste" + " devrait renvoyer true",true,l.remove(new Double(2.4))); }
/** * @pre - * @post verifie le bon fonctionnement de la methode contains sur plusieurs * exemples d'utilisation de la liste de Double: vérification de * l'échec de la méthode quand il la faut. */ public void testEchecContains() { // Creation de la liste DList l=new DList(); // Ajout de trois Double l.add(new Double(2.3)); l.add(new Double(2.4)); l.add(new Double(2.4)); // Verification sur base des elements ajoutes assertEquals("La verification de la presence d'un element absent de la liste devrait renvoyer false",false,l.contains(new Double(2.2))); // Verification que l'element supprime n'est plus contenu dans la liste l.remove(new Double(2.3)); assertEquals("La verification de la presence d'un element qui n'est plus present dans la liste devrait renvoyer false",false,l.contains(new Double(2.3))); }
/** * Searches if the newly added chip already exists in the input list * * @param list is a DList being checked * @return whether the newly added chip already exists in the input list; return false if there it * already exists and true if it doesn't exist */ private boolean noSameChip(DList list) { if (list.length() - 1 == 0) { return true; } DListNode currNode; try { currNode = (DListNode) list.front().next(); for (int i = 0; i < list.length() - 1; i++) { if (((Chip) currNode.item()).equal((Chip) list.front().item())) { return false; } currNode = (DListNode) currNode.next(); } return true; } catch (InvalidNodeException e) { e.printStackTrace(); } return false; }
/** * Searches around the 8 cells around the given cell/chip to see if there is any chip of the same * color * * @param coord is the coordinate of the give chip * @param color is the color of the chip being searched * @return a DList containing all the chips of the same color around a given cell */ private DList neighborList(Coordinate coord, int color, DList list) { int x = coord.getX(); int y = coord.getY(); for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { if (!(i == x && j == y) && i >= 0 && i <= 7 && j >= 0 && j <= 7) { if (getColor(i, j) == color) { Coordinate coor = new Coordinate(i, j); list.insertBack(coor); } } } } return list; }
private static void test_dds() { System.out.println("DDS test:"); DataDDS table = new DataDDS(new ServerVersion(2, 16)); table.setName("test_table"); // add variables to it DUInt32 myUInt = new DUInt32("myUInt"); myUInt.setValue(42); table.addVariable(myUInt); // note that arrays and lists take their name from the addVariable method DArray myArray = new DArray(); myArray.addVariable(new DByte("myArray")); myArray.appendDim(10, "dummy"); myArray.setLength(10); BytePrimitiveVector bpv = (BytePrimitiveVector) myArray.getPrimitiveVector(); for (int i = 0; i < 10; i++) bpv.setValue(i, (byte) (i * 10)); table.addVariable(myArray); DList myList = new DList(); myList.addVariable(new DURL("myList")); myList.setLength(10); BaseTypePrimitiveVector btpv = (BaseTypePrimitiveVector) myList.getPrimitiveVector(); for (int i = 0; i < 10; i++) { DURL testURL = new DURL(); testURL.setValue("http://" + i); btpv.setValue(i, testURL); } table.addVariable(myList); DStructure myStructure = new DStructure("myStructure"); DFloat64 structFloat = new DFloat64("structFloat"); structFloat.setValue(42.0); myStructure.addVariable(structFloat); DString structString = new DString("structString"); structString.setValue("test value"); myStructure.addVariable(structString); table.addVariable(myStructure); DGrid myGrid = new DGrid("myGrid"); DArray gridArray = (DArray) myArray.clone(); gridArray.setName("gridArray"); myGrid.addVariable(gridArray, DGrid.ARRAY); DArray gridMap = new DArray(); gridMap.addVariable(new DInt32("gridMap")); gridMap.appendDim(10, "dummy"); gridMap.setLength(10); Int32PrimitiveVector ipv = (Int32PrimitiveVector) gridMap.getPrimitiveVector(); for (int i = 0; i < 10; i++) ipv.setValue(i, i * 10); myGrid.addVariable(gridMap, DGrid.MAPS); table.addVariable(myGrid); // this is the one case where two DODS variables can have the same name: // each row should have the same exact variables (name doesn't matter) DSequence mySequence = new DSequence("mySequence"); mySequence.addVariable(new DInt32("seqInt32")); mySequence.addVariable(new DString("seqString")); Vector seqRow = new Vector(); // a row of the sequence DInt32 seqVar1 = new DInt32("seqInt32"); seqVar1.setValue(1); seqRow.addElement(seqVar1); DString seqVar2 = new DString("seqString"); seqVar2.setValue("string"); seqRow.addElement(seqVar2); mySequence.addRow(seqRow); seqRow = new Vector(); // add a second row seqVar1 = new DInt32("seqInt32"); seqVar1.setValue(3); seqRow.addElement(seqVar1); seqVar2 = new DString("seqString"); seqVar2.setValue("another string"); seqRow.addElement(seqVar2); mySequence.addRow(seqRow); table.addVariable(mySequence); try { table.checkSemantics(); System.out.println("DDS passed semantic check"); } catch (BadSemanticsException e) { System.out.println("DDS failed semantic check:\n" + e); } try { table.checkSemantics(true); System.out.println("DDS passed full semantic check"); } catch (BadSemanticsException e) { System.out.println("DDS failed full semantic check:\n" + e); } // print the declarations System.out.println("declarations:"); table.print(System.out); // print the data System.out.println("\nData:"); table.printVal(System.out); System.out.println(); // and read it programmatically try { int testValue1 = ((DUInt32) table.getVariable("myUInt")).getValue(); System.out.println("myUInt = " + testValue1); byte testValue2 = ((BytePrimitiveVector) ((DArray) table.getVariable("myArray")).getPrimitiveVector()) .getValue(5); System.out.println("myArray[5] = " + testValue2); String testValue3 = ((DString) ((BaseTypePrimitiveVector) ((DList) table.getVariable("myList")).getPrimitiveVector()) .getValue(5)) .getValue(); System.out.println("myList[5] = " + testValue3); double testValue4 = ((DFloat64) ((DStructure) table.getVariable("myStructure")).getVariable("structFloat")) .getValue(); System.out.println("myStructure.structFloat = " + testValue4); int testValue5 = ((Int32PrimitiveVector) ((DArray) ((DGrid) table.getVariable("myGrid")).getVariable("gridMap")) .getPrimitiveVector()) .getValue(5); System.out.println("myGrid.gridMap[5] = " + testValue5); String testValue7 = ((DString) ((DSequence) table.getVariable("mySequence")).getVariable(0, "seqString")) .getValue(); System.out.println("mySequence[0].seqString = " + testValue7); } catch (NoSuchVariableException e) { System.out.println("Error getting variable:\n" + e); } System.out.println(); DataDDS table2 = (DataDDS) table.clone(); // test Cloneable interface try { table2.checkSemantics(); System.out.println("DDS passed semantic check"); } catch (BadSemanticsException e) { System.out.println("DDS failed semantic check:\n" + e); } try { table2.checkSemantics(true); System.out.println("DDS passed full semantic check"); } catch (BadSemanticsException e) { System.out.println("DDS failed full semantic check:\n" + e); } // print the declarations and data System.out.println("clone declarations:"); table2.print(System.out); System.out.println("\nData:"); table2.printVal(System.out); System.out.println(); // add some values to the original table DInt32 myNewInt = new DInt32("myNewInt"); myNewInt.setValue(420); table.addVariable(myNewInt); // verify that they aren't in the cloned table try { DInt32 testInt = (DInt32) table2.getVariable("myNewInt"); System.out.println("Error: value from table in table2"); } catch (NoSuchVariableException e) { System.out.println("Variable cloning looks good"); } }
/** * Returns all networks in a DList regardless of length starting from Chip start. * * @param start is the starting Chip of the network. * @param depth records depth of the method in a recursive call. */ DList allNetwork(Chip start, int depth) throws InvalidNodeException { DList set = new DList(); if (inGoal(start)) { DList network = new DList(); network.insertFront(start); set.insertFront(network); return set; } else if (depth > 10) { return set; } else if (connections(start).length() == 0) { return set; } else { DListNode current = (DListNode) connections(start).front(); int length = connections(start).length(); for (int i = 0; i < length; i++) { if (!inStartGoal((Chip) current.item())) { DList rest = allNetwork((Chip) current.item(), depth + 1); if (rest.length() > 0) { DListNode currentNetworkNode = (DListNode) rest.front(); for (int j = 0; j < rest.length(); j++) { DList currentNetwork = (DList) currentNetworkNode.item(); currentNetwork.insertFront(start); if (isTurnCorner(currentNetwork) && noSameChip(currentNetwork)) { set.insertFront(currentNetwork); } currentNetworkNode = (DListNode) currentNetworkNode.next(); } } } current = (DListNode) current.next(); } return set; } }
/** * Returns a DList that stores Move objects that represent all the valid next moves this Board can * make for the given color. If there is no valid move this Board can make, return an empty DList. * * @param color is the color that is to make the next move. */ DList allValidMoves(int color) { DList allMoves = new DList(); try { if (totalChips(color) >= 10) { DList emptyList = new DList(); DList chipList = new DList(); for (int i = 0; i < Board.DIMENSION; i++) { for (int j = 0; j < Board.DIMENSION; j++) { Coordinate currCoord = new Coordinate(i, j); if (getColor(i, j) == EMPTY) { emptyList.insertBack(currCoord); } else if (getColor(i, j) == color) { chipList.insertBack(currCoord); } } } DListNode chipNode = (DListNode) chipList.front(); for (int i = 0; i < chipList.length(); i++) { DListNode emptyNode = (DListNode) emptyList.front(); for (int j = 0; j < emptyList.length(); j++) { Coordinate emptyCoord = (Coordinate) emptyNode.item(); Coordinate chipCoord = (Coordinate) chipNode.item(); Move stepMove = new Move(emptyCoord.getX(), emptyCoord.getY(), chipCoord.getX(), chipCoord.getY()); setColor(stepMove.x2, stepMove.y2, EMPTY); if (isValidMove(stepMove, color)) { allMoves.insertBack(stepMove); } setColor(stepMove.x2, stepMove.y2, color); emptyNode = (DListNode) emptyNode.next(); } chipNode = (DListNode) chipNode.next(); } } else { for (int i = 0; i < DIMENSION; i++) { for (int j = 0; j < DIMENSION; j++) { if (getColor(i, j) == EMPTY) { Move addMove = new Move(i, j); if (isValidMove(addMove, color)) { allMoves.insertBack(addMove); } } } } } } catch (InvalidNodeException e) { System.out.println(e + "in move generations"); } return allMoves; }
/** * Returns a DList that stores Chip objects that represent all the chips connected to Chip c on * this Board. If c has no connection, return an empty DList. * * @param c is the Chip to which connections are searched. */ DList connections(Chip c) { int color = c.getColor(); int x = c.getX(); int y = c.getY(); DList connected = new DList(); for (int i = x - 1; i >= 0; i--) { if (board[i][y] == color) { Chip left = new Chip(color, i, y); connected.insertBack(left); break; } else if (board[i][y] == 1 - color) { break; } } for (int i = x + 1; i < DIMENSION; i++) { if (board[i][y] == color) { Chip right = new Chip(color, i, y); connected.insertBack(right); break; } else if (board[i][y] == 1 - color) { break; } } for (int i = y - 1; i >= 0; i--) { if (board[x][i] == color) { Chip up = new Chip(color, x, i); connected.insertBack(up); break; } else if (board[x][i] == 1 - color) { break; } } for (int i = y + 1; i < DIMENSION; i++) { if (board[x][i] == color) { Chip down = new Chip(color, x, i); connected.insertBack(down); break; } else if (board[x][i] == 1 - color) { break; } } for (int i = 1; x - i >= 0 && y - i >= 0; i++) { if (board[x - i][y - i] == color) { Chip upperleft = new Chip(color, x - i, y - i); connected.insertBack(upperleft); break; } else if (board[x - i][y - i] == 1 - color) { break; } } for (int i = 1; x + i < DIMENSION && y - i >= 0; i++) { if (board[x + i][y - i] == color) { Chip upperright = new Chip(color, x + i, y - i); connected.insertBack(upperright); break; } else if (board[x + i][y - i] == 1 - color) { break; } } for (int i = 1; x - i >= 0 && y + i < DIMENSION; i++) { if (board[x - i][y + i] == color) { Chip lowerleft = new Chip(color, x - i, y + i); connected.insertBack(lowerleft); break; } else if (board[x - i][y + i] == 1 - color) { break; } } for (int i = 1; x + i < DIMENSION && y + i < DIMENSION; i++) { if (board[x + i][y + i] == color) { Chip lowerright = new Chip(color, x + i, y + i); connected.insertBack(lowerright); break; } else if (board[x + i][y + i] == 1 - color) { break; } } return connected; }
/** * Determines whether the move by the chip of a certain color is valid If m is a legal move for * the given color, return true If m is not a legal move for the given color, return false * * @param m is an assigned move * @param color is the color of the chip being moved * @return whether the move is valid */ boolean isValidMove(Move m, int color) { if (m.moveKind == Move.STEP) { if (getColor(m.x1, m.y1) != EMPTY) { return false; } if (!isNotGoal(m, 1 - color)) { return false; } if (!isValidBound(m)) { return false; } DList list = new DList(); Coordinate coord = new Coordinate(m.x1, m.y1); setColor(m.x2, m.y2, EMPTY); DList firstList = neighborList(coord, color, list); if (firstList.length() >= 2) { setColor(m.x2, m.y2, color); return false; } else if (firstList.length() == 1) { try { Coordinate item = (Coordinate) firstList.front().item(); DList secondList = neighborList(item, color, list); setColor(m.x2, m.y2, color); if (secondList.length() >= 2) { setColor(m.x2, m.y2, color); return false; } } catch (InvalidNodeException e) { } } setColor(m.x2, m.y2, color); return true; } else if (m.moveKind == Move.ADD) { if (totalChips(color) >= 10) { return false; } if (!isValidBound(m)) { return false; } if (getColor(m.x1, m.y1) != EMPTY) { return false; } if (!isNotGoal(m, 1 - color)) { return false; } DList list = new DList(); Coordinate coord = new Coordinate(m.x1, m.y1); DList firstList = neighborList(coord, color, list); if (firstList.length() >= 2) { return false; } else if (firstList.length() == 1) { try { Coordinate item = (Coordinate) firstList.front().item(); DList secondList = neighborList(item, color, list); if (secondList.length() >= 2) { return false; } } catch (InvalidNodeException e) { e.printStackTrace(); } } } return true; }