wigga
May 30th, 2009, 05:52 AM
hi i tried searching some timer tutorials but i cannot find what i am looking for.
coming from a C++ background, this assignment is my homework.
i am perfectly fluent in C++, but i dont know much about C#.
How do i go about passing the Pet object object into the static timer function?
Does anyone have tips on my code? please hlep me on this one.
here is my code:
using System;
using System.Text;
using System.Timers;
namespace Tamagotchi
{
class Pet
{
public int Age;
public int Mood;
public int Weight;
public int Energy;
public bool Crapped;
public bool Healthy;
public int Hunger;
public bool Alive;
public Timer Event = new Timer();
public string Name;
protected virtual void OnAge() { }
protected virtual void OnCrapped() { }
protected virtual void OnHealth() { }
protected virtual void OnMood(int x) { }
protected virtual void OnHunger(int x) { }
protected virtual void OnWeight(int x) { }
protected virtual void OnEnergy(int x) { }
protected virtual void OnAlive() { }
private static void Event_Tick(object sender, ElapsedEventArgs args)
{
Random s = new Random();
if (Crapped)
{
int i = s.Next(1000, 2000);
if (i > 1400 && i < 1600)
{
Healthy = false;
OnHealth();
}
}
switch(s.Next(0, 3))
{
case 0:
int x = Hunger++;
OnHunger(x);
int i = s.Next(1000, 2000);
if (i > 1400 && i < 1600)
{
Crapped = true;
OnCrapped();
}
break;
case 1:
int x = Mood--;
OnMood(x);
break;
case 2:
int x = Weight--;
OnWeight(x);
break;
case 3:
int x = Energy--;
OnEnergy(x);
break;
}
Event.Interval = s.Next(1000, 2000);
}
public void Cure()
{
Healthy = true;
}
public Pet()
{
Event.Elapsed += new ElapsedEventHandler(Event_Tick);
Event.Interval = 1000;
}
public void Start()
{
Healthy = true;
Crapped = false;
Energy = 0;
Age = 0;
Mood = 0;
Hunger = 0;
Weight = 0;
Alive = true;
Event.Start();
OnAlive();
}
public void Clean()
{
Crapped = false;
}
public void End()
{
Event.Stop();
Alive = false;
OnAlive();
}
public void Feed()
{
Hunger++;
Weight++;
}
public void Play()
{
Mood++;
Weight--;
}
}
class Program
{
class MyPet: Pet
{
protected override void OnAge()
{
Console.WriteLine(Name + " older.");
}
protected override void OnMood(int x)
{
if(Mood > 5)
Console.WriteLine(Name + " happy.");
else if(Mood < -5)
Console.WriteLine(Name + " unhappy.");
}
protected override void OnWeight(int x)
{
if (Weight < 5)
Console.WriteLine(Name + " skinny.");
else if (Weight > 10)
Console.WriteLine(Name + " fat.");
}
protected override void OnHunger(int x)
{
if(Hunger > 0)
Console.WriteLine(Name + " starving.");
}
protected override void OnCrapped()
{
if (Crapped)
Console.WriteLine(Name + " poo.");
else
Console.WriteLine(Name + " flush.");
}
protected override void OnAlive()
{
if (Alive)
Console.WriteLine(Name + " born.");
else
Console.WriteLine(Name + " die.");
}
public void Clean()
{
base.Clean();
Console.WriteLine(Name + " cleaned.");
}
public void Feed()
{
base.Feed();
Console.WriteLine(Name + " fed.");
}
public void Play()
{
base.Play();
Console.WriteLine(Name + " play.");
}
public void Cure()
{
base.Cure();
Console.WriteLine(Name + " cure.");
}
public void PrintStatus()
{
Console.WriteLine("Name: " + Name + ".");
Console.WriteLine("Age: " + Age + ".");
Console.WriteLine("Mood: " + Mood + ".");
Console.WriteLine("Hunger: " + Hunger + ".");
Console.WriteLine("Weight: " + Weight + ".");
Console.WriteLine("Energy: " + Energy + ".");
Console.WriteLine("Crapped: " + Crapped + ".");
Console.WriteLine("Healthy: " + Healthy + ".");
}
};
static MyPet pet = new MyPet();
static void PrintMenu()
{
Console.WriteLine("0. Quit");
Console.WriteLine("1. Pet Status");
Console.WriteLine("2. Feed Pet");
Console.WriteLine("3. Play Pet");
Console.WriteLine("4. Clean Pet");
Console.WriteLine("5. Cure Pet");
Console.WriteLine("X. Save Pet");
}
static void Main(string[] args)
{
Console.WriteLine("1. New Pet");
Console.WriteLine("X. Load Pet");
switch (Console.ReadKey(true).KeyChar)
{
case '1':
Console.Write("Enter a name for your pet: ");
pet.Name = Console.ReadLine();
pet.Start();
break;
}
PrintMenu();
while (true)
{
switch (Console.ReadKey(true).KeyChar)
{
case '0':
return;
case '1':
pet.PrintStatus();
break;
case '2':
pet.Feed();
break;
case '3':
pet.Play();
break;
case '4':
pet.Clean();
break;
case '5':
pet.Cure();
break;
default:
Console.WriteLine("Invalid command.");
PrintMenu();
break;
}
}
}
}
}
coming from a C++ background, this assignment is my homework.
i am perfectly fluent in C++, but i dont know much about C#.
How do i go about passing the Pet object object into the static timer function?
Does anyone have tips on my code? please hlep me on this one.
here is my code:
using System;
using System.Text;
using System.Timers;
namespace Tamagotchi
{
class Pet
{
public int Age;
public int Mood;
public int Weight;
public int Energy;
public bool Crapped;
public bool Healthy;
public int Hunger;
public bool Alive;
public Timer Event = new Timer();
public string Name;
protected virtual void OnAge() { }
protected virtual void OnCrapped() { }
protected virtual void OnHealth() { }
protected virtual void OnMood(int x) { }
protected virtual void OnHunger(int x) { }
protected virtual void OnWeight(int x) { }
protected virtual void OnEnergy(int x) { }
protected virtual void OnAlive() { }
private static void Event_Tick(object sender, ElapsedEventArgs args)
{
Random s = new Random();
if (Crapped)
{
int i = s.Next(1000, 2000);
if (i > 1400 && i < 1600)
{
Healthy = false;
OnHealth();
}
}
switch(s.Next(0, 3))
{
case 0:
int x = Hunger++;
OnHunger(x);
int i = s.Next(1000, 2000);
if (i > 1400 && i < 1600)
{
Crapped = true;
OnCrapped();
}
break;
case 1:
int x = Mood--;
OnMood(x);
break;
case 2:
int x = Weight--;
OnWeight(x);
break;
case 3:
int x = Energy--;
OnEnergy(x);
break;
}
Event.Interval = s.Next(1000, 2000);
}
public void Cure()
{
Healthy = true;
}
public Pet()
{
Event.Elapsed += new ElapsedEventHandler(Event_Tick);
Event.Interval = 1000;
}
public void Start()
{
Healthy = true;
Crapped = false;
Energy = 0;
Age = 0;
Mood = 0;
Hunger = 0;
Weight = 0;
Alive = true;
Event.Start();
OnAlive();
}
public void Clean()
{
Crapped = false;
}
public void End()
{
Event.Stop();
Alive = false;
OnAlive();
}
public void Feed()
{
Hunger++;
Weight++;
}
public void Play()
{
Mood++;
Weight--;
}
}
class Program
{
class MyPet: Pet
{
protected override void OnAge()
{
Console.WriteLine(Name + " older.");
}
protected override void OnMood(int x)
{
if(Mood > 5)
Console.WriteLine(Name + " happy.");
else if(Mood < -5)
Console.WriteLine(Name + " unhappy.");
}
protected override void OnWeight(int x)
{
if (Weight < 5)
Console.WriteLine(Name + " skinny.");
else if (Weight > 10)
Console.WriteLine(Name + " fat.");
}
protected override void OnHunger(int x)
{
if(Hunger > 0)
Console.WriteLine(Name + " starving.");
}
protected override void OnCrapped()
{
if (Crapped)
Console.WriteLine(Name + " poo.");
else
Console.WriteLine(Name + " flush.");
}
protected override void OnAlive()
{
if (Alive)
Console.WriteLine(Name + " born.");
else
Console.WriteLine(Name + " die.");
}
public void Clean()
{
base.Clean();
Console.WriteLine(Name + " cleaned.");
}
public void Feed()
{
base.Feed();
Console.WriteLine(Name + " fed.");
}
public void Play()
{
base.Play();
Console.WriteLine(Name + " play.");
}
public void Cure()
{
base.Cure();
Console.WriteLine(Name + " cure.");
}
public void PrintStatus()
{
Console.WriteLine("Name: " + Name + ".");
Console.WriteLine("Age: " + Age + ".");
Console.WriteLine("Mood: " + Mood + ".");
Console.WriteLine("Hunger: " + Hunger + ".");
Console.WriteLine("Weight: " + Weight + ".");
Console.WriteLine("Energy: " + Energy + ".");
Console.WriteLine("Crapped: " + Crapped + ".");
Console.WriteLine("Healthy: " + Healthy + ".");
}
};
static MyPet pet = new MyPet();
static void PrintMenu()
{
Console.WriteLine("0. Quit");
Console.WriteLine("1. Pet Status");
Console.WriteLine("2. Feed Pet");
Console.WriteLine("3. Play Pet");
Console.WriteLine("4. Clean Pet");
Console.WriteLine("5. Cure Pet");
Console.WriteLine("X. Save Pet");
}
static void Main(string[] args)
{
Console.WriteLine("1. New Pet");
Console.WriteLine("X. Load Pet");
switch (Console.ReadKey(true).KeyChar)
{
case '1':
Console.Write("Enter a name for your pet: ");
pet.Name = Console.ReadLine();
pet.Start();
break;
}
PrintMenu();
while (true)
{
switch (Console.ReadKey(true).KeyChar)
{
case '0':
return;
case '1':
pet.PrintStatus();
break;
case '2':
pet.Feed();
break;
case '3':
pet.Play();
break;
case '4':
pet.Clean();
break;
case '5':
pet.Cure();
break;
default:
Console.WriteLine("Invalid command.");
PrintMenu();
break;
}
}
}
}
}