cards.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* $NetBSD: cards.c,v 1.7 2003/08/07 09:37:08 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: cards.c,v 1.7 2003/08/07 09:37:08 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <curses.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42. #include "deck.h"
  43. #include "cribbage.h"
  44. /*
  45. * Initialize a deck of cards to contain one of each type.
  46. */
  47. void
  48. makedeck(d)
  49. CARD d[];
  50. {
  51. int i, j, k;
  52. i = time(NULL);
  53. i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
  54. srand(i);
  55. k = 0;
  56. for (i = 0; i < RANKS; i++)
  57. for (j = 0; j < SUITS; j++) {
  58. d[k].suit = j;
  59. d[k++].rank = i;
  60. }
  61. }
  62. /*
  63. * Given a deck of cards, shuffle it -- i.e. randomize it
  64. * see Knuth, vol. 2, page 125.
  65. */
  66. void
  67. shuffle(d)
  68. CARD d[];
  69. {
  70. int j, k;
  71. CARD c;
  72. for (j = CARDS; j > 0; --j) {
  73. k = (rand() >> 4) % j; /* random 0 <= k < j */
  74. c = d[j - 1]; /* exchange (j - 1) and k */
  75. d[j - 1] = d[k];
  76. d[k] = c;
  77. }
  78. }
  79. /*
  80. * return true if the two cards are equal...
  81. */
  82. int
  83. eq(a, b)
  84. CARD a, b;
  85. {
  86. return ((a.rank == b.rank) && (a.suit == b.suit));
  87. }
  88. /*
  89. * is_one returns TRUE if a is in the set of cards b
  90. */
  91. int
  92. is_one(a, b, n)
  93. CARD a;
  94. const CARD b[];
  95. int n;
  96. {
  97. int i;
  98. for (i = 0; i < n; i++)
  99. if (eq(a, b[i]))
  100. return (TRUE);
  101. return (FALSE);
  102. }
  103. /*
  104. * remove the card a from the deck d of n cards
  105. */
  106. void
  107. cremove(a, d, n)
  108. CARD a, d[];
  109. int n;
  110. {
  111. int i, j;
  112. for (i = j = 0; i < n; i++)
  113. if (!eq(a, d[i]))
  114. d[j++] = d[i];
  115. if (j < n)
  116. d[j].suit = d[j].rank = EMPTY;
  117. }
  118. /*
  119. * sorthand:
  120. * Sort a hand of n cards
  121. */
  122. void
  123. sorthand(h, n)
  124. CARD h[];
  125. int n;
  126. {
  127. CARD *cp, *endp;
  128. CARD c;
  129. for (endp = &h[n]; h < endp - 1; h++)
  130. for (cp = h + 1; cp < endp; cp++)
  131. if ((cp->rank < h->rank) ||
  132. (cp->rank == h->rank && cp->suit < h->suit)) {
  133. c = *h;
  134. *h = *cp;
  135. *cp = c;
  136. }
  137. }