using System;
using System.Windows.Forms;
using Views;
public class StarLordsTwo {
///
/// The StarLords Program Bishop and Horspool Sept 2002
/// =====================
/// Simulates a battle, and has two levels of lords
/// Illustrates inheritance, ref parameters and
/// extensive use of Views.
///
Views.Form f;
void SetUpForm() {
try {
f = new Views.Form( @"
" );
} catch( Exception e ) {
Console.WriteLine(
"Exception caught, message follows:{0}", e.Message);
}
}
void Go() {
SetUpForm();
SetUpCharacters();
DoBattle();
}
void Display(Character lord, string x) {
// inserts into the following sub-view
//
//
f.PutValue(x+("strength"),lord.Strength);
f.PutValue(x+("points"),lord.Points);
}
Character A;
Character B;
void SetUpCharacters () {
Label lab = f["A"];
A = new Character(lab.Text, 80);
lab = f["B"];
B = new Character(lab.Text, 74);
Display(A,"A");
Display(B,"B");
}
void DoBattle() {
for ( ; ; ) {
string c = f.GetControl();
if (c == null) break;
if (A.Points > 0 && B.Points > 0) {
ActionPerformed(c);
}
if (A.Points <= 0 || B.Points <= 0) {
if (A.Points > B.Points)
f.PutText("Winner",A.Name);
else
f.PutText("Winner",B.Name);
}
}
f.CloseGUI();
}
bool AsTurn = true;
void Enhance(ref Character c, string x) {
int rememberPoints = c.Points;
c = new SuperCharacter(c);
c.Points = rememberPoints;
ProgressBar p = f[x+"strength"];
p.ForeColor = System.Drawing.Color.Red;
PictureBox pic = f[x+"image"];
f.PutImage(x+"image","starlord"+x+"enhanced.jpg");
Display(c,x);
}
void ActionPerformed(string c) {
switch (c) {
case "Attack" : {
if (AsTurn)
A.Attack(B);
else
B.Attack(A);
AsTurn=!AsTurn;
Display(A,"A");
Display(B,"B");
break;
}
case "Aenhance" : {
Enhance(ref A,"A");
break;
}
case "Benhance" : {
Enhance(ref B, "B");
break;
}
}
}
static void Main () {
new StarLordsTwo().Go();
}
}
///
/// A class of StarLord characters with certain characteristics
/// and the ability to battle against other StarLords
///
public class Character {
string name;
int points;
int strength;
Random r = new Random();
public Character() {
// required because of derived class
}
public Character(string n, int s) {
name = n;
strength = s;
points = s;
}
public string Name {
get {return name; }
}
public int Points {
get {return points; }
set {points = value;}
}
public int Strength {
get {return strength; }
set {strength = value;}
}
public virtual void Attack(Character opponent) {
int damage = r.Next(strength/3+points/2);
opponent.Points -= damage;
}
public override string ToString () {
return name + " at " + points;
}
}
///
/// A derived class of StarLord characters with
/// enhanced attacking characteristics
///
public class SuperCharacter : Character {
Random r = new Random();
public SuperCharacter (Character c)
// start with the same as before
: base (c.Name, c.Strength) {
// enhance strength
Strength += r.Next(10);
}
// new attacking method
// more strength (i.e. /2 not 3)
// but more risky - there is a random +- factor
public override void Attack (Character opponent) {
// was int damage = r.Next(strength/3+points/2);
int damage = r.Next(Strength/2+Points/2)-r.Next(1)*4;
opponent.Points -= damage;
}
}