File: PyraminxF.c

package info (click to toggle)
xpuzzles 8.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,900 kB
  • sloc: ansic: 153,737; makefile: 6,780; sh: 937; perl: 80
file content (425 lines) | stat: -rw-r--r-- 10,248 bytes parent folder | download | duplicates (2)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * @(#)PyraminxF.c
 *
 * Copyright 2023  David A. Bagley, bagleyd AT verizon.net
 *
 * 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.
 */

/* Find moves file for Pyraminx */

#include "rngs.h"
#define JMP
#ifdef JMP
#include <setjmp.h> /* longjmp ... interrupt */
#endif
#include "PyraminxP.h"

static Boolean findingFlag = False;
#ifdef JMP
static Boolean abortFindingFlag = False;
static jmp_buf find_env;

static void
abortFinding(void)
{
	if (findingFlag)
		abortFindingFlag = True;
}

#ifdef WINVER
static Boolean
processMessage(UINT msg)
{
	switch (msg) {
	case WM_KEYDOWN:
	case WM_CLOSE:
	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
		abortFindng();
		return True;
	default:
		return False;
	}
}
#else
static void
processButton(void /*XButtonEvent *event*/)
{
	abortFinding();
}

static void
processVisibility(XVisibilityEvent *event)
{
	if (event->state != VisibilityUnobscured)
		abortFinding();
}

static void
getNextEvent(PyraminxWidget w, XEvent *event)
{
	if (!XCheckMaskEvent(XtDisplay(w), VisibilityChangeMask, event))
		(void) XNextEvent(XtDisplay(w), event);
}

static void
processEvent(XEvent *event)
{
	switch(event->type) {
	case KeyPress:
	case ButtonPress:
		processButton(/*&event->xbutton*/);
		break;
	case VisibilityNotify:
		processVisibility(&event->xvisibility);
		break;
	default:
		break;
	}
}

static void
processEvents(PyraminxWidget w)
{
	XEvent event;

	while (XPending(XtDisplay(w))) {
		getNextEvent(w, &event);
		processEvent(&event);
	}
}
#endif
#endif

static void
movePuzzlePiece(PyraminxWidget w, int face, int position,
	int direction, int style, int control)
{
#ifdef JMP
#ifdef WINVER
	MSG msg;

	if (PeekMessage(&msg, NULL, 0, 0, 0)) {
		if (!processMessage(msg.message)) {
			if (GetMessage(&msg, NULL, 0, 0))
				DispatchMessage(&msg);
		}
	}
#else
	processEvents(w);
#endif
	if (findingFlag && abortFindingFlag)
		longjmp(find_env, 1);
#endif
	movePuzzleDelay(w, face, position, direction, style, control);
}

static void
rotateEdge(PyraminxWidget w, int face, int position, int dir)
{
	movePuzzlePiece(w, face, position, dir, PERIOD2, FALSE);
}

static void
rotateFace(PyraminxWidget w, int face, int position, int dir)
{
	movePuzzlePiece(w, face, position, dir, PERIOD3, FALSE);
}

static int
checkIfEqual(PyraminxWidget w, int face, int corner1, int corner2) {
	return (w->pyraminx.facetLoc[face][corner1].face ==
		w->pyraminx.facetLoc[face][corner2].face
#if 0
		&&
		w->pyraminx.facetLoc[face][corner1].rotation ==
		w->pyraminx.facetLoc[face][corner2].rotation
#endif
		) ? 1: 0;
}

static Boolean
checkClosePeriod2x3(PyraminxWidget w, int *cornerCount, int *edgeCount, int *centerCount, int *special)
{
	int face, position, count, total = 22;
	*special = 0;
	if (*cornerCount != -1)
		*cornerCount = 0;
	if (*edgeCount != -1)
		*edgeCount = 0;
	if (*centerCount != -1)
		*centerCount = 0;
	for (face = 0; face < MAX_FACES; face++) {
		if (*special == 1)
			*special = 0;
		for (position = 0; position < 9; position++) {
			switch (position) {
			case 0:
			case 4:
			case 8:
				if (*cornerCount != -1)
					*cornerCount += checkIfEqual(w, face, 0, position);
				break;
			case 1:
			case 6:
				(*special)++;
			case 3:
				if (*edgeCount != -1)
					*edgeCount += checkIfEqual(w, face, 0, position);
				break;
			case 2:
			case 5:
			case 7:
				if (*centerCount != -1)
					*centerCount += checkIfEqual(w, face, 0, position);
				break;
			}
		}
	}
	if (*cornerCount == -1)
		*cornerCount = 4;
	else
		*cornerCount /= 3;
	if (*edgeCount == -1)
		*edgeCount = 6;
	else
		*edgeCount /= 2;
	if (*centerCount == -1)
		*centerCount = 12;
	count = total - *cornerCount - *edgeCount - *centerCount;
	/*(void) printf("count = %d %d %d %d\n",
		count, *cornerCount, *edgeCount, *centerCount);*/
	return count;
}

static void
findMovesPeriod2x3(PyraminxWidget w, FILE *fp)
{
	int maxChanged = 3, maxChangedLog = 3, maxMovesCheck = 50; /*change to suit */
	int i, face, position, dir, changedTotal = 22;
	int temp, lastMoveDir = -1;
	int cornerCount = 0, edgeCount = 0, centerCount = 0, specialCount = 0;
	PyraminxStack log = {NULL, NULL, NULL, 0};

	newMoves(&log);
	for (i = 0; i < maxMovesCheck; i++) {
		do {
			face = NRAND(MAX_FACES);
			temp = NRAND(3);
			dir = (temp == 1) ? TR : (temp == 0) ? TOP : RIGHT;
			temp = NRAND(3);
			position = (temp == 0) ? 2 : (temp == 1) ? 5 : 7;
		} while (lastMoveDir == dir);
		setMove(&log, dir, 2, False, face, position);
		rotateEdge(w, face, position, dir);
		centerCount = -1; /* do not care about centers as will solve later */
		if (lastMoveDir >= 0)
			changedTotal = checkClosePeriod2x3(w,
				&cornerCount, &edgeCount, &centerCount, &specialCount);
		lastMoveDir = dir;
		if (changedTotal == 0) {
			/*(void) printf("solved\n");*/
			break;
		} else if (changedTotal <= maxChanged) {
			/* weeding out moves already found */
			if (centerCount < 12 && centerCount != 10 && i >= 7) {
				(void) printf("center %d, moves %d\n", 12 - centerCount, i + 1);
				break;
			}
			if (edgeCount < 6 && edgeCount == 4 && i >= 5 && specialCount > 2) {
				(void) printf("edge %d, moves %d\n", 6 - edgeCount, i + 1);
				break;
			}
			printMoves(fp, &log);
			(void) fprintf(fp,
				"moves: %d, changed: total=%d corner=%d edge=%d center=%d\n",
				i + 1, changedTotal, cornerCount, edgeCount, centerCount);
			(void) printf("%d in %d moves!\n", changedTotal, i + 1);
			break;
		}
		if (changedTotal < maxChangedLog)
			(void) printf("%d\n", changedTotal);
	}
	flushMoves(w, &log, False);
	clearPieces(w);
}

static Boolean
checkClosePeriod3x4(PyraminxWidget w, int *cornerCount, int *edgeCount, int *centerCount, int *edgeLRCount, int *exactCenterCount)
{
	int face, position, count, total = 30;
	*cornerCount = 0;
	*edgeCount = 0;
	*centerCount = 0;
	*edgeLRCount = 0;
	*exactCenterCount = 0;
	for (face = 0; face < MAX_FACES; face++) {
		for (position = 0; position < 16; position++) {
			switch (position) {
			case 0:
			case 9:
			case 15:
				*cornerCount += checkIfEqual(w, face, 0, position);
				break;
			case 5:
			case 7:
			case 12:
				*edgeCount += checkIfEqual(w, face, 0, position);
				break;
			case 2:
			case 10:
			case 14:
				*centerCount += checkIfEqual(w, face, 0, position);
				break;
			case 1:
			case 3:
			case 4:
			case 8:
			case 11:
			case 13:
				*edgeLRCount += checkIfEqual(w, face, 0, position);
				break;
			case 6:
				*exactCenterCount += checkIfEqual(w, face, 0, position);
				break;
			}
		}
	}
	*cornerCount /= 3;
	*edgeCount /= 2;
	*centerCount /= 3;
	*edgeLRCount /= 2;
	count = total - *cornerCount - *edgeCount - *centerCount
		- *edgeLRCount - *exactCenterCount;
	/*(void) printf("count = %d %d %d %d %d %d\n",
		count, *cornerCount, *edgeCount, *centerCount,
		*edgeLRCount, *exactCenterCount);*/
	return count;
}

static void
findMovesPeriod3x4(PyraminxWidget w, FILE *fp)
{
	int maxChanged = 4, maxChangedLog = 5, maxMovesCheck = 23; /*change to suit */
	int i, face, position, dir, changedTotal = 30;
	int lastMoveFace = -1, lastMoveDir = -1;
	int cornerCount = 0, edgeCount = 0, centerCount = 0,
		edgeLRCount = 0, exactCenterCount = 0;
	PyraminxStack log = {NULL, NULL, NULL, 0};

	newMoves(&log);
	for (i = 0; i < maxMovesCheck; i++) {
		do {
#if 1
			face = NRAND(MAX_FACES);
			if (face != 1)
				face = 0;
			if (face == 1)
				dir = (NRAND(2) == 1) ? TR : BL;
			else
				dir = NRAND(6);
#endif
#if 0
			int temp = NRAND(4);
			face = 0;
			if (temp >= 2)
				dir = (temp == 2) ? TOP : BOTTOM;
			else
				dir = (temp == 2) ? LEFT : RIGHT;
#endif
#if 0
			face = 0;
			dir = NRAND(6);
#endif
			position = 6;
		} while ((face == 1 && lastMoveFace == face) ||
			(face == 0 && lastMoveFace == 0 && (lastMoveDir % 3) == (dir % 3)));
		setMove(&log, dir, 3, False, face, position);
		rotateFace(w, face, position, dir);
		if (lastMoveDir >= 0)
			changedTotal = checkClosePeriod3x4(w,
				&cornerCount, &edgeCount, &centerCount,
				&edgeLRCount, &exactCenterCount);
		lastMoveFace = face;
		lastMoveDir = dir;
		if (changedTotal == 0) {
			/*(void) printf("solved\n");*/
			break;
		} else if (changedTotal <= maxChanged) {
			if (exactCenterCount > 3)
				continue;
			printMoves(fp, &log);
			(void) fprintf(fp,
				"moves: %d, changed: total=%d corner=%d edge=%d center=%d edgeLR=%d exactCenter=%d\n",
				i + 1, changedTotal, cornerCount, edgeCount, centerCount,
				edgeLRCount, exactCenterCount);
			(void) printf("%d in %d moves!\n", changedTotal, i + 1);
			break;
		}
		if (changedTotal < maxChangedLog)
			(void) printf("%d\n", changedTotal);
	}
	flushMoves(w, &log, False);
	clearPieces(w);
}

/* This procedure coordinates the search process. */
void
findSomeMoves(PyraminxWidget w)
{
	FILE *fp;
	char * fileName = (char *) "pyraminx.txt";

	if (w->pyraminx.size == 3)
		fileName = (char *) "pyraminx3.txt";
	else if (w->pyraminx.size == 4)
		fileName = (char *) "pyraminx4.txt";
	if ((fp = fopen(fileName, "a")) == NULL) {
		(void) printf("problem opening %s\n", fileName);
		return;
	}
#if !defined( __MSDOS__ ) && !defined( __CYGWIN__ )
	/* This gets rid of the unwanted buffering in UNIX */
	(void) setlinebuf(fp);
#endif
	setPuzzle(w, ACTION_RESET);
	if (findingFlag) {
		fclose(fp);
		return;
	}
#ifdef JMP
	if (!setjmp(find_env))
#endif
	{
		findingFlag = True;
		if (checkSolved(w)) {
			for (;;) {
				if (w->pyraminx.size == 3)
					findMovesPeriod2x3(w, fp);
				if (w->pyraminx.size == 4)
					findMovesPeriod3x4(w, fp);
				usleep(1);
			}
		}
	}
#ifdef JMP
	abortFindingFlag = False;
#endif
	fclose(fp);
	findingFlag = False;
	w->pyraminx.cheat = True; /* Assume the worst. */
	setPuzzle(w, ACTION_COMPUTED);
}