Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2010-10-11T09:47:09+00:00 → 2011-10-26T11:13:12+00:00
<language>Java</language> | |
| − | mport java.util.List; |
| − | import java.util.ArrayList; |
| + | <code id="4bc899628b157" cols="20" rows="3"> |
| + | int[] liste = { |
| + | 1,2,9,5,2,1 |
| + | }; |
| + | </code> |
| − | /** |
| − | * Wombat. A Wombat moves forward until it can't do so anymore, at |
| − | * which point it turns left. If a wombat finds a leaf, it eats it. |
| − | * |
| − | * @author Michael Kolling |
| − | * @version 1.0.1 |
| − | */ |
| − | public class Wombat extends Actor |
| − | { |
| − | private static final int EAST = 0; |
| − | private static final int WEST = 1; |
| − | private static final int NORTH = 2; |
| − | private static final int SOUTH = 3; |
| + | <code id="4bc899628b913" rows="12"> |
| + | void selectionSort() { |
| + | for (int i=0; i < liste.length; i++){ |
| + | int min = i; |
| + | for (int j = i+1;j<liste.length;j++) |
| + | if (liste[min]>liste[j]) |
| + | min = j; |
| − | private int direction; |
| − | private int leavesEaten; |
| + | int tmp = liste[i]; |
| + | liste[i]= liste[min]; |
| + | liste[min]= tmp; |
| + | } |
| + | } |
| + | </code> |
| − | public Wombat() |
| − | { |
| − | setDirection(EAST); |
| − | leavesEaten = 0; |
| − | } |
| − | |
| − | /** |
| − | * Do whatever the wombat likes to to just now. |
| − | */ |
| − | public void act() |
| − | { |
| − | if(foundLeaf()) { |
| − | eatLeaf(); |
| − | } |
| − | else if(canMove()) { |
| − | move(); |
| − | } |
| − | else { |
| − | turnLeft(); |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Check whether there is a leaf in the same cell as we are. |
| − | */ |
| − | public boolean foundLeaf() |
| − | { |
| − | Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); |
| − | if(leaf != null) { |
| − | return true; |
| − | } |
| − | else { |
| − | return false; |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Eat a leaf. |
| − | */ |
| − | public void eatLeaf() |
| − | { |
| − | Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); |
| − | if(leaf != null) { |
| − | // eat the leaf... |
| − | getWorld().removeObject(leaf); |
| − | leavesEaten = leavesEaten + 1; |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Move one cell forward in the current direction. |
| − | */ |
| − | public void move() |
| − | { |
| − | if (!canMove()) { |
| − | return; |
| − | } |
| − | switch(direction) { |
| − | case SOUTH : |
| − | setLocation(getX(), getY() + 1); |
| − | break; |
| − | case EAST : |
| − | setLocation(getX() + 1, getY()); |
| − | break; |
| − | case NORTH : |
| − | setLocation(getX(), getY() - 1); |
| − | break; |
| − | case WEST : |
| − | setLocation(getX() - 1, getY()); |
| − | break; |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Test if we can move forward. Return true if we can, false otherwise. |
| − | */ |
| − | public boolean canMove() |
| − | { |
| − | World myWorld = getWorld(); |
| − | int x = getX(); |
| − | int y = getY(); |
| − | switch(direction) { |
| − | case SOUTH : |
| − | y++; |
| − | break; |
| − | case EAST : |
| − | x++; |
| − | break; |
| − | case NORTH : |
| − | y--; |
| − | break; |
| − | case WEST : |
| − | x--; |
| − | break; |
| − | } |
| − | // test for outside border |
| − | if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { |
| − | return false; |
| − | } |
| − | else if (x < 0 || y < 0) { |
| − | return false; |
| − | } |
| − | return true; |
| − | } |
| − | |
| − | /** |
| − | * Turns towards the left. |
| − | */ |
| − | public void turnLeft() |
| − | { |
| − | switch(direction) { |
| − | case SOUTH : |
| − | setDirection(EAST); |
| − | break; |
| − | case EAST : |
| − | setDirection(NORTH); |
| − | break; |
| − | case NORTH : |
| − | setDirection(WEST); |
| − | break; |
| − | case WEST : |
| − | setDirection(SOUTH); |
| − | break; |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Sets the direction we're facing. |
| − | */ |
| − | public void setDirection(int direction) |
| − | { |
| − | this.direction = direction; |
| − | switch(direction) { |
| − | case SOUTH : |
| − | setRotation(90); |
| − | break; |
| − | case EAST : |
| − | setRotation(0); |
| − | break; |
| − | case NORTH : |
| − | setRotation(270); |
| − | break; |
| − | case WEST : |
| − | setRotation(180); |
| − | break; |
| − | default : |
| − | break; |
| − | } |
| − | } |
| − | |
| − | /** |
| − | * Tell how many leaves we have eaten. |
| − | */ |
| − | public int getLeavesEaten() |
| − | { |
| − | return leavesEaten; |
| − | } |
| + | <code id="4bc899628c0e6"> |
| + | String print(){ |
| + | String result = "" ; |
| + | for (int i=0; i < liste.length; i++){ |
| + | result += liste[i]+","; |
| + | } |
| + | return result; |
} | |
| + | </code> |
| + | |
| + | <run id="4bc899628c8b1" rows="2"> |
| + | selectionSort(); |
| + | print(); |
| + | </run> |