stoc.c 3.0 KB

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