구조체
이것도 게임 만들 때 필수인 문법인 것 같다
#include <iostream>
using namespace std;
struct tagHero
{
const char* name; //영웅이름
int hp; //영웅체력
int atk; //영웅공격력
float criPer; //치확
float criDam; //치피
bool isDead; //사망여부
};
void main()
{
tagHero mario;
mario.name = "마리오";
mario.hp = 100;
mario.atk = 100;
mario.criPer = 10.0f;
mario.criDam = 1000;
mario.isDead = false;
cout << mario.name << "의 이름:" << mario.name << endl;
cout << mario.name << "의 체력:" << mario.hp << endl;
cout << mario.name << "의 공격력:" << mario.atk << endl;
cout << mario.name << "의 치확:" << mario.criPer << endl;
cout << mario.name << "의 치피:" << mario.criDam << endl;
cout << mario.name << "의 생존여부:" << mario.isDead << endl;
tagHero jim;
jim.name = "짐 레이너";
jim.hp = 200;
jim.atk = 10;
jim.criPer = 5;
jim.criDam = 500;
jim.isDead = false;
while (jim.hp > 0 || mario.hp > 0)
{
jim.hp -= mario.atk;
mario.hp -= jim.atk;
}
}
배열
이후에 만들어볼 카드게임에 꼭 필요한 문법
#include <iostream>
using namespace std;
void main()
{
//배열
//같은 변수의 연속적인 나열
int bullet[5];
for (int i = 0; i < 5; i++)
{
cout << bullet[i] << endl;
}
int bullet2[] = { 1, 2, 3, 4 };
int num[5] = {1,2,3,4};
for (int i = 0; i < 5; i++)
{
cout << num[i] << endl;
}
//이중배열
int number[3][3];
number[0][0];
number[0][1];
number[0][2];
number[1][0];
number[1][1];
number[1][2];
number[2][0];
number[2][1];
number[2][2];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
number[i][j];
}
}
}
셔플
#include <iostream>
#include <time.h>
using namespace std;
void main()
{
srand(time(NULL));
int number[45];
int dest, sour, temp;
for (int i = 0; i < 45; i++)
{
number[i] = i + 1;
}
//40번 섞을 예정
for (int i = 0; i < 40; i++)
{
dest = rand() % 45;
sour = rand() % 45;
temp = number[dest];
number[dest] = number[sour];
number[sour] = temp;
}
for (int i = 0; i < 45; i++)
{
cout << number[i] << endl;
}
}