1
$\begingroup$

I am DMing a game of DnD and one of my players is really into fear effects, which is cool, but the effect of having monsters suffer from the "panicked" condition gets tedious to render via dice rolls.

The rule is, on the battle grid the monster will run for 1 square in a random direction, then from that new position it will move into another random adjacent square. repeat this process until its moved its full move speed.

movespeed = 6;
points = Point[
   NestList[{(#[[1]] + RandomChoice[{-1, 0, 1}]), #[[2]] + 
       RandomChoice[{-1, 0, 1}]} &, {11/2, 11/2}, movespeed]];
Graphics[{PointSize[Large], points}, 
 GridLines -> {Range[0, 11], Range[0, 11]}, 
 PlotRange -> {{0, 11}, {0, 11}}, Axes -> True]

I have written some code that shows me the squares the monster moves through, but I would love to replace the little black dots with numbers like "1", "2",...,"6" so that I know the path it actually took.

$\endgroup$

2 Answers 2

1
$\begingroup$

Something like he following?

movespeed = 6;
points = NestList[{(#[[1]] + RandomChoice[{-1, 0, 1}]), #[[2]] + 
      RandomChoice[{-1, 0, 1}]} &, {11/2, 11/2}, movespeed];
c = 1;
track = MapIndexed[{#1, Sequence[#2]} &, points];
track = GatherBy[track, # [[1]] &];
track = Text[
     ToString@
      If[Length[#] == 1, #[[1, 2, 1]], #[[All, 2, 1]]], #[[1, 1]]] & /@
    track;
Graphics[track, GridLines -> {Range[0, 11], Range[0, 11]}, 
 PlotRange -> {{0, 11}, {0, 11}}, Axes -> True]

enter image description here

$\endgroup$
2
  • $\begingroup$ Worth pointing out that the first argument to Text doesn't need to be a string $\endgroup$
    – b3m2a1
    Apr 18, 2021 at 22:21
  • $\begingroup$ @ b3m2a1 Thanks, there is always something to learn. $\endgroup$ Apr 19, 2021 at 7:47
1
$\begingroup$

Here's a very small adaption to what you had that just uses MapIndexed to count positions. If a square was tracked over multiple times it only shows the latest

movespeed = 6;
points =
  NestList[
   {(#[[1]] + RandomChoice[{-1, 0, 1}]), #[[2]] + 
      RandomChoice[{-1, 0, 1}]} &, {11/2, 11/2},
   movespeed
   ];
Graphics[
 {
  PointSize[1/(11*1.5)],
  MapIndexed[
   {
     Point[#1],
     {White, Inset[#2[[1]], #1]}
     } &,
   points
   ]
  },
 GridLines -> {Range[0, 11], Range[0, 11]}, PlotRange -> {{0, 11}, {0, 11}},
 Axes -> True
 ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.