]> git.cworth.org Git - ttt/blob - kub/kub.c
955817c473a256d6790d2c978e5204fe9ae74b59
[ttt] / kub / kub.c
1 #include <stdio.h>\r
2 #include <stdlib.h>\r
3 #include <time.h>\r
4
5 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
6
7 typedef enum {BLACK, BLUE, RED, YELLOW} color_t;
8
9 typedef struct card {
10     color_t color;
11     int number;
12 } card_t;
13
14 #define DECK_MAX_CARDS 104
15
16 typedef struct deck {
17     card_t cards[DECK_MAX_CARDS];
18     int num_cards;
19 } deck_t;
20
21 #define CARD_GROUP_MAX_CARDS DECK_MAX_CARDS
22
23 typedef struct card_group {
24     card_t cards[CARD_GROUP_MAX_CARDS];
25     int num_cards;
26 } card_group_t;
27
28 #define BOARD_MAX_CARD_GROUPS (DECK_MAX_CARDS / 3)
29
30 typedef struct board {
31     card_group_t groups[BOARD_MAX_CARD_GROUPS];
32     int num_groups;
33 } board_t;
34
35 typedef struct player {
36     card_group_t hand;
37 } player_t;
38
39 #define GAME_MAX_PLAYERS 4
40
41 typedef struct game {
42     player_t players[GAME_MAX_PLAYERS];
43     int num_players;
44     board_t board;
45     deck_t deck;
46 } game_t;
47
48 static void card_print(card_t card)
49 {
50     printf("%6s %2d\n", colors[card.color], card.number + 1);
51 }
52
53 static void card_group_init(card_group_t *card_group)
54 {
55     card_group->num_cards = 0;
56 }
57
58 static void board_init(board_t *board)
59 {
60     int i;
61     board->num_groups = 0;
62     
63     for (i = 0; i <= BOARD_MAX_CARD_GROUPS; ++i) 
64     {
65         card_group_init(&board->groups[i]);
66     }
67 }
68
69 static void player_init(player_t *player)
70 {
71     card_group_init(&player->hand);
72 }
73
74 static int card_group_is_run(card_group_t *card_group)
75 {
76     int i;
77     int lowest = 14, highest = 0;
78     if (card_group->num_cards > 13 || card_group->num_cards < 3)
79     {
80         return 0;
81     }
82     for (i = 0; i < card_group->num_cards - 1; ++i)
83     {
84         if (card_group->cards[i].color != card_group->cards[i + 1].color)
85         {
86             return 0;
87         }
88         if (card_group->cards[i].number > highest)
89         {
90             highest = card_group->cards[i].number;
91         }
92         if (card_group->cards[i].number < lowest)
93         {
94             lowest = card_group->cards[i].number;
95         }
96     }
97     if (highest - lowest = card_group->num_cards - 1)
98     {
99         return 1;
100     }
101 }
102
103
104 static int card_group_is_set(card_group_t *card_group)
105 {
106     int i;
107     color_t seen_color[card_group->num_cards];
108     
109     if (card_group->num_cards > 4 || card_group->num_cards < 3)
110     {
111         return 0;
112     }
113     for (i = 0; i < card_group->num_cards - 1; ++i) 
114     {
115         if (card_group->cards[i].number != card_group->cards[i + 1].number)
116         {
117             return 0;
118         }
119     }
120     seen_color[i] = card_group->cards[i].color;
121     for (i = 0; i < card_group->num_cards; ++i)
122     {
123         seen_color[card_group->cards[i].color]++;
124         if (seen_color[card_group->cards[i].color] > 1)
125         {
126             return 0;
127         }
128     }
129     return 1;
130 }
131
132 #if 0
133 static void deck_deal(deck_t *deck, game_t *game)
134 {
135     card_t temp;
136     int rand_card;
137     int i, j;
138     for (i = 0; i < PLAYERS; ++i)
139     {
140         for (j = 0; j < 13; ++j)
141         {
142             rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
143             temp = deck->cards[rand_card];
144             deck->cards[rand_card] = deck->cards[last];
145             game->players[i]->hand->cards[j] = temp;
146         }
147     }
148 }
149 #endif
150
151 static void deck_init(deck_t *deck)
152 {
153     int h, i, j;  
154     deck->num_cards = 0;
155     for (h = 0; h <= 1; ++h)
156     {
157         for (i = 0; i <= 3; ++i) 
158         {
159             for (j = 0; j <= 12; ++j) 
160             {
161                 deck->cards[j + (i * 13) + (h * 52)].color = i;
162                 deck->cards[j + (i * 13) + (h * 52)].number = j;
163                 deck->num_cards += 1;
164                 printf ("There are %d tiles in the deck\n", deck->num_cards);
165             }
166         }
167     }
168
169
170 static void deck_shuffle(deck_t *deck)
171 {
172     card_t temp;
173     int rand_card;
174     int last;
175     for (last = deck->num_cards; last > 0; --last)
176     {
177         rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
178         temp = deck->cards[rand_card];
179         deck->cards[rand_card] = deck->cards[last];
180         deck->cards[last] = temp;
181     }
182 }
183
184 static void deck_print(deck_t *deck)
185 {
186     int h, i, j;  
187     for (h = 0; h <= 1; ++h)
188     {
189         for (i = 0; i <= 3; ++i)
190         {
191             for (j = 0; j <= 12; ++j)
192             {
193                 card_print(deck->cards[j + (i * 13) + (h * 52)]);
194             }
195         }
196     }
197     printf ("There are %d tiles in the deck\n" , deck->num_cards);
198 }
199
200 static void game_init(game_t *game)
201 {
202     int i;
203     game->num_players = 0;
204     
205     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
206     {
207         player_init(&game->players[i]);
208         game->num_players += 1;
209     }
210     
211     board_init(&game->board);
212     deck_init(&game->deck);
213     deck_shuffle(&game->deck);
214 }
215
216 int main(void)
217 {
218     game_t game;
219     
220     srand(time(NULL));
221     
222     game_init(&game);
223     deck_print(&game.deck);
224     
225     return 0;
226     
227 }