2026-03-29(토) map과struct

조범근·2026년 3월 28일

TIL

목록 보기
28/83

C++ Week 5

팀프로젝트 4일차

Study

팀프로젝트


Today I Learned


map과 struct

Overview

map<int, std::vector<struct> my_map;

my_map[1] = {{"description", hp , atk},
			{"description2", hp , atk},
            {"description3", hp , atk}
            };

이렇게 vector<struct>로 2중으로 만들 수 있다. 1을 랜덤으로 뽑고 거기서 또 가지를 나눠서 랜덤으로 뽑는게 가능. 생성자에서 초기화한다.




복수의 몬스터와 싸우기

Overview

 Monster* target = nullptr;
 for (auto monster : m)
 {
     if (monster->getHealth() > 0)
     {
         target = monster;
         break;
     }
 }
 // Check die monsters
 if (target == nullptr)
    break;

Player가 공격하는 target을 어떻게 정할까 고민이였는데 해결책을 얻었다.
Monster* target = nullptr;을 반복문 시작에 선언하고 몬스터의 체력이 Health > 0 이라면 target으로 정한다.
만약 모든 몬스터들을 체크했는데 nullptr에서 바뀌지않았다면 반복문 탈출

0개의 댓글