如何优雅地调用各种顺序的方法?

时间:2015-02-28 00:08:37

标签: java python algorithm

我有四种方法:

right();
up();
left();
down();

然后我以下列方式打电话给他们:

result = right();
if checkResult(result)
  return(result);

result = up();
if checkResult(result)
  return(result);

result = left();
if checkResult(result)
  return(result);

result = down();
if checkResult(result)
  return(result);

所以它的优先级。我知道其中一个会起作用。但我需要根据优先级进行检查:[right, up, left, down]

现在,诀窍是:我的优先级随着时间而变化。确切地说,这取决于之前的举动。所以:

if previous move was up(), then priority is [right, up, left, down]
if previous move was left(), then priority is [up, left, down, right]
if previous move was down(), then priority is [left, down, right, up]

所以你可以清楚地看到这里的逻辑。问题是:如何在给定(可变)优先级中调用函数?

显然,我可以直接切换和复制粘贴调用块,但这很难看。这样做是一种优雅的方式吗?

此外,Java和Python有哪些方法?

4 个答案:

答案 0 :(得分:1)

也许你可以在python中使用dict作为你的键和移动列表,按优先级排序为值。

然后你可以另外保存最后一步并迭代相应的列表并做你需要做的事情。

答案 1 :(得分:1)

正如你在问题中写的那样。按下某个键后,更改列表以反映所需的优先级:

# global, before continuous update

order = [right, up, left, down]

# during continuous update

# 1. Test for Key Presses
# 2. Change Priorities

if left_is_pressed: 
    order = [up, left, down, right]

# 3. call functions in order

for key_function in order:
    key_function()

答案 2 :(得分:1)

使用dict定义方向检查方法+另一个定义优先级可能是你想要的。您可能希望修改检查是否按下该键的方法(或检查条件)也返回方向,以便可以从检查所有输入条件的顶级方法返回它。然后可以将该方向存储为以前的'方向,用于检查下一个条件。

UP = 1
DOWN = 2
RIGHT = 3
LEFT = 4
# Assuming that `result` is the result of that method
def should_go_up():
    return (result, UP)
def should_go_down():
    return (result, DOWN)
def should_go_right():
    return (result, RIGHT)
def should_go_left():
    return (result, LEFT)

input_checks = {
   UP: should_go_up,
   DOWN: should_go_down,
   RIGHT: should_go_right,
   LEFT: should_go_left
}

input_check_priorities = {
    UP:[RIGHT, UP, LEFT, DOWN],
    LEFT:[UP, LEFT, DOWN, RIGHT],
    ...
}

def move(prev_move):
    if prev_move is None:
        prev_move = UP # or whatever is the default
    input_check_priority = input_check_priorites[prev_move]
    for direction in input_check_priority:
        result = input_checks[direction]()
        if result[0]:
            return result
    return (False, None)

答案 3 :(得分:1)

我会为方法分配整数ID,例如:

right() - 0
up() - 1
left() - 2
down() - 3

我将previousMove存储为一个整数,它对应于最后一个被调用的方法。因此,只要调用up(),previousMove就等于1等等(你可以根据你想要的逻辑在任何地方设置它)

然后我会循环如下:

public class Test {
    public static void main(String[] args) {
        int previousMove = 3; //Change this to test

        int startMethod = previousMove - 1 < 0 ? 3 : previousMove - 1; //Represents the ID of the method before the previousMove, accounting for negative values and wrapping around
        int offset = startMethod - 1 < 0 ? 3 : startMethod - 1; //Represents the ID of the method before the startMethod, accounting for negative values and wrapping around

        for (int i = startMethod; true; i++) {
            if (i > 3) i = 0; //Wrap around
            switch (i) {
                case 0: right(); break;
                case 1: up(); break;
                case 2: left(); break;
                case 3: down(); break;
            }
            if (i == offset) break; //Break once all 4 methods have been called
        }
    }

    private static void right() {
        System.out.println("Calling right");
    }

    private static void up() {
        System.out.println("Calling up");
    }

    private static void left() {
        System.out.println("Calling left");
    }

    private static void down() {
        System.out.println("Calling down");
    }
}

使用previousMove = 3(向下)输出:

Calling left
Calling down
Calling right
Calling up

解释:我们从startMethod(previousMove之前的一个)循环到4个方法,直到我们全部调用它们。因此,如果你的“之前的移动”是left()方法,你可以将previousMove设置为2,并且循环将根据需要调用第1,第2,第3和第4个方法。

一开始可能很难理解整数,但我可以向您保证,通过尝试理解上述代码而不仅仅是将其粘贴到您的程序中,您将获益更多:)