summaryrefslogtreecommitdiff
path: root/docs/random_simple.tex
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2012-07-29 23:43:29 +1000
committerPeter Ward <peteraward@gmail.com>2012-07-29 23:43:29 +1000
commitc35db75eba1f67c6d6bbca9fefe7aaefb6b6d6e9 (patch)
treed32b851da74ed9b053248351f308a734b8c2a8b6 /docs/random_simple.tex
parentb62e033801895ad633f702685e50cf367ae0adee (diff)
Add start of tutorial.
Diffstat (limited to 'docs/random_simple.tex')
-rw-r--r--docs/random_simple.tex63
1 files changed, 63 insertions, 0 deletions
diff --git a/docs/random_simple.tex b/docs/random_simple.tex
new file mode 100644
index 0000000..7ccb3a5
--- /dev/null
+++ b/docs/random_simple.tex
@@ -0,0 +1,63 @@
+%%- from "macros.tex" import make_board -%%
+
+\section{Random Bot}
+\fasttrack{Choose a direction at random.}
+
+The next bot we’ll write is one which instead of moving in just one direction,
+chooses a direction at random to move in.
+Go on, try writing it yourself! I’ll wait here until you’re ready.
+
+Got it working? Good work!
+But you’ve probably noticed that there’s a problem:
+it doesn’t take long for our random bot to die.
+But why does it die?
+The answer is that once it eats an apple, it then has a tail, and since it
+doesn’t know any better, it will happily move into the square where its tail is.
+
+\begin{board}
+\hfill
+%
+\begin{subfigure}{.3\linewidth}
+< make_board([' *', ' A* ', ' * '])>
+\caption{Our intrepid snake heads towards an apple. Next move: \textbf{R}}
+\label{brd:random-death:1}
+\end{subfigure}
+\hfill
+%
+\begin{subfigure}{.3\linewidth}
+< make_board(['* *', ' aA ', ' * ']) >
+\caption{It has eaten the apple, and now has a tail. Next move: \textbf{L}}
+\label{brd:random-death:2}
+\end{subfigure}
+\hfill
+%
+\begin{subfigure}{.3\linewidth}
+< make_board(['* *', ' ', ' * ']) >
+\caption{It decided to move left, and ran into itself, oh no!}
+\label{brd:random-death:3}
+\end{subfigure}
+%
+\hfill
+
+\caption{The last moves of Random Bot before death.}
+\label{brd:random-death}
+\end{board}
+
+\pagebreak
+
+By the way, how long was your solution?
+If you’re still learning Python, you might like to have a peek at my solution to
+this bot, it’s only three lines long.
+Hopefully you didn’t write too much more than that!
+
+\pythonfile{random_simple.py}
+
+There are two key things that make my solution work.
+The first is the \texttt{random.choice} function,
+which returns a random item chosen from a sequence you give it.
+The second thing is that a string is a sequence:
+it is made up of the characters in it.
+So if you write \mint{python}|choice('UDLR')|,
+that’s the same as if you had written
+\mint{python}|choice(['U', 'D', 'L', 'R'])|.
+