emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/pacmacs de66c1ce17 112/472: Refactor out pacmacs--step-poi


From: ELPA Syncer
Subject: [nongnu] elpa/pacmacs de66c1ce17 112/472: Refactor out pacmacs--step-point (#74)
Date: Thu, 6 Jan 2022 21:59:17 -0500 (EST)

branch: elpa/pacmacs
commit de66c1ce17b74bfe1c4971403efcbf64b3ecca4b
Author: rexim <reximkut@gmail.com>
Commit: rexim <reximkut@gmail.com>

    Refactor out pacmacs--step-point (#74)
---
 pacmacs-board.el           | 12 ++++++++++++
 pacmacs-utils.el           |  7 +++++++
 pacmacs.el                 | 15 ++++-----------
 test/pacmacs-board-test.el | 13 +++++++++++++
 4 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/pacmacs-board.el b/pacmacs-board.el
index 894bd5d245..33934a1acd 100644
--- a/pacmacs-board.el
+++ b/pacmacs-board.el
@@ -32,6 +32,8 @@
 
 ;;; Code:
 
+(require 'pacmacs-utils)
+
 (defun pacmacs--make-board (width height)
   (let ((board (make-vector height nil)))
     (dotimes (row height)
@@ -70,6 +72,16 @@
                           (cons row column)))
                     objects))))
 
+(defun pacmacs--step-point (board row column direction)
+  (plist-bind ((width :width)
+               (height :heigth))
+      board
+    (let* ((velocity (pacmacs--direction-vector direction))
+           (d-row (car velocity))
+           (d-column (cdr velocity)))
+      (cons (mod (+ row d-row) height)
+            (mod (+ column d-column) width)))))
+
 (provide 'pacmacs-board)
 
 ;;; pacmacs-board.el ends here
diff --git a/pacmacs-utils.el b/pacmacs-utils.el
index a0631cdf51..9d649bcb5a 100644
--- a/pacmacs-utils.el
+++ b/pacmacs-utils.el
@@ -50,6 +50,13 @@ side-effects."
     (plist-put plist property
                (funcall transformer value))))
 
+(defun pacmacs--direction-vector (direction)
+  (let ((direction-table (list 'left  (cons  0 -1)
+                               'right (cons  0  1)
+                               'up    (cons -1  0)
+                               'down  (cons  1  0))))
+    (plist-get direction-table direction)))
+
 (provide 'pacmacs-utils)
 
 ;;; pacmacs.el ends here
diff --git a/pacmacs.el b/pacmacs.el
index 5186c7695c..244def47c9 100644
--- a/pacmacs.el
+++ b/pacmacs.el
@@ -52,12 +52,7 @@
 (defvar pacmacs-board-height 10)
 (defvar pacmacs-score 0)
 
-(defvar pacmacs-direction-table nil)
-(setq pacmacs-direction-table
-      (list 'left  (cons -1 0)
-            'right (cons 1 0)
-            'up    (cons 0 -1)
-            'down  (cons 0 1)))
+
 
 (defvar pacmacs-inversed-direction-table nil)
 (setq pacmacs-inversed-direction-table
@@ -182,11 +177,9 @@
                (speed :speed))
       game-object
     (if (zerop speed-counter)
-        (let* ((velocity (plist-get pacmacs-direction-table direction))
-               (new-row (mod (+ row (cdr velocity))
-                             pacmacs-board-height))
-               (new-column (mod (+ column (car velocity))
-                                pacmacs-board-width)))
+        (let* ((new-point (pacmacs--step-point pacmacs-board row column))
+               (new-row (car new-point))
+               (new-column (cdr new-point)))
           (plist-put game-object :speed-counter speed)
           (when (not (pacmacs--wall-at-p new-row new-column))
             (plist-put game-object :row new-row)
diff --git a/test/pacmacs-board-test.el b/test/pacmacs-board-test.el
index 162887571d..fa81b026ce 100644
--- a/test/pacmacs-board-test.el
+++ b/test/pacmacs-board-test.el
@@ -41,3 +41,16 @@
     (should (not (pacmacs--object-at-p board 0 1 objects)))
     (should (pacmacs--object-at-p board 0 5 objects))
     (should (not (pacmacs--object-at-p board 1 5 objects)))))
+
+(ert-deftest pacmacs--step-point-test ()
+  (let ((board (list :width 3
+                     :heigth 2))
+        (row 0)
+        (column 0))
+    (should (equal (cons 0 1) (pacmacs--step-point board
+                                                   row column
+                                                   'right)))
+    (should (equal (cons 0 2) (pacmacs--step-point board
+                                                   row column
+                                                   'left)))))
+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]