From a7e018d5e8925a96146494d469690dc51c704835 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sun, 16 Apr 2023 05:05:21 -0700 Subject: [PATCH] Improve 2020/ferguson1/termcaps.c --- 2020/ferguson1/termcaps.c | 24 ++++++------ 2020/ferguson1/terminals.md | 76 ++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/2020/ferguson1/termcaps.c b/2020/ferguson1/termcaps.c index 024615700..65818ec7b 100644 --- a/2020/ferguson1/termcaps.c +++ b/2020/ferguson1/termcaps.c @@ -1,11 +1,13 @@ -/* This will imperfectly: - - (1) Test if terminal supports the features Snake uses. - (2) Report terminal (and playing) size(s). - (3) Estimates the minimum number of columns to play unhindered (based on the - size, max size and growth rate). - (4) Reports the max size of the Snake and the number of bugs required to - reach that size (doesn't use shedding). +/* ioccc/2020/ferguson1 - Snake terminal testing utility + * + * This will imperfectly: + * + * (0) Test if terminal supports the features Snake uses. + * (1) Report terminal (and playing) size(s). + * (2) Estimates the minimum number of columns to play unhindered (based on the + * size, max size and growth rate). + * (3) Reports the max size of the Snake and the number of bugs required to + * reach that size but doesn't use shedding. */ #include @@ -84,7 +86,7 @@ int main(void) { i[caps].sup = tigetstr(caps[i].caps); /* tigetstr() returns (char *)-1 if the capability name isn't - * recognised; check for this before suggesting it's supported: */ + * recognised; check for this before suggesting it's (un)?supported: */ if (caps[i].sup == (char *)-1) caps[i].sup = NULL; if (!caps[i].sup) ++k; @@ -119,10 +121,10 @@ int main(void) rows = 14 + b(Y)*2 + a(y)*2 + a(x)*2 + b(N); if (x < (int)rows + 1) ++k; - printf("at least %zu columns recommended for snake size %-5zu (is %d)\n", rows+1,Y, x); + printf("at least %zu columns (currently %d) recommended for snake size %-5zu\n", rows+1,x, Y); rows = 14 + b(K)*2 + a(y)*2 + a(x)*2 + b(c(K,X)); - printf("at least %zu columns recommended for snake size %-5zu (is %d)\n\n", rows + 1, K, x); + printf("at least %zu columns (currently %d) recommended for capped snake size %-5zu\n\n", rows + 1,x, K); if (!k) diff --git a/2020/ferguson1/terminals.md b/2020/ferguson1/terminals.md index bc85fc516..914869f63 100644 --- a/2020/ferguson1/terminals.md +++ b/2020/ferguson1/terminals.md @@ -42,54 +42,68 @@ for the capabilities I use in the entry. It also tries to determine the minimum number of columns to play without the status line overflowing - even with the dynamic length. Try: - $ make test +```sh +$ make test +``` -This compiles the program and runs it; you should see something like: +This compiles the program [termcaps.c](termcaps.c) and runs it; you should see something like: - terminal supports cursor movement - terminal supports making cursor invisible - terminal supports bold - terminal supports colours - terminal rows 42 (39 playable) - terminal cols 157 (155 playable) +``` +terminal supports cursor movement +terminal supports making cursor invisible +terminal supports bold +terminal supports colours - snake size: 997 (max size: 6006) - bugs: 199 (max size: 1201) +terminal rows 41 (38 playable) +terminal cols 155 (153 playable) - at least 34 columns recommended for snake size 997 (is 157) - at least 37 columns recommended for snake size 6006 (is 157) +snake size: 997 (max size: 5776) + bugs: 199 (max size: 1155) - No problems detected. +at least 34 columns (currently 155) recommended for snake size 997 +at least 37 columns (currently 155) recommended for capped snake size 5776 -If I pass in any of the variables **GROW**, **MAXSIZE**, **SIZE** and/or -**LINES** or **COLUMNS** it bases its calculations on those variables. For +No problems detected. +``` + +Terminal rows and cols are the current values (the number of columns shown later +as well). The snake size is the default size to win (based on the variables, see +below). The `max size: 5776` is the capped size (or 'capsize'). The 199 bugs is +approximately the `snake_size / growth_size` i.e. how many bugs it +will take to win the game. The max bugs in the `bugs` line is approximately +`max_snake_size / growth_size` i.e. how many bugs it'll take to win at the +maximum (capped) snake size. + +If I pass in any of the variables **`GROW`**, **`MAXSIZE`**, **`SIZE`** and/or +**`LINES`** or **`COLUMNS`** it bases its calculations on those variables. For example: - $ MAXSIZE=-1 GROW=1 make test - terminal supports cursor movement - terminal supports making cursor invisible - terminal supports bold - terminal supports colours +```sh +terminal supports cursor movement +terminal supports making cursor invisible +terminal supports bold +terminal supports colours - terminal rows 42 (39 playable) - terminal cols 157 (155 playable) +terminal rows 41 (38 playable) +terminal cols 155 (153 playable) - snake size: 6006 (max size: 6006) - bugs: 6001 (max size: 6001) +snake size: 5776 (max size: 5776) + bugs: 5771 (max size: 5771) - at least 37 columns recommended for snake size 6006 (is 157) - at least 37 columns recommended for snake size 6006 (is 157) +at least 37 columns (currently 155) recommended for snake size 5776 +at least 37 columns (currently 155) recommended for capped snake size 5776 - No problems detected. +No problems detected. +``` - -As you can see it tells you the max size (the 'capsized' snake) as well as -the requested max size (if the requested size is bigger than the capsize then it +As you can see it tells you the max size (the 'capsized' snake) as well as the +requested max size (if the requested size is bigger than the capped size then it will be set to that value instead) of the snake. The capped size is like in the Snake game itself but the suggested minimum number of columns is based on a number of variables. + If I were to do the above but set columns to less < 35 then there would be a problem. But why if the utility recommends at least 37? This is because the max size of the snake also depends on the terminal size. This utility is not @@ -108,7 +122,7 @@ this should give you an idea. If there are any problems it will say how many (fatal and otherwise). The fatal problems are if curses fails to initialise or if movement of cursor cannot be -done (both seem odd). +done (both seem unlikely to happen nowadays). Both scripts run this utility and prompt if there are any problems; you can continue either way.