-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.c
181 lines (134 loc) · 5.07 KB
/
play.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
181
/*
PENTOMINO
Copyright (C) 1995-2023 Andreas Huggel <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/************************************************/
/* play.c */
/* These are the functions to play the game, */
/* i.e., search the tree for solutions. */
/************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "pentomino.h"
#include "data.h"
#include "setup.h"
#include "print.h"
#include "play.h"
/* local variables */
static struct fieldT the_game;
/* local function declarations */
void go(struct tnode *);
/*****************************************************/
/* set all pieces from the tryfile on the gameboard, */
/* return the first piece which is not used yet */
/*****************************************************/
struct tnode *setup_gameboard(void)
{
struct tnode *curr_piece;
f_clear(&the_game);
for (curr_piece = gme.first_piece;
curr_piece != NULL && curr_piece->pos != NULL;
curr_piece = curr_piece->next)
{
f_set(&the_game, curr_piece->pos);
}
if (!gme.f_plausible_fct(the_game, NULL))
{
fprintf(stderr, "%s: %s: This try fails the plausibility check\n",
prg.progname, prg.tryfile);
exit(1);
}
return curr_piece;
}
/********************************************************/
/* check all combinations of the pieces on the board */
/* starting with start_piece and the (preset) gameboard */
/********************************************************/
void search_tree(struct tnode *start_piece)
{
while( start_piece->last_pos != find_pos(start_piece) )
{
go(start_piece);
}
}
/***********************************************************/
/* go does the trial and error job recursively. The number */
/* of branches at each node depends on the field, find_pos */
/* gets them all. */
/***********************************************************/
void go(struct tnode *piece)
{
f_set(&the_game, piece->pos);
if (piece->next == NULL)
{
/* This is a solution */
display_it();
}
else
{
while( find_pos(piece->next) )
{
go(piece->next);
}
}
f_rm(&the_game, piece->pos);
}
/***********************************************************/
/* find the next position of a piece, which fits and seems */
/* to make sense, set it at the piece and return it */
/***********************************************************/
struct pnode *find_pos(struct tnode *piece)
{
struct pnode *pos;
do
{
while ( (pos = t_next_pos(piece)) && !f_fits(the_game, pos) );
}
while ( pos && !gme.f_plausible_fct(the_game, pos) );
return pos;
}
/********************************************************************/
/* Plausibility check whether it makes sense to set piece in field. */
/* If pos is NULL, the field alone is checked. */
/* This is the function for equally sized pieces. */
/********************************************************************/
int f_plausible1(struct fieldT field, struct pnode *pos)
{
int i,j;
int ok=OK;
struct fieldT test_field=field;
f_set(&test_field, pos);
/* Doing this from the opposite end and skipping every other */
/* x-position(!) improves the overall performance by about 20% */
for (i=YDIM-1; i>=0 && ok; i-=1)
{
for (j=XDIM-2; j>=0 && ok; j-=2)
{
ok = (f_fill(&test_field, j, i) % gme.piece_sizes == 0);
}
}
return ok;
}
/********************************************************************/
/* Plausibility check whether it makes sense to set piece in field. */
/* If pos is NULL, the field alone is checked. */
/* This is the function for pieces of different sizes. */
/********************************************************************/
int f_plausible2(struct fieldT field, struct pnode *pos)
{
int ok=OK;
/* check to be implemented */
return ok;
}