old/sokosol2.c
author viric@llimona
Fri, 16 Feb 2007 23:53:32 +0100
changeset 27 545f73869d65
parent 3 29cc57a9678e
permissions -rw-r--r--
Added USR1 status shower.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BOX '*'
#define WALL '#'
#define MAN '8'
#define PLATFORM '+'
#define BOXINPLATFORM 'O'
#define MANINPLATFORM 'E'
#define BLANK ' '

#define DIR_LEFT 0
#define DIR_RIGHT 1
#define DIR_UP 2
#define DIR_DOWN 3

#define MAX_X 50
#define MAX_Y 50
#define MAX_MOVES 100

struct Map
{
	char Cells[MAX_X][MAX_Y];
	int SizeX, SizeY;
	int ManX, ManY;
	int NumPlatforms;
	int NumBoxesInPlatform;
};



void ReadMap(struct Map *M, char *FileName)
{
	FILE *Fitxer;
	int i,j;

	if(!(Fitxer = fopen(FileName, "r")))
	{
		printf("Error opening %s!", FileName);
		exit(1);
	}

	M->SizeX=0;
	M->SizeY=0;
	while (!feof(Fitxer))
	{
		fgets(M->Cells[M->SizeY++], MAX_X, Fitxer);
	}
	M->SizeY--;
	M->SizeX = strlen(M->Cells[0]) - 1;

	M->NumPlatforms = 0;
	M->NumBoxesInPlatform = 0;
	for (j = 0; j<M->SizeY; j++)
		for (i=0; i<M->SizeX; i++)
		{
			if (M->Cells[j][i] == MAN)
			{ M->ManX = i; M->ManY = j; }
			else if (M->Cells[j][i] == PLATFORM)
				M->NumPlatforms++;
			else if (M->Cells[j][i] == BOXINPLATFORM)
			{
				M->NumPlatforms++;
				M->NumBoxesInPlatform++;
			}
		}
}


void ShowMap (struct Map *M)
{
	int i,j;
	for (j = 0; j<M->SizeY; j++)
	{
		for (i=0; i<M->SizeX; i++)
			printf("%c", M->Cells[j][i]);
		printf("\n");
	}
	printf("Man is at (%i,%i)\n", M->ManX, M->ManY);
	printf("Platforms: %i, BoxesInPlatform: %i\n", M->NumPlatforms,
			M->NumBoxesInPlatform);
}



void CopyMap (struct Map *Mdest, struct Map *Morig)
{
	memcpy((void *) Mdest, (void *) Morig, sizeof (struct Map));
}

int MoveMan (struct Map *M, int Direction)
{
	int NewX, NewY;

	// Check if man is where it should be
	if (M->Cells[M->ManY][M->ManX] != MAN &&
		M->Cells[M->ManY][M->ManX] != MANINPLATFORM)
	{
		printf("Man isn't where it should be!\n");
		exit(2);
	}

	// Process Movement
	if (Direction == DIR_LEFT)
	{ NewX = M->ManX - 1; NewY = M->ManY; }
	else if (Direction == DIR_RIGHT)
	{ NewX = M->ManX + 1; NewY = M->ManY; }
	else if (Direction == DIR_UP)
	{ NewX = M->ManX; NewY = M->ManY - 1; }
	else if (Direction == DIR_DOWN)		// if not needed
	{ NewX = M->ManX; NewY = M->ManY + 1; }



	// What's in front of the man?

	if (M->Cells[NewY][NewX] == WALL)
	{
		return 0;	// ILLEGAL MOVE
	}
	else if (M->Cells[NewY][NewX] == BOX)
	{
		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
			BLANK)
		{
			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
				= BOX;

			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
			{	// Man is over Platform
				M->Cells[M->ManY][M->ManX] = PLATFORM;
				M->Cells[NewY][NewX] = MAN;
			}
			else	// Man is over Blank
			{
				M->Cells[M->ManY][M->ManX] = BLANK;
				M->Cells[NewY][NewX] = MAN;
			}
			M->ManX = NewX; M->ManY = NewY;
		}
		else
		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
				PLATFORM)
		{
			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
				= BOXINPLATFORM;

			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
			{	// Man is over Platform
				M->Cells[M->ManY][M->ManX] = PLATFORM;
				M->Cells[NewY][NewX] = MAN;
			}
			else	// Man is over Blank
			{
				M->Cells[M->ManY][M->ManX] = BLANK;
				M->Cells[NewY][NewX] = MAN;
			}
			M->ManX = NewX; M->ManY = NewY;

			M->NumBoxesInPlatform++;
		}
		else
		{
			return 0;	// ILLEGAL MOVE
		}
	}else
	if (M->Cells[NewY][NewX] == BOXINPLATFORM)
	{
		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
			BLANK)
		{
			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
				= BOX;

			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
			{	// Man is over Platform
				M->Cells[M->ManY][M->ManX] = PLATFORM;
				M->Cells[NewY][NewX] = MANINPLATFORM;
			}
			else	// Man is over Blank
			{
				M->Cells[M->ManY][M->ManX] = BLANK;
				M->Cells[NewY][NewX] = MANINPLATFORM;
			}
			M->ManX = NewX; M->ManY = NewY;
			M->NumBoxesInPlatform--;
		}
		else
		if (M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)] ==
				PLATFORM)
		{
			M->Cells[NewY + (NewY-M->ManY)][NewX + (NewX-M->ManX)]
				= BOXINPLATFORM;

			if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
			{	// Man is over Platform
				M->Cells[M->ManY][M->ManX] = PLATFORM;
				M->Cells[NewY][NewX] = MANINPLATFORM;
			}
			else	// Man is over Blank
			{
				M->Cells[M->ManY][M->ManX] = BLANK;
				M->Cells[NewY][NewX] = MANINPLATFORM;
			}
			M->ManX = NewX; M->ManY = NewY;
		}
		else
		{
			return 0;	// ILLEGAL MOVE
		}
	}
	else if (M->Cells[NewY][NewX] == PLATFORM)
	{
		if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
		{	// Man is over Platform
			M->Cells[M->ManY][M->ManX] = PLATFORM;
			M->Cells[NewY][NewX] = MANINPLATFORM;
		}
		else	// Man is over Blank
		{
			M->Cells[M->ManY][M->ManX] = BLANK;
			M->Cells[NewY][NewX] = MANINPLATFORM;
		}
		M->ManX = NewX; M->ManY = NewY;
	}
	else if (M->Cells[NewY][NewX] == BLANK)		// IF not needed
	{
		if (M->Cells[M->ManY][M->ManX] == MANINPLATFORM)
		{	// Man is over Platform
			M->Cells[M->ManY][M->ManX] = PLATFORM;
			M->Cells[NewY][NewX] = MAN;
		}
		else	// Man is over Blank
		{
			M->Cells[M->ManY][M->ManX] = BLANK;
			M->Cells[NewY][NewX] = MAN;
		}
		M->ManX = NewX; M->ManY = NewY;
	}
	return 1;
}

int main()
{
	struct Map Morigin, M;
	char Moves[MAX_MOVES];
	int NumMoves;
	int IllegalMove;
	int Carry;
	int Arrel;

	int i, j;

	ReadMap(&Morigin, "model");

	ShowMap(&Morigin);

	CopyMap(&M, &Morigin);

	printf("Numero de moviments a provar: ");
	scanf("%i", &NumMoves);
	printf("Arrel: ");
	scanf("%i", &Arrel);
	srand(Arrel);

	// Process the combinations.
	// Order: Left, Right, Up, Down
	while (M.NumPlatforms != M.NumBoxesInPlatform)
	{
		// Reget the original map
		CopyMap(&M, &Morigin);

/*
		// Print the combination

		for (i=0; i < NumMoves; i++)
		{
			printf("%c", Moves[i]);
		}
		printf("\n");
*/

		// Process the combination
		for (i = 0; i < NumMoves; i++)
		{
			if (!MoveMan(&M, Moves[i]=rand()%4))
				break;
		}
					
	}

	// Print the combination
	for (i=0; i < NumMoves; i++)
	{
		printf("%c", Moves[i]+48);
	}
	printf("\n");

/*
	// Print The Steps
	strcpy(Moves, "DDDRUU");

	for (i=0; i < 6; i++)
	{
		printf("%i", MoveMan(&M, Moves[i]));
		ShowMap(&M);
	}
*/

}