Looping in a spiral

前端 未结 30 2683
独厮守ぢ
独厮守ぢ 2020-11-22 15:07

A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fe

30条回答
  •  旧时难觅i
    2020-11-22 15:18

    This is my very very bad solution, made from bare minimum knowledge of Java. Here I have to place units on a field in a spiral. Units cannot be placed on top of other units or on mountains or in the ocean.

    To be clear. This is not a good solution. This is a very bad solution added for the fun of other people to laugh at how bad it can be done

    private void unitPlacementAlgorithm(Position p, Unit u){
        int i = p.getRow();
        int j = p.getColumn();
    
        int iCounter = 1;
        int jCounter = 0;
    
        if (getUnitAt(p) == null) {
                unitMap.put(p, u);
        } else {
            iWhileLoop(i, j, iCounter, jCounter, -1, u);
        }
    
    }
    
    private void iWhileLoop(int i, int j, int iCounter, int jCounter, int fortegn, Unit u){
        if(iCounter == 3) {
            for(int k = 0; k < 3; k++) {
                if(k == 2) { //This was added to make the looping stop after 9 units
                    System.out.println("There is no more room around the city");
                    return; 
                }
                i--;
    
                if (getUnitAt(new Position(i, j)) == null 
                    && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.OCEANS)) 
                    && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.MOUNTAINS))) {
                        unitMap.put(new Position(i, j), u);
                        return;
                }
                iCounter--;
            }
        }
    
        while (iCounter > 0) {
            if (fortegn > 0) {
                i++;
            } else {
                i--;
            }
    
            if (getUnitAt(new Position(i, j)) == null 
                && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.OCEANS)) 
                && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.MOUNTAINS))) {
                    unitMap.put(new Position(i, j), u);
                    return;
            }
            iCounter--;
            jCounter++;
        }
        fortegn *= -1;
        jWhileLoop(i, j, iCounter, jCounter, fortegn, u);
    }
    
    private void jWhileLoop(int i, int j, int iCounter, int jCounter,
            int fortegn, Unit u) {
        while (jCounter > 0) {
            if (fortegn > 0) {
                j++;
            } else {
                j--;
            }
    
            if (getUnitAt(new Position(i, j)) == null 
                && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.OCEANS)) 
                && !(getTileAt(new Position(i, j)).getTypeString().equals(GameConstants.MOUNTAINS))) {
                    unitMap.put(new Position(i, j), u);
                    return;
    
            }
            jCounter--;
            iCounter++;
            if (jCounter == 0) {
                iCounter++;
            }
    
        }
        iWhileLoop(i, j, iCounter, jCounter, fortegn, u);
    }
    

    Cudos to anyone who can actually read this

    Bonus question: What is the running time of this "algorithm"? :P

提交回复
热议问题