Commit Graph

2328 Commits

Author SHA1 Message Date
rillig 76069dc145 gomoku: use consistent variable names
Previously, the name 's' was used for the index of a spot on the board,
as well as for the value of a combo, and for a few other purposes. Use
different names and mark the spot indexes using a custom type.

No binary change.
2022-05-29 00:12:11 +00:00
rillig cc51471792 gomoku: use custom magic value for end of file input
This allows the type for a spot index to be changed to an unsigned type.

No functional change.
2022-05-28 23:05:45 +00:00
rillig d9395f0376 gomoku: use unsigned arithmetic for bitboard
No functional change.
2022-05-28 21:48:21 +00:00
rillig ffd3ec5309 gomoku: document that 'whatsup' is called on ^C as well 2022-05-28 21:31:41 +00:00
rillig f8dbe39b74 gomoku: allow saving a finished game without pressing Shift 2022-05-28 20:57:26 +00:00
rillig 5f9b935517 gomoku: remove redundant parentheses, braces, newlines, clean up get_key
No functional change.
2022-05-28 20:54:31 +00:00
rillig 60c448cea2 gomoku: clean up initialization of overlap information
Fix variable names 'ia' and 'ib', which I forgot in the previous commit.

Move the documentation in the right places.

Split off another part of init_overlap, to make each function fit on a
single screen. Reducing the number of nested 'for' loops allows the
indentation to follow the NetBSD style.

No functional change.
2022-05-28 19:47:24 +00:00
rillig 463eec3668 gomoku: clean up local variable names in init_overlap
By clearly associating each variable to either frame A or frame B,
suddenly the code becomes understandable. For the benefit of future
readers, explain the naming scheme.

No functional change.
2022-05-28 18:55:16 +00:00
rillig 14facc424c gomoku: split initialization of the board into smaller functions
The comments about the "top border" and "bottom border" had been wrong
all the time. Mention the corners in the comments, to remove the magic
from the various '+ 1' in the code.

No functional change.
2022-05-28 17:51:27 +00:00
rillig 8fc3223a93 gomoku: highlight the winning frame 2022-05-28 08:32:55 +00:00
rillig a347aeb809 gomoku: group movelog and nmoves into a game struct
No functional change.
2022-05-28 08:19:18 +00:00
rillig b45d205818 gomoku: split makemove into smaller functions
No functional change.
2022-05-28 08:09:22 +00:00
rillig 408da2c120 gomoku: announce tie as early as possible 2022-05-28 07:58:35 +00:00
rillig 3a536c56b8 gomoku: extract update_overlap_different_direction
No functional change.
2022-05-28 06:25:35 +00:00
rillig f2e0424f1c gomoku: extract separate function update_overlap_same_direction
No functional change.
2022-05-28 05:44:41 +00:00
rillig 5685a50629 gomoku: reduce number of variables in update_overlap
No functional change.
2022-05-28 05:14:34 +00:00
rillig a94d789129 gomoku: reduce scope of local variable
No functional change.
2022-05-28 04:52:23 +00:00
rillig 256d3e3c22 gomoku: reduce variable reuse, make code more uniform
No functional change.
2022-05-27 23:29:15 +00:00
rillig 4a862cfde5 gomoku: reduce scope of local variables
No binary change.
2022-05-27 23:10:54 +00:00
rillig 32465329f0 gomoku: allow Black to win the game in the very last move 2022-05-27 20:48:42 +00:00
rillig 9aebef9fb0 gomoku: fix off-by-one error when deciding that the game is a tie 2022-05-27 20:35:58 +00:00
rillig 42e4930deb gomoku: replace 1-based movenum with 0-based nmoves
No functional change, not even the TIE that is wrongly announced when
the very last spot on the board is yet to be filled by Black.  Even
without this off-by-one error, it could be that filling the very last
spot completes a frame, so that code has been wrong all the time.

In practical terms, this situation only arises when the human player is
unconcentrated or the computer player has a bad strategy.  The latter
may well be, as the computer moves in the (boring) endgame are not
directed towards winning -- they fill irrelevant spots before relevant
ones.
2022-05-27 19:59:56 +00:00
rillig 267ff9487f gomoku: split local variable into separate variables
No binary change.
2022-05-27 19:30:56 +00:00
andvar 9f4a9600be fix various typos in comments, docs and log messages. 2022-05-24 06:27:59 +00:00
rillig 8473846f80 gomoku: add mouse support
NetBSD 10 provides the mouse handling functions from <curses.h> but does
not actually implement the mouse handling.  For the benefit of other
platforms, add mouse support; when linked with ncurses instead of
curses, it works.

Depending on the input device, mouse clicks are either reported as
"button 1 clicked" (mouse) or "button 1 pressed/released" (touchpad);
support both.

Be strict about the X coordinate when clicking.  Since the coordinates
are integer numbers, getting the location between two spots is
ambiguous, as it could be just one pixel away or right in the middle of
the space between the spots.
2022-05-22 13:38:08 +00:00
rillig 1b7e8737f3 gomoku: clean up coordinate calculations in get_coord
No functional change.
2022-05-22 12:42:54 +00:00
rillig ce8273398b gomoku: make coordinates in get_coord 1-based
The rest of the game has 1-based coordinates as well.

No functional change.
2022-05-22 12:30:05 +00:00
andvar f42f89fd6f fix various small typos, mainly in comments. 2022-05-22 11:27:33 +00:00
rillig fbd9c37871 gomoku: document a missed immediate win
No binary change.
2022-05-22 10:45:02 +00:00
rillig c997c7038b gomoku: extract main_game_loop from main
No functional change.
2022-05-22 09:17:15 +00:00
rillig ea1cda2971 gomoku: turn comments for testing mode into actual code
No functional change.
2022-05-22 08:58:31 +00:00
rillig e38c0d0293 gomoku: extract read_color from main
No functional change.
2022-05-22 08:47:26 +00:00
rillig 5e7b08baaa gomoku: extract declare_winner from main 2022-05-22 08:36:15 +00:00
rillig 3cf5ffad79 gomoku: extract ask_user_color from main
No functional change.
2022-05-22 08:31:12 +00:00
rillig 1b8a16f639 gomoku: extract read_move from main
No functional change.
2022-05-22 08:28:10 +00:00
rillig ab62ad42a7 gomoku: extract set_input_sources from main
No functional change.
2022-05-22 08:22:43 +00:00
rillig 863c7020c3 gomoku: extract parse_args from main
No functional change.
2022-05-22 08:18:49 +00:00
rillig c881ee6814 gomoku: extract save_game from main
No functional change.
2022-05-22 08:12:15 +00:00
rillig 91e5439d10 gomoku: fix error handling when reading moves from a file
The columns of the board are labeled from A to H and J to T, which makes
I5 or i5 an invalid coordinate.  Previously, reading this invalid
coordinate from a file resulted in the string "<6" appearing in the move
log.

The 'i' was converted into the nonexistent column 20, and PT(20, 5) got
an out-of-bounds argument, resulting in spot 120.  Converting this spot
back into coordinates resulted in PT(0, 6).  The '<' comes from
'letters[0]'.
2022-05-21 19:02:14 +00:00
rillig be456ab9a1 gomoku: right-align column header for move number 2022-05-21 17:19:10 +00:00
rillig 5b16bd53b8 gomoku: provide readable names for c.a and c.b
No binary change.
2022-05-21 16:39:14 +00:00
rillig 06d9af6489 gomoku: move comments closer to their corresponding entities
No binary change.
2022-05-21 15:21:40 +00:00
rillig 08019d5024 gomoku: inline macro MAXCOMBO
The name of the macro was not descriptive enough, the other constants in
bdinit also don't have names.

No binary change.
2022-05-21 15:11:24 +00:00
rillig 1425a8ae20 gomoku: convert input source constants to an enum
I also tried converting other macros, but s_occ would use more memory
and the return values for makemove are special values, besides the usual
coordinates in the form PT(x, y), so turning the special values into an
enum would be confusing.

No functional change.
2022-05-21 14:55:26 +00:00
rillig 7e168a678f gomoku: shorten code, error out on too many arguments 2022-05-21 14:23:10 +00:00
rillig 2865e412a1 gomoku: warn before overwriting a saved game file 2022-05-21 12:29:34 +00:00
rillig 5680343ed2 gomoku: do not accept overly long lines from the input
Typing beyond the screen and then pressing and holding backspace still
removes the prompt, just as before.
2022-05-21 12:16:53 +00:00
rillig 7d01361fb9 gomoku: fix deleting a character when reading a line 2022-05-21 12:08:06 +00:00
rillig 64454c622b gomoku: prevent thinking for too long
At search depth 9, picking a move takes about a minute on modern
hardware, which is enough for casual game play.  Even then, gomoku does
not always find the perfect move, so investing that much time seems
questionable.  Limiting the search depth also puts an upper bound on the
memory usage, which is quite high with 150 MB.
2022-05-21 10:01:49 +00:00
rillig f5e2b7b5aa gomoku: in debug mode, after pressing 'd', allow another debug command
While here, remove the sleeping, as there is no obvious reason for it.
2022-05-21 09:57:53 +00:00
rillig 8e7debfab2 gomoku: make drawing the player's names simpler
No functional change.
2022-05-21 09:25:51 +00:00
rillig a79b5066a6 gomoku: fix spelling and grammar in comments 2022-05-20 19:30:17 +00:00
rillig 6d8c791c47 gomoku: remove unused function 'list_eq' 2022-05-19 23:12:40 +00:00
rillig 2695c7a747 gomoku: use combined curses functions
To save some screen space in the source code and some bytes in the
generated binary.

No functional change.
2022-05-19 22:49:05 +00:00
rillig 05b0af0c8d gomoku: use NULL instead of more verbose null pointer
No binary change.
2022-05-19 22:29:36 +00:00
rillig 1e96790954 gomoku: clean up coordinate for the currently selected spot
There is no need to make this coordinate dependent on the board size.

No binary change.
2022-05-19 22:24:54 +00:00
rillig e80842a607 gomoku: clean up version control sections
Lint can handle these definitions, and the '#if 0' is redundant.

No functional change.
2022-05-19 22:19:18 +00:00
rillig 1acd9ee500 gomoku: remove unreachable code at the end of main
In C99 mode, lint accepts when control gets to the end of main without
returning a value.  It does not know anything about __dead though.

No binary change.
2022-05-19 20:09:46 +00:00
rillig e123f10c5a gomoku: replace magic numbers with proper coordinate calculation 2022-05-19 19:52:56 +00:00
rillig 6599d74ac5 gomoku: properly center the player's names 2022-05-19 19:16:38 +00:00
rillig 0144aec45b gomoku: de-obfuscate screen coordinate calculation
Modern compilers optimize linear integer arithmetic, so there is no
reason to use strange or misleading formulas.

Replace several magic numbers with proper formulas.

No binary change.
2022-05-19 18:58:59 +00:00
rillig 6646a33d72 gomoku: fix error message for small screen
- gomoku: Screen too small (need 8018d)
+ gomoku: Screen too small (need 80x24)
2022-05-19 17:02:51 +00:00
rillig 47863a265f gomoku: fix type mismatch in call to curses function
Not yet detected by lint, as lint is more permissive for type mismatches
if the value is a constant 0.  Being permissive doesn't make sense in
this case.  Now the calls to 'keypad' and 'leaveok' are consistent.

No binary change.
2022-05-19 16:58:39 +00:00
rillig 8a25385c9e gomoku: reduce scope of local variables
No binary change.
2022-05-18 22:35:13 +00:00
rillig 40aee379df gomoku: reduce scope of 'for' loop variables
No binary change.
2022-05-18 22:30:19 +00:00
rillig bd075df80d gomoku: rename local variable
Not every array of "characters" is a string.  In this case, it's rather
an array of bool.

No binary change.
2022-05-18 21:45:40 +00:00
rillig d1157f9be9 gomoku: call setprogname, for portability
No functional change on NetBSD, but the games are distributed to other
platforms as well.
2022-05-18 19:29:00 +00:00
rillig ee5b7371a9 gomoku: highlight the last played move 2022-05-16 22:03:16 +00:00
rillig ea4f930572 gomoku: add usage 2022-05-16 21:53:41 +00:00
rillig 509aea6249 gomoku: enable lint's strict bool mode
No functional change.
2022-05-16 21:48:45 +00:00
rillig 03582a4d36 gomoku: fix remaining lint warnings in -DDEBUG mode
The generated code changes since the calculations are now performed with
int precision.  No functional change since no overflow occurs.
2022-05-16 21:38:46 +00:00
rillig 46332bb5af gomoku: fix most lint warnings in -DDEBUG mode
No binary change.
2022-05-16 21:35:39 +00:00
rillig 73770304b0 gomoku: allow combination of WARNS=6 and -DDEBUG 2022-05-16 21:02:18 +00:00
rillig 21be9673ff gomoku: prepare lint's strict bool mode
No binary change.
2022-05-16 20:57:01 +00:00
rillig fb3a24af4a gomoku: fix lint warnings
Most warnings were about implicit conversions from ptrdiff_t to int; add
explicit cast for them, as they are far from overflowing int.

The casts from one pointer type to 'struct combostr **' were indeed
suspicious.  In these cases, a single region of memory is allocated to
store two objects of different type, without declaring a struct type for
their combination.  The second object is an array of variable size.

No binary change.
2022-05-16 19:55:58 +00:00
rillig 9af749df4c gomoku: condense code to decide which spot is better
The comments in the conditions only serve to align the expressions, to
allow humans to check that they correspond.

No functional change.
2022-05-16 19:20:25 +00:00
rillig af50285f47 gomoku: inline macros BSZ1, BSZ2, BSZ3, BSZ4
These macro names didn't convey any meaning, so replace them with their
replacement text, to have fewer identifiers to worry about.

No binary change.
2022-05-15 22:56:20 +00:00
rillig 20e9d475ef gomoku: remove write-only members from overlap_info
Since overlap_info is only used in pickmove.c, move it there.

No functional change.  In particular, in the middle of a game, gomoku
still tends to fall into analysis paralysis, thinking about the best
move for more than 3 minutes on modern hardware.  Since the algorithm is
basically unchanged since the 1990s, it must have been a long waiting
time back then, probably an hour per move.
2022-05-15 22:41:51 +00:00
rillig 3189ab5b3f gomoku: remove column macros
Of the 19 macros, only 3 were actually used.  Replace their uses with
expressions based on the board size.  It's a small step to making the
board size adjustable.  There are still other places using hard-coded
numbers.

No binary change.
2022-05-15 22:18:36 +00:00
rillig 0a647d356c gomoku: remove redundant parentheses around return values
No binary change.
2022-05-15 22:08:05 +00:00
rillig ee07f44521 gomoku: apply some style fixes towards KNF
There are still parts of the code that use an indentation level of 4
instead of the usual 8.  Fixing that right now would introduce more
unnatural line breaks, so defer that until later.

No binary change.
2022-05-15 22:00:11 +00:00
rillig 8e531acc76 gomoku: clean up unused code and lint warnings
No binary change.
2022-05-14 16:21:04 +00:00
rillig 79fb0bd9c4 cgram: clear bottom line of the screen at the end
Previously, the prompt line of the shell overwrote only part of the
prompt of cgram, the rest was still visible.
2022-05-14 14:20:10 +00:00
andvar f84252b461 fix various typos in comments and log messages. 2022-04-16 18:15:20 +00:00
andvar 2fa7e14158 fix various typos, mainly in comments, but also log messages, docs, game text. 2022-04-08 10:17:52 +00:00
andvar e82c4d9bb4 fix various typos, mainly in comments. 2022-04-04 19:33:44 +00:00
rillig e50ed3e547 cgram: define a word as a sequence of letters, not non-whitespace
Pressing Tab or Shift+Tab now advances to the next letter that could be
substituted, it no longer stops at punctuation or digits.  Since only
letters are scrambled, these are most interesting to be edited.
2022-03-28 20:00:29 +00:00
andvar f465408163 s/totaled/totalled/ 2021-12-31 12:42:24 +00:00
andvar 1cb7819f04 fix various typos in comments. 2021-12-12 22:20:52 +00:00
snj 758ded1782 fix typo, pointed out by Andras Farkas 2021-12-08 23:38:51 +00:00
rillig 9164d1568c dab: fix typo in comment 2021-12-05 09:53:15 +00:00
rillig ed0ae49597 dab.6: fix URL reference to the book 2021-12-05 09:46:05 +00:00
rillig 2824f61734 dab: clean up introductory comments
The filenames in these comments were redundant, half of them were wrong.
2021-12-05 09:22:45 +00:00
nia 82d379c3ac describe screenblank(1) 2021-11-26 17:38:07 +00:00
rillig dfa14757fe games/caesar: fix error handling for I/O
The '!= 0' is needed for an upcoming fix to lint's strict bool mode.

The additional text in the error messages is necessary because errx only
outputs the given string, with no details from errno.  Since ferror does
not set errno, no such details are available, so just output a generic
message, which is still better than just "caesar: <stdout>".
2021-11-16 20:42:47 +00:00
nat 3377e30f0d Better display in monochrome. 2021-11-13 20:59:13 +00:00
andvar 369abe069b fix various typos, mainly s/prefered/preferred/ 2021-11-07 20:31:09 +00:00
nia 7a33def692 cgram(6): realloc(x * y) -> reallocarr 2021-10-29 11:45:39 +00:00
nia 5a55ecfc78 atc(6): simplify reallocation logic 2021-10-29 11:44:22 +00:00
nia a373ad5865 sail(6): convert realloc(x * y) to reallocarr 2021-10-29 11:42:34 +00:00