Jasonhuangx
BAN USER4/25 Goodchap road
I am not sure what's the best solution for this. The below is my example.
public abstract class Monster
{
public int m_Health;// <=0 means die/disappear
public int m_Level;// 1-100
public float m_Defense;
public string m_Name;
public abstract int GetStatus();
public abstract int Hit(int nDamage);
}
public class Monster1 : Monster
{
public Monster1(string sName, int health, int level)
{
m_Health = health;
m_Name = sName;
m_Level = level;
//Eg. level is 1, so defense is 99% * damage, if level is 99, then defense is 1% * damage, very hard to kill the monster
m_Defense = ((float)(100 - m_Level) / (float)100);
}
public override int Hit(int nDamage)
{
if (m_Health < 1)
{
return 0;//oops, die!
}
m_Health -= (int)(nDamage * m_Defense);
return GetStatus();
}
public override int GetStatus()
{
if (m_Health > 0)
{
Console.WriteLine("I am still alive [" + m_Health.ToString() + "], be careful!!");
}
else
{
Console.WriteLine("oops, die!");
}
return m_Health;
}
}
public abstract class Weapon
{
public int m_Damage;// 1 bullet causes how many damage for the monster
public int m_Level;// 1-100
public int m_Ammo;
public float m_AttackRate;
public string m_Name;
public abstract int Shot();
}
public class Weapon1 : Weapon
{
public Weapon1(string sName, int level,int Damage, int ammo)
{
m_Damage = Damage;
m_Name = sName;
m_Level = level;
m_Ammo = ammo;
m_AttackRate = ((float)((float)m_Level / 100) + 1);
}
public override int Shot()
{
int nRet = 0;
if (m_Ammo < 1) //no more bullets
{
Console.WriteLine("oops, run out of bullets!");
return nRet;
}
m_Ammo -= 1;
nRet = (int)(m_AttackRate * m_Damage);
return nRet;
}
}
//Maybe need an abstract class for hero, but ..., ignore it
public class Heros
{
public void ShotMonster(Weapon wp, Monster ms)
{
for (int i = 0; i < 11; i++)
{
ms.Hit(wp.Shot());
}
}
}
public class TetGame
{
public static void TestGame1()
{
Weapon wp1 = new Weapon1("Gun", 5, 100, 10);
Monster ms1 = new Monster1("Ghost", 300, 5);
Heros ho1 = new Heros();
ho1.ShotMonster(wp1,ms1);
}
}
Output:
I am still alive [202], be careful!!
I am still alive [104], be careful!!
I am still alive [6], be careful!!
oops, die!
oops, run out of bullets!
private static LinkedList MergeLinkedLists(LinkedList p1, LinkedList p2)
{
LinkedList newHead = new LinkedList();
if (p1 == null && p2 == null) return null;
if (p1 != null && p2 == null) return p1;
if (p1 == null && p2 != null) return p2;
Node Head1 = p1.Head;
Node Head2 = p2.Head;
while (Head1 != null && Head2 != null)
{
if (Head1.Data < Head2.Data)
{
newHead.Add(Head1);
Head1 = Head1.Next;
}
else if (Head1.Data == Head2.Data)
{
newHead.Add(Head1);
Head1 = Head1.Next;
newHead.Add(Head2);
Head2 = Head2.Next;
}
else
{
newHead.Add(Head2);
Head2 = Head2.Next;
}
}
if (Head1 != null)
{
newHead.Current.Next = Head1;
}
if (Head2 != null)
{
newHead.Current.Next = Head2;
}
return newHead;
}
public static void TestMergeLinkedLists()
{
LinkedList p1 = new LinkedList();
LinkedList p2 = new LinkedList();
p1.Add(new Node(3));
p1.Add(new Node(4));
p2.Add(new Node(1));
p2.Add(new Node(1));
p2.Add(new Node(1));
p2.Add(new Node(7));
Console.Write("List1 = ");
p1.Print();
Console.Write("List2 = ");
p2.Print();
Console.WriteLine("---------------------------");
LinkedList lst = MergeLinkedLists(p1,p2);
lst.Print();
}
public class LinkedList
{
private Node _Head;
private Node _Current;
public Node Head
{
get
{
return _Head;
}
set
{
_Head = value;
}
}
public Node Current
{
get
{
return _Current;
}
set
{
_Current = value;
}
}
public LinkedList(Node pH)
{
_Head = pH;
_Current = _Head;
}
public LinkedList()
{
_Head = null;
_Current = null;
}
public void Add(Node pD)
{
if (pD == null) return;
if (_Current != null)
{
_Current.Next = pD;
_Current = _Current.Next;
}
else
{
_Head = pD;
_Current = _Head;
}
}
public void Print()
{
Node pH = _Head;
while (pH != null)
{
if (pH.Next != null)
Console.Write(pH.Data.ToString() + "->");
else
Console.Write(pH.Data.ToString());
pH = pH.Next;
}
Console.WriteLine("");
}
}
Output:
List1 = 3->4
List2 = 1->1->1->7
---------------------------
1->1->1->3->4->7
Firstly, I thought it's easy one. but when I run two threads and simulate 0.5 billion visits, I always got some problems. After several hours debugging and changing, finally I made code work.
OK, let's stop talk and show my c# code.
Output:
========It's the Monday, reset all of daily counters!======
==========buffer index = [0] =============
0 -- 60511274
1 -- 50673865
2 -- 38735856
3 -- 38527673
4 -- 37408341
5 -- 37447992
6 -- 38037093
Weekly = 301342094
==========buffer index = [1] =============
0 -- 0
1 -- 7673
2 -- 0
3 -- 0
4 -- 0
5 -- 0
6 -- 0
Weekly = 7670
==========buffer index = [0] =============
0 -- 0
1 -- 0
2 -- 0
3 -- 0
4 -- 0
5 -- 0
6 -- 0
Weekly = 0
==========buffer index = [1] =============
0 -- 0
1 -- 198657906
2 -- 0
3 -- 0
4 -- 0
5 -- 0
6 -- 0
Weekly = 198657906
============End of the test =============
Guess what! 301342094 + 198657906 = 500000000! Bingo!!
Note:
1 why it's different
==========buffer index = [1] =============
0 -- 0
1 -- 7673
2 -- 0
3 -- 0
4 -- 0
5 -- 0
6 -- 0
Weekly = 7670
Look at code in PrintCounters()
Because those two threads keep visiting, there are three visits happened between two lines of code.
- Jasonhuangx March 14, 20142 Why not count 7670/7673 in, because 198657906 already contains those visits.