You are on page 1of 16

Assignments Day 02

Objective:
Problem Description:

Assignment 1:
Write classes to represent 3 different types of Bee - Worker, Queen and Drone.
Each Bee has a floating-point health property, which is not writable externally and upon creation
is set to a value of 100 (percent).
Each Bee has a Damage() method that takes a single integer parameter that should be a value
between 0 and 100. When this method is called, the health of the bee is to be reduced by that
percentage of their current health.
When a Worker has a health below 70% it cannot fly and therefore is pronounced Dead. When a
Queen has a health below 20%, or a Drone below 50%, it is pronounced dead. This 'Dead'
property should be readable from each Bee. When a Bee is dead, no further health deductions
should be recorded by the bee, although the Damage() method should still be invokable without
error.
Your application should create a single list containing 10 instances of each type of Bee and store
in a list or array. It must support methods to allow Damage() to be called for each Bee, and to
return the health status of each bee, including whether it is alive or not.
Your application interface must contains 2 functions ( user press 1 or 2 to activate this
function :
1 Create bee list Clear current bee list and create new random bees, then display in the
console windows
2 Attack bees - Attack current bee list , a different random value between 0 and 80 should be
selected for each bee and applied with a call to Damage(). After attacked, the user interface
should refresh to show the health status of the bees in console windows
3 - A Worker and Drone can attack other bees, except Queen. a random value between 0 and 30
for Worker and 10 and 40 for Drone to attack a random bee. when they attack other bees, their
health to be reduced 50% attack point.
4 - Each Bee has a integer defense property (a random value between 10 and 20). Added to each
bee defense function, when a bee is attacked, it will call to Defense() to reduce amount of
damage caused by other bees.
Ong th c kh nng i ly phn hoa v xy t. Hoa c 2 thuc tnh c bn l tn v s lng
phn, ng thi phn ca mt s loi hoa c c t. Tt c cc ong th trong n u c chc
nng ly phn hoa.

1. To ra danh sch 10 loi hoa, mi loi c 10 bng hoa. S lng phn c gi tr mc nh


2.
3.

4.
5.

6.

l 50v.
To ra mt t ong (Hive) cha danh sch cc con ong ( thc hin phn trc). T
ong s c thm thuc tnh Mt ong v bn.
Khi gi chc nng ly phn, tt c cc ong th trong t s i tm v ly phn hoa. Mi
con ong ch c th ly c 10v. Khi hoa khng cn phn th hoa s cht. Nhng nu
ng ht phi phn ca loi hoa c c, ong s cht.
Sau khi ly mt hoa, Ong th s mang v t, t 10v thu c, s chuyn thnh mt ong
vi t l tht bi t 30-50%, v chuyn 50% s lng mt thnh cng thnh bn ca t.
Ong th v ong c c kh nng tn cng cc t ong khc. Sc tn cng ca cc con ong
ch tc ng 5% vo bn ca t ong. To ra mt t ong vi bn 100 v cho n
ong tn cng 1 t ong .
Thc hin chng trnh c th xem c cc thng tin v t ong ( bn, s lng mt
thu c) v thng tin ca n ong trong t (nhng con ong cn sng v nhng con ong
cht).

Other requirements:
-

Application type: Console

Estimated time: 3.5 hours

Assignment 2 Opt1 - BTNB

To Class Queen
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB
{
public sealed class Queen : Bee
{
public Queen() : base(3, "Queen", 20) { }
}
}

To Class Worker
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB
{
public sealed class Worker : Bee
{
public Worker() : base(1, "Worker", 70) { }
}
}

To Class Drone
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB
{
public sealed class Drone : Bee
{
public Drone() : base(2, "Drone", 50) { }
}
}

To Class Hive
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB
{
public class Hive
{
public int NumBee { get; set; }
public int Honey { get; set; }
public int durability { get; set; }
public int _Pollen { get; set; }
}
}

To Class Flower
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB

{
public class Flower
{
public virtual string FlowerName { get; set; }
public bool poison { get; set; }
public virtual int LowerType { get; set; }
public int pollen { get; set; }
public virtual bool Death
{
get
{
return (this.pollen == 0);
}
}
}
}

To Class Bee
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace Ass2_BTNB
{
public abstract class Bee
{
public virtual int Type { get; set; }
public virtual string Name { get; set; }
public virtual float Health { get; set; }
public virtual int _defense { get; set; }
protected int HealthLimit { get; set; }
public virtual bool Alive
{
get
{
return (this.Health > this.HealthLimit);
}
}
public virtual void Damage(int damage)
{
if (this.Alive && damage > 0 && damage < 100)
{
this.Health -= (damage - this._defense);
if (this.Health > 100)
this.Health = 100;
}
if (this.Health < 0)

{
this.Health = 0;
}
if (this.Alive == false)
{
this.Health = 0;
}
}
public virtual void WorkDroreDamage(int damage)
{
if (this.Alive && damage > 0 && damage < 100)
{
this.Health -= Math.Abs(Convert.ToInt32((damage * 0.5) - this._defense));
this.Health -= (damage - this._defense);
if (this.Health > 100)
this.Health = 100;
}
if (this.Health < 0)
{
this.Health = 0;
}
if (this.Alive == false)
{
this.Health = 0;
}
}
public virtual void Defense(int def)
{
if (this.Alive == true)
{
this._defense = def;
}
if (this.Health < 0)
{
this.Health = 0;
}
if (this.Alive == false)
{
this.Health = 0;
}
}
protected Bee(int type, string name, int healthLimit)
{
this.Name = name;
this.Type = type;
this.Health = 100;
this.HealthLimit = healthLimit;
}
}

Chng trnh Main


using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ass2_BTNB
{
class Program
{
private static void PrintBee(int number, Bee bee)
{
Console.WriteLine("{0}: {1}\talive = {2}\thealth = {3} \tdefense = {4}",
number, bee.Name, bee.Alive, bee.Health, bee._defense);
}
private static void PrintHive(Hive hive)
{
Console.WriteLine("Hive \t Durability: {0} \t Honey: {1} \n",
hive.durability, hive.Honey);
}
private static void PrintFlower(Flower lw)
{
Console.WriteLine("Fower Name: {0}:\t \tDeath: {1} \t Pollen = {2} \t Poison:
{3}", lw.FlowerName, lw.Death, lw.pollen,lw.poison);
}
private static void CreateNewBees(List<Bee> list, int num)
{
list.Clear();
Random rd; int numBee;
for (int i = 0; i < num; i++)
{
rd = new Random();
numBee = rd.Next(1,4);
switch(numBee)
{
case 1: list.Add(new Worker()); break;
case 2: list.Add(new Drone()); break;
case 3: list.Add(new Queen()); break;
}
}
}
private static void CreateHive(List<Hive> list ,int NBee)
{
list.Clear();
Hive hive = new Hive();
hive.NumBee = NBee;
hive.Honey = 0;
hive.durability = 100;
hive._Pollen = 0;
list.Add(hive);
}
public static void CreateFlower(List<Flower> list, int Ftype, int NumF)
{
list.Clear();
Flower lw;
Random rd;
int nname = 0; int n;

for (int i = 0; i < Ftype; i++)


{
for (int j = 0; j < NumF; j++)
{
lw = new Flower(); rd = new Random(); n = rd.Next(0, 2);

lw.FlowerName = "Flower " + nname.ToString();


lw.LowerType = i;
if (n == 0)
lw.poison = false;
else
lw.poison = true;
lw.pollen = 50;
list.Add(lw);
nname++;

}
public static void BuilHive(Hive hive, int rnd, Bee bee)
{
if (hive.durability > 100)
hive.durability = 100;
else
if (hive.durability < 0)
hive.durability = 0;
else
if (bee.Health == 0)
{
hive.durability += 0;
hive.Honey += 0;
}
else
{
hive.durability += Convert.ToInt32((10 - Convert.ToInt32(10 *
(rnd * 0.01))) * 0.5);
if (hive.durability > 100)
hive.durability = 100;
hive.Honey = (hive._Pollen - (Convert.ToInt32(hive._Pollen * (rnd
* 0.1)))) - Convert.ToInt32((hive._Pollen - (Convert.ToInt32(hive._Pollen * (rnd *
0.1)))) * 0.5);
}
}
private static void GetPoisen(Hive hive, Bee bee, Flower flower)
{
if (bee.Health == 0)
{
hive._Pollen += 0;
flower.pollen -= 0;
}
else
if (flower.pollen == 0)
{
hive._Pollen += 0;

}
else
if (flower.poison == true)
{
bee.Health = 0;
hive._Pollen += 0;
flower.pollen -= 0;
}
else
{
flower.pollen -= 10;
hive._Pollen += 10;
}
}
private static void AttactHive( Hive hibe, int dame)
{
hibe.durability -= Math.Abs(Convert.ToInt32(0.05 * dame));
}
public static void Main(string[] args)
{
List<Bee> list = new List<Bee>();
List<Hive> lhive = new List<Hive>();
List<Flower> lflower = new List<Flower>();
bool exit = false;
while (!exit)
{
Console.Write("\n1. Create new list Bee \n" +
"2. Attact Bees \n" +
"3. A Worker and Drone attact other Bees \n" +
"4. Bees defense\n"+
"5. Create Flower have 10 type and 100 flower\n"+
"6. Create Hive 'have 10 bees'\n"+
"7. Get Pollen\n"+
" Press number keys 0 to Exit ! \n"
);
Console.Write("Choose: ");
int choose = Int32.Parse(Console.ReadLine());
switch (choose)
{
case 1:
CreateNewBees(list, 10);
for (int i = 0; i < list.Count; i++)
{
PrintBee(i + 1, list[i]);
}
CreateHive(lhive, 10);
break;
case 2:
var random = new Random();
for (int i = 0; i < list.Count; i++)
{
var damage = random.Next(0, 80);

list[i].Damage(damage);
PrintBee(i + 1, list[i]);
AttactHive(lhive[0], damage);
}
break;
case 3: var rd = new Random(); int dame;
for (int i = 0; i < list.Count; i++)
{
rd = new Random();
if (list[i].Type == 1)
{
dame = rd.Next(0, 30);
list[i].WorkDroreDamage(dame);
PrintBee(i + 1, list[i]);
AttactHive(lhive[0],dame);
}
if (list[i].Type == 2)
{
dame = rd.Next(0, 40);
list[i].WorkDroreDamage(dame);
PrintBee(i + 1, list[i]);
AttactHive(lhive[0], dame);
}
if (list[i].Type == 3)
{
dame = 0;
list[i].WorkDroreDamage(dame);
PrintBee(i + 1, list[i]);
}

AttactHive(lhive[0], dame);

}
break;
case 4: var rnd = new Random(); int def;
for (int i = 0; i < list.Count; i++)
{
rnd = new Random();
def = rnd.Next(10, 20);
list[i].Defense(def);
}
break;
case 5:
CreateFlower(lflower, 10, 10);
for (int i = 0; i < lflower.Count; i++)
{
PrintFlower(lflower[i]);
}
break;
case 6:
CreateNewBees(list, 10);
for (int i = 0; i < list.Count; i++)
{
PrintBee(i + 1, list[i]);
}
CreateHive(lhive, 10);

PrintHive(lhive[0]);
break;
case 7:
for (int i = 0; i < list.Count ; i++)
{
rd = new Random();
GetPoisen(lhive[0], list[i], lflower[rd.Next(0, 100)]);
rd = new Random();
BuilHive(lhive[0], rd.Next(3, 6),list[i]);
}
foreach (Bee bee in list)
Console.WriteLine(" {0}\talive = {1}\thealth = {2}
\tdefense = {3} ", bee.Name, bee.Alive, bee.Health, bee._defense);
foreach (Hive hive in lhive)
Console.WriteLine("Hive \t Durability: {0} \t Honey:
{1} \t Pollen: {2} \n", hive.durability, hive.Honey, hive._Pollen);
foreach(Flower lw in lflower)
Console.WriteLine("Fower Name: {0}:\t \tDeath: {1} \t
Pollen = {2} \t Poison: {3} ", lw.FlowerName, lw.Death, lw.pollen, lw.poison);
break;
default:
exit = true; break;
}
}
}
}
}

Kt qu sau khi chy chng trnh

Nhn phm 1 - Create new list Bee

Nhn phm 2 - Attact Bees

Nhn phm 3 - A Worker and Drone attact other Bees

Nhn phm 4 ri nhn phm 2 - Bees defense

Nhn phm 5 - Create Flower have 10 type and 100 flower

Nhn phm 6 ri nhn phm 2 - Create Hive have 10 bees

Nhn phm 7 - Get Pollen

You might also like