Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

데블스캠프2010/다섯째날/ObjectCraft/미션3/김상호: Difference between revisions

From ZeroWiki
imported>rlaace423
No edit summary
 
(Repair batch-0004 pages from live compare)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
#include<stdio.h>
= class 사용 전 =
#include<stdlib.h>
#include&lt;stdlib.h&gt;
#include<time.h>
#include&lt;time.h&gt;
#define NUM 4
#define NUM 4
typedef struct z{
typedef struct z{
int gong;
int gong;
int bang;
int bang;
int hp;
int hp;
int name;
int name;
}unit;
}unit;
 
void attack(unit a, unit &b)
void attack(unit a, unit &amp;b)
{
{
if(b.hp<=0 || a.hp<=0)
if(b.hp&lt;=0 || a.hp&lt;=0)
return;
return;
b.hp -=a.gong;
b.hp -=a.gong;
printf("저글링 %d이 저글링 %d에게 데미지 %d를 입혀 HP가 %d가 되었다.\n", a.name, b.name, a.gong, b.hp);
printf("저글링 %d이 저글링 %d에게 데미지 %d를 입혀 HP가 %d가 되었다.\n", a.name, b.name, a.gong, b.hp);
if(b.hp==0)
if(b.hp==0)
printf("저글링 %d이 전사했습니다.\n", b.name);
printf("저글링 %d이 전사했습니다.\n", b.name);
}
}
void init_unit(unit a[[NUM]]){
void init_unit(unit a&#91;NUM&#93;){
int n;
int n;
for(n=0 ; n<NUM ; n++){
for(n=0 ; n&lt;NUM ; n++){
a&#91;n&#93;.gong=5;
a&#91;n&#93;.gong=5;
a&#91;n&#93;.bang=0;
a&#91;n&#93;.bang=0;
a&#91;n&#93;.hp=50;
a&#91;n&#93;.hp=50;
a&#91;n&#93;.name=n;
a&#91;n&#93;.name=n;
}
}
}
int main()
{
unit a&#91;NUM&#93;;
init_unit(a);
int sel;
srand(time(NULL));
while(!((a&#91;0&#93;.hp&lt;=0 &amp;&amp; a&#91;1&#93;.hp&lt;=0) || (a&#91;2&#93;.hp&lt;=0 &amp;&amp; a&#91;3&#93;.hp&lt;=0))) //0과 1이 죽으면 끝or 2와 3이 죽으면 끝
{
sel=rand()%(NUM*2);
int attackerTeam = rand() % 2;
int attackerUnit = rand() % 2;
int defenderunit = rand() % 2;
attack(a&#91;attackerTeam * 2 + attackerUnit&#93;,a&#91;(!attackerTeam) * 2 + defenderunit&#93;);
}
if(a&#91;0&#93;.hp&lt;=0 &amp;&amp; a&#91;1&#93;.hp&lt;=0)
printf("뒷 팀이 이겼습니다!!\n");
if(a&#91;2&#93;.hp&lt;=0 &amp;&amp; a&#91;3&#93;.hp&lt;=0)
printf("앞 팀이 이겼습니다!!\n");
return 0;
}
}
int main()
= class 사용 후 =
{
== 메인함수 ==
unit a[[NUM]];
#include&lt;stdlib.h&gt;
init_unit(a);
#include&lt;time.h&gt;
int sel;
#include"unit.h"
 
 
#define NUM 4
srand(time(NULL));
while(!((a&#91;0&#93;.hp<=0 && a&#91;1&#93;.hp<=0) || (a&#91;2&#93;.hp<=0 && a&#91;3&#93;.hp<=0))) //0과 1이 죽으면 끝or 2와 3이 죽으면 끝
int main()
{
{
 
unit a&#91;NUM&#93;;
sel=rand()%(NUM*2);
for(int i=0 ; i&lt;NUM ; i++)
int attackerTeam = rand() % 2;
a&#91;i&#93;.init_unit(i);
int attackerUnit = rand() % 2;
int sel;
int defenderunit = rand() % 2;
attack(a[[attackerTeam * 2 + attackerUnit]],a[[(!attackerTeam) * 2 + defenderunit]]);
srand(time(NULL));
}
while(!((a&#91;0&#93;.is_dead() &amp;&amp; a&#91;1&#93;.is_dead()) || (a&#91;2&#93;.is_dead() &amp;&amp; a&#91;3&#93;.is_dead()))) //0과 1이 죽으면 끝or 2와 3이 죽으면 끝
if(a&#91;0&#93;.hp<=0 && a&#91;1&#93;.hp<=0)
{
printf("뒷 팀이 이겼습니다!!\n");
if(a&#91;2&#93;.hp<=0 && a&#91;3&#93;.hp<=0)
sel=rand()%(NUM*2);
printf("앞 팀이 이겼습니다!!\n");
int attackerTeam = rand() % 2;
 
int attackerUnit = rand() % 2;
return 0;
int defenderunit = rand() % 2;
a&#91;attackerTeam * 2 + attackerUnit&#93;.attack(a&#91;(!attackerTeam) * 2 + defenderunit&#93;);
}
if(a&#91;0&#93;.is_dead() &amp;&amp; a&#91;1&#93;.is_dead())
printf("뒷 팀이 이겼습니다!!\n");
if(a&#91;2&#93;.is_dead() &amp;&amp; a&#91;3&#93;.is_dead())
printf("앞 팀이 이겼습니다!!\n");
return 0;
}
}
 
== unit.cpp ==
#include"unit.h"
#include&lt;stdio.h&gt;
void unit::attack(unit &amp;b)
{
if(b.hp&lt;=0 || this-&gt;hp&lt;=0)
return;
b.hp -=this-&gt;gong;
printf("저글링 %d이 저글링 %d에게 데미지 %d를 입혀 HP가 %d가 되었다.\n", this-&gt;name, b.name, this-&gt;gong, b.hp);
if(b.hp==0)
printf("저글링 %d이 전사했습니다.\n", b.name);
}
void unit::init_unit(int n){
this-&gt;gong=5;
this-&gt;bang=0;
this-&gt;hp=50;
this-&gt;name=n;
}
bool unit::is_dead()
{
if(this-&gt;hp&lt;=0)
return true;
else
return false;
}
== unit.h ==
class unit{
int gong;
int bang;
int hp;
int name;
public:
void attack(unit &amp;b);
void init_unit(int n);
bool is_dead();
};

Latest revision as of 00:37, 27 March 2026

class 사용 전

#include<stdlib.h>
#include<time.h>
#define NUM 4
typedef struct z{
	int gong;
	int bang;
	int hp;
	int name;
}unit;

void attack(unit a, unit &b)
{
	if(b.hp<=0 || a.hp<=0)
		return;
	b.hp -=a.gong;
	printf("저글링 %d이 저글링 %d에게 데미지 %d를 입혀 HP가 %d가 되었다.\n", a.name, b.name, a.gong, b.hp);
	if(b.hp==0)
		printf("저글링 %d이 전사했습니다.\n", b.name);
}	
void init_unit(unit a[NUM]){
	
	int n;
	for(n=0 ; n<NUM ; n++){
		a[n].gong=5;
		a[n].bang=0;
		a[n].hp=50;
		a[n].name=n;
	}
}
int main()
{	
	unit a[NUM];
	init_unit(a);
	int sel;


	srand(time(NULL));
	while(!((a[0].hp<=0 && a[1].hp<=0) || (a[2].hp<=0 && a[3].hp<=0))) //0과 1이 죽으면 끝or 2와 3이 죽으면 끝
	{

		sel=rand()%(NUM*2);
		int attackerTeam = rand() % 2;
		int attackerUnit = rand() % 2;
		int defenderunit = rand() % 2;
	
		attack(a[attackerTeam * 2 + attackerUnit],a[(!attackerTeam) * 2 + defenderunit]);
	}
	if(a[0].hp<=0 && a[1].hp<=0)
		printf("뒷 팀이 이겼습니다!!\n");
	if(a[2].hp<=0 && a[3].hp<=0)
		printf("앞 팀이 이겼습니다!!\n");

	return 0;

}

class 사용 후

메인함수

#include<stdlib.h>
#include<time.h>
#include"unit.h"

#define NUM 4

int main()
{	
	unit a[NUM];
	for(int i=0 ; i<NUM ; i++)
		a[i].init_unit(i);
	int sel;


	srand(time(NULL));
	while(!((a[0].is_dead() && a[1].is_dead()) || (a[2].is_dead() && a[3].is_dead()))) //0과 1이 죽으면 끝or 2와 3이 죽으면 끝
	{

		sel=rand()%(NUM*2);
		int attackerTeam = rand() % 2;
		int attackerUnit = rand() % 2;
		int defenderunit = rand() % 2;
	
		a[attackerTeam * 2 + attackerUnit].attack(a[(!attackerTeam) * 2 + defenderunit]);
	}
	if(a[0].is_dead() && a[1].is_dead())
		printf("뒷 팀이 이겼습니다!!\n");
	if(a[2].is_dead() && a[3].is_dead())
		printf("앞 팀이 이겼습니다!!\n");

	return 0;

}

unit.cpp

#include"unit.h"
#include<stdio.h>

void unit::attack(unit &b)
{
	if(b.hp<=0 || this->hp<=0)
		return;
	b.hp -=this->gong;
	printf("저글링 %d이 저글링 %d에게 데미지 %d를 입혀 HP가 %d가 되었다.\n", this->name, b.name, this->gong, b.hp);
	if(b.hp==0)
		printf("저글링 %d이 전사했습니다.\n", b.name);
}	
void unit::init_unit(int n){


	this->gong=5;
	this->bang=0;
	this->hp=50;
	this->name=n;

}
bool unit::is_dead()
{
	if(this->hp<=0)
		return true;
	else
		return false;

}

unit.h

class unit{
	int gong;
	int bang;
	int hp;
	int name;
public:
	void attack(unit &b);
	void init_unit(int n);
	bool is_dead();
};