Subject: 24.5; [PATCH] Fix apple spawning behaviour

From: Vasilij Schneidermann <[email-masked]>
Date: Thu, 27 Aug 2015 08:49:11 +0200

Classic renditions of Snake initially place an apple at a random
position, then spawn it anew if and only if it has been eaten by the
snake.  M-x snake behaves differently, it does spawn apples in regular
time intervals at former positions of the snake's tail.  To put it
bluntly, this makes it look as if the snake is leaking apples.

The attached patch introduces a new function, `snake-set-dot` which is
called at game initialization and upon collision with an apple.  The
code responsible for placing apples in periodic intervals got snipped
out, together with the now unnecessary `snake-cycle` variable
responsible for tracking at which position the apple emission cycle is.

In GNU Emacs 24.5.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.16.2)
 of 2015-04-20 on bitzer.hoetzel.info
Windowing system distributor `The X.Org Foundation', version 11.0.11702000
System Description: Arch Linux

Configured using:
 `configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --with-x-toolkit=gtk3 --with-xft
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong
 --param=ssp-buffer-size=4' CPPFLAGS=-D_FORTIFY_SOURCE=2
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro'

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
easymenu mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util help-fns mail-prsvr mail-utils time-date tooltip electric
uniquify ediff-hook vc-hooks lisp-float-type mwheel x-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list newcomment lisp-mode
prog-mode register page menu-bar rfn-eshadow timer select scroll-bar
mouse jit-lock font-lock syntax facemenu font-core frame cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese case-table epa-hook jka-cmpr-hook help simple abbrev
minibuffer nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote make-network-process
dbusbind gfilenotify dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty emacs)

Memory information:
((conses 16 71052 7148)
 (symbols 48 17559 0)
 (miscs 40 41 113)
 (strings 32 9062 4671)
 (string-bytes 1 249099)
 (vectors 16 8907)
 (vector-slots 8 383084 18361)
 (floats 8 63 83)
 (intervals 56 180 10)
 (buffers 960 11)
 (heap 1024 37966 926))
diff --git a/lisp/play/snake.el b/lisp/play/snake.el
index d42ba44..c1e7e9f 100644
--- a/lisp/play/snake.el
+++ b/lisp/play/snake.el
@@ -144,7 +144,6 @@
 (defvar snake-velocity-x 1)
 (defvar snake-velocity-y 0)
 (defvar snake-positions nil)
-(defvar snake-cycle 0)
 (defvar snake-score 0)
 (defvar snake-paused nil)
 (defvar snake-moved-p nil)
@@ -164,7 +163,6 @@ and then start moving it leftwards.")
 (make-variable-buffer-local 'snake-velocity-x)
 (make-variable-buffer-local 'snake-velocity-y)
 (make-variable-buffer-local 'snake-positions)
-(make-variable-buffer-local 'snake-cycle)
 (make-variable-buffer-local 'snake-score)
 (make-variable-buffer-local 'snake-paused)
 (make-variable-buffer-local 'snake-moved-p)
@@ -237,7 +235,6 @@ and then start moving it leftwards.")
 	snake-velocity-x	snake-initial-velocity-x
 	snake-velocity-y	snake-initial-velocity-y
 	snake-positions		nil
-	snake-cycle		1
 	snake-score		0
 	snake-paused		nil
 	snake-moved-p           nil
@@ -251,6 +248,14 @@ and then start moving it leftwards.")
       (cl-incf y snake-velocity-y)))
   (snake-update-score))
 
+(defun snake-set-dot ()
+  (let ((x (random snake-width))
+	(y (random snake-height)))
+    (while (not (= (gamegrid-get-cell x y) snake-blank))
+      (setq x (random snake-width))
+      (setq y (random snake-height)))
+    (gamegrid-set-cell x y snake-dot)))
+
 (defun snake-update-game (snake-buffer)
   "Called on each clock tick.
 Advances the snake one square, testing for collision.
@@ -268,23 +273,20 @@ Argument SNAKE-BUFFER is the name of the buffer."
 	(cond ((= c snake-dot)
 	       (cl-incf snake-length)
 	       (cl-incf snake-score)
-	       (snake-update-score))
+	       (snake-update-score)
+	       (snake-set-dot))
 	      (t
 	       (let* ((last-cons (nthcdr (- snake-length 2)
 					 snake-positions))
 		      (tail-pos (cadr last-cons))
 		      (x0 (aref tail-pos 0))
 		      (y0 (aref tail-pos 1)))
-		 (gamegrid-set-cell x0 y0
-				    (if (= (% snake-cycle 5) 0)
-					snake-dot
-				      snake-blank))
-		 (cl-incf snake-cycle)
+		 (gamegrid-set-cell x0 y0 snake-blank)
 		 (setcdr last-cons nil))))
 	(gamegrid-set-cell x y snake-snake)
 	(setq snake-positions
 	      (cons (vector x y) snake-positions))
-	  (setq snake-moved-p nil)))))
+	(setq snake-moved-p nil)))))
 
 (defun snake-update-velocity ()
   (unless snake-moved-p
@@ -339,6 +341,7 @@ Argument SNAKE-BUFFER is the name of the buffer."
   "Start a new game of Snake."
   (interactive)
   (snake-reset-game)
+  (snake-set-dot)
   (use-local-map snake-mode-map)
   (gamegrid-start-timer snake-tick-period 'snake-update-game))