C \ C++/22_2 프로그래밍 (C)

소코반 개작 프로젝트

안정민 2023. 2. 3. 16:07

20214777 안정민 기말프로젝트과제.c
0.00MB

파일 첨부

 

#include <stdio.h>
#include <conio.h>
#include "cursor.h"

enum { BACK = 13, ESC = 27, LEFT = 75, RIGHT = 77, UP = 72, DOWN = 80};  //��� ����� ���� Ű�� ġ�� ����
#define putchxy(x, y, c) {gotoxy(x, y);puts(c);}
#define putsxy(x, y, s) {gotoxy(x, y);puts(s);}

char arStage[][18][21] = { 
	{
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111000111111111111",
		"11111200111111111111",
		"11111002111111111111",
		"11100202011111111111",
		"11101011011111111111",
		"10001011011111003311",
		"10200200040000003311",
		"11111011101011003311",
		"11111000001111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
	},
	{
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11113300100000111111",
		"11113300102002001111",
		"11113300121111001111",
		"11113300004011001111",
		"11113300101002011111",
		"11111111101120201111",
		"11111102002020201111",
		"11111100001000001111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
	},
	{
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111110000041111",
		"11111111110212011111",
		"11111111110200211111",
		"11111111111202011111",
		"11111111110201011111",
		"11333300110200200111",
		"11133300002002000111",
		"11333300111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
		"11111111111111111111",
	},
};  // 0�� ���� 1�� �� 2�� ��ֹ��ڽ� 3�� â�� 4�� ��� -->> "  ", "���", "����", "�ء�", "��"
char ns[18][21];
int nx, ny;
const char* arTile[] = { "  ", "���", "����", "�ء�", "��" };
enum { EMPTY = 48, WALL, PACK, DEPOT, MAN };
int stage = 0;



int main()
{
	int ch;
	int x, y;
	int dx, dy;
	BOOL bEnd;
	int NumMove=0;

	showcursor(FALSE);

	
	for (;;) {
		clock_t start, finish;
		double duration;
		start = clock();
		memcpy(ns, arStage[stage], sizeof(ns));
		for (y = 0; y < 18; y++) {
			for (x = 0; x < 20; x++) {
				if (ns[y][x] == MAN) {
					nx = x;
					ny = y;
					ns[y][x] = EMPTY;
				}
			}
		}
		clrscr();
		NumMove = 0;
		putsxy(45, 2, "SOKOBAN");
		putsxy(45, 4, "ESC :����, ENTER : �ǵ�����");
		putsxy(45, 6, "1, 2, 3 : �������� �̵�");
		

		for (;;) {
			for (y = 0; y < 18; y++) {
				for (x = 0; x < 20; x++) {
					putsxy(x * 2, y, arTile[ns[y][x] - '0']);
				}
			}
			putsxy(nx * 2, ny, arTile[MAN - '0']);

			ch = getch();
			finish = clock();
			duration = (double)(finish - start) / CLOCKS_PER_SEC;
			gotoxy(45, 10);
			printf("%f�� �Դϴ�.\n", duration);

			if (ch == 0xE0 || ch == 0) {
				ch = getch();
				dx = dy = 0;
				switch (ch) {
				case LEFT:
					dx = -1;
					break;
				case RIGHT:
					dx = 1;
					break;
				case UP:
					dy = -1;
					break;
				case DOWN:
					dy = 1;
					break;
				}

				if (ns[ny + dy][nx + dx] != WALL) {
					if (ns[ny + dy][nx + dx] == PACK) {
						if (ns[ny + dy * 2][nx + dx * 2] == EMPTY ||
							ns[ny + dy * 2][nx + dx * 2] == DEPOT) {
							if (arStage[stage][ny + dy][nx + dx] == DEPOT) {
								ns[ny + dy][nx + dx] = DEPOT;
							}
							else {
								ns[ny + dy][nx + dx] = EMPTY;
							}
							ns[ny + dy * 2][nx + dx * 2] = PACK;
						}
						else {
							dx = dy = 0;
						}
					}
					nx += dx;
					ny += dy;
					NumMove += 1;
					gotoxy(45, 8);
					printf("������ Ƚ�� : %d", NumMove);
				}
					
			}
			else if (ch == BACK) {
				int i = 0;
				for (int i = 0; i < 2; i++) {
					nx -= dx;
					ny -= dy;
					putsxy(nx * 2, ny, arTile[MAN - '0']);
					i = i + 1;
					NumMove = NumMove - 1;
					gotoxy(45, 8);
					printf("������ Ƚ�� : %d", NumMove);

				}
			}
			else {
				if (ch == ESC) {
					showcursor(TRUE);
					return;
				}
				if (ch == '1') {
					stage = 0;
					break;
				}
				if (ch == '2') {
					stage = 1;
					break;
				}
				if (ch == '3') {
					stage = 2;
					break;
				}
			}

			bEnd = TRUE;
			for (y = 0; y < 18; y++) {
				for (x = 0; x < 20; x++) {
					if (ns[y][x] == DEPOT) {
						bEnd = FALSE;
					}
				}
			}

			if (bEnd) {
				clrscr();
				putsxy(10, 10, "�� ���߾��, ���� ���������� �̵��մϴ�.");
				delay(2000);
				stage++;
				if (stage == 3) stage = 0;
				break;
			}
		}
	}
}