简体   繁体   中英

Can't figure out how to make method recursive

For my programming course I have to write recursive functions, but aside from the theoretical questions given during the classes I can't figure out how to do it with my own code. If anyone could help me out and give me a pointer on where to start it'd be great!

The method is as follows:

    public boolean hasColumn(Marble m) {
    boolean hasColumn = false;
    for (int i = 0; i < DIM && hasColumn == false; i++) {
        int winCount = 0;
        for (int j = 0; j < DIM && hasColumn == false; j++) {
            if (j == 0) {
                winCount = 1;
            } else {
                if (getField(j, i).equals(getField(j - 1, i))
                        && getField(j, i).equals(m)) {
                    winCount++;
                    if (winCount == WINLENGTH) {
                        hasColumn = true;
                    }
                } else {
                    winCount = 1;
                }
            }
            if (!(getField(j, i).equals(m))) {
                hasColumn = false;
            }
        }
    }

    return hasColumn;
   }

There's a field[DIM][DIM], which stores Marbles. Marble has a Mark, which is 0-4, with 0 being empty and 1-4 being colour values. The method determines whether someone has a marble column of 5 and wins. Input is the Marble type of a player. Output is boolean hasColumn true or false. The output value is correct, there's just no recursion.

The idea is to make it find a vertical column in a recursive way. This also has to be done with horizontal/vertical, but I figured when I get this figured out I'll manage those by myself.

Thank you in advance!

There's a duality between certain types of recursion and iteration.

Consider that in your iterative function you are iteratinng over columns using two variables, i and j . Could you transform those local variables into parameters to the function? You would be transforming state internal to the function (local variables) into state implicit in the function call.

public boolean hasColumn(Marble m, int i, int j, int wincount) {
    if (wincount == WINLENGTH)
        return true;
    if (i == DIM)
        return false;
    if (j == DIM)
        return hasColumn(m, i + 1, 0, 0);
    return hasColumn(m, i, j + 1, getField(j, i).equals(m) ? wincount + 1 : 0);
}

Depending on whether you'd like to find a line/column of elements equal to a given Marble element or rather of same value, you may call this method:

hasColumn(aMarble, 0, 0, 0);
hasColumn(getField(0, 0), 0, 0, 0);

Looks like task sounds like: 1. We have a square matrix of Marble elements(it can be simple integers) with dimension DIM. 2. We have a method getField(int, int) return a marble from this matrix 3. We have an iterative decision to discover if this matrix has any column with equal values of marble elements

Our goal is write recursive variant of this method

So, look here. Recursive algorithm check ROW existing with same value:

public class Marble {
    public static final int DIM = 10;
    public int[][] marbleAr = new int[DIM][DIM];

    public void init(){
        for(int i=0;i<DIM;i++){
            for(int j=0;j<DIM;j++){
                marbleAr[i][j] = new Random().nextInt(10);
                if(i == 2){
                    marbleAr[i][j] = 7;
                }
            }
        }
    }

    public int get(int i, int j){
        return marbleAr[i][j];
    }

    public void printMarbleAr(){
        for(int i=0;i<DIM;i++){
            for(int j=0;j<DIM;j++){
                System.out.print(marbleAr[i][j] + "  ");
            }
            System.out.println();
        }
    }

    public boolean hasColumn(int val, int col, int row){
        if(row == 0){       
            return true;
        }
        if(this.hasColumn(val, col, row-1)){
            if(this.get(col, row) == this.get(col,row-1)){
                return true;
            }else{
                if(col == DIM-1){
                    return false;
                }
                return this.hasColumn(val, col+1, row);
            }           
        }
        return false;
    }

    public static void main(String[] args) {
        int v = 7;      
        Marble marble = new Marble();
        marble.init();
        marble.printMarbleAr();
        System.out.println(marble.hasColumn(v, 0, DIM-1));
    }
}
  1. Your method name is hasColumn and return variable name is hasColumn . That's BAD .
  2. I don't see hasColumn invoked inside the method again to actually go down to recursion path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM