Initializing a New Game of Grand Theft Wumpus

With our graph construction stuff out of the way, we can write a simple function that initializes a brand-new game of Grand Theft Wumpus:

(defun new-game ()
    (setf *congestion-city-edges* (make-city-edges))
    (setf *congestion-city-nodes* (make-city-nodes *congestion-city-edges*))
   (setf *player-pos* (find-empty-node))
    (setf *visited-nodes* (list *player-pos*))
    (draw-city))

There are two new functions here. One, the find-empty-node function , ensures that the player doesn’t end up on top of a bad guy right at the beginning of the game. Here’s the code for that function:

(defun find-empty-node ()
   (let ((x (random-node)))
     (if (cdr (assoc x *congestion-city-nodes*))
         (find-empty-node)
          x)))

The find-empty-node function is pretty simple. First, it picks a random node to consider as the player’s starting position. Then it checks whether it is a completely empty node . If there’s stuff in that node, it simply calls itself again, trying another random spot .

Warning

If you ever decide to modify the game and make it more crowded with bad guys, you could end up in a situation where no empty nodes exist. In that case, this function will search forever and lock up your Lisp REPL, since we didn’t put in any checks to detect this situation.

The other new function in our new-game command is draw-city, which we’ll write next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.9