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

데블스캠프2010/다섯째날/ObjectCraft/미션2/서민관: Difference between revisions

From ZeroWiki
imported>smksyj
No edit summary
 
(Repair batch-0004 pages from live compare)
 
(2 intermediate revisions by one other user not shown)
Line 6: Line 6:
  {
  {
  //zergling** z = NULL;
  //zergling** z = NULL;
  //z[0] = createZergling();
  //z[0] = createZergling();
  //z[1] = createZergling();
  //z[1] = createZergling();
   
   
  zergling* z1 = createZergling();
  zergling* z1 = createZergling(1);
  zergling* z2 = createZergling();
  zergling* z2 = createZergling(2);
   
   
  // attack(z1, z2);
  // fight(z1, z2);
fightwith(z1, z2);
   
   
  fight(z1, z2);
  return 0;
  }
  }
== zergling.cpp ==
== zergling.cpp ==
Line 23: Line 24:
  using namespace std;
  using namespace std;
   
   
  zergling* createZergling()
  zergling* createZergling(int num)
  {
  {
  zergling* z1 = (zergling*)malloc(sizeof(zergling));
  zergling* z1 = (zergling*)malloc(sizeof(zergling));
Line 29: Line 30:
  z1->def = 0;
  z1->def = 0;
  z1->HP = 50;
  z1->HP = 50;
  z1->number = 0;
  z1->number = num;
   
   
  return z1;
  return z1;
Line 37: Line 38:
  {
  {
  //sleep(1000);
  //sleep(1000);
cout << z1->number << "이 " << z2->number << "에게 데미지 " << z1->atk << "를 입혀 HP가 " << z2->HP << "가 되었다." << endl;
z2->HP -= z1->atk;
}
   
   
  for ( ; z2->HP != 0 ; )
void fight(zergling* z1, zergling* z2)
{
  while ( z1->HP != 0 && z2->HP != 0 )
  {
  {
  if ( z2->HP == 0 )
  attack(z1, z2);
{
  attack(z2, z1);
cout << "z2가 죽었습니다." << endl;
return;
}
cout << "z1이 z2에게 데미지 " << z1->atk << "를 입혀 HP가 " << z2->HP << "가 되었다." << endl;
  z2->HP -= z1->atk;
  }
  }
which_is_dead(z1, z2);
  }
  }
   
   
  void fight(zergling* z1, zergling* z2)
  void fightwith(zergling* z1, zergling* z2)
  {
  {
  while ( z1->HP != 0 && z2->HP != 0 )
  while ( z1->HP != 0 && z2->HP != 0 )
  {
  {
  cout << "z1이 z2에게 데미지 " << z1->atk << "를 입혀 HP가 " << z2->HP << "가 되었다." << endl;
  attack(z1, z2);
z2->HP -= z1->atk;
  attack(z2, z1);
  cout << "z2가 z1에게 데미지 " << z2->atk << "를 입혀 HP가 " << z1->HP << "가 되었다." << endl;
z1->HP -= z2->atk;
  }
  }
  }
  }
   
   
  void fight2(zergling** z)
  void new_fightwith(zergling* z1, zergling* z2)
  {
  {
  z[0]->number = 0;
  int which = rand() % 2;
z[1]->number = 1;
   
   
  while ( z[0]->HP != 0 && z[1]->HP != 0 )
  if ( which == 0 )
  {
  {
  cout << "저글링" << z[0]->number << "이 z2에게 데미지 " << z[0]->atk << "를 입혀 HP가 " << z[0]->HP << "가 되었다." << endl;
  fight(z1, z2);
z[1]->HP -= z[0]->atk;
cout << "저글링" << z[1]->number << "z2가 z1에게 데미지 " << z[1]->atk << "를 입혀 HP가 " << z[1]->HP << "가 되었다." << endl;
z[0]->HP -= z[1]->atk;
  }
  }
}
else
{
fight(z2, z1);
}
}
void which_is_dead(zergling* z1, zergling* z2)
{
if ( z1->HP == 0 )
{
cout << z1->number << "이 죽었습니다." << endl;
}
else
{
cout << z2->number << "가 죽었습니다." << endl;
}
}


== zergling.h ==
== zergling.h ==
Line 84: Line 96:
  } zergling;
  } zergling;
   
   
  zergling* createZergling();
  zergling* createZergling(int num);
  void attack(zergling* z1, zergling* z2);
  void attack(zergling* z1, zergling* z2);
  void fight(zergling* z1, zergling* z2);
  void fight(zergling* z1, zergling* z2);
  void fight2(zergling** z);
  void fightwith(zergling* z1, zergling* z2);
 
void new_fightwith(zergling* z1, zergling* z2);
void which_is_dead(zergling* z1, zergling* z2);

Latest revision as of 00:37, 27 March 2026

메인함수

#include <iostream>
#include "zergling.h"

int main(void)
{
	//zergling** z = NULL;
	//z[0] = createZergling();
	//z[1] = createZergling();

	zergling* z1 = createZergling(1);
	zergling* z2 = createZergling(2);

	// fight(z1, z2);
	fightwith(z1, z2);

	return 0;
}

zergling.cpp

#include <iostream>
#include "zergling.h"
#include <time.h>

using namespace std;

zergling* createZergling(int num)
{
	zergling* z1 = (zergling*)malloc(sizeof(zergling));
	z1->atk = 5;
	z1->def = 0;
	z1->HP = 50;
	z1->number = num;

	return z1;
}

void attack(zergling* z1, zergling* z2)
{
	//sleep(1000);
		cout << z1->number << "이 " << z2->number << "에게 데미지 " << z1->atk << "를 입혀 HP가 " << z2->HP << "가 되었다." << endl;
		z2->HP -= z1->atk;
}

void fight(zergling* z1, zergling* z2)
{
	while ( z1->HP != 0 && z2->HP != 0 )
	{
		attack(z1, z2);
		attack(z2, z1);
	}
	which_is_dead(z1, z2);
}

void fightwith(zergling* z1, zergling* z2)
{
	while ( z1->HP != 0 && z2->HP != 0 )
	{
		attack(z1, z2);
		attack(z2, z1);
	}
}

void new_fightwith(zergling* z1, zergling* z2)
{
	int which = rand() % 2;

	if ( which == 0 )
	{
		fight(z1, z2);
	}
	else
	{
		fight(z2, z1);
	}
}

void which_is_dead(zergling* z1, zergling* z2)
{
	if ( z1->HP == 0 )
	{
		cout << z1->number << "이 죽었습니다." << endl;
	}
	else
	{
		cout << z2->number << "가 죽었습니다." << endl;
	}
}

zergling.h

typedef struct zergling
{
	int atk;
	int def;
	int HP;
	int number;
} zergling;

zergling* createZergling(int num);
void attack(zergling* z1, zergling* z2);
void fight(zergling* z1, zergling* z2);
void fightwith(zergling* z1, zergling* z2);
void new_fightwith(zergling* z1, zergling* z2);
void which_is_dead(zergling* z1, zergling* z2);