Module Name:    src
Committed By:   rillig
Date:           Sun May 22 08:18:49 UTC 2022

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

Log Message:
gomoku: extract parse_args from main

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 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/main.c
diff -u src/games/gomoku/main.c:1.50 src/games/gomoku/main.c:1.51
--- src/games/gomoku/main.c:1.50	Sun May 22 08:12:15 2022
+++ src/games/gomoku/main.c	Sun May 22 08:18:49 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.50 2022/05/22 08:12:15 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.51 2022/05/22 08:18:49 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.50 2022/05/22 08:12:15 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.51 2022/05/22 08:18:49 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -114,23 +114,10 @@ save_game(void)
 	fclose(fp);
 }
 
-int
-main(int argc, char **argv)
+static void
+parse_args(int argc, char **argv)
 {
-	char buf[128];
-	char *user_name;
-	int color, curmove, i, ch;
-	enum input_source input[2];
-
-	/* Revoke setgid privileges */
-	setgid(getgid());
-
-	setprogname(argv[0]);
-
-	user_name = getlogin();
-	strlcpy(user, user_name != NULL ? user_name : "you", sizeof(user));
-
-	color = curmove = 0;
+	int ch;
 
 	prog = strrchr(argv[0], '/');
 	prog = prog != NULL ? prog + 1 : argv[0];
@@ -140,6 +127,9 @@ main(int argc, char **argv)
 		case 'b':	/* background */
 			interactive = false;
 			break;
+		case 'c':	/* testing: computer versus computer */
+			test = 2;
+			break;
 		case 'd':
 			debug++;
 			break;
@@ -150,14 +140,11 @@ main(int argc, char **argv)
 		case 'u':	/* testing: user versus user */
 			test = 1;
 			break;
-		case 'c':	/* testing: computer versus computer */
-			test = 2;
-			break;
 		default:
 		usage:
 			fprintf(stderr, "usage: %s [-bcdu] [-Dfile] [file]\n",
 			    getprogname());
-			return EXIT_FAILURE;
+			exit(EXIT_FAILURE);
 		}
 	}
 	argc -= optind;
@@ -166,6 +153,27 @@ main(int argc, char **argv)
 		goto usage;
 	if (argc == 1 && (inputfp = fopen(*argv, "r")) == NULL)
 		err(1, "%s", *argv);
+}
+
+int
+main(int argc, char **argv)
+{
+	char buf[128];
+	char *user_name;
+	int color, curmove;
+	enum input_source input[2];
+
+	/* Revoke setgid privileges */
+	setgid(getgid());
+
+	setprogname(argv[0]);
+
+	user_name = getlogin();
+	strlcpy(user, user_name != NULL ? user_name : "you", sizeof(user));
+
+	color = curmove = 0;
+
+	parse_args(argc, argv);
 
 	if (debug == 0)
 		srandom((unsigned int)time(0));
@@ -187,7 +195,7 @@ again:
 			mvprintw(BSZ + 3, 0, "Black moves first. ");
 			ask("(B)lack or (W)hite? ");
 			for (;;) {
-				ch = get_key(NULL);
+				int ch = get_key(NULL);
 				if (ch == 'b' || ch == 'B') {
 					color = BLACK;
 					break;
@@ -246,6 +254,7 @@ again:
 		refresh();
 	}
 
+	int outcome;
 	for (color = BLACK; ; color = color != BLACK ? BLACK : WHITE) {
 	top:
 		switch (input[color]) {
@@ -313,14 +322,14 @@ again:
 			misclog("%3d%*s%-6s", movenum,
 			    color == BLACK ? 2 : 9, "", stoc(curmove));
 		}
-		if ((i = makemove(color, curmove)) != MOVEOK)
+		if ((outcome = makemove(color, curmove)) != MOVEOK)
 			break;
 		if (interactive)
 			bdisp();
 	}
 	if (interactive) {
 		move(BSZ + 3, 0);
-		switch (i) {
+		switch (outcome) {
 		case WIN:
 			if (input[color] == PROGRAM)
 				addstr("Ha ha, I won");
@@ -338,10 +347,10 @@ again:
 		}
 		clrtoeol();
 		bdisp();
-		if (i != RESIGN) {
+		if (outcome != RESIGN) {
 		replay:
 			ask("Play again? ");
-			ch = get_key("YyNnQqSs");
+			int ch = get_key("YyNnQqSs");
 			if (ch == 'Y' || ch == 'y')
 				goto again;
 			if (ch == 'S') {

Reply via email to