encore

Répétitions espacées dans le terminal
git clone git://git.asteride.xyz/~ldp/encore.git
Journaux | Fichiers | Références

commit 7a9e8ed3447f649089e488b424dd1d7452cccec2
parent 5a2dece65c423305458c48720fac924ffa50e89a
Auteur: Selve <selve@asteride.xyz>
Date:   Thu, 29 Feb 2024 19:27:34 -0500

sélection aléatoire des fiches

Le nouveau drapeau PQ_DRAP_ALEA a pour effet de rendre aléatoire la sélection
des nouvelles fiches. Il peut être activé par la commande

	$ encore m -a1 [paquet]

Diffstat:
Mencore.c | 12++++++++++--
Mpaquet.c | 28+++++++++++++++++++++++++++-
Mpaquet.h | 7++++---
3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/encore.c b/encore.c @@ -343,7 +343,7 @@ cmd_m(int argc, char **argv) struct paquet pq; unsigned int n; char opt; - char *opts = "s:i:n:f:q:c:h"; + char *opts = "s:i:n:f:q:c:a:h"; assert(argc >= 0); assert(argv != NULL); @@ -395,6 +395,14 @@ cmd_m(int argc, char **argv) else if (optarg[0] == '1') pq.entete.draps |= PQ_DRAP_NOUV; continue; + case 'a': + if (optarg[1] != '\0') + goto err_util; + if (optarg[0] == '0') + pq.entete.draps &= ~PQ_DRAP_ALEA; + else if (optarg[0] == '1') + pq.entete.draps |= PQ_DRAP_ALEA; + continue; case 'h': utilisation(stdout, CMD_M); return 0; @@ -475,7 +483,7 @@ utilisation(FILE *f, int cmd) "e ??? paquet", "i [-h] paquet", ("m [-h] [-s pins] [-i pipi] [-n pnpi] [-f fi] [-q nfsn] " - "[-c dn] paquet"), + "[-c dn] [-a aléa] paquet"), "c [-h] ??? paquet" }; assert(f != NULL); diff --git a/paquet.c b/paquet.c @@ -68,6 +68,7 @@ static int pq_fiche_copier(struct paquet *, uint32_t, uint8_t *); static int pq_fiche_coller(struct paquet *, uint32_t, uint8_t *); +static int pq_fiches_echanger(struct paquet *, uint32_t, uint32_t); static int pq_fiche_monter(struct paquet *, uint32_t, uint8_t *); static int pq_fiche_descendre(struct paquet *, uint32_t, uint8_t *); @@ -218,13 +219,20 @@ pq_prochain(struct paquet *pq, int opts) indice = pq->entete.deb_n; } else { pq->entete.draps |= PQ_DRAP_QST; - if (pq->entete.nb - pq->entete.deb_n > 0 && (pq->entete.nfs > 0 || !(pq->entete.draps & PQ_DRAP_NOUV) || opts & PQ_OPT_PRCH_NOUV) && !(opts & PQ_OPT_PRCH_VIEU)) { pq->entete.draps &= ~PQ_DRAP_MNC; + if (pq->entete.draps & PQ_DRAP_ALEA) { + srand(time(NULL)); + indice = rand() + % (pq->entete.nb - pq->entete.deb_n) + + pq->entete.deb_n; + if (pq_fiches_echanger(pq, indice, pq->entete.deb_n) < 0) + return -1; + } indice = pq->entete.deb_n; } else if (pq->entete.deb_s > 0) { pq->entete.draps |= PQ_DRAP_MNC; @@ -252,6 +260,24 @@ pq_prochain(struct paquet *pq, int opts) return LIRE_3(fiche + DEC_FICHE_ID); } +static int +pq_fiches_echanger(struct paquet *pq, uint32_t pos1, uint32_t pos2) { + if (pos1 != pos2) { + uint8_t fiche1[PQ_FICHE_LG]; + uint8_t fiche2[PQ_FICHE_LG]; + if (pq_fiche_copier(pq, pos1, fiche1) < 0) + return -1; + if (pq_fiche_copier(pq, pos2, fiche2) < 0) + return -1; + if (pq_fiche_coller(pq, pos1, fiche2) < 0) + return -1; + if (pq_fiche_coller(pq, pos2, fiche1) < 0) + return -1; + } + + return 0; +} + int pq_reponse(struct paquet *pq, int rep) { diff --git a/paquet.h b/paquet.h @@ -6,9 +6,10 @@ #define PQ_OPT_PRCH_VIEU 01 /* doit être un vieux, a préséance */ #define PQ_OPT_PRCH_NOUV 02 /* peut être un nouveau */ -#define PQ_DRAP_NOUV 01 /* prendre en compte nfs */ -#define PQ_DRAP_QST 02 /* si on a posé une question */ -#define PQ_DRAP_MNC 04 /* si la nouvelle question se trouve dans le monceau */ +#define PQ_DRAP_NOUV 01 /* prendre en compte nfs */ +#define PQ_DRAP_QST 02 /* si on a posé une question */ +#define PQ_DRAP_MNC 04 /* si la nouvelle question se trouve dans le monceau */ +#define PQ_DRAP_ALEA 010 /* si les nouvelles fiches sont choisies aléatoirement */ #define PQ_PHASE_N (1 << 15)