Module Name:    src
Committed By:   rillig
Date:           Sun May 22 08:12:15 UTC 2022

Modified Files:
        src/games/gomoku: bdisp.c main.c

Log Message:
gomoku: extract save_game from main

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/games/gomoku/bdisp.c
cvs rdiff -u -r1.49 -r1.50 src/games/gomoku/main.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/gomoku/bdisp.c
diff -u src/games/gomoku/bdisp.c:1.42 src/games/gomoku/bdisp.c:1.43
--- src/games/gomoku/bdisp.c:1.42	Sat May 21 17:19:10 2022
+++ src/games/gomoku/bdisp.c	Sun May 22 08:12:15 2022
@@ -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 @@ get_line(char *buf, int size, void (*on_
 }
 
 /*
- * 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)

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.49 src/games/gomoku/main.c:1.50
--- src/games/gomoku/main.c:1.49	Sat May 21 19:02:14 2022
+++ src/games/gomoku/main.c	Sun May 22 08:12:15 2022
@@ -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 @@ warn_if_exists(const char *fname)
 		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 @@ again:
 				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 @@ again:
 			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;
 			}
 		}

Reply via email to