all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#56116: Feature: new Tetris randomizer
@ 2022-06-20 21:41 Timothée Denizou
  2022-06-21 10:59 ` Lars Ingebrigtsen
  2022-06-21 11:04 ` Eli Zaretskii
  0 siblings, 2 replies; 8+ messages in thread
From: Timothée Denizou @ 2022-06-20 21:41 UTC (permalink / raw)
  To: 56116


[-- Attachment #1.1: Type: text/plain, Size: 516 bytes --]

Getting frustrated about having 4 time in a row the same tetromino ?

Wait no further !

This patch implement the seven bag randomizer, pulling a piece out of
the bag each time and filling the bag when it is empty. This allows for
a more uniform distribution on small samples.

At our school, we have plenty of time at the end of mcqs to play tetris
in emacs and this was something I wanted to change for a bit.

This is also my first contribution ever on an open source project.
The code may not be well formatted.

[-- Attachment #1.2: Type: text/html, Size: 578 bytes --]

[-- Attachment #2: 0001-Feature-new-tetris-randomizer.patch --]
[-- Type: application/octet-stream, Size: 1662 bytes --]

From 3b5b59a654606ce0bd637f62cbf73c3185b79e29 Mon Sep 17 00:00:00 2001
From: Timothee Denizou <timothee.denizou@epita.fr>
Date: Mon, 20 Jun 2022 23:33:32 +0200
Subject: [PATCH] Feature new tetris randomizer

* Added 7 bag randomizer for tetris
 A piece is selected from the bag and removed each time we want a piece
 When the bag is empty, refill the bag with the seven piece and shuffle it
---
 lisp/play/tetris.el | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lisp/play/tetris.el b/lisp/play/tetris.el
index 8ce2453c75..d8862259ed 100644
--- a/lisp/play/tetris.el
+++ b/lisp/play/tetris.el
@@ -233,6 +233,7 @@ each one of its four blocks.")
 (defvar-local tetris-pos-x 0)
 (defvar-local tetris-pos-y 0)
 (defvar-local tetris-paused nil)
+(defvar-local tetris-bag nil)
 
 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -341,10 +342,21 @@ each one of its four blocks.")
   (let ((period (tetris-get-tick-period)))
     (if period (gamegrid-set-timer period))))
 
+(defun tetris-shuffle (sequence)
+  (loop for i from (length sequence) downto 2 do
+    (rotatef (elt sequence (random i))
+             (elt sequence (1- i))))
+  sequence)
+
+(defun tetris-seven-bag ()
+  (when (not tetris-bag)
+    (setq tetris-bag (tetris-shuffle (list 0 1 2 3 4 5 6))))
+  (pop tetris-bag))
+
 (defun tetris-new-shape ()
   (setq tetris-shape tetris-next-shape)
   (setq tetris-rot 0)
-  (setq tetris-next-shape (random 7))
+  (setq tetris-next-shape (tetris-seven-bag))
   (setq tetris-pos-x (/ (- tetris-width (tetris-shape-width)) 2))
   (setq tetris-pos-y 0)
   (if (tetris-test-shape)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-06-22  2:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-20 21:41 bug#56116: Feature: new Tetris randomizer Timothée Denizou
2022-06-21 10:59 ` Lars Ingebrigtsen
2022-06-21 13:08   ` Visuwesh
2022-06-21 17:37     ` Timothée Denizou
2022-06-21 19:51       ` Lars Ingebrigtsen
2022-06-21 20:40         ` Timothée Denizou
2022-06-22  2:25       ` Eli Zaretskii
2022-06-21 11:04 ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.