Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/gomoku gomoku: extract save_game from main



details:   https://anonhg.NetBSD.org/src/rev/22fa069e3081
branches:  trunk
changeset: 366295:22fa069e3081
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun May 22 08:12:15 2022 +0000

description:
gomoku: extract save_game from main

No functional change.

diffstat:

 games/gomoku/bdisp.c |  10 ++++++----
 games/gomoku/main.c  |  50 +++++++++++++++++++++-----------------------------
 2 files changed, 27 insertions(+), 33 deletions(-)

diffs (120 lines):

diff -r 5a1192781c15 -r 22fa069e3081 games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Sun May 22 05:33:46 2022 +0000
+++ b/games/gomoku/bdisp.c      Sun May 22 08:12:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.42 2022/05/21 17:19:10 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.43 2022/05/22 08:12:15 rillig Exp $        */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     @(#)bdisp.c     8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: bdisp.c,v 1.42 2022/05/21 17:19:10 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.43 2022/05/22 08:12:15 rillig Exp $");
 
 #include <curses.h>
 #include <string.h>
@@ -324,8 +324,10 @@
 }
 
 /*
- * Decent (n)curses interface for the game, based on Eric S. Raymond's
- * modifications to the battleship (bs) user interface.
+ * Ask the user for the coordinate of a move, or return RESIGN or SAVE.
+ *
+ * Based on Eric S. Raymond's modifications to the battleship (bs) user
+ * interface.
  */
 int
 get_coord(void)
diff -r 5a1192781c15 -r 22fa069e3081 games/gomoku/main.c
--- a/games/gomoku/main.c       Sun May 22 05:33:46 2022 +0000
+++ b/games/gomoku/main.c       Sun May 22 08:12:15 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.49 2022/05/21 19:02:14 rillig Exp $ */
+/*     $NetBSD: main.c,v 1.50 2022/05/22 08:12:15 rillig Exp $ */
 
 /*
  * Copyright (c) 1994
@@ -36,7 +36,7 @@
 __COPYRIGHT("@(#) Copyright (c) 1994\
  The Regents of the University of California.  All rights reserved.");
 /*     @(#)main.c      8.4 (Berkeley) 5/4/95   */
-__RCSID("$NetBSD: main.c,v 1.49 2022/05/21 19:02:14 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.50 2022/05/22 08:12:15 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -97,11 +97,27 @@
                clrtoeol();
 }
 
+static void
+save_game(void)
+{
+       char fname[PATH_MAX];
+       FILE *fp;
+
+       ask("Save file name? ");
+       (void)get_line(fname, sizeof(fname), warn_if_exists);
+       if ((fp = fopen(fname, "w")) == NULL) {
+               misclog("cannot create save file");
+               return;
+       }
+       for (int i = 0; i < movenum - 1; i++)
+               fprintf(fp, "%s\n", stoc(movelog[i]));
+       fclose(fp);
+}
+
 int
 main(int argc, char **argv)
 {
        char buf[128];
-       char fname[PATH_MAX];
        char *user_name;
        int color, curmove, i, ch;
        enum input_source input[2];
@@ -267,19 +283,7 @@
                                ask("Select move, (S)ave or (Q)uit.");
                                curmove = get_coord();
                                if (curmove == SAVE) {
-                                       FILE *fp;
-
-                                       ask("Save file name? ");
-                                       (void)get_line(fname, sizeof(fname),
-                                           warn_if_exists);
-                                       if ((fp = fopen(fname, "w")) == NULL) {
-                                               misclog("cannot create save file");
-                                               goto getinput;
-                                       }
-                                       for (i = 0; i < movenum - 1; i++)
-                                               fprintf(fp, "%s\n",
-                                                   stoc(movelog[i]));
-                                       fclose(fp);
+                                       save_game();
                                        goto getinput;
                                }
                                if (curmove != RESIGN &&
@@ -341,19 +345,7 @@
                        if (ch == 'Y' || ch == 'y')
                                goto again;
                        if (ch == 'S') {
-                               FILE *fp;
-
-                               ask("Save file name? ");
-                               (void)get_line(fname, sizeof(fname),
-                                   warn_if_exists);
-                               if ((fp = fopen(fname, "w")) == NULL) {
-                                       misclog("cannot create save file");
-                                       goto replay;
-                               }
-                               for (i = 0; i < movenum - 1; i++)
-                                       fprintf(fp, "%s\n",
-                                           stoc(movelog[i]));
-                               fclose(fp);
+                               save_game();
                                goto replay;
                        }
                }



Home | Main Index | Thread Index | Old Index