# Skip other If conditions

I'm trying to write a code to "random" move in a matrix but it can't go to a certain position if it has a certain value (in this case I'm using the number 2). How do I make it to skip all other If conditions if one of them is found True? Another thing, is it possible to store the RandomChoise in a variable to be used later? Thank you

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{up,down,right,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{up,down,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{up,down,right}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{down,right,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{up,right,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]!=2], RandomChoice[{right,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{down,right}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{down,left}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{up,right}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{up,left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{up,down}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]!=2,matrix[[i,j-1]]=2], RandomChoice[{right}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]!=2], RandomChoice[{left}]]

If[And[matrix[[i+1,j]]!=2,matrix[[i-1,j]]=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{down}]]

If[And[matrix[[i+1,j]]=2,matrix[[i-1,j]]!=2,matrix[[i,j+1]]=2,matrix[[i,j-1]]=2], RandomChoice[{up}]]

-
Check out Which... – kale Jun 4 '14 at 18:33
And also remember that = is Set and == is Equal used for comparison purposes. – kale Jun 4 '14 at 18:36

This is more than you've asked but tell me if this works for you. I've added artificial border with 2s around the world :)

init[] := (dim = {10, 10};
world = ArrayPad[RandomInteger[{1, 10}, dim], {1, 1}, 2];
known = ArrayPad[ConstantArray[0, dim], {1, 1}, 2];
p = {2, 2};
(known[[##]] = world[[##]]) & @@ p;)

init[]
MatrixForm /@ {world, known}


    While[
True,
Composition[
p = #[[1]]; (known[[##]] = world[[##]]) & @@ p;
If[#[[2]] == 10, Break[]]; &,
If[# === {}, Break[], RandomChoice[#]] &,
DeleteCases[#, {_, 2}] &,
][
# + p & /@ {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
]
]


### Dynamic fun:

init[];
Dynamic@MatrixForm@known

While[
True,
Composition[
p = #[[1]]; (known[[##]] = Style[world[[##]], Bold, 15, Red]) & @@ p;
Pause@.1;  If[#[[2]] == 10, Break[]]; &,
If[# === {}, Break[], RandomChoice[#]] &,
DeleteCases[#, {_, 2}] &,
(known[[##]] = Style[world[[##]], Bold, 15]) & @@ p;
][
# + p & /@ {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
]]


-
Kuba's answer very much reminded me of Christian Jacob's works about Evolutionary Algorithms. Some fascinating Mathematica examples examples can be downloaded here: pages.cpsc.ucalgary.ca/~jacob/Evolvica/… – eldo Jun 4 '14 at 20:27
@AndréF. The DeleteCases part is responsible for dropping walls. The direction is chosen from the remaining parts. You must skip this part and include your code then. – Kuba Jun 9 '14 at 17:30
@AndréF. Try this Composition[ With[{val = #[[2]], pos = #[[1]]}, (known[[##]] = world[[##]]) & @@ pos; Switch[val, 10, p = pos; Break[], 2, {}, _, p = pos]] &, RandomChoice, Thread[{#, Extract[world, #]}] & ]. Materials about what? :) – Kuba Jun 9 '14 at 18:29
@AndréF. I find those tutorials helpful. Choose one you want to focus at the moment and parse it. – Kuba Jun 9 '14 at 18:36
@AndréF. Switch those two compositions ;) Please delete old not relevant comments. – Kuba Jun 9 '14 at 18:44

I like Pick for this:

Pick[
{up, down, left, right},
Extract[matrix, {{i + 1, j}, {i - 1, j}, {i, j - 1}, {i, j + 1}}],
x_ /; x != 2
] // RandomChoice

-
a = matrix[[i + 1, j]];
b = matrix[[i - 1, j]];
c = matrix[[i, j + 1]];
d = matrix[[i, j - 1]];

Which[
And[a != 2, b != 2, c != 2, d != 2], RandomChoice[{up, down, right, left}],
And[a != 2, b != 2, c == 2, d != 2], RandomChoice[{up, down, left}],
(* ... *)
And[a == 2, b != 2, c == 2, d == 2], up
]


If you want to store the choice in a variable just precede the above with

variable = Which[ ... ]


Last but not least:

 RandomChoice[{up}]]


is redundant.

In case you want to implement a kind of random walk with this, have a look here: http://demonstrations.wolfram.com/TurtleGraphics/

-
My objective is no random walk until it find a certain value. There will be two different tables, one representing what is known by the subject and another representing the world. When the subject walk it has to update the table containing what is known by him and in case it find a wall (the number two) it should update and return to previous position. – André F. Jun 4 '14 at 19:15

  choices=Flatten@{If[ matrix[[i+1,j]]!=2 , up ,{}],
If[ matrix[[i-1,j]]!=2 , down ,{}],
If[ matrix[[i,j-1]]!=2 , left ,{}],
If[ matrix[[i,j+1]]!=2 , right ,{}]}
If[choices!={},RandomChoice[choices]]


or even

  choices=Flatten@MapThread[If[ #1!=2,#2,{} ]&,
{{matrix[[i+1,j]],matrix[[i-1,j]],matrix[[i,j-1]],matrix[[i,j+1]]},
{up,             down,            left,           right}}]
If[choices!={},RandomChoice[choices]]


note in this case the first approach is probably better since your next problem will be throwing errors when you hit the edge so you should do:

   If[ i>1 && matrix[[i-1,j]]!=2 , down ,{}]


etc.

-
pure intuition tells me that your approach would give another result than the definition of the OP. You should, therefore, run it over a short example matrix and compare. – eldo Jun 4 '14 at 19:57
the only issue is I failed to check the case of no choices, will fix. – george2079 Jun 4 '14 at 20:08