]> git.cworth.org Git - wordgame/blob - bag.c
Increase the window size a bit
[wordgame] / bag.c
1 /*
2  * Copyright © 2006 Carl Worth
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
17  */
18
19 #include "word-game.h"
20
21 #include <string.h>
22
23 static char tile_distribution[BAG_SIZE+1] =
24 "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLL"
25 "MMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ??";
26
27 static int
28 rand_within (int num_values)
29 {
30     return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
31 }
32
33 static void
34 shuffle (char *array, int length)
35 {
36     int i, r, tmp;
37
38     for (i = 0; i < length; i++)
39     {
40         r = i + rand_within (length - i);
41         tmp = array[i];
42         array[i] = array[r];
43         array[r] = tmp;
44     }
45 }
46
47 void
48 bag_init (bag_t *bag)
49 {
50     memcpy (bag->tiles, tile_distribution, BAG_SIZE);
51 }
52
53 void
54 bag_shuffle (bag_t *bag)
55 {
56     shuffle (bag->tiles, BAG_SIZE);
57 }
58