#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h> // Windows Àü¿ë, Ű ÀÔ·ÂÀ» ºñµ¿±â·Î ó¸®
#include <windows.h> // ÄÜ¼Ö Ä¿¼ Á¦¾î
#define SIZE 10
#define MONSTER_COUNT 5
// °ÔÀÓ ¸Ê°ú ij¸¯ÅÍ Á¤º¸
char map[SIZE][SIZE];
int player_x = 0, player_y = 0; // ij¸¯ÅÍ Ãʱâ À§Ä¡
int monster_x[MONSTER_COUNT], monster_y[MONSTER_COUNT]; //Ãß°¡
int score = 0;
// ÄÜ¼Ö Ä¿¼ À§Ä¡ ¼³Á¤
void set_cursor_position(int x, int y) {
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Ä¿¼ ¼û±â±â
void hide_cursor() {
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.bVisible = FALSE; // Ä¿¼ ¼û±è
cursorInfo.dwSize = 1; // Ä¿¼ Å©±â
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// ¸Ê ÃʱâÈ
void initialize_map() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
map[i][j] = '.'; // ºó °ø°£
}
}
// ij¸¯ÅÍ Ãʱâ À§Ä¡
map[player_y][player_x] = 'C';
// ¸ó½ºÅÍ À§Ä¡ ¼³Á¤
srand(time(NULL));
for (int i = 0; i < MONSTER_COUNT; i++) { // ¸ó½ºÅÍ 5°³ »ý¼º
/*int monster_x = rand() % SIZE;
int monster_y = rand() % SIZE;*/
monster_x[i] = rand() % SIZE;
monster_y[i] = rand() % SIZE;
if (map[monster_y[i]][monster_x[i]] == '.') {
map[monster_y[i]][monster_x[i]] = 'M';
}
else {
i--; // ÀÌ¹Ì ÀÖ´Â À§Ä¡¸é ´Ù½Ã ½Ãµµ
}
}
}
// ¸Ê Ãâ·Â
void print_map() {
set_cursor_position(0, 0); // Ä¿¼¸¦ È¸é ¸Ç À§·Î À̵¿
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", map[i][j]);
}
printf("\n");
}
printf("\nÁ¡¼ö: %d\n", score);
}
// ij¸¯ÅÍ À̵¿
void move_player(char direction) {
// ÇöÀç À§Ä¡ ÃʱâÈ
map[player_y][player_x] = '.';
// ¹æÇâ¿¡ µû¶ó À̵¿
if (direction == 'w' && player_y > 0) player_y--; // ˤ
if (direction == 's' && player_y < SIZE - 1) player_y++; // ¾Æ·¡
if (direction == 'a' && player_x > 0) player_x--; // ¿ÞÂÊ
if (direction == 'd' && player_x < SIZE - 1) player_x++; // ¿À¸¥ÂÊ
// À̵¿ÇÑ À§Ä¡¿¡ ¸ó½ºÅͰ¡ ÀÖÀ¸¸é Á¡¼ö Áõ°¡
if (map[player_y][player_x] == 'M') {
score++;
}
// ij¸¯ÅÍ À§Ä¡ ¾÷µ¥ÀÌÆ®
map[player_y][player_x] = 'C';
}
// ¸ó½ºÅÍ À̵¿
void move_monster() {
for (int i = 0; i < MONSTER_COUNT; i++) {
int prev_y = monster_y[i], prev_x = monster_x[i];
// ÇöÀç À§Ä¡ ÃʱâÈ
map[monster_y[i]][monster_x[i]] = '.';
int direction = rand() % 4;
switch (direction)
{
case 0: if (monster_y[i] > 0) monster_y[i]--; //ˤ
break;
case 1: if (monster_y[i] < SIZE - 1) monster_y[i]++; //¾Æ·¡
break;
case 2: if (monster_x[i] > 0) monster_x[i]--; //¿ÞÂÊ
break;
case 3: if (monster_x[i] < SIZE - 1) monster_x[i]++; //¿À¸¥ÂÊ
break;
}
if (map[monster_y[i]][monster_x[i]] == '.') map[monster_y[i]][monster_x[i]] = 'M';
else map[prev_y][prev_x] = 'M';
}
}
// ¸ÞÀÎ ÇÔ¼ö
int main() {
char input;
initialize_map();
system("cls"); // Ãʱâ ȸé Á¤¸®
hide_cursor(); // Ä¿¼ ¼û±â±â
while (1) {
print_map();
move_monster();
// Ű ÀÔ·Â ´ë±â
if (_kbhit()) {
input = _getch(); // Ű ÀԷ¹ޱâ
if (input == 'q') { // q ÀÔ·Â ½Ã °ÔÀÓ Á¾·á
printf("°ÔÀÓ Á¾·á! ÃÖÁ¾ Á¡¼ö: %d\n", score);
break;
}
move_player(input);
}
Sleep(200);
}
return 0;
}