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