shapes.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* $NetBSD: shapes.c,v 1.6 2003/08/07 09:37:48 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Chris Torek and Darren F. Provine.
  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. * @(#)shapes.c 8.1 (Berkeley) 5/31/93
  34. */
  35. /*
  36. * Tetris shapes and related routines.
  37. *
  38. * Note that the first 7 are `well known'.
  39. */
  40. #include <sys/cdefs.h>
  41. #include "tetris.h"
  42. #define TL -B_COLS-1 /* top left */
  43. #define TC -B_COLS /* top center */
  44. #define TR -B_COLS+1 /* top right */
  45. #define ML -1 /* middle left */
  46. #define MR 1 /* middle right */
  47. #define BL B_COLS-1 /* bottom left */
  48. #define BC B_COLS /* bottom center */
  49. #define BR B_COLS+1 /* bottom right */
  50. const struct shape shapes[] = {
  51. /* 0*/ { 7, { TL, TC, MR, } },
  52. /* 1*/ { 8, { TC, TR, ML, } },
  53. /* 2*/ { 9, { ML, MR, BC, } },
  54. /* 3*/ { 3, { TL, TC, ML, } },
  55. /* 4*/ { 12, { ML, BL, MR, } },
  56. /* 5*/ { 15, { ML, BR, MR, } },
  57. /* 6*/ { 18, { ML, MR, 2 } }, /* sticks out */
  58. /* 7*/ { 0, { TC, ML, BL, } },
  59. /* 8*/ { 1, { TC, MR, BR, } },
  60. /* 9*/ { 10, { TC, MR, BC, } },
  61. /*10*/ { 11, { TC, ML, MR, } },
  62. /*11*/ { 2, { TC, ML, BC, } },
  63. /*12*/ { 13, { TC, BC, BR, } },
  64. /*13*/ { 14, { TR, ML, MR, } },
  65. /*14*/ { 4, { TL, TC, BC, } },
  66. /*15*/ { 16, { TR, TC, BC, } },
  67. /*16*/ { 17, { TL, MR, ML, } },
  68. /*17*/ { 5, { TC, BC, BL, } },
  69. /*18*/ { 6, { TC, BC, 2*B_COLS } } /* sticks out */
  70. };
  71. /*
  72. * Return true iff the given shape fits in the given position,
  73. * taking the current board into account.
  74. */
  75. int
  76. fits_in(shape, pos)
  77. const struct shape *shape;
  78. int pos;
  79. {
  80. int *o = shape->off;
  81. if (board[pos] || board[pos + *o++] || board[pos + *o++] ||
  82. board[pos + *o])
  83. return 0;
  84. return 1;
  85. }
  86. /*
  87. * Write the given shape into the current board, turning it on
  88. * if `onoff' is 1, and off if `onoff' is 0.
  89. */
  90. void
  91. place(shape, pos, onoff)
  92. const struct shape *shape;
  93. int pos, onoff;
  94. {
  95. int *o = shape->off;
  96. board[pos] = onoff;
  97. board[pos + *o++] = onoff;
  98. board[pos + *o++] = onoff;
  99. board[pos + *o] = onoff;
  100. }