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