게임프로그래밍/게임 프로그래머 입문 올인원

[게임 프로그래머 입문 올인원] 문자열, 참조, 로또 번호 생성기, 달팽이 문제, 블루프린트 실습 - 3

shine94 2025. 1. 29. 07:04

* 해당 글은 게임 프로그래머 입문 올인원 강의를 보고 정리한 글입니다.

   강의_바로가기

 

[게임 프로그래머 입문 올인원] C++ & 자료구조/알고리즘 & STL & 게임 수학 & Windows API & 게임 서버

Rookiss | 어디부터 시작할지 막막한 게임 프로그래밍 입문자를 위한 All-In-One 커리큘럼입니다. C++, 자료구조/알고리즘, STL, 게임 수학, Windows API, 게임 서버 입문으로 이어지는 알찬 커리큘럼으로

www.inflearn.com

 

 

 

 

* 옛날 C언어 스타일 문자열

   널 터미널 스트링(Null-terminated string)

   문자를 포함하는 배열로 저장되고 널 문자로 끝나는 문자열을 말한다

 

#include <iostream>
using namespace std;

int main()
{
	char str1[] = { 'H', 'e', 'l', 'l', '0', 0 };
	char str2[] = "Hello";
	const char* str3 = "Hello";

	cout << str1 << endl;
	cout << str2 << endl;

	return 0;
}

 

"Hello" 문자열 리터럴은 Read Only Data 영역

str1, str2, str3은 스택 영역

 

* str[i] == *(str + i)

 

* 널문자

   '\0', 0

 

* 참조

   내부는 포인터, 사용은 일반적인 선언처럼

   누군가에게 닉네임이기 때문에 nullptr로 둘 수 없다

   [주로 많이 쓰는 상황]

   const StatInfo& player

   포인터로 사용하면 은근 null pointer exception이 많이 나기 때문에 이 부분을 애초에 방지하기 위함

   [그럼에도 수정하는 참조로 만들고 싶다면]

   #define OUT

   OUT StatInfo& player

   실제로 컴파일되면 없어지는데 굳이 #define OUT 해서 표시하는건 수정하는 참조라는 것을 표시하기 위함

 

#include <iostream>
using namespace std;

struct StatInfo
{
	int hp;
	int attack;
	int defence;
};

// 1. 값 복사 방식
void PrintByCopy(StatInfo player)
{
	cout << "-----------------------------" << endl;
	cout << "HP : " << player.hp << endl;
	cout << "ATT : " << player.attack << endl;
	cout << "DEF : " << player.defence << endl;
	cout << "-----------------------------" << endl;
}

// 2. 주소 전달 방식
void PrintByPointer(StatInfo* player)
{
	cout << "-----------------------------" << endl;
	cout << "HP : " << player->hp << endl;
	cout << "ATT : " << player->attack << endl;
	cout << "DEF : " << (*player).defence << endl;
	cout << "-----------------------------" << endl;
}

// 3. 참조 전달 방식
void PrintByRef(StatInfo& player)
{
	cout << "-----------------------------" << endl;
	cout << "HP : " << player.hp << endl;
	cout << "ATT : " << player.attack << endl;
	cout << "DEF : " << player.defence << endl;
	cout << "-----------------------------" << endl;
}

int main()
{
	StatInfo player = { 100,10,1 };

	PrintByCopy(player);
	PrintByPointer(&player);
	PrintByRef(player);

	return 0;
}

 

* 달팽이 문제(내 코드)

#include <iostream>
#include <iomanip>
using namespace std;

const int MAX = 100;
int board[MAX][MAX];
int N;

void PrintBord()
{
	for (int y = 0; y < N; y++)
	{
		for (int x = 0; x < N; x++)
		{
			cout << setfill('0') << setw(2) << board[y][x] << " ";
		}
		cout << endl;
	}
}

int main(void)
{
	cin >> N;

	int startY = 0;
	int endY = N;

	int startX = 0;
	int endX = N;

	int x, y, count = 1;

	for (int i = 0; i < N; i++)
	{
		y = startY;
		for (x = startX; x < endX; x++)
		{
			board[y][x] = count++;
		}

		x = endX - 1;
		for (y = startY + 1; y < endY; y++)
		{
			board[y][x] = count++;
		}
		
		y = endY - 1;
		for (x = endX - 2; x >= startX; x--)
		{
			board[y][x] = count++;
		}

		x = startX;
		for (y = endY - 2; y >= startY + 1; y--)
		{
			board[y][x] = count++;
		}

		startX += 1; startY += 1;
		endX -= 1; endY -= 1;
	}

	PrintBord();
	
	return 0;
}

 

* 다른 방법

1. enum과 switch

enum DIR
{
	RIGHT = 0,
	DOWN = 1,
	LEFT = 2,
	UP = 3
};

 

2. 배열 이용

int dy[] = { 0, 1, 0, -1 };
int dx[] = { 1, 0, -1, 0 };

int nextY = y + dy[dir];
int nextX = x + dx[dir];

 

3. 방향을 switch문 말고 연산으로도 가능

dir = (dir + 1) % 4;

 

* 블루브린트

1. Swap 함수

 

2. 버블 정렬

 

* 블루프린트에서의 배열은 동적 배열