[TIB] Matrix row-swapping (lots of code)


[Prev][Index][Thread]

[TIB] Matrix row-swapping (lots of code)





Here's a problem I'm working on, for my "Hunt the Wumpus"
game (see ticalc.org).  The game map consists of "rooms"
(nodes) connected by "passages" (edges) such that each of
the 20 nodes is connected to three other distinct nodes.
The graph is represented internally by the 3x20 matrix [D]:

[ 2  1  1  1  2  3  3  4  2  4  6  7 11 12 13  5  8 15  9 10 ]
[ 3  5  6  8  6  5  8  7 10  9 12 11 15 15 14 13 14 19 16 17 ]
[ 4  9  7 10 16 11 12 17 19 20 13 14 16 17 18 19 20 20 18 18 ]

where given a node R, the three nodes adjacent to R are

  [D](1,R)   [D](2,R)   [D](3,R)

At this point, it might help those who are paying attention to
draw an exploded dodecahedron and label the nodes 1-20 corresponding
to the elements of [D].  I hope that's clear.

Now, what I want to do is spice up this map a little bit, so the
player isn't always wandering around the same old [D].  In particular,
I want to swap two nodes in the graph.  For example, swapping nodes
1 and 20 in the graph gives the new matrix

[ 10 20 20 20  2  3  3  4  2  4  6  7 11 12 13  5  8 15  9  2 ]
[ 17  5  6  8  6  5  8  7 10  9 12 11 15 15 14 13 14 19 16  3 ]
[ 18  9  7 10 16 11 12 17 19  1 13 14 16 17 18 19  1  1 18  4 ]

You see we've swapped the columns corresponding to (1) and (20), and
also changed all the matrix's 1's to 20's and vice versa.

What I need is a fast FOOLPROOF algorithm for doing the swap.  Meaning
that swapping two distinct nodes will always produce the correct result.
My attempts so far have failed given two adjacent nodes.

The current, broken code:

  randInt(2,8)->A
  randInt(9,20)->B
  For(G,1,3
    For(H,1,3
      [D](G,[D](H,A
      Ans + (B-A)(Ans=A) -> T
      [D](G,[D](H,B
      Ans + (A-B)(Ans=B) -> [D](G,[D](H,B
      T -> [D](G,[D](H,A
    End
  End
  rowSwap([D]^T,A,B)^T -> [D]         {where ^T is the transpose operator}

Anyone have working code?