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
|
/*
* @(#)PyraminxU.c
*
* Copyright 1994 - 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.
*/
/* Undo algorithm for Pyraminx */
#include "PyraminxP.h"
PyraminxLoc *startLoc[MAX_FACES] =
{
NULL, NULL, NULL, NULL
};
static void
newStack(PyraminxStack *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(PyraminxStack *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(PyraminxStack *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(PyraminxStack *s)
{
return &(s->lastMove->previous->move);
}
static int
emptyStack(PyraminxStack *s)
{
return (s->lastMove->previous == s->firstMove);
}
static void
flushStack(PyraminxStack *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(PyraminxStack *s)
{
flushStack(s);
if (s->firstMove) {
free(s->firstMove);
s->firstMove = NULL;
}
if (s->lastMove) {
free(s->lastMove);
s->lastMove = NULL;
}
}
/**********************************/
void
newMoves(PyraminxStack *s)
{
newStack(s);
}
void
deleteMoves(PyraminxStack *s)
{
deleteStack(s);
}
static void
writeMove(moveRecord *move, int direction, int style, int control,
int face, int position)
{
#if 0
move->direction = direction;
move->style = style;
move->control = control;
move->face = face;
#endif
move->packed = (unsigned short int) (((control & 0xF) << 12) +
((style & 0xF) << 8) + ((direction & 0xF) << 4) +
(face & 0xF));
move->position = position;
}
static void
readMove(moveRecord *move, int *direction, int *style, int *control,
int *face, int *position)
{
#if 0
*direction = move->direction;
*style = move->style;
*control = move->control;
*face = move->face;
#endif
*face = (int) (move->packed & 0xF);
*direction = (int) ((move->packed >> 4) & 0xF);
*style = (int) ((move->packed >> 8) & 0xF);
*control = (int) ((move->packed >> 12) & 0xF);
*position = move->position;
}
void
setMove(PyraminxStack *s, int direction, int style, int control,
int face, int position)
{
moveRecord *move;
pushStack(s, &move);
writeMove(move, direction, style, control, face, position);
}
void
getMove(PyraminxStack *s, int *direction, int *style, int *control,
int *face, int *position)
{
readMove(topStack(s), direction, style, control, face, position);
popStack(s);
}
int
madeMoves(PyraminxStack *s)
{
return !emptyStack(s);
}
void
flushMoves(PyraminxWidget w, PyraminxStack *s, Boolean undo)
{
int face, position;
flushStack(s);
if (undo) {
for (face = 0; face < MAX_FACES; face++)
for (position = 0; position < w->pyraminx.sizeSize; position++) {
startLoc[face][position].face =
w->pyraminx.facetLoc[face][position].face;
startLoc[face][position].rotation =
w->pyraminx.facetLoc[face][position].rotation;
}
}
}
int
numMoves(PyraminxStack *s)
{
return s->count;
}
Boolean
scanMoves(FILE *fp, PyraminxWidget w, int moves)
{
int direction, style, control, face, position, k, c;
for (k = 0; k < moves; k++) {
while ((c = getc(fp)) != EOF && c != SYMBOL);
if (fscanf(fp, "%d %d %d %d %d",
&direction, &style, &control, &face, &position) != 5) {
(void) fprintf(stderr,
"corrupt scan, expecting 5 integers for move %d\n", k);
return False;
}
movePuzzle(w, face, position, direction,
style, control, INSTANT);
}
return True;
}
void
printMoves(FILE *fp, PyraminxStack *s)
{
int direction, style, control, face, position, counter = 0;
s->currMove = s->firstMove->next;
(void) fprintf(fp, "moves\tdir\tstyle\tcontrol\tface\tposition\n");
while (s->currMove != s->lastMove) {
readMove(&(s->currMove->move),
&direction, &style, &control, &face, &position);
(void) fprintf(fp, "%d%c\t%d\t%d\t%d\t%d\t%d\n",
++counter, SYMBOL, direction, style, control, face, position);
s->currMove = s->currMove->next;
}
}
Boolean
scanStartPosition(FILE *fp, PyraminxWidget w)
{
int face, position, num, c;
while ((c = getc(fp)) != EOF && c != SYMBOL);
for (face = 0; face < MAX_FACES; face++)
for (position = 0; position < w->pyraminx.sizeSize; position++) {
if (fscanf(fp, "%d ", &num) != 1) {
(void) fprintf(stderr,
"corrupt start record, expecting an integer for %d %d\n",
face, position);
return False;
}
startLoc[face][position].face = num;
if (w->pyraminx.orient) {
if (fscanf(fp, "%d ", &num) != 1) {
(void) fprintf(stderr,
"corrupt start record, expecting an integer for orient %d %d\n",
face, position);
return False;
}
startLoc[face][position].rotation = num;
}
}
return True;
}
void
printStartPosition(FILE *fp, PyraminxWidget w)
{
int face, position;
(void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
for (face = 0; face < MAX_FACES; face++) {
int square_1 = 0, sqrt = 1;
for (position = 0; position < w->pyraminx.sizeSize; position++) {
(void) fprintf(fp, "%4d",
startLoc[face][position].face);
if (w->pyraminx.orient) {
(void) fprintf(fp, "%3d",
startLoc[face][position].rotation);
}
if (position == square_1) {
(void) fprintf(fp, "\n");
++sqrt;
square_1 = sqrt * sqrt - 1;
}
}
(void) fprintf(fp, "\n");
}
}
void
setStartPosition(PyraminxWidget w)
{
int face, position;
for (face = 0; face < MAX_FACES; face++)
for (position = 0; position < w->pyraminx.sizeSize; position++) {
w->pyraminx.facetLoc[face][position].face =
startLoc[face][position].face;
if (w->pyraminx.orient) {
w->pyraminx.facetLoc[face][position].rotation =
startLoc[face][position].rotation;
}
}
drawAllPieces(w);
}
|