简体   繁体   English

无法弄清楚如何使方法递归

[英]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. 有一个字段[DIM] [DIM],用于存储大理石。 Marble has a Mark, which is 0-4, with 0 being empty and 1-4 being colour values. 大理石有一个标记,它是0-4,其中0为空,而1-4为颜色值。 The method determines whether someone has a marble column of 5 and wins. 该方法确定某人是否有5的大理石柱并获胜。 Input is the Marble type of a player. 输入是玩家的大理石类型。 Output is boolean hasColumn true or false. 输出为boolean hasColumn true或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 . 考虑在迭代函数中,您使用两个变量ij在列上进行迭代。 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: 根据是否要查找等于给定Marble元素或具有相同值的元素的行/列,可以调用此方法:

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. 任务听起来像:1.我们有一个尺寸为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 2.我们有一个方法getField(int,int)从此矩阵返回大理石。3.我们有一个迭代决策,以确定此矩阵是否有任何具有相等大理石元素值的列。

Our goal is write recursive variant of this method 我们的目标是此方法的写递归变体

So, look here. 所以,看这里。 Recursive algorithm check ROW existing with same value: 递归算法检查ROW是否存在相同值:

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 . 您的方法名称为hasColumn ,返回变量名称为hasColumn That's BAD . 那是坏的
  2. I don't see hasColumn invoked inside the method again to actually go down to recursion path. 我看不到hasColumn再次在方法内部被调用而实际上进入了递归路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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