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

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

[nongnu] elpa/pacmacs c865fda214 120/472: Merge branch 'board-refactorin


From: ELPA Syncer
Subject: [nongnu] elpa/pacmacs c865fda214 120/472: Merge branch 'board-refactoring-74'. Close #74
Date: Thu, 6 Jan 2022 21:59:18 -0500 (EST)

branch: elpa/pacmacs
commit c865fda214c4fbdcc5a17f78f706f03386edd064
Merge: 2c0bcf993f e5bd77740d
Author: rexim <reximkut@gmail.com>
Commit: rexim <reximkut@gmail.com>

    Merge branch 'board-refactoring-74'. Close #74
---
 pacmacs-board.el                                   |  97 ++++++++++++++++
 pacmacs-utils.el                                   |   7 ++
 pacmacs.el                                         | 126 ++++++++-------------
 test/{pacman-anim-test.el => pacmacs-anim-test.el} |   0
 test/pacmacs-board-test.el                         |  68 +++++++++++
 ...{pacman-image-test.el => pacmacs-image-test.el} |   0
 test/pacmacs-test.el                               |  74 ++++--------
 ...{pacman-utils-test.el => pacmacs-utils-test.el} |   0
 8 files changed, 243 insertions(+), 129 deletions(-)

diff --git a/pacmacs-board.el b/pacmacs-board.el
new file mode 100644
index 0000000000..a3deeaa812
--- /dev/null
+++ b/pacmacs-board.el
@@ -0,0 +1,97 @@
+;;; pacmacs-board.el --- Pacman for Emacs
+
+;; Copyright (C) 2015 Codingteam
+
+;; Author: Codingteam <codingteam@conference.jabber.ru>
+;; Maintainer: Alexey Kutepov <reximkut@gmail.com>
+;; URL: http://github.com/rexim/pacmacs.el
+
+;; Permission is hereby granted, free of charge, to any person
+;; obtaining a copy of this software and associated documentation
+;; files (the "Software"), to deal in the Software without
+;; restriction, including without limitation the rights to use, copy,
+;; modify, merge, publish, distribute, sublicense, and/or sell copies
+;; of the Software, and to permit persons to whom the Software is
+;; furnished to do so, subject to the following conditions:
+
+;; The above copyright notice and this permission notice shall be
+;; included in all copies or substantial portions of the Software.
+
+;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+;; SOFTWARE.
+
+;;; Commentary:
+
+;; Routines for working with board
+
+;;; Code:
+
+(require 'pacmacs-utils)
+
+(defun pacmacs--make-board (width height)
+  (let ((board (make-vector height nil)))
+    (dotimes (row height)
+      (aset board row (make-vector width nil)))
+    (list :width width
+          :height height
+          :data board)))
+
+(defun pacmacs--cell-get (board row column)
+  (plist-bind ((width :width)
+               (height :height)
+               (data :data))
+      board
+    (aref (aref data (mod row height))
+          (mod column width))))
+
+(defun pacmacs--cell-set (board row column value)
+  (plist-bind ((width :width)
+               (height :height)
+               (data :data))
+      board
+    (aset (aref data (mod row height))
+          (mod column width)
+          value)))
+
+(defun pacmacs--object-at-p (board row column objects)
+  (plist-bind ((width :width)
+               (height :height))
+      board
+    (member (cons (mod row height)
+                  (mod column width))
+            (mapcar #'(lambda (object)
+                        (plist-bind ((row :row)
+                                     (column :column))
+                            object
+                          (cons row column)))
+                    objects))))
+
+(defun pacmacs--step-point (board row column direction)
+  (plist-bind ((width :width)
+               (height :height))
+      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)))))
+
+(defun pacmacs--fill-board (board value)
+  (plist-bind ((width :width)
+               (height :height)
+               (data :data))
+      board
+    (dotimes (row height)
+      (dotimes (column width)
+        (aset (aref data row) column value)))))
+
+(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 3d21f5447e..691946b85d 100644
--- a/pacmacs.el
+++ b/pacmacs.el
@@ -38,6 +38,7 @@
 (require 'dash)
 
 (require 'pacmacs-anim)
+(require 'pacmacs-board)
 (require 'pacmacs-image)
 (require 'pacmacs-utils)
 
@@ -51,13 +52,6 @@
 (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
       (list (cons (cons -1 0) 'left)
@@ -133,12 +127,6 @@
         :speed 0
         :speed-counter 0))
 
-(defun pacmacs--init-board (width height)
-  (let ((board (make-vector height nil)))
-    (dotimes (row height)
-      (aset board row (make-vector width nil)))
-    board))
-
 (defun pacmacs--kill-buffer-and-its-window (buffer-or-name)
   (let ((buffer-window (get-buffer-window buffer-or-name)))
     (if (and buffer-window
@@ -147,21 +135,15 @@
           (kill-buffer-and-window))
       (kill-buffer buffer-or-name))))
 
-(defun pacmacs--object-at-p (row column objects)
-  (member (cons (mod row pacmacs-board-height)
-                (mod column pacmacs-board-width))
-          (mapcar #'(lambda (object)
-                      (plist-bind ((row :row)
-                                   (column :column))
-                          object
-                        (cons row column)))
-                  objects)))
-
 (defun pacmacs--wall-at-p (row column)
-  (pacmacs--object-at-p row column pacmacs-wall-cells))
+  (pacmacs--object-at-p pacmacs-board
+                        row column
+                        pacmacs-wall-cells))
 
 (defun pacmacs--pill-at-p (row column)
-  (pacmacs--object-at-p row column pacmacs-pills))
+  (pacmacs--object-at-p pacmacs-board
+                        row column
+                        pacmacs-pills))
 
 (defun pacmacs-quit ()
   (interactive)
@@ -169,8 +151,7 @@
     (pacmacs--kill-buffer-and-its-window pacmacs-buffer-name)))
 
 (defun pacmacs--cell-tracked-p (row column)
-  (aref (aref pacmacs-track-board (mod row pacmacs-board-height))
-        (mod column pacmacs-board-width)))
+  (pacmacs--cell-get pacmacs-track-board row column))
 
 (defun pacmacs--switch-direction (game-object direction)
   (plist-bind ((direction-animations :direction-animations))
@@ -194,22 +175,15 @@
                (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 
direction))
+               (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)
             (plist-put game-object :column new-column)))
       (plist-put game-object :speed-counter (1- speed-counter)))))
 
-(defun pacmacs--fill-board (board width height value)
-  (dotimes (row height)
-    (dotimes (column width)
-      (aset (aref board row) column value))))
-
 (defun pacmacs--possible-ways (row column)
   (list (cons (1+ row) column)
         (cons row (1+ column))
@@ -231,18 +205,15 @@
 
          (d-row (- end-row start-row))
          (d-column (- end-column start-column)))
-    (aset (aref pacmacs-track-board (mod start-row pacmacs-board-height))
-          (mod start-column pacmacs-board-width)
-          (cdr
-           (assoc (cons d-column
-                        d-row)
-                  pacmacs-inversed-direction-table)))))
+    
+    (pacmacs--cell-set pacmacs-track-board
+                       start-row start-column
+                       (cdr
+                        (assoc (cons d-column d-row)
+                               pacmacs-inversed-direction-table)))))
 
 (defun pacmacs--recalc-track-board ()
-  (pacmacs--fill-board pacmacs-track-board
-                       pacmacs-board-width
-                       pacmacs-board-height
-                       nil)
+  (pacmacs--fill-board pacmacs-track-board nil)
   (plist-bind ((player-row :row)
                (player-column :column))
       pacmacs-player-state
@@ -265,7 +236,7 @@
   (plist-bind ((row :row)
                (column :column))
       game-object
-    (let ((direction (aref (aref pacmacs-track-board row) column)))
+    (let ((direction (pacmacs--cell-get pacmacs-track-board row column)))
       (pacmacs--switch-direction game-object direction))))
 
 (defun pacmacs-tick ()
@@ -313,26 +284,27 @@
   (plist-bind ((row :row)
                (column :column))
       anim-object
-    (when (and (<= 0 row) (<= row (1- pacmacs-board-height))
-               (<= 0 column) (<= column (1- pacmacs-board-width)))
-      (aset (aref pacmacs-board row) column anim-object))))
+    (pacmacs--cell-set pacmacs-board row column anim-object)))
 
 (defun pacmacs-render-track-board ()
-  (dotimes (row pacmacs-board-height)
-    (dotimes (column pacmacs-board-width)
-      (let ((x (aref (aref pacmacs-track-board row) column)))
-        (cond
-         ((null x)
-          (insert "."))
-         ((equal x 'left)
-          (insert "<"))
-         ((equal x 'right)
-          (insert ">"))
-         ((equal x 'up)
-          (insert "^"))
-         ((equal x 'down)
-          (insert "v")))))
-    (insert "\n")))
+  (plist-bind ((width :width)
+               (height :height))
+      pacmacs-board
+    (dotimes (row height)
+      (dotimes (column width)
+        (let ((x (pacmacs--cell-get pacmacs-track-board row column)))
+          (cond
+           ((null x)
+            (insert "."))
+           ((equal x 'left)
+            (insert "<"))
+           ((equal x 'right)
+            (insert ">"))
+           ((equal x 'up)
+            (insert "^"))
+           ((equal x 'down)
+            (insert "v")))))
+      (insert "\n"))))
 
 (defun pacmacs-render-state ()
   (insert (format "Score: %d\n" pacmacs-score))
@@ -340,10 +312,7 @@
   (when pacmacs-debug-output
     (pacmacs-render-track-board))
 
-  (pacmacs--fill-board pacmacs-board
-                       pacmacs-board-width
-                       pacmacs-board-height
-                       (pacmacs--make-empty-cell))
+  (pacmacs--fill-board pacmacs-board (pacmacs--make-empty-cell))
 
   (pacmacs--put-object pacmacs-player-state)
 
@@ -356,11 +325,14 @@
   (dolist (wall pacmacs-wall-cells)
     (pacmacs--put-object wall))
 
-  (dotimes (row pacmacs-board-height)
-    (dotimes (column pacmacs-board-width)
-      (let ((anim-object (aref (aref pacmacs-board row) column)))
-        (pacmacs-render-object anim-object)))
-    (insert "\n")))
+  (plist-bind ((width :width)
+               (height :height))
+      pacmacs-board
+    (dotimes (row height)
+      (dotimes (column width)
+        (let ((anim-object (pacmacs--cell-get pacmacs-board row column)))
+          (pacmacs-render-object anim-object)))
+      (insert "\n"))))
 
 (defun pacmacs-up ()
   (interactive)
@@ -390,9 +362,9 @@
     (setq pacmacs-board-width board-width)
     (setq pacmacs-board-height board-height)
 
-    (setq pacmacs-board (pacmacs--init-board pacmacs-board-width
+    (setq pacmacs-board (pacmacs--make-board pacmacs-board-width
                                              pacmacs-board-height))
-    (setq pacmacs-track-board (pacmacs--init-board pacmacs-board-width
+    (setq pacmacs-track-board (pacmacs--make-board pacmacs-board-width
                                                    pacmacs-board-height))
 
     (setq pacmacs-wall-cells nil)
diff --git a/test/pacman-anim-test.el b/test/pacmacs-anim-test.el
similarity index 100%
rename from test/pacman-anim-test.el
rename to test/pacmacs-anim-test.el
diff --git a/test/pacmacs-board-test.el b/test/pacmacs-board-test.el
new file mode 100644
index 0000000000..f936a17905
--- /dev/null
+++ b/test/pacmacs-board-test.el
@@ -0,0 +1,68 @@
+(ert-deftest pacmacs--make-board-test ()
+  (let ((width 5)
+        (height 4)
+        (expected-board (list :width 5
+                              :height 4
+                              :data [[nil nil nil nil nil]
+                                     [nil nil nil nil nil]
+                                     [nil nil nil nil nil]
+                                     [nil nil nil nil nil]])))
+    (should (equal expected-board
+                   (pacmacs--make-board width height)))))
+
+(ert-deftest pacmacs--cell-get-test ()
+  (let ((input-board (list :width 3
+                           :height 2
+                           :data [[nil nil nil]
+                                  [nil 42 nil]])))
+    (should (equal 42 (pacmacs--cell-get input-board 1 1)))))
+
+(ert-deftest pacmacs--cell-set-test ()
+  (let ((input-board (list :width 3
+                           :height 2
+                           :data [[nil nil nil]
+                                  [nil nil nil]]))
+        (expected-board (list :width 3
+                              :height 2
+                              :data [[nil nil nil]
+                                     [nil 42 nil]])))
+    (pacmacs--cell-set input-board 1 1 42)
+    (should (equal expected-board
+                   input-board))))
+
+(ert-deftest pacmacs--object-at-p-test ()
+  (let ((board (list :width 5
+                     :height 4))
+        (objects (-map #'(lambda (x)
+                           (list :row x
+                                 :column x))
+                       (number-sequence 0 3))))
+    (should (pacmacs--object-at-p board 0 0 objects))
+    (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)))))
+
+(ert-deftest pacmacs--fill-board-test ()
+  (let ((input-board (list :width 2
+                           :height 2
+                           :data [[nil nil]
+                                  [nil nil]]))
+        (expected-board (list :width 2
+                              :height 2
+                              :data [[5 5]
+                                     [5 5]])))
+    (pacmacs--fill-board input-board 5)
+    (should (equal expected-board
+                   input-board))))
diff --git a/test/pacman-image-test.el b/test/pacmacs-image-test.el
similarity index 100%
rename from test/pacman-image-test.el
rename to test/pacmacs-image-test.el
diff --git a/test/pacmacs-test.el b/test/pacmacs-test.el
index 4e02b4299f..e27a3af2e1 100644
--- a/test/pacmacs-test.el
+++ b/test/pacmacs-test.el
@@ -1,63 +1,31 @@
 
-(ert-deftest pacmacs--fill-board-test ()
-  (let ((input-board [[nil nil]
-                      [nil nil]])
-        (expected-board [[5 5]
-                         [5 5]]))
-    (pacmacs--fill-board input-board 2 2 5)
-    (should (equal expected-board
-                   input-board))))
-
-
-(ert-deftest pacmacs--init-board-test ()
-  (let ((width 5)
-        (height 4)
-        (expected-board [[nil nil nil nil nil]
-                         [nil nil nil nil nil]
-                         [nil nil nil nil nil]
-                         [nil nil nil nil nil]]))
-    (should (equal expected-board
-                   (pacmacs--init-board width height)))))
-
-(ert-deftest pacmacs--object-at-p-test ()
-  (let ((pacmacs-board-width 5)
-        (pacmacs-board-height 4)
-        (objects (-map #'(lambda (x)
-                           (list :row x
-                                 :column x))
-                       (number-sequence 0 3))))
-    (should (pacmacs--object-at-p 0 0 objects))
-    (should (not (pacmacs--object-at-p 0 1 objects)))
-    (should (pacmacs--object-at-p 0 5 objects))
-    (should (not (pacmacs--object-at-p 1 5 objects)))))
-
 (ert-deftest pacmacs--cell-tracked-p-test ()
-  (let ((pacmacs-board-width 2)
-        (pacmacs-board-height 2)
-        (pacmacs-track-board [[nil nil]
-                              ['left 'right]]))
+  (let ((pacmacs-track-board (list :width 2
+                                   :height 2
+                                   :data [[nil nil]
+                                          ['left 'right]])))
     (should (not (pacmacs--cell-tracked-p 0 0)))
     (should (pacmacs--cell-tracked-p 1 0))))
 
 (ert-deftest pacmacs--track-point-test ()
-  (let ((pacmacs-board-width 2)
-        (pacmacs-board-height 2)
-        (pacmacs-track-board [[nil nil]
-                              [nil nil]]))
+  (let ((pacmacs-track-board (list :width 2
+                                   :height 2
+                                   :data [[nil nil]
+                                          [nil nil]])))
     (pacmacs--track-point (cons 0 0) (cons 0 1))
     (should (equal [[right nil]
                     [nil nil]]
-                   pacmacs-track-board))
+                   (plist-get pacmacs-track-board :data)))
     (pacmacs--track-point (cons 1 0) (cons 1 -1))
     (should (equal [[right nil]
                     [left nil]]
-                   pacmacs-track-board))))
+                   (plist-get pacmacs-track-board :data)))))
 
 (ert-deftest pacmacs--track-object-test ()
-  (let ((pacmacs-board-width 2)
-        (pacmacs-board-height 2)
-        (pacmacs-track-board [[right down]
-                              [up left]])
+  (let ((pacmacs-track-board (list :width 2
+                                   :height 2
+                                   :data [[right down]
+                                          [up left]]))
         (game-object (list :row 0
                            :column 0)))
     (with-mock
@@ -65,12 +33,14 @@
      (pacmacs--track-object game-object))))
 
 (ert-deftest pacmacs--put-object-test ()
-  (let ((pacmacs-board-width 2)
-        (pacmacs-board-height 2)
-        (pacmacs-board [[nil nil]
-                        [nil nil]])
+  (let ((pacmacs-board (list :width 2
+                             :height 2
+                             :data [[nil nil]
+                                    [nil nil]]))
         (anim-object (list :row 0 :column 1)))
     (pacmacs--put-object anim-object)
-    (should (equal [[nil (:row 0 :column 1)]
-                    [nil nil]]
+    (should (equal (list :width 2
+                         :height 2
+                         :data [[nil (:row 0 :column 1)]
+                                [nil nil]])
                    pacmacs-board))))
diff --git a/test/pacman-utils-test.el b/test/pacmacs-utils-test.el
similarity index 100%
rename from test/pacman-utils-test.el
rename to test/pacmacs-utils-test.el



reply via email to

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