C++ 함수선언 질문입니다.
조회수 503회
#include "pch.h"
#include <iostream>
#include <string>
#include <time.h>
#include <conio.h>
using namespace std;
int main()
{
struct player
{
string name;
int gold;
int stamina;
};
player me;
me.name = "동호";
me.gold = 50;
me.stamina = 20;
struct Tile
{
bool forest; // 숲 -2 스테 ♣
bool swamp; // 늪 -3 스테 ▦
bool ground; // 땅 -1 스테 □
bool mine; // 골드 +50골드 ⓖ
bool exit; // 탈출 X
bool store; // 상점 S
bool player; // 플레이어 O
};
int x;
int y;
cout << "가로의 크기를 입력하세요. : ";
cin >> x;
cout << endl;
cout << "세로의 크기를 입력하세요. : ";
cin >> y;
cout << endl;
// 힙 메모리에 int* 타입 * x만큼 메모리 할당
Tile**map = new Tile*[x];
// size 횟수 반복으로 int 타입 * y 만큼 메모리 할당
for (int j = 0; j < x; j++)
{
for (int i = 0; i <= y; i++)
{
map[i] = new Tile[y];
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
map[i][j].ground = false;
map[i][j].forest = false;
map[i][j].swamp = false;
map[i][j].mine = false;
map[i][j].store = false;
map[i][j].exit = false;
map[i][j].player = false;
}
}
// 값을 대입
int rand_Tile;
for (int i = 0; i <= x; i++)
{
for (int j = 0; j <= y; j++)
{
rand_Tile = rand() % 3;
if (rand_Tile == 0)
{
map[i][j].ground = true;
map[i][j].forest = false;
map[i][j].swamp = false;
map[i][j].mine = false;
map[i][j].store = false;
map[i][j].exit = false;
map[i][j].player = false;
}
else if (rand_Tile == 1)
{
map[i][j].ground = false;
map[i][j].forest = true;
map[i][j].swamp = false;
map[i][j].mine = false;
map[i][j].store = false;
map[i][j].exit = false;
map[i][j].player = false;
}
else if (rand_Tile == 2)
{
map[i][j].ground = false;
map[i][j].forest = false;
map[i][j].swamp = true;
map[i][j].mine = false;
map[i][j].store = false;
map[i][j].exit = false;
map[i][j].player = false;
}
}
}
srand(time(NULL));
int player_X = 0, player_Y = 0;
int mine_X = rand() % x;
int mine_Y = rand() % y;
for (int i = 0; i < x-3; i++)
{
while (map[mine_X][mine_Y].mine == true)
{
mine_X = rand() % x;
mine_Y = rand() % y;
}
map[mine_X][mine_Y].mine = true;
}
map[player_X][player_Y].player = true;
int store_X = rand() % x;
int store_Y = rand() % y;
map[store_X][store_Y].store = true;
int exit_X = rand() % x;
int exit_Y = rand() % y;
map[exit_X][exit_Y].exit = true;
int gold_get;
while (1)
{
gold_get = rand() % x + 1;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map[i][j].forest == true)
{
if (map[i][j].player == true)
{
cout << "옷";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "♣";
}
}
else if (map[i][j].ground == true)
{
if (map[i][j].player == true)
{
cout << "옷";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "□";
}
}
else if (map[i][j].swamp == true)
{
if (map[i][j].player == true)
{
cout << "옷";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "▦";
}
}
}
cout << endl;
}
cout << "이름 : " << me.name << endl;
cout << "보유 골드 : " << me.gold << endl;
cout << "스테미너 : " << me.stamina+(x*y) << endl;
if (map[player_X][player_Y].forest == true)
{
cout << "숲타일에 있습니다." << endl;
}
else if (map[player_X][player_Y].swamp == true)
{
cout << "늪타일에 있습니다." << endl;
}
else if (map[player_X][player_Y].ground == true)
{
cout << "땅타일에 있습니다." << endl;
}
char key = _getch();
if ((int)key == 87 || (int)key == 119)
{
if (player_X - 1 >= 0)
{
map[player_X][player_Y].player = false;
player_X -= 1;
map[player_X][player_Y].player = true;
if (map[player_X][player_Y].forest == true)
{
me.stamina -= 2;
}
else if (map[player_X][player_Y].swamp == true)
{
me.stamina -= 3;
}
else if (map[player_X][player_Y].ground == true)
{
me.stamina -= 1;
}
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map[i][j].forest == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "♣";
}
}
else if (map[i][j].ground == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "□";
}
}
else if (map[i][j].swamp == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "▦";
}
}
}
cout << endl;
}
}
}
if ((int)key == 83 || (int)key == 115)
{
if (player_X + 1 < x)
{
map[player_X][player_Y].player = false;
player_X += 1;
map[player_X][player_Y].player = true;
if (map[player_X][player_Y].forest == true)
{
me.stamina -= 2;
}
else if (map[player_X][player_Y].swamp == true)
{
me.stamina -= 3;
}
else if (map[player_X][player_Y].ground == true)
{
me.stamina -= 1;
}
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map[i][j].forest == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "♣";
}
}
else if (map[i][j].ground == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "□";
}
}
else if (map[i][j].swamp == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "▦";
}
}
}
cout << endl;
}
}
}
if ((int)key == 65 || (int)key == 97)
{
if (player_Y - 1 >= 0)
{
map[player_X][player_Y].player = false;
player_Y -= 1;
map[player_X][player_Y].player = true;
if (map[player_X][player_Y].forest == true)
{
me.stamina -= 2;
}
else if (map[player_X][player_Y].swamp == true)
{
me.stamina -= 3;
}
else if (map[player_X][player_Y].ground == true)
{
me.stamina -= 1;
}
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map[i][j].forest == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "♣";
}
}
else if (map[i][j].ground == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "□";
}
}
else if (map[i][j].swamp == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "▦";
}
}
}
cout << endl;
}
}
}
if ((int)key == 68 || (int)key == 100)
{
if (player_Y + 1 < y)
{
map[player_X][player_Y].player = false;
player_Y += 1;
map[player_X][player_Y].player = true;
if (map[player_X][player_Y].forest == true)
{
me.stamina -= 2;
}
else if (map[player_X][player_Y].swamp == true)
{
me.stamina -= 3;
}
else if (map[player_X][player_Y].ground == true)
{
me.stamina -= 1;
}
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map[i][j].forest == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "♣";
}
}
else if (map[i][j].ground == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "□";
}
}
else if (map[i][j].swamp == true)
{
if (map[i][j].player == true)
{
cout << "옥";
}
else if (map[i][j].mine == true)
{
cout << "ⓖ";
}
else if (map[i][j].exit == true)
{
cout << "X";
}
else if (map[i][j].store == true)
{
cout << "S";
}
else
{
cout << "▦";
}
}
}
cout << endl;
}
}
}
if (map[player_X][player_Y].mine == true)
{
map[player_X][player_Y].mine = false;
if (gold_get == 1 || gold_get == 3 || gold_get == 5)
{
cout << "50골드를 획득하였습니다." << endl;
me.gold += 50;
}
else
{
cout << "골드가 아니었습니다." << endl;
}
}
else if (map[player_X][player_Y].exit == true)
{
system("cls");
cout << endl;
cout << "탈출에 성공하였습니다.";
break;
}
if (map[player_X][player_Y].store == true)
{
if (me.gold <= 50)
{
cout << "보유 골드가 부족합니다." << endl;
}
else if (me.gold >= 100)
{
cout << "100골드를 사용하여 스테미너를 회복합니다." << endl;
me.stamina += 100;
if (me.stamina > 100)
{
me.stamina = 100;
}
me.gold -= 100;
}
}
if (me.stamina <= 0)
{
system("cls");
cout << endl;
cout << "탈출에 실패하였습니다.";
break;
}
system("cls");
}
for (int i = 0; i < x; i++)
{
delete[] map[i];
}
delete[] map;
}
굉장히 장문 코드라서 죄송합니다..
열심히 배워서 만든 코드인데 이걸 함수를 사용해서 최대한 줄여보라고 하는데.. 전혀 1도 감이 오지 않아서 질문드립니다.
단순 사칙연산 함수는 MAIN함수 바깥에서 선언한 뒤에 본문에 적용하는 법을 알겠는데..
지금 제가 짠 코드에서는 어떤 방식으로 함수선언을 이용해서 최대한 줄여야할지 모르겠습니다.
꼭 답이 아니더라도 힌트라도 주시면 너무너무 감사하겠습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
먼저 함수라는 단어를 이해 하면 쉽게 접근할 수 있을 것 같습니다. 함수란 영어로 function 인데 이 단어의 뜻은 기능이죠. 즉, 함수를 작성한다는 기능을 구현한다 라고 생각 하시면 될 것 같습니다. 예를 들어, 사칙 연산을 생각해 보죠
//여기에 코드를 입력하세요 int sum (int a, int b) { return a+b; } int main() { int a = 10; int b = 20; int result; result = sum(a, b); printf("결과는 %d\n", result); }
이런 코드가 있다고 가정했을 때 sum() 이라는 함수는 사칙 연산 중에 덧셈 "기능"을 하는 함수가 됩니다. 그럼 나머지 뺄셈, 곱셈, 나눗셈 들도 각각의 함수로 표현할 수 있겠지요.
그럼 위의 코드를 보면
//여기에 코드를 입력하세요 struct player { string name; int gold; int stamina; }; player me; me.name = "동호"; me.gold = 50; me.stamina = 20;
이 부분을 보면 어떤 역할을 하는 것 같나요? player라는 구조체를 선언 했고 안의 값에 초기 값을 넣은 것 같은데 맞나요? 맞다면 이 부분의 코드를 빼서 player를 초기화 하는 함수로 작성 할 수 있을 겁니다. 대충 만들어 본다면
//여기에 코드를 입력하세요 void player_init(void) { struct player { string name; int gold; int stamina; }; player me; me.name = "동호"; me.gold = 50; me.stamina = 20; } int main() { player_init(); ... }
이렇게 할 수 있을 겁니다. 그런데 이 것은 의도와 맞지 않죠. 왜냐하면 구조체를 만든 이유는 어딘가에서 값을 불러와 사용하기 위함 인데 함수 내에서 선언을 해버리면 다른 곳에서는 사용할 수 없으니까요. 구조체 선언과 메모리 할당 및 사용 방법은 스스로 찾으시길 바랄께요..
결론은 함수는 function == 기능, 메인 함수의 코드들을 기능 별로 분리하여 작성해서 메인 함수를 줄여보라는 뜻 입니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력