]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/stoc.c
636691515904fc2c2d1b201b2b28ec03754ac4e8
[bsdgames-darwin.git] / gomoku / stoc.c
1 /*
2 * Copyright (c) 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ralph Campbell.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)stoc.c 8.1 (Berkeley) 7/24/94";
39 #endif /* not lint */
40
41 #include "gomoku.h"
42 #include <ctype.h>
43
44 char *letters = "<ABCDEFGHJKLMNOPQRST>";
45
46 struct mvstr {
47 int m_code;
48 char *m_text;
49 };
50 static struct mvstr mv[] = {
51 RESIGN, "resign",
52 RESIGN, "quit",
53 SAVE, "save",
54 -1, 0
55 };
56
57 /*
58 * Turn the spot number form of a move into the character form.
59 */
60 char *
61 stoc(s)
62 int s;
63 {
64 static char buf[32];
65 register 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 sprintf(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 ctos(mp)
78 char *mp;
79 {
80 register 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(mp[0]))
86 return(ILLEGAL);
87 i = atoi(&mp[1]);
88 if (i < 1 || i > 19)
89 return(ILLEGAL);
90 return(PT(lton(mp[0]), i));
91 }
92
93 /*
94 * Turn a letter into a number.
95 */
96 lton(c)
97 int c;
98 {
99 register 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 }