[PushPush] 완성본

 

#pragma warning (disable : 4996)
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
#include <time.h>
#include <conio.h>

#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77

#define WALL 1
#define MOVESTAR 2
#define TAGETSTAR 3
#define R_WALL 4
#define PLAYER 5

#define FINISH 123

int count = 0;
int display = 1;

int _con = 20;
int _row = 20;

int original_map[20][20] = { 
	{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,5,0,0,2,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0,3,1 },
	{ 1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,3,3,1 },
	{ 1,0,0,0,0,0,2,0,0,0,0,1,1,1,0,0,0,3,3,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1 },
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} };
int play_map[20][20];


int * TargetStar[9];

int getKey(void) {
	int key;
	key = getch();
	if (key == 224)
		key = getch();
	return key;
}

void R_wall(void) {
	srand(time(NULL));
	int i, j;
	while (1) {
		i = rand() % 19;
		if (0 == i) {
			continue;
		}
		j = rand() % 19;
		if (0 == j || 19 == j) {
			continue;
		}
		if (0 == play_map[i][j]) {
			play_map[i][j] = WALL;
			break;
		}
	}
}

void Init_Map(void) {
	int i, j;
	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++) {
			play_map[i][j] = original_map[i][j];
		}
	}
}

void Map_Display (void){
	int i, j, _rand = 0;
	srand(time(NULL));
	char str[8] = "Count :";
	char str2[40] = "S.N : ******** / NAME : *************";
	
	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++) {
			if (0 == play_map[i][j])
				printf("  ");
			if (WALL == play_map[i][j]) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
				printf("■");		// 벽
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
			}
			if (MOVESTAR == play_map[i][j]) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
				printf("★");		// 움직이는 별
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
			}
			if (TAGETSTAR == play_map[i][j]) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
				printf("☆");		// 목적지
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
			}
			if (R_WALL == play_map[i][j]) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
				printf("▩");		// 랜덤 블럭
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
			}
			if (PLAYER == play_map[i][j]) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
				printf("ⓟ");		// 플레이어
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
			}
			if (j % 19 == 0 && j != 0) {
				printf("\n");
			}
		}
	}
	for (i = 0; i < 8; i++) {
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
		printf("%c", str[i]);
	}
	printf("%d \n", count);
	for (i = 0; i < 40; i++) {
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 10);
		printf("%c", str2[i]);
	}
}

int Finish_Check(void) {
	int i;
	int result = 0;
	for (i = 0; i < 9; i++) {
		if (MOVESTAR == *TargetStar[i]) {
			result++;
		}
	}
	if (9 == result) {
		return FINISH;
	}
	return -1;
}

void Target_SAVER(void) {		// 수정 필요
	int i;
	
	for (i = 0; i < 9; i++) {
		if (0 == *TargetStar[i]) {
			*TargetStar[i] = TAGETSTAR;
		}
	}
}

void Move_Palyer(int key) {
	int i, j;
	int temp = 0;	// 이중 for문 break를 위해서
	switch (key)
	{
	case UP:
		for (i = 0; i <	20; i++) {
			for (j = 0; j < 20; j++) {
				if (MOVESTAR == play_map[i - 1][j]) {
					if (MOVESTAR != play_map[i - 2][j] && WALL != play_map[i - 2][j]) {
						if (PLAYER == play_map[i][j] && WALL != play_map[i - 1][j]) {
							play_map[i][j] = 0;
							play_map[i - 1][j] = PLAYER;
							temp = 20;
							play_map[i - 2][j] = MOVESTAR;
							display = 1;
							count++;
							break;
						}
					}
				}
				else if (PLAYER == play_map[i][j] && WALL != play_map[i - 1][j]) {
					play_map[i][j] = 0;
					play_map[i - 1][j] = PLAYER;
					temp = 20;
					display = 1;
					count++;
					break;
				}
			}
			if (20 == temp) {
				temp = 0;
				break;
			}
		}
		break;
	case DOWN:
		for (i = 0; i < 20; i++) {
			for (j = 0; j < 20; j++) {
				if (MOVESTAR == play_map[i + 1][j]) {
					if (MOVESTAR != play_map[i + 2][j] && WALL != play_map[i + 2][j]) {
						if (PLAYER == play_map[i][j] && WALL != play_map[i + 1][j]) {
							play_map[i][j] = 0;
							play_map[i + 1][j] = PLAYER;
							temp = 20;
							play_map[i + 2][j] = MOVESTAR;
							count++;
							display = 1;
							break;
						}
					}
				}
				else  if (PLAYER == play_map[i][j] && WALL != play_map[i+1][j]) {
					play_map[i][j] = 0;
					play_map[i + 1][j] = PLAYER;
					temp = 20;
					count++;
					display = 1;
					break;
				}
			}
			if (20 == temp) {
				temp = 0;
				break;
			}
		}
		break;
	case LEFT:
		for (i = 0; i < 20; i++) {
			for (j = 0; j < 20; j++) {
				if (MOVESTAR == play_map[i][j - 1]) {
					if (MOVESTAR != play_map[i][j - 2] && WALL != play_map[i][j - 2]) {
						if (PLAYER == play_map[i][j] && WALL != play_map[i][j - 1]) {
							play_map[i][j] = 0;
							play_map[i][j - 1] = PLAYER;
							temp = 20;
							play_map[i][j - 2] = MOVESTAR;
							count++;
							display = 1;
							break;
						}
					}
				}
				else if (PLAYER == play_map[i][j] && WALL != play_map[i][j - 1]) {
					play_map[i][j] = 0;
					play_map[i][j - 1] = PLAYER;
					temp = 20;
					count++;
					display = 1;
					break;
				}
			}
			if (20 == temp) {
				temp = 0;
				break;
			}
		}
		break;
	case RIGHT:
		for (i = 0; i < 20; i++) {
			for (j = 0; j < 20; j++) {
				if (MOVESTAR == play_map[i][j + 1]) {
					if (MOVESTAR != play_map[i][j + 2] && WALL != play_map[i][j + 2]) {
						if (PLAYER == play_map[i][j] && WALL != play_map[i][j + 1]) {
							play_map[i][j] = 0;
							play_map[i][j + 1] = PLAYER;
							temp = 20;
							play_map[i][j + 2] = MOVESTAR;
							count++;
							display = 1;
							break;
						}
					}
				}
				else if (PLAYER == play_map[i][j] && WALL != play_map[i][j + 1]) {
					play_map[i][j] = 0;
					play_map[i][j + 1] = PLAYER;
					temp = 20;
					count++;
					display = 1;
					break;
				}
			}
			if (20 == temp) {
				temp = 0;
				break;
			}
		}
		break;
	}
}

int main(int argc, char** argv) {
	/* fopen {{ */
	/* }} */

	int i, j, star = 0;
	int delayTime = 0, kount = 0;
	system("mode con: cols=50 lines=25");	// 콘솔 크기 정함
	Init_Map();

	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++) {
			if (TAGETSTAR == play_map[i][j]) {
				TargetStar[star] = &play_map[i][j];
				star++;
			}
		}
	}

	Map_Display();
	while (123 != Finish_Check()) {

		if (kbhit()) {
			Move_Palyer(getKey());
			if (display == 1) {
				Target_SAVER();
				system("cls");
				Map_Display();
			}
		}
		else
		{
			delayTime++;
			if (delayTime == 50000) {
				system("cls");
				Map_Display();
				kount++;
				delayTime = 0;
			}
			else if (2 == kount) {
				R_wall();
				kount = 0;
			}
		}
			
		
		/* conifg.txt 랜덤 블럭 여부 {{ */

		/* }} */
	}
	system("cls");
	Map_Display();

	printf("\n\n You Win ! \n");



}

특이한 함수 정리

Header : <Windows.h>

Function : 

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Number);
>> 이후 출력 되는 모든 문자의 색을 Number에 따라 정해진 색으로 출력됨

system(“mode con: cols=50 lines=25”);
>> 콘솔의 크기를 정해 줌

 

글의 문제가 있다면 댓글을 달아 주세요.

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.