1 ## Copyright (C) 2009 Jaroslav Hajek <highegg@gmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} rolldices (@var{n})
18 ## @deftypefnx{Function File} rolldices (@var{n}, @var{nrep}, @var{delay})
19 ## Returns @var{n} random numbers from the 1:6 range, displaying a visual selection
22 ## @var{nrep} sets the number of rolls, @var{delay} specifies time between
23 ## successive rolls in seconds. Default is nrep = 25 and delay = 0.1.
25 ## Requires a terminal with ANSI escape sequences enabled.
28 function numbers = rolldices (n, nrep = 25, delay = .1)
33 persistent matrices = getmatrices ();
35 screen_cols = getenv ("COLUMNS");
36 if (isempty (screen_cols))
39 screen_cols = str2num (screen_cols);
42 dices_per_row = floor ((screen_cols-1) / 7);
44 oldpso = page_screen_output (0);
50 m = min (n, dices_per_row);
53 puts (char ([27, 91, 51, 70]));
56 nums = ceil (6 * rand (1, m));
57 disp (matrices(:,:,nums)(:,:));
59 numbers = [numbers, nums];
63 unwind_protect_cleanup
64 page_screen_output (oldpso);
69 function matrices = getmatrices ()
70 lbrk = [27, 91, 55, 109](ones (1, 3), :);
71 rbrk = [27, 91, 50, 55, 109](ones (1, 3), :);
72 spcs = [32, 32; 32, 32; 32, 32];
76 " @ @@ @@ @@ @"], [3, 5, 6]);
78 matrices = mat2cell (dchrs, 3, 5, ones (1, 6));
79 matrices = cellfun (@(mat) [spcs, lbrk, mat, rbrk], matrices, "UniformOutput", false);
80 matrices = cat (3, matrices{:});