네이버 지식인에서 어떤 사람이 올렸었는데 재밌어 보여서 완성해본 코드입니다.

목적지에 물건을 옮기는 게임인데 간단하게 만들어봤습니다.

 

#include <stdio.h>
#include <conio.h>  
#include <windows.h> 
#include <string.h>

enum Map { Blank = 0, Wall = 1, Block = 2, Goal = 3, Player = 4 };
enum KeyBoard { RIGHT = 77, LEFT = 75, UP = 72, DOWN = 80 };
const char icon[5][5] = { "..", "■", "★", "_G", "♬" };
const int StageCount = 1;
const int MaxRow = 10;
const int MaxCol = 10;

int gameOver = 0;
int currentStage[MaxRow][MaxCol];
int round;
int preIcon = 0;
COORD playerCoord;

int MAP[StageCount][MaxRow][MaxCol] = 
{
//1round
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,3,1},
{1,0,1,1,0,0,0,0,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,2,0,1,0,4,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
}
//next..
};

void gotoxy(int row, int col, const char* str)
{
	COORD Pos = { col * 2 + 1, row };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
	printf("%s", str);
}

void ShowMainMessage()
{
	for (int i = 0; i < 10; ++i)
	{
		gotoxy(5, 20 + i, "-");
	}
	for (int i = 0; i < 10; ++i)
	{
		gotoxy(10, 20 + i, "-");
	}

	gotoxy(7, 18, "게임 시작! (Press Any Key)");
}

void drawMap()
{
	for (int row = 0; row < MaxRow; row++)
	{
		for (int col = 0; col < MaxCol; col++)
		{
			int index = currentStage[row][col];

			if (index > Player)
			{
				gotoxy(row, col, "  ");
				continue;
			}

			gotoxy(row, col, icon[index]);
		}
	}
}

void initStageInfo()
{
	for (int row = 0; row < MaxRow; ++row)
	{
		for (int col = 0; col < MaxCol; ++col)
		{
			currentStage[row][col] = MAP[round][row][col];

			if (currentStage[row][col] == Player) 
			{
				playerCoord.Y = row;
				playerCoord.X = col;
			}
		}
	}
}

int CanMove(int row, int col)
{
	return currentStage[playerCoord.Y + row][playerCoord.X + col] != Wall;
}

int IsExistBlock(int row, int col)
{
	return currentStage[playerCoord.Y + row][playerCoord.X + col] == Block;
}

int IsGoal(int row, int col)
{
	return currentStage[playerCoord.Y + row][playerCoord.X + col] == Goal;
}

int CanPush(int row, int col)
{
	return currentStage[playerCoord.Y + row][playerCoord.X + col] == Blank || IsGoal(row, col);
}

void PrintState()
{
	gotoxy(15, 0, "현재 상태\n");
	for (int i = 0; i < MaxRow; ++i)
	{
		for (int j = 0; j < MaxCol; ++j)
		{
			printf("%d ", currentStage[i][j]);
		}
		printf("\n");
	}
}

void tryMove(int row, int col)
{
	if (CanMove(row, col))
	{		
		if (IsExistBlock(row, col))
		{
			//블록을 플레이어가 이동한 방향 + 1 만큼 밀어주기 위해 * 2를 해줌

			if (IsGoal(row * 2, col * 2))
			{
				gameOver = 1;
			}

			if (CanPush(row * 2, col * 2))
			{
				//민 블록을 그려준다.
				currentStage[playerCoord.Y + row * 2][playerCoord.X + col * 2] = Block;
				gotoxy(playerCoord.Y + row * 2, playerCoord.X + col * 2, icon[Block]);

				//현재 위치를 최근 아이콘으로 교체해준다.
				currentStage[playerCoord.Y][playerCoord.X] = preIcon;
				gotoxy(playerCoord.Y, playerCoord.X, icon[preIcon]);
				
				//플레이어 이동 및 갱신
				playerCoord.Y += row;
				playerCoord.X += col;
				currentStage[playerCoord.Y][playerCoord.X] = Player;
				gotoxy(playerCoord.Y, playerCoord.X, icon[Player]);
			}

			//테스트용
			PrintState();
			return;
		}

		//이동 전 기존 아이콘으로 현재 자리를 갱신한다.
		currentStage[playerCoord.Y][playerCoord.X] = preIcon;
		gotoxy(playerCoord.Y, playerCoord.X, icon[preIcon]);

		//최근 아이콘은 앞 칸의 아이콘
		preIcon = currentStage[playerCoord.Y + row][playerCoord.X + col];

		//플레이어 이동 및 갱신
		playerCoord.Y += row;
		playerCoord.X += col;
		currentStage[playerCoord.Y][playerCoord.X] = Player;
		gotoxy(playerCoord.Y, playerCoord.X, icon[Player]);

		//테스트용
		PrintState();
	}
}

int main()
{
	ShowMainMessage();

	//콘솔 버퍼 안보이게 설정
	CONSOLE_CURSOR_INFO ci = { 1, FALSE };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
	getch();
	
	initStageInfo();
	drawMap();

	while (gameOver == 0)
	{
		int key = getch();
		if (key == 0xe0 || key == 0) //방향키는 두번의 입력으로 인식됩니다.
		{
			key = getch();

			switch (key)
			{
				case RIGHT:
					tryMove(0, 1);
					break;
				case LEFT:
					tryMove(0, -1);
					break;
				case UP:
					tryMove(-1, 0);
					break;
				case DOWN:
					tryMove(1, 0);
					break;
			}
		}
	}

	gotoxy(12, 0, "승리! (키를 눌러 종료해주세요.)");
	Sleep(1000);
	getch();
	return 0;
}

+ Recent posts