-
Notifications
You must be signed in to change notification settings - Fork 0
/
takudoGeneretor.c
101 lines (99 loc) · 2.66 KB
/
takudoGeneretor.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
#include <stdio.h>
#include <stdlib.h>
#include "takudo.h"
#include "takudoGeneretor.h"
#include "cnf.h"
#include <time.h>
char *generator(int jieshu)
{
char *address = "generator.txt";
int k = jieshu * jieshu;
char test[k];
srand(time(NULL));
while(1)
{
initialRand(jieshu,test,address);
char *filef = convertToCnf(address);
struct result myResult;
struct clauseSet myClauseSet = readCnf(filef);
myResult = DPLL(myClauseSet);
if(myResult.state == 1) break;
}
judgeGuess(jieshu,test,address);
return address;
}
void judgeGuess(int jieshu,char test[],char *address)
{
int traverseAllRes = jieshu * jieshu;
int i;
for(i = 0; i < traverseAllRes;i++)
{
if(test[i] == '.')
{
FILE *fp = fopen(address,"w");
int flag = 0;
test[i] = '0';
fprintf(fp,"%s",test);
fclose(fp);
struct result myResult;
char *filef = convertToCnf(address);
struct clauseSet myClauseSet = readCnf(filef);
myResult = DPLL(myClauseSet);
if(myResult.state == 1) flag++;
if(flag == 0)
{
fp = fopen(address,"w");
test[i] = '.';
fprintf(fp,"%s",test);
fclose(fp);
continue;
}
else
{
fp = fopen(address,"w");
test[i] = '1';
fprintf(fp,"%s",test);
fclose(fp);
struct result myResult;
char *filef = convertToCnf(address);
struct clauseSet myClauseSet = readCnf(filef);
myResult = DPLL(myClauseSet);
if(myResult.state == 1) flag++;
}
if(flag != 2)
{
fp = fopen(address,"w");
test[i] = '.';
fprintf(fp,"%s",test);
fclose(fp);
continue;
}
else
{
fp = fopen(address,"w");
int r = rand() % 2;
test[i] = r + '0';
fprintf(fp,"%s",test);
fclose(fp);
}
}
else continue;
}
}
void initialRand(int jieshu,char test[],char *address)
{
FILE *fp = fopen(address,"w");
int i;
int k = jieshu * jieshu;
for(i = 0;i < k;i++) test[i] = '.';
test[k] = '\0';
for(i = 0;i < jieshu;i++)
{
int random = rand() % k;
int ass = rand() % 2;
if(ass == 0) test[random] = '0';
else test[random] = '1';
}
fprintf(fp,"%s",test);
fclose(fp);
}