-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.c
180 lines (156 loc) · 3.85 KB
/
generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// -------------------------------------------------------------
//
// This program creates instances of freecell solitaire problems
// Instances are created randomly, however a check is performed
// after creation in order to ensure that the instance is solvable.
// Non-solvable instances are ignored.
// Instances are named from <prefix><id1>.txt to <prefix><id2>.txt .
//
// Syntax:
// generator <prefix> <id1> <id2>
//
// Author: Ioannis Refanidis, March 2010
//
// --------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 13
#define HEARTS 0
#define DIAMONDS 1
#define SPADES 2
#define CLUBS 3
struct card{
int suit;
int value;
};
struct card deck[4*N];
void initialize_deck()
{
int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < N; j++)
{
deck[i*N+j].suit = i;
deck[i*N+j].value = j;
}
}
// This function creates randomly a new puzzle.
void shuffle_deck(int K)
{
long i;
int x, y;
for(i = 0; i < K; i++)
{
x = (double) rand() / (RAND_MAX + 1) * (4*N);
y = (double) rand() / (RAND_MAX + 1) * (4*N);
{
int ts=deck[x].suit;
int tv=deck[x].value;
deck[x].suit=deck[y].suit;
deck[x].value=deck[y].value;
deck[y].suit=ts;
deck[y].value=tv;
}
}
}
// Function that displays a message in case of wrong input parameters.
void syntax_message()
{
printf("Use syntax:\n\n");
printf("\tgenerator <prefix> <id1> <id2>\n\n");
printf("where:\n ");
printf("\t<prefix> = the prefix of the filename of the instances to be generated\n");
printf("\t<id1> = a number indicating the suffix of the first instance.\n");
printf("\t<id2> = a number indicating the suffix of the last instance.\n\n");
printf("e.g. the call \n\n\tgenerator test 1 10\n\ngenerates 10 instances with names ranging from test1.txt to test10.txt.\n");
printf("Constraints: id1 > 0, id2 > 0, id1 <= id2.\n");
}
void number2string(int n,char* s)
{
int i=0,j;
while (n>0)
{
// shift one position to the right
for(j=i-1;j>=0;j--)
s[j+1]=s[j];
s[0]=(char) (n % 10)+'0';
n=n / 10;
i++;
}
s[i]='\0';
}
// This function writes the puzzle to a file
void write_to_file(int id, char *filename)
{
int i;
char *temp=(char*) malloc(10*sizeof(char));
FILE *fout;
char s[255];
strcpy(s,filename);
number2string(id,temp);
strcat(s,temp);
strcat(s,".txt");
fout=fopen(s,"w");
for(i = 0; i < 4*N; i++)
{
switch(deck[i].suit)
{
case HEARTS: fprintf(fout,"H");
break;
case DIAMONDS: fprintf(fout,"D");
break;
case CLUBS: fprintf(fout,"C");
break;
case SPADES: fprintf(fout,"S");
break;
}
fprintf(fout,"%d",deck[i].value);
if ( (N%2) == 0)
{
if (i == N/2-1 || i==N-1 || i==3*N/2-1 || i==2*N-1
|| i==5*N/2-1 || i==3*N-1 || i==7*N/2-1 || i==4*N-1)
fprintf(fout, "\n");
else
fprintf(fout, " ");
}
else
{
if (i == N/2 || i==N || i==3*N/2+1 || i==2*N+1
|| i==5*N/2+1 || i==3*N || i==7*N/2 || i==4*N)
fprintf(fout, "\n");
else
fprintf(fout, " ");
}
}
fclose(fout);
}
int main(int argc, char** argv)
{
long id1, id2, i;
char* endPtr;
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
srand( (unsigned) time ( NULL ) );
if (argc!=4)
{
printf("Wrong number of arguments. Use correct syntax:\n");
syntax_message();
return -1;
}
id1 = strtol(argv[2], &endPtr, 10);
id2 = strtol(argv[3], &endPtr, 10);
if (id1 <= 0 || id2 <= 0 || id1 > id2)
{
syntax_message();
return -1;
}
for(i = id1; i <= id2; i++)
{
initialize_deck();
shuffle_deck(400 * N);
write_to_file(i, argv[1]);
}
return 0;
}