Ejemplo n.º 1
0
int solve(Puzzle *puzzle, Table fixes) {
	Table nextmap;
	Position pos;
	int i, j, m, n = 1;
	for (i = 1; i <= Y_SIZE - 2; i++, n++) {
		dcode(printf("i=%d\n", i));
		for (j = 1; j <= X_SIZE - 2; j++, n++) {
			pos = number2postion(n);
			if (moveNumber(puzzle, fixes, n, pos) < 0) {
				printPuzzle(puzzle);
				printf("error j=%d\n", n);
				return -1;
			}
			fixes[pos.y][pos.x] = FIXED;
		}
		moveRight2Numbers(puzzle, fixes, j, i, n++);
	}
	for (j = 1; j <= X_SIZE - 2; j++, n++) {
		moveLeft2Numbers(puzzle, fixes, j, i, n);
	}
	moveNumber(puzzle, fixes, n, number2postion(n));
	n++;
	moveNumber(puzzle, fixes, n, number2postion(n));
	pos = makePosition(X_SIZE, Y_SIZE);
	searchSpaceRoot(puzzle, fixes, nextmap, pos);
	moveSpace(puzzle, nextmap, pos);
	return 0;
}
Ejemplo n.º 2
0
void display() {
  GLfloat lightPos[4] = {sun.x, sun.y, sun.z, 1};
  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

  moveSpace(&space, camera.eye.x, camera.eye.y, camera.eye.z);
  drawSpace(&space);
  drawShip(&ship);
  drawSun(&sun);
}
Ejemplo n.º 3
0
void moveLeft2Numbers(Puzzle *puzzle, Table fixes, const int x, const int y, int n) {
	int m = n;
	Position pos1 = makePosition(x, y);
	Position pos2 = makePosition(x, y + 1);
	Position pos3 = makePosition(x + 1, y);
	Position pos4 = makePosition(x + 1, y + 1);
	n += X_SIZE;
	dcode(printf("moveLeft2Numbers x=%d y=%d n=%d\n", x, y, m));
	for (;;) {
		Table nextmap;
		int p1 = puzzle->table[y][x];
		int p2 = puzzle->table[y + 1][x];
		int p3 = puzzle->table[y][x + 1];
		int p4 = puzzle->table[y + 1][x + 1];
		dputs("loop head");
		if (p1 == m && p2 == n) {
			dputs("OK");
			break;
		} else
		if (p1 != m && p1 != n && p2 != m && p2 != n) {
			Table tempM, tempN;
			Position posM = searchNumber(puzzle, m);
			Position posN = searchNumber(puzzle, n);
			int rM = searchRoot(fixes, tempM, posM, pos2);
			int rN = searchRoot(fixes, tempN, posN, pos1);
			dputs("not found case");
			if (rM >= 0 && rM < rN) {
				if (moveNumber(puzzle, fixes, m, pos2) < 0) {
					printPuzzle(puzzle);
					puts("error");
					return;
				}
			} else if (rN >= 0) {
				if (moveNumber(puzzle, fixes, n, pos1) < 0) {
					printPuzzle(puzzle);
					puts("error2");
					return;
				}
			} else {
				printPuzzle(puzzle);
				puts("error3");
				return;
			}
			continue;
		} else
		if ((p1 == m && p2 == 0 && p4 == n) || (p1 == 0 && p2 == n && p3 == m)) {
			dputs("rare case");
			moveRightSpace(puzzle);
			dputs("OK2");
			break;
		} else
		if (p1 == n && p3 == m) {
			int f3 = fixes[pos3.y][pos3.x];
			dputs("p1==n&&p3==m");
			fixes[pos1.y][pos1.x] = FIXED;
			fixes[pos3.y][pos3.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos3.y][pos3.x] = f3;
			moveSpace(puzzle, nextmap, pos2);
			moveUpSpace(puzzle);
			moveRightSpace(puzzle);
			dputs("OK3");
			break;
		} else
		if (p2 == m && p4 == n) {
			int f4 = fixes[pos4.y][pos4.x];
			dputs("p2==m&&p4==n");
			fixes[pos2.y][pos2.x] = FIXED;
			fixes[pos4.y][pos4.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos4.y][pos4.x] = f4;
			moveSpace(puzzle, nextmap, pos1);
			moveDownSpace(puzzle);
			moveRightSpace(puzzle);
			dputs("OK4");
			break;
		} else
		if (p1 == 0 && p2 == m && p3 == n) {
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n && p2 == 0 && p4 == m) {
			moveRightSpace(puzzle);
			moveUpSpace(puzzle);
			moveLeftSpace(puzzle);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n && p2 == m) {
			searchSpaceRoot(puzzle, fixes, nextmap, pos3);
			moveSpace(puzzle, nextmap, pos3);
			moveLeftSpace(puzzle);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p2 == n && p4 == m) {
			if (p1 == 0) {
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			} else
			if (p3 == 0) {
				moveLeftSpace(puzzle);
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			} else {
				Position pos = makePosition(x + 2, y);
				searchSpaceRoot(puzzle, fixes, nextmap, pos);
				moveSpace(puzzle, nextmap, pos);
				moveLeftSpace(puzzle);
				moveLeftSpace(puzzle);
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			}
		} else
		if (p1 == m && p3 == n) {
			Position pos = makePosition(x + 2, y + 1);
			int f = fixes[pos.y][pos.x];
			dputs("wrong case (move n)");
			dcode(printTable("fixes", fixes));
			if (moveNumber(puzzle, fixes, n, pos) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): n pos %d\n", m);
				return;
			}
			dputs("wrong case (move m)");
			dcode(printTable("fixes", fixes));
			fixes[pos.y][pos.x] = FIXED;
			if (moveNumber(puzzle, fixes, m, pos2) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): m pos2 %d\n", m);
				return;
			}
			fixes[pos.y][pos.x] = f;
			continue;
		} else
		if (p1 == m) {
			int f1 = fixes[pos1.y][pos1.x];
			dputs("p1==m");
			fixes[pos1.y][pos1.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos1.y][pos1.x] = f1;
			moveSpace(puzzle, nextmap, pos2);
			moveUpSpace(puzzle);
			continue;
		} else
		if (p2 == n) {
			int f2 = fixes[pos2.y][pos2.x];
			dputs("p2==n");
			fixes[pos2.y][pos2.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos2.y][pos2.x] = f2;
			moveSpace(puzzle, nextmap, pos1);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n) {
			int f1 = fixes[pos1.y][pos1.x];
			dputs("p1==n");
			fixes[pos1.y][pos1.x] = FIXED;
			if (moveNumber(puzzle, fixes, m, pos3) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): p1==n %d\n", m);
				return;
			}
			fixes[pos1.y][pos1.x] = f1;
			continue;
		} else
		if (p2 == m) {
			int f2 = fixes[pos2.y][pos2.x];
			dputs("p2==m");
			fixes[pos2.y][pos2.x] = FIXED;
			if (moveNumber(puzzle, fixes, n, pos4) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): p2==m %d\n", m);
				return;
			}
			fixes[pos2.y][pos2.x] = f2;
			continue;
		} else {
			printPuzzle(puzzle);
			puts("error dayo");
			return;
		}
	}
	fixes[pos1.y][pos1.x] = FIXED;
	fixes[pos2.y][pos2.x] = FIXED;
}
Ejemplo n.º 4
0
int moveNumber(Puzzle *puzzle, Table fixes, const int number, const Position pos1) {
	if (puzzle->table[pos1.y][pos1.x] == number) {
		return 0;
	}
	dputs("moveNumber");
	{
		Position pos2 = searchNumber(puzzle, number), pos3;
		int x = pos2.x;
		int y = pos2.y;
		Table rootmap, temp1, temp2, temp3, temp4;
		int r = searchRoot(fixes, rootmap, pos2, pos1) - 1;
		int z, mv, zz, f;
		if (r < 0) {
			return -1;
		}
		while (x != pos1.x || y != pos1.y) {
			dputs("loop head");
			zz = INT_MAX;
			mv = 0;
			f = fixes[y][x];
			fixes[y][x] = FIXED;
			if (rootmap[y][x - 1] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp2, pos2 = makePosition(x - 1, y));
				if (z < INT_MAX) {
					mv = MOVE_RIGHT;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y][x + 1] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp1, pos2 = makePosition(x + 1, y));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_LEFT;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y - 1][x] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp4, pos2 = makePosition(x, y - 1));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_DOWN;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y + 1][x] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp3, pos2 = makePosition(x, y + 1));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_UP;
					pos3 = pos2;
				}
			}
			fixes[y][x] = f;
			dcode(printTable("rootmap", rootmap));
			dcode(printf("number=%d x=%d y=%d x3=%d y3=%d mv=%d r=%d\n", number, x, y, pos3.x, pos3.y, mv, r));
			switch (mv) {
			case MOVE_LEFT:
				moveSpace(puzzle, temp1, pos3);
				moveLeftSpace(puzzle);
				break;
			case MOVE_RIGHT:
				moveSpace(puzzle, temp2, pos3);
				moveRightSpace(puzzle);
				break;
			case MOVE_UP:
				moveSpace(puzzle, temp3, pos3);
				moveUpSpace(puzzle);
				break;
			case MOVE_DOWN:
				moveSpace(puzzle, temp4, pos3);
				moveDownSpace(puzzle);
				break;
			default:
				/* error */
				return -3;
			}
			x = pos3.x;
			y = pos3.y;
			r--;
		}
	}
	return 1;
}