File: TrianglesU.c

package info (click to toggle)
xpuzzles 7.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,208 kB
  • sloc: ansic: 94,274; makefile: 7,477; sh: 3,221
file content (275 lines) | stat: -rw-r--r-- 6,459 bytes parent folder | download | duplicates (6)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * @(#)TrianglesU.c
 *
 * Copyright 1994 - 2008  David A. Bagley, bagleyd@tux.org
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * This program is distributed in the hope that it will be "useful",
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

/* Undo algorithm for Triangles */

#include "TrianglesP.h"

static int startSpace[MAX_ORIENT];
static int startRow[MAX_ORIENT][ROW_TYPES];
int *startPosition = NULL;

static void
newStack(TrianglesStack *s)
{
	if (s->lastMove != NULL || s->firstMove != NULL)
		return;
	if (!(s->lastMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	if (!(s->firstMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->firstMove->previous = s->lastMove->next = NULL;
	s->firstMove->next = s->lastMove;
	s->lastMove->previous = s->firstMove;
	s->count = 0;
}

static void
pushStack(TrianglesStack *s, MoveRecord **move)
{
	if (!(s->currMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->lastMove->previous->next = s->currMove;
	s->currMove->previous = s->lastMove->previous;
	s->currMove->next = s->lastMove;
	s->lastMove->previous = s->currMove;
	*move = &(s->currMove->move);
	s->count++;
}

static void
popStack(TrianglesStack *s)
{
	s->currMove = s->lastMove->previous;
	s->lastMove->previous->previous->next = s->lastMove;
	s->lastMove->previous = s->lastMove->previous->previous;
	free(s->currMove);
	s->count--;
}


static MoveRecord *
topStack(TrianglesStack *s)
{
	return &(s->lastMove->previous->move);
}

static int
emptyStack(TrianglesStack *s)
{
	return (s->lastMove->previous == s->firstMove);
}

static void
flushStack(TrianglesStack *s)
{
	while (s->lastMove->previous != s->firstMove) {
		s->currMove = s->lastMove->previous;
		s->lastMove->previous->previous->next = s->lastMove;
		s->lastMove->previous = s->lastMove->previous->previous;
		free(s->currMove);
	}
	s->count = 0;
}

static void
deleteStack(TrianglesStack *s)
{
	flushStack(s);
	if (s->firstMove) {
		free(s->firstMove);
		s->firstMove = NULL;
	}
	if (s->lastMove) {
		free(s->lastMove);
		s->lastMove = NULL;
	}
}

/**********************************/

void
newMoves(TrianglesStack *s)
{
	newStack(s);
}

void
deleteMoves(TrianglesStack *s)
{
	deleteStack(s);
}

static void
writeMove(MoveRecord *move, int direction)
{
#if 0
	move->direction = direction;
#endif
	move->packed = direction & 0xF;
}

static void
readMove(MoveRecord *move, int *direction)
{
#if 0
	*direction = move->direction;
#endif
	*direction = (int) (move->packed & 0xF);
}

void
setMove(TrianglesStack *s, int direction)
{
	MoveRecord *move;

	pushStack(s, &move);
	writeMove(move, direction);
}

void
getMove(TrianglesStack *s, int *direction)
{
	readMove(topStack(s), direction);
	popStack(s);
}

int
madeMoves(TrianglesStack *s)
{
	return !emptyStack(s);
}

void
flushMoves(TrianglesWidget w, TrianglesStack *s, Boolean undo)
{
	int i;

	flushStack(s);
	if (undo) {
		startSpace[UP] = w->triangles.spacePosition[UP];
		startSpace[DOWN] = w->triangles.spacePosition[DOWN];
		startRow[UP][TRBL] = w->triangles.spaceRow[UP][TRBL];
		startRow[UP][TLBR] = w->triangles.spaceRow[UP][TLBR];
		startRow[UP][ROW] = w->triangles.spaceRow[UP][ROW];
		startRow[DOWN][TRBL] = w->triangles.spaceRow[DOWN][TRBL];
		startRow[DOWN][TLBR] = w->triangles.spaceRow[DOWN][TLBR];
		startRow[DOWN][ROW] = w->triangles.spaceRow[DOWN][ROW];
		for (i = 0; i < w->triangles.sizeSize; i++)
			startPosition[i] = w->triangles.tileOfPosition[i];
	}
}

int
numMoves(TrianglesStack *s)
{
	return s->count;
}

void
scanMoves(FILE *fp, TrianglesWidget w, int moves)
{
	int direction, l, c;

	for (l = 0; l < moves; l++) {
		while ((c = getc(fp)) != EOF && c != SYMBOL);
		(void) fscanf(fp, "%d", &direction);
		if (!movePuzzleDir(w, direction, INSTANT))
			(void) fprintf(stderr,
				"%d move in direction %d, can not be made.",
				l, direction);
	}
}

void
printMoves(FILE *fp, TrianglesStack *s)
{
	int direction, counter = 0;

	s->currMove = s->firstMove->next;
	(void) fprintf(fp, "moves\tdirection\n");
	while (s->currMove != s->lastMove) {
		readMove(&(s->currMove->move), &direction);
		(void) fprintf(fp, "%d%c\t%d\n", ++counter, SYMBOL, direction);
		s->currMove = s->currMove->next;
	}
}

void
scanStartPosition(FILE *fp, TrianglesWidget w)
{
	int i, num, c;

	while ((c = getc(fp)) != EOF && c != SYMBOL);
	for (i = 0; i < w->triangles.sizeSize; i++) {
		(void) fscanf(fp, "%d ", &num);
		startPosition[i] = num;
		if (!num)
			startSpace[UP] = i;
		else if (num == -1)
			startSpace[DOWN] = i;
	}
	for (i = DOWN; i <= UP; i++) {
		startRow[i][ROW] = toRow(startSpace[i]);
		startRow[i][TRBL] = toTrBl(startSpace[i], startRow[i][ROW]);
		startRow[i][TLBR] = toTlBr(startSpace[i], startRow[i][ROW]);
	}
}

void
printStartPosition(FILE *fp, TrianglesWidget w)
{
	int r = 0, p, space, temp;

	(void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
	for (p = 0; p < w->triangles.sizeSize; p++) {
		if (p == r * r)
			for (space = 0; space < w->triangles.size - r - 1; space++)
				(void) fprintf(fp, "    ");
		(void) fprintf(fp, "%3d ", startPosition[p]);
		temp = (r + 1) * (r + 1) - 1;
		if (p == temp) {
			(void) fprintf(fp, "\n");
			r++;
		}
	}
	(void) fprintf(fp, "\n");
}

void
setStartPosition(TrianglesWidget w)
{
	int i;

	w->triangles.spacePosition[UP] = startSpace[UP];
	w->triangles.spacePosition[DOWN] = startSpace[DOWN];
	w->triangles.spaceRow[UP][TRBL] = startRow[UP][TRBL];
	w->triangles.spaceRow[UP][TLBR] = startRow[UP][TLBR];
	w->triangles.spaceRow[UP][ROW] = startRow[UP][ROW];
	w->triangles.spaceRow[DOWN][TRBL] = startRow[DOWN][TRBL];
	w->triangles.spaceRow[DOWN][TLBR] = startRow[DOWN][TLBR];
	w->triangles.spaceRow[DOWN][ROW] = startRow[DOWN][ROW];
	for (i = 0; i < w->triangles.sizeSize; i++)
		w->triangles.tileOfPosition[i] = startPosition[i];
	drawAllTiles(w);
}