1 1.21 rillig /* $NetBSD: stoc.c,v 1.21 2022/05/29 00:12:11 rillig Exp $ */ 2 1.3 cgd 3 1.1 tls /* 4 1.1 tls * Copyright (c) 1994 5 1.1 tls * The Regents of the University of California. All rights reserved. 6 1.1 tls * 7 1.1 tls * This code is derived from software contributed to Berkeley by 8 1.1 tls * Ralph Campbell. 9 1.1 tls * 10 1.1 tls * Redistribution and use in source and binary forms, with or without 11 1.1 tls * modification, are permitted provided that the following conditions 12 1.1 tls * are met: 13 1.1 tls * 1. Redistributions of source code must retain the above copyright 14 1.1 tls * notice, this list of conditions and the following disclaimer. 15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 tls * notice, this list of conditions and the following disclaimer in the 17 1.1 tls * documentation and/or other materials provided with the distribution. 18 1.7 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 tls * may be used to endorse or promote products derived from this software 20 1.1 tls * without specific prior written permission. 21 1.1 tls * 22 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 tls * SUCH DAMAGE. 33 1.1 tls */ 34 1.1 tls 35 1.4 lukem #include <sys/cdefs.h> 36 1.18 rillig /* @(#)stoc.c 8.1 (Berkeley) 7/24/94 */ 37 1.21 rillig __RCSID("$NetBSD: stoc.c,v 1.21 2022/05/29 00:12:11 rillig Exp $"); 38 1.1 tls 39 1.4 lukem #include <ctype.h> 40 1.4 lukem #include <stdlib.h> 41 1.6 matt #include <string.h> 42 1.1 tls #include "gomoku.h" 43 1.1 tls 44 1.19 rillig const char letters[] = "<ABCDEFGHJKLMNOPQRST>"; 45 1.1 tls 46 1.12 dholland 47 1.1 tls /* 48 1.1 tls * Turn the spot number form of a move into the character form. 49 1.1 tls */ 50 1.5 jsm const char * 51 1.21 rillig stoc(spot_index s) 52 1.1 tls { 53 1.1 tls static char buf[32]; 54 1.1 tls 55 1.20 rillig if (s == RESIGN) 56 1.20 rillig return "resign"; 57 1.20 rillig if (s == SAVE) 58 1.20 rillig return "save"; 59 1.16 rillig snprintf(buf, sizeof(buf), "%c%d", 60 1.16 rillig letters[s % (BSZ + 1)], s / (BSZ + 1)); 61 1.15 rillig return buf; 62 1.1 tls } 63 1.1 tls 64 1.1 tls /* 65 1.1 tls * Turn the character form of a move into the spot number form. 66 1.1 tls */ 67 1.21 rillig spot_index 68 1.10 dholland ctos(const char *mp) 69 1.1 tls { 70 1.1 tls 71 1.20 rillig if (strcmp(mp, "resign") == 0 || strcmp(mp, "quit") == 0) 72 1.20 rillig return RESIGN; 73 1.20 rillig if (strcmp(mp, "save") == 0) 74 1.20 rillig return SAVE; 75 1.20 rillig 76 1.20 rillig int letter = toupper((unsigned char)mp[0]); 77 1.20 rillig int x = 1; 78 1.20 rillig while (x <= BSZ && letters[x] != letter) 79 1.20 rillig x++; 80 1.20 rillig if (x > BSZ) 81 1.15 rillig return ILLEGAL; 82 1.20 rillig 83 1.20 rillig int y = atoi(&mp[1]); 84 1.20 rillig if (y < 1 || y > 19) 85 1.15 rillig return ILLEGAL; 86 1.1 tls 87 1.20 rillig return PT(x, y); 88 1.1 tls } 89