1*5c1af24dSmestre /* $OpenBSD: stoc.c,v 1.11 2016/01/08 21:38:33 mestre Exp $ */
202e92172Sdownsj /*
302e92172Sdownsj * Copyright (c) 1994
402e92172Sdownsj * The Regents of the University of California. All rights reserved.
502e92172Sdownsj *
602e92172Sdownsj * This code is derived from software contributed to Berkeley by
702e92172Sdownsj * Ralph Campbell.
802e92172Sdownsj *
902e92172Sdownsj * Redistribution and use in source and binary forms, with or without
1002e92172Sdownsj * modification, are permitted provided that the following conditions
1102e92172Sdownsj * are met:
1202e92172Sdownsj * 1. Redistributions of source code must retain the above copyright
1302e92172Sdownsj * notice, this list of conditions and the following disclaimer.
1402e92172Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
1502e92172Sdownsj * notice, this list of conditions and the following disclaimer in the
1602e92172Sdownsj * documentation and/or other materials provided with the distribution.
177a09557bSmillert * 3. Neither the name of the University nor the names of its contributors
1802e92172Sdownsj * may be used to endorse or promote products derived from this software
1902e92172Sdownsj * without specific prior written permission.
2002e92172Sdownsj *
2102e92172Sdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2202e92172Sdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2302e92172Sdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2402e92172Sdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2502e92172Sdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2602e92172Sdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2702e92172Sdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2802e92172Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2902e92172Sdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3002e92172Sdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3102e92172Sdownsj * SUCH DAMAGE.
3202e92172Sdownsj */
3302e92172Sdownsj
342010f3c8Smestre #include <ctype.h>
3540aa9d5fStholo #include <stdlib.h>
3670ef01f6Sdavid #include <string.h>
372010f3c8Smestre
382010f3c8Smestre #include "gomoku.h"
3902e92172Sdownsj
4002e92172Sdownsj char *letters = "<ABCDEFGHJKLMNOPQRST>";
4102e92172Sdownsj
4202e92172Sdownsj struct mvstr {
4302e92172Sdownsj int m_code;
4402e92172Sdownsj char *m_text;
4502e92172Sdownsj };
4602e92172Sdownsj static struct mvstr mv[] = {
4740aa9d5fStholo {RESIGN, "resign" },
4840aa9d5fStholo {RESIGN, "quit" },
4940aa9d5fStholo {SAVE, "save" },
5040aa9d5fStholo {-1, 0}
5102e92172Sdownsj };
5202e92172Sdownsj
5302e92172Sdownsj /*
5402e92172Sdownsj * Turn the spot number form of a move into the character form.
5502e92172Sdownsj */
5602e92172Sdownsj char *
stoc(int s)57*5c1af24dSmestre stoc(int s)
5802e92172Sdownsj {
5902e92172Sdownsj static char buf[32];
6097419aa0Spjanzen int i;
6102e92172Sdownsj
6202e92172Sdownsj for (i = 0; mv[i].m_code >= 0; i++)
6302e92172Sdownsj if (s == mv[i].m_code)
6402e92172Sdownsj return(mv[i].m_text);
6542ceebb3Sderaadt snprintf(buf, sizeof buf, "%c%d", letters[s % BSZ1], s / BSZ1);
6602e92172Sdownsj return(buf);
6702e92172Sdownsj }
6802e92172Sdownsj
6902e92172Sdownsj /*
7002e92172Sdownsj * Turn the character form of a move into the spot number form.
7102e92172Sdownsj */
7240aa9d5fStholo int
ctos(char * mp)73*5c1af24dSmestre ctos(char *mp)
7402e92172Sdownsj {
7597419aa0Spjanzen int i;
7602e92172Sdownsj
7702e92172Sdownsj for (i = 0; mv[i].m_code >= 0; i++)
7802e92172Sdownsj if (strcmp(mp, mv[i].m_text) == 0)
7902e92172Sdownsj return(mv[i].m_code);
8002fcf05aSmmcc if (!isalpha((unsigned char)mp[0]))
8102e92172Sdownsj return(ILLEGAL);
8202e92172Sdownsj i = atoi(&mp[1]);
8302e92172Sdownsj if (i < 1 || i > 19)
8402e92172Sdownsj return(ILLEGAL);
8502e92172Sdownsj return(PT(lton(mp[0]), i));
8602e92172Sdownsj }
8702e92172Sdownsj
8802e92172Sdownsj /*
8902e92172Sdownsj * Turn a letter into a number.
9002e92172Sdownsj */
9140aa9d5fStholo int
lton(int c)92*5c1af24dSmestre lton(int c)
9302e92172Sdownsj {
9497419aa0Spjanzen int i;
9502e92172Sdownsj
9602e92172Sdownsj if (islower(c))
9702e92172Sdownsj c = toupper(c);
9802e92172Sdownsj for (i = 1; i <= BSZ && letters[i] != c; i++)
9902e92172Sdownsj ;
10002e92172Sdownsj return(i);
10102e92172Sdownsj }
102