1 /* $Header: /home/jcb/MahJong/newmj/RCS/tiles.h,v 12.0 2009/06/28 20:43:13 jcb Rel $ 2 * tiles.h 3 * Declares the datatype representing a tile, and the access functions. 4 * At present, the access functions are actually macros, but you're not 5 * supposed to know that. 6 */ 7 /****************** COPYRIGHT STATEMENT ********************** 8 * This file is Copyright (c) 2000 by J. C. Bradfield. * 9 * Distribution and use is governed by the LICENCE file that * 10 * accompanies this file. * 11 * The moral rights of the author are asserted. * 12 * * 13 ***************** DISCLAIMER OF WARRANTY ******************** 14 * This code is not warranted fit for any purpose. See the * 15 * LICENCE file for further information. * 16 * * 17 *************************************************************/ 18 19 #ifndef TILES_H_INCLUDED 20 #define TILES_H_INCLUDED 21 22 /* A tile is actually a (signed) short. */ 23 24 typedef short Tile; 25 26 /* The internal representation is viewed as a two digit decimal number 27 (for ease of debugging, ahem; but why not?). 28 The first digit is the suit, the second the value. Specials hacked in. 29 The code -1 is returned as an error. 30 The code 0 denotes a blank tile. (Distinguished from White Dragon!). 31 Otherwise: first digit 1,2,3 denotes characters,bamboo,circles, 32 and second digit 1-9 gives the value; 33 first digit 4 is winds, second digit 1,2,3,4 is E,S,W,N; 34 first digit 5 is dragons, second digit 1,2,3 is Red,White,Green; 35 first digit 6 is flowers, 1,2,3,4 is erm...whatever 36 first digit 7 is seasons, 1,2,3,4 is erm...what order are they? 37 (The fact that Winds, Dragons, etc are honorary suits is public 38 information; but the is_suit function refers to real suits. 39 Hm. This is maybe wrong.) 40 41 The following commitments are made about these datatype; external 42 code that relies on anything else is asking for trouble: 43 (1) Tile is a signed integer (of some length) datatype. 44 (2) the HiddenTile value is 0. 45 (3) the ErrorTile value is -1. 46 (4) within each suit, the tile values form consecutive integers; 47 in particular, 1 or 2 can be added to/subtracted from a suit tile 48 to obtain the tile 1 or 2 away. 49 (5) for wind and dragon tiles, the value_of is the appropriate 50 wind or dragon enum. 51 (7) the flowers and seasons appear in the order corresponding to the winds, 52 and the value_of a flower or season is the Wind corresponding to it. 53 */ 54 55 #define ErrorTile ((Tile) -1) 56 #define HiddenTile ((Tile) 0) 57 /* MaxTile is guaranteed to be bigger than any real tile value, and so 58 can be used to specify the sizes of arrays that want to be indexed by 59 tile values */ 60 #define MaxTile 80 61 62 /* these values are not guaranteed */ 63 typedef enum { 64 BambooSuit = 1, 65 CharacterSuit = 2, 66 CircleSuit = 3, 67 WindSuit = 4, 68 DragonSuit = 5, 69 FlowerSuit = 6, 70 SeasonSuit = 7 71 } TileSuit ; 72 73 /* These values are guaranteed */ 74 typedef enum { 75 UnknownWind = 0, /* for convenience ... */ 76 EastWind = 1, 77 SouthWind = 2, 78 WestWind = 3, 79 NorthWind = 4 80 } TileWind ; 81 82 /* These values are guaranteed */ 83 typedef enum { 84 RedDragon = 1, 85 WhiteDragon = 2, 86 GreenDragon = 3 87 } TileDragon ; 88 89 /* These values are guaranteed, by the requirement that 90 flowers/seasons match their winds 91 */ 92 93 typedef enum { 94 Plum = 1, 95 Orchid = 2, 96 Chrysanthemum = 3, 97 Bamboo = 4 98 } TileFlower; 99 100 typedef enum { 101 Spring = 1, 102 Summer = 2, 103 Autumn = 3, 104 Winter = 4 105 } TileSeason; 106 107 #define suit_of(t) ((TileSuit)((short) t/10)) 108 #define value_of(t) ((short) t % 10) 109 110 #define is_suit(t) (suit_of(t) >= 1 && suit_of(t) <= 3) 111 #define is_wind(t) (suit_of(t) == (short)WindSuit) 112 #define is_dragon(t) (suit_of(t) == (short)DragonSuit) 113 #define is_season(t) (suit_of(t) == (short)SeasonSuit) 114 #define is_flower(t) (suit_of(t) == (short)FlowerSuit) 115 116 #define is_normal(t) (is_suit(t)||is_wind(t)||is_dragon(t)) 117 #define is_special(t) (is_season(t)||is_flower(t)) 118 #define is_honour(t) (is_wind(t)||is_dragon(t)) 119 #define is_terminal(t) (is_suit(t) && (value_of(t) == 1 || value_of(t) == 9)) 120 #define is_simple(t) (is_suit(t) && (value_of(t) >= 2 || value_of(t) <= 8)) 121 #define is_minor is_simple 122 #define is_major(t) (is_honour(t) || is_terminal(t)) 123 /* this says whether a tile is green in the sense of Imperial Jade */ 124 int is_green(Tile t); 125 126 /* this is an array of thirteen tiles listing the 13 unique wonders */ 127 extern Tile thirteen_wonders[]; 128 129 /* this function is a convenience for iterating over all tiles. 130 The initial argument should be HiddenTile; if then fed its result, 131 it will generate all the tiles (in some order), finishing by 132 returning HiddenTile. 133 The second argument says whether to include the flowers and seasons. 134 */ 135 Tile tile_iterate(Tile t,int includespecials); 136 137 /* return the next wind in playing order */ 138 TileWind next_wind(TileWind w); 139 140 /* and the previous */ 141 TileWind prev_wind(TileWind w); 142 143 /* make_tile: takes a suit and a value, and returns the tile. 144 The value arg is an int, as it will frequently be calculated */ 145 146 Tile make_tile(TileSuit s,int v); 147 148 /* tile_name: takes a tile and returns a string describing it 149 The returned string is guaranteed to be in static storage and 150 not to change.*/ 151 152 const char *tile_name(Tile t); 153 154 /* tile_code: returns a two letter code for the tile. 155 Result is static and immutable. 156 */ 157 const char *tile_code(Tile t); 158 159 /* tile_decode: turn a two-letter code into a tile. */ 160 Tile tile_decode(const char *c); 161 162 /* random_tiles: the argument should point to an array of tiles 163 big enough to take all the required tiles. 164 This function will deal tiles into the array randomly. 165 The second argument says whether to include flowers and seasons. 166 */ 167 void random_tiles(Tile *tp,int includespecials); 168 169 #include "tiles-enums.h" 170 171 #endif /* TILES_H_INCLUDED */ 172