겨울팥죽 여름빙수
[알고리즘] 이진트리 만들기
게임을 만들자/C++ 2021. 5. 22. 15:22

프로그래머스 문제를 풀다보면, 간혹 이진트리를 만들어 풀어야하는 경우가 있다. 깊이우선 탐색, 너비우선 탐색, 힙 정렬 등이 있다. (힙 같은 경우 배열로 할 수 있다.) 갑자기 이진트리를 만드려고 하니, 순간 머리가 멍해지는 느낌이 있어서, 아무래도 한번쯤 정리해야겠다는 생각을 하게 됐다. struct Node{ int value = 0; Node* left = nullptr; Node* right = nullptr; Node* parent = nullptr; }; 우선 노드는 위와 같다. 왼쪽, 오른쪽 그리고 부모를 가르키는 포인터를 두었다.(사실 부모를 가르키는 것은 안둬도 된다.) void addNode(queue& add_queue, int value) { //큐에서 노드를 꺼낸다.(제거하지 않..

c++11 min, max 범위, 난수 생성
게임을 만들자/C++ 2020. 4. 7. 10:29

기존의 rand함수와 %연산으로 범쉬를 설정해 난수를 설정 했었으나, c++11에 관련 기능이 있으니 써보기로 했다. #include #include int Util::Random(int min, int includ_max) { random_device rn; mt19937_64 rnd( rn() ); uniform_int_distribution range(min, includ_max); return range( rnd ); } uniform_int_distribution를 통해 min,max 범위를 설정하고, 값을 리턴하면 끝이다. 간단하다. 여기서 유의해야 할 점은 min, max값이 제외가 아니라 포함이다. int r = Util::Random(-5,10) //r [-5 ~ 10] 사이의 정수 반환..

c++ string tokenizer
게임을 만들자/C++ 2015. 1. 9. 22:08

1. ImplementThis code use string class instead of strtok included in . void StringTokenize(const string& str, vector& tokens, const string& delimiters) { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != last..

article thumbnail
cJSON parsing error using window utf-8 txt file, Remove UTF-8 BOM
게임을 만들자/C++ 2015. 1. 8. 12:28

Sometime we get error from cJSON. Window utf-8 txt file attach big-endian information [EF BB BF]. It cause error when we parse json or use setcookie in PHP. I removed this information using Notepad++.Download Link : http://notepad-plus-plus.org/ 1. Save txt file to UTF-8 File->Save to other -> Set encoding to UTF-8 2. Using Notepad ++ Open txt file in Notepad++. Select UTF-8 without BOM

article thumbnail
c++ Easing code
게임을 만들자/C++ 2014. 10. 16. 01:44

1. Easing Code Many games and App use Easing Algorithm like scrolling, character moving, etc. Blow show c++ code of Easing Algorithm. start_value : 0 frame's value.change_value : End frame's value - start_value inline float easeLinear(float current_frame, float start_value, float end_frame, float change_value) { return change_value*current_frame/end_frame + start_value; } inline float easeQuadra..

c++ Builder 패턴
게임을 만들자/C++ 2014. 4. 16. 10:54

1. 다수의 멤버변수 보통 UI클래스나, 많은 정보를 가진 데이터 클래스(java의 bean객체 같은)의 경우, 많은 멤버 변수를 가진다. 이런 멤버 변수들을 생성자 파라미터로 초기화하면, 코드가 많이 복잡해 보일 수 있다. 예를 들어, 아래 클래스 처럼 new를 통해 파라미터로 값을 초기화하는 경우, 가독성이 많이 떨어진다(각각의 파라미터가 어떤 의미인지 알기 어렵다). 이런 문제를 해결하기 위해 get, set함수를 만들고 사용하기도 하는데 별로 만족 스럽지 못하다. class Node { protected: vec2m_position; vec2m_draw_position; floatm_width; floatm_height; AlignTypem_child_align_type; Marginm_margi..

c++ const
게임을 만들자/C++ 2014. 4. 15. 13:41

1. 변수 const : 변수 값을 변경하지 못하게 한다.const int n = 5;n = 10 // 에러 1-2 const int& n2 = n; : n2가 참조하는 곳의 값을 바꿀 수 없다.int n = 5;const int& n2 = n;n2 = 10; //에러n = 10 ; //가능. 2. 포인터 변수 const : 포인터가 가르키는 곳의 값을 변경하지 못하게 한다.int n = 5; const int* ap = &n; (int const* ap도 똑같다.)*ap = 10; //에러 int n2 = 10;ap = &n2; //가능함. 포인터 변경은 가능 int * ap2 = ap; //에러. 포인터가 가르키는 곳을 다른 포인터가 값을 변경할 수도 있기 때문에, 허용하지 않는다.const int ..