vendredi 27 février 2015

How elegantly to call methods with various order?



I have four methods:



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


Then I call them in the following fashion:



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);


So it's kind of priority. I know that one of them would work. But I need to check it according to priority: [right, up, left, down].


Now, the trick is: my priority changes over time. To be precise it depends on previous move. So:



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]


So you can clearly see the logic here. The question is: how do I do call the functions in given (variable) priority?


Obviously, I can just do switch and copy-paste calling block, but that's ugly. So is it an elegant way of doing it?


Furthermore, what are the ways for Java and Python?




Aucun commentaire:

Enregistrer un commentaire