From 66f1b5a757488d13fabff57c6d1b45fbc235ac16 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 23 Feb 2009 21:02:12 +0100 Subject: [PATCH] * converted line-endings to UNIX style --- Einwohner.cs | 132 ++++++++++++++++++------------------- Output_CLI.cs | 170 ++++++++++++++++++++++++------------------------ Output_GUI.cs | 44 ++++++------- Position.cs | 38 +++++------ Program.cs | 39 ++++++----- Settings.cs | 56 ++++++++-------- Spielfeld.cs | 76 +++++++++++----------- Spielfigur.cs | 50 +++++++------- Spielflaeche.cs | 166 +++++++++++++++++++++++----------------------- Vampir.cs | 138 +++++++++++++++++++-------------------- 10 files changed, 454 insertions(+), 455 deletions(-) diff --git a/Einwohner.cs b/Einwohner.cs index fe77e88..b6dc73b 100644 --- a/Einwohner.cs +++ b/Einwohner.cs @@ -1,28 +1,28 @@ -using System; - -namespace vampi -{ - class Einwohner : Spielfigur { +using System; + +namespace vampi +{ + class Einwohner : Spielfigur { public const int F_SEX = 10; public const int SEX_MALE = 1; public const int SEX_FEMALE = 2; public const int F_INFECTED = 11; - - private static int count = 0; - public static int Count { - get { return Einwohner.count; } - } - public bool Infected { - get { return (this.props[F_INFECTED] != 0); } - } - public string name; - - public Einwohner(Spielfeld sfeld) - : base(sfeld) { - this.props[F_TYPE] = TYPE_HUMAN; - this.props[F_MAXAGE] = Settings.humanMaxAge; - this.props[F_AGE] = Program.random.Next(0, Settings.humanMaxInitAge); + + private static int count = 0; + public static int Count { + get { return Einwohner.count; } + } + public bool Infected { + get { return (this.props[F_INFECTED] != 0); } + } + public string name; + + public Einwohner(Spielfeld sfeld) + : base(sfeld) { + this.props[F_TYPE] = TYPE_HUMAN; + this.props[F_MAXAGE] = Settings.humanMaxAge; + this.props[F_AGE] = Program.random.Next(0, Settings.humanMaxInitAge); Einwohner.count++; this.props[F_SEX] = (Program.random.Next(0, 100) < 85)?SEX_MALE:SEX_FEMALE; if (this.props[F_SEX] == SEX_MALE) { @@ -31,57 +31,57 @@ namespace vampi this.name = Settings.namesFemale[Program.random.Next(0, Settings.namesFemale.GetLength(0))]; } // Console.WriteLine(this.name+" is born! (" + ((this.ismale)?"m":"f") + ")"); - } - - public void infect() { - if (this.Infected) + } + + public void infect() { + if (this.Infected) return; - // Console.WriteLine(this.name+" got infected!"); - this.props[F_INFECTED] = 1; - if (this.props[F_AGE] < this.props[F_MAXAGE] - Settings.humanInfectedMaxAge) this.props[F_AGE] = this.props[F_MAXAGE] - Settings.humanInfectedMaxAge; - } - - public override void die() { - Einwohner.count--; - if (this.Infected) { - int rvalue = Program.random.Next(0, 100); - if (rvalue <= Settings.humanVampireConversionPercent) { - new Vampir(this.sfeld); - return; - } - } - base.die(); - } - - public override void runStep() { - base.runStep(); + // Console.WriteLine(this.name+" got infected!"); + this.props[F_INFECTED] = 1; + if (this.props[F_AGE] < this.props[F_MAXAGE] - Settings.humanInfectedMaxAge) this.props[F_AGE] = this.props[F_MAXAGE] - Settings.humanInfectedMaxAge; + } + + public override void die() { + Einwohner.count--; + if (this.Infected) { + int rvalue = Program.random.Next(0, 100); + if (rvalue <= Settings.humanVampireConversionPercent) { + new Vampir(this.sfeld); + return; + } + } + base.die(); + } + + public override void runStep() { + base.runStep(); this.tryMate(); } protected void tryMate() { - if (this.Infected && !Settings.humanInfectedCanReproduceWithNormal) return; - - // search for constraints (empty field, partner to mate > 10 yrs) - Spielfeld birthplace = null; - bool mateFound = false; - for (int i = 1; i <= 8; i++) { - Spielfeld neighbor = this.sfeld.getNachbarfeld(i); - if (neighbor != null && neighbor.Sfigur != null) { - if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { + if (this.Infected && !Settings.humanInfectedCanReproduceWithNormal) return; + + // search for constraints (empty field, partner to mate > 10 yrs) + Spielfeld birthplace = null; + bool mateFound = false; + for (int i = 1; i <= 8; i++) { + Spielfeld neighbor = this.sfeld.getNachbarfeld(i); + if (neighbor != null && neighbor.Sfigur != null) { + if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { if (neighbor.Sfigur.Age >= Settings.humanLegalSexAge && (Settings.humanNormalCanReproduceWithInfected || !((Einwohner)neighbor.Sfigur).Infected)) { if (neighbor.Sfigur.props[F_SEX] != this.props[F_SEX]) { mateFound = true; - } - } - } - } else if (neighbor != null && neighbor.Sfigur == null) { - birthplace = neighbor; - } - } - // reproduce! - if (mateFound && birthplace != null) { - new Einwohner(birthplace); - } - } - } -}//namespace + } + } + } + } else if (neighbor != null && neighbor.Sfigur == null) { + birthplace = neighbor; + } + } + // reproduce! + if (mateFound && birthplace != null) { + new Einwohner(birthplace); + } + } + } +}//namespace diff --git a/Output_CLI.cs b/Output_CLI.cs index 3289454..7dd8953 100644 --- a/Output_CLI.cs +++ b/Output_CLI.cs @@ -7,23 +7,23 @@ namespace vampi { public class Output_CLI : Output { public Output_CLI() { - Console.BackgroundColor = ConsoleColor.Black; - //Console.CursorVisible = false; - Console.SetWindowSize(1, 1); - /* - Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight); - Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); + Console.BackgroundColor = ConsoleColor.Black; + //Console.CursorVisible = false; + Console.SetWindowSize(1, 1); + /* + Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight); + Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); */ if (System.Environment.OSVersion.Platform.ToString() != "Unix") { - Console.SetBufferSize(Settings.size + 2, Settings.size + 8); + Console.SetBufferSize(Settings.size + 2, Settings.size + 8); Console.SetWindowSize(Settings.size + 2, Settings.size + 8); - } - Console.Title = "Vampi CLI --- ©2008 Markus Birth, FA76"; - Console.Clear(); + } + Console.Title = "Vampi CLI --- ©2008 Markus Birth, FA76"; + Console.Clear(); } ~Output_CLI() { - Console.Read(); + Console.Read(); } public override void doOutput() { @@ -31,82 +31,82 @@ namespace vampi { this.drawStatistics(); } - public void testSpielfeld() { - Position pos; - Spielfeld nachbarfeld; - - //Schleife ueber alle Spielfelder - for (int y = 1; y <= Settings.size; y++) { - for (int x = 1; x <= Settings.size; x++) { - pos.x = x; - pos.y = y; - - Console.SetCursorPosition(x, y); - Console.BackgroundColor = ConsoleColor.Green; - Console.Write(' '); - - //Schleife ueber alle Nachbarfelder eines Spielfeldes - for (int lage = 1; lage <= 8; lage++) { - nachbarfeld = Program.sflaeche.getSpielfeld(pos).getNachbarfeld(lage); - - if (nachbarfeld != null) { //Nachbarfeld existiert - Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); - - Console.BackgroundColor = ConsoleColor.Red; - Console.Write(' '); - - Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); - - System.Threading.Thread.Sleep(10); - - Console.BackgroundColor = ConsoleColor.Gray; - Console.Write(' '); - } - } - Console.SetCursorPosition(x, y); - Console.BackgroundColor = ConsoleColor.Gray; - Console.Write(' '); - }//for - }//for - } - - public void drawGameMap() { - Position pos; - Console.SetCursorPosition(0, 0); - for (pos.y = 1; pos.y <= Settings.size; pos.y++) { - for (pos.x = 1; pos.x <= Settings.size; pos.x++) { - Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur; - - if (sf == null) //Spielfeld leer - Console.BackgroundColor = Settings.colorEmpty; - else { //Spielfeld besetzt - switch (sf.Typ) { - case Spielfigur.TYPE_HUMAN: - if (((Einwohner)sf).Infected) - Console.BackgroundColor = Settings.colorHumanInfected; - else - Console.BackgroundColor = Settings.colorHuman; - break; - case Spielfigur.TYPE_VAMPIRE: - Console.BackgroundColor = Settings.colorVampire; - break; - }//switch - }//else - Console.Write(' '); - }//for - Console.WriteLine(); - }//for - Console.ResetColor(); + public void testSpielfeld() { + Position pos; + Spielfeld nachbarfeld; + + //Schleife ueber alle Spielfelder + for (int y = 1; y <= Settings.size; y++) { + for (int x = 1; x <= Settings.size; x++) { + pos.x = x; + pos.y = y; + + Console.SetCursorPosition(x, y); + Console.BackgroundColor = ConsoleColor.Green; + Console.Write(' '); + + //Schleife ueber alle Nachbarfelder eines Spielfeldes + for (int lage = 1; lage <= 8; lage++) { + nachbarfeld = Program.sflaeche.getSpielfeld(pos).getNachbarfeld(lage); + + if (nachbarfeld != null) { //Nachbarfeld existiert + Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); + + Console.BackgroundColor = ConsoleColor.Red; + Console.Write(' '); + + Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); + + System.Threading.Thread.Sleep(10); + + Console.BackgroundColor = ConsoleColor.Gray; + Console.Write(' '); + } + } + Console.SetCursorPosition(x, y); + Console.BackgroundColor = ConsoleColor.Gray; + Console.Write(' '); + }//for + }//for } - public void drawStatistics() { - int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); - int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); - Console.WriteLine("\n" + String.Format("Steps Done: {0:D5} ", Program.AnzSimDone)); - Console.WriteLine(String.Format("Einwohner: {0:D} / Vampire: {1:D} ", Ecount, Vcount)); - Console.WriteLine(String.Format("Verhältnis Vampire/Einwohner = 1/{0:N5} ", (double)Ecount / Vcount)); - Console.WriteLine(String.Format("Bedeckung: {0:N5} %", (double)(Ecount + Vcount) / Settings.size*Settings.size * 100) + " "); - } + public void drawGameMap() { + Position pos; + Console.SetCursorPosition(0, 0); + for (pos.y = 1; pos.y <= Settings.size; pos.y++) { + for (pos.x = 1; pos.x <= Settings.size; pos.x++) { + Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur; + + if (sf == null) //Spielfeld leer + Console.BackgroundColor = Settings.colorEmpty; + else { //Spielfeld besetzt + switch (sf.Typ) { + case Spielfigur.TYPE_HUMAN: + if (((Einwohner)sf).Infected) + Console.BackgroundColor = Settings.colorHumanInfected; + else + Console.BackgroundColor = Settings.colorHuman; + break; + case Spielfigur.TYPE_VAMPIRE: + Console.BackgroundColor = Settings.colorVampire; + break; + }//switch + }//else + Console.Write(' '); + }//for + Console.WriteLine(); + }//for + Console.ResetColor(); + } + + public void drawStatistics() { + int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); + int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); + Console.WriteLine("\n" + String.Format("Steps Done: {0:D5} ", Program.AnzSimDone)); + Console.WriteLine(String.Format("Einwohner: {0:D} / Vampire: {1:D} ", Ecount, Vcount)); + Console.WriteLine(String.Format("Verhältnis Vampire/Einwohner = 1/{0:N5} ", (double)Ecount / Vcount)); + Console.WriteLine(String.Format("Bedeckung: {0:N5} %", (double)(Ecount + Vcount) / Settings.size*Settings.size * 100) + " "); + } } } diff --git a/Output_GUI.cs b/Output_GUI.cs index 4bffb6d..2864268 100644 --- a/Output_GUI.cs +++ b/Output_GUI.cs @@ -108,29 +108,29 @@ namespace vampi { fp.Lock(); Position pos; Color c = Color.Black; - for (pos.y = 1; pos.y <= Settings.size; pos.y++) { - for (pos.x = 1; pos.x <= Settings.size; pos.x++) { - Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur; - - if (sf == null) c = Settings.guiColorEmpty; //Spielfeld leer - else { //Spielfeld besetzt - switch (sf.Typ) { - case Spielfigur.TYPE_HUMAN: - if (((Einwohner)sf).Infected) - c = getGradient(Settings.guiColorHumanInfected, 100-(double)(sf.Age-Settings.humanMaxAge+Settings.humanInfectedMaxAge)*100/(double)Settings.humanInfectedMaxAge); + for (pos.y = 1; pos.y <= Settings.size; pos.y++) { + for (pos.x = 1; pos.x <= Settings.size; pos.x++) { + Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur; + + if (sf == null) c = Settings.guiColorEmpty; //Spielfeld leer + else { //Spielfeld besetzt + switch (sf.Typ) { + case Spielfigur.TYPE_HUMAN: + if (((Einwohner)sf).Infected) + c = getGradient(Settings.guiColorHumanInfected, 100-(double)(sf.Age-Settings.humanMaxAge+Settings.humanInfectedMaxAge)*100/(double)Settings.humanInfectedMaxAge); else - if (sf.props[Einwohner.F_SEX] == Einwohner.SEX_MALE) + if (sf.props[Einwohner.F_SEX] == Einwohner.SEX_MALE) c = getGradient(Settings.guiColorHumanMale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge); else - c = getGradient(Settings.guiColorHumanFemale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge); - break; - case Spielfigur.TYPE_VAMPIRE: - c = getGradient(Settings.guiColorVampire, 100-(double)sf.Age*100/(double)Settings.vampireMaxAge); - break; - } - } - fp.SetPixel(pos.x-1, pos.y-1, c); - } + c = getGradient(Settings.guiColorHumanFemale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge); + break; + case Spielfigur.TYPE_VAMPIRE: + c = getGradient(Settings.guiColorVampire, 100-(double)sf.Age*100/(double)Settings.vampireMaxAge); + break; + } + } + fp.SetPixel(pos.x-1, pos.y-1, c); + } } fp.Unlock(true); fg.DrawImage(this.field, 0, Output_GUI.statsHeight, Settings.size * this.scale, Settings.size * this.scale); @@ -144,8 +144,8 @@ namespace vampi { g.Clear(Color.White); int lineSpc = 10; - int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); - int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); + int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); + int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); g.DrawString(String.Format("Step: {0:D5}", Program.AnzSimDone), Settings.guiFont, Settings.guiFontBrush, 5, 0); g.DrawString(String.Format("T{0:N} = {1:D}/sec", Program.lastOverallTime, (int)Math.Floor(1000/(Program.lastOverallTime))), Settings.guiFont, Settings.guiFontBrush, 100, 0); g.DrawString(String.Format("C{0:N} D{1:N}", Program.lastCalcTime, Program.lastStatsTime), Settings.guiFont, Settings.guiFontBrush, 200, 0); diff --git a/Position.cs b/Position.cs index 7cba167..4cd5308 100644 --- a/Position.cs +++ b/Position.cs @@ -1,19 +1,19 @@ -using System; - -namespace vampi { - public struct Position { - public int x; - public int y; - - public Position(int x, int y) { - this.x = x; - this.y = y; - } - - public Position(Position pos) { - this.x = pos.x; - this.y = pos.y; - } - } - -} +using System; + +namespace vampi { + public struct Position { + public int x; + public int y; + + public Position(int x, int y) { + this.x = x; + this.y = y; + } + + public Position(Position pos) { + this.x = pos.x; + this.y = pos.y; + } + } + +} diff --git a/Program.cs b/Program.cs index d76c433..ed77e7a 100644 --- a/Program.cs +++ b/Program.cs @@ -1,23 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System; +using System.Collections.Generic; +using System.Text; using System.Timers; -using System.Diagnostics; - -namespace vampi { - class Program { - private static int anz_sim = 100000; +using System.Diagnostics; + +namespace vampi { + class Program { + private static int anz_sim = 100000; private static int anz_sim_done = 0; public static int AnzSimDone { get { return Program.anz_sim_done; } - } - public static Random random = new Random(); + } + public static Random random = new Random(); public static Spielflaeche sflaeche = new Spielflaeche(Settings.size, Settings.coveragePercent, Settings.vampireRatio); public static double lastCalcTime = 0; public static double lastStatsTime = 0; - public static double lastOverallTime = 0; - + public static double lastOverallTime = 0; + static void Main(string[] args) { Output output = new Output_GUI(); Stopwatch sw = new Stopwatch(); @@ -28,10 +27,10 @@ namespace vampi { sw2.Stop(); Program.lastOverallTime = sw2.Elapsed.TotalMilliseconds/Settings.drawEveryNthStep; sw2.Reset(); - sw2.Start(); + sw2.Start(); sw.Reset(); sw.Start(); - Program.lastCalcTime /= Settings.drawEveryNthStep; + Program.lastCalcTime /= Settings.drawEveryNthStep; output.doOutput(); sw.Stop(); Program.lastCalcTime = 0; @@ -39,11 +38,11 @@ namespace vampi { } if (output.requestAbort) break; sw.Reset(); - sw.Start(); + sw.Start(); sflaeche.simulateStep(); sw.Stop(); Program.lastCalcTime += sw.Elapsed.TotalMilliseconds; - } - }//Main - }//class -}//namespace + } + }//Main + }//class +}//namespace diff --git a/Settings.cs b/Settings.cs index a2429ed..2b347f6 100644 --- a/Settings.cs +++ b/Settings.cs @@ -1,16 +1,16 @@ using System; -using System.Drawing; - -namespace vampi { - public abstract class Settings { - public const int size = 120; - public const int coveragePercent = 77; - public const int vampireRatio = 3; +using System.Drawing; + +namespace vampi { + public abstract class Settings { + public const int size = 120; + public const int coveragePercent = 77; + public const int vampireRatio = 3; public const int drawEveryNthStep = 1; - - public const ConsoleColor colorHuman = ConsoleColor.Green; - public const ConsoleColor colorHumanInfected = ConsoleColor.DarkMagenta; - public const ConsoleColor colorVampire = ConsoleColor.Red; + + public const ConsoleColor colorHuman = ConsoleColor.Green; + public const ConsoleColor colorHumanInfected = ConsoleColor.DarkMagenta; + public const ConsoleColor colorVampire = ConsoleColor.Red; public const ConsoleColor colorEmpty = ConsoleColor.Gray; /* public static Color guiColorHuman = Color.LimeGreen; @@ -25,22 +25,22 @@ namespace vampi { public static Font guiFont = new Font("sans-serif", 8); public static Brush guiFontBrush = Brushes.Black; - - public const int humanLegalSexAge = 10; + + public const int humanLegalSexAge = 10; public const int humanVampireConversionPercent = 5; - public const int humanMaxInitAge = 10; - public const int humanMaxAge = 80; - public const int humanInfectedMaxAge = 20; - public const bool humanInfectedCanReproduceWithNormal = false; - public const bool humanNormalCanReproduceWithInfected = false; - + public const int humanMaxInitAge = 10; + public const int humanMaxAge = 80; + public const int humanInfectedMaxAge = 20; + public const bool humanInfectedCanReproduceWithNormal = false; + public const bool humanNormalCanReproduceWithInfected = false; + public const int vampireInitialFill = 100; - public const int vampireMaxInitAge = 60; - public const int vampireMaxAge = 500; - public const int vampireDecreaseFillPerStep = 3; - public const int vampireIncreaseFillPerBite = 1; - public const bool vampireInfectOnlyOneHuman = false; - public const bool vampireInfectOneOrMoreHumans = false; + public const int vampireMaxInitAge = 60; + public const int vampireMaxAge = 500; + public const int vampireDecreaseFillPerStep = 3; + public const int vampireIncreaseFillPerBite = 1; + public const bool vampireInfectOnlyOneHuman = false; + public const bool vampireInfectOneOrMoreHumans = false; public static string[] namesMale = { "Aaron", "Achim", "Adalger", "Adam", "Adelar", "Adrian", "Aginulf", "Agomar", "Alain", "Alarich", "Alban", "Albero", "Albert", "Alderich", "Aldo", "Alexander", "Alger", "Ali", "Allen", "Amon", "Andrew", "Antoine", "Arnaud", "Arnie", "Art", "Arwed", "Arve", "Aquarius", @@ -98,6 +98,6 @@ namespace vampi { "Yolande", "Yvette", "Yerba", "Yvonne", "Zoe", "Zenja", "Zancara", "Zarah" }; - - } -} + + } +} diff --git a/Spielfeld.cs b/Spielfeld.cs index 5f36802..89ee802 100644 --- a/Spielfeld.cs +++ b/Spielfeld.cs @@ -1,38 +1,38 @@ -using System; - -namespace vampi -{ - public class Spielfeld { - private Position pos; - private Spielflaeche sflaeche; - private Spielfigur sfigur = null; - private int[] mx = {-1, 0, 1, 1, 1, 0, -1, -1}; - private int[] my = {-1, -1, -1, 0, 1, 1, 1, 0}; - - public Spielfigur Sfigur { - get { return this.sfigur; } - set { this.sfigur = value; } - } - - public Position Pos { - get { return pos; } - } - - public Spielfeld(Position pos, Spielflaeche sflaeche) { - this.pos = pos; - this.sflaeche = sflaeche; - } - - public Spielfeld getNachbarfeld(int lage) { - return this.sflaeche.getSpielfeld(this.getNachbarpos(lage)); - } - - private Position getNachbarpos(int lage) { - Position nachbarpos = new Position(this.pos); - nachbarpos.x += mx[lage - 1]; - nachbarpos.y += my[lage - 1]; - return nachbarpos; - } - - } -} +using System; + +namespace vampi +{ + public class Spielfeld { + private Position pos; + private Spielflaeche sflaeche; + private Spielfigur sfigur = null; + private int[] mx = {-1, 0, 1, 1, 1, 0, -1, -1}; + private int[] my = {-1, -1, -1, 0, 1, 1, 1, 0}; + + public Spielfigur Sfigur { + get { return this.sfigur; } + set { this.sfigur = value; } + } + + public Position Pos { + get { return pos; } + } + + public Spielfeld(Position pos, Spielflaeche sflaeche) { + this.pos = pos; + this.sflaeche = sflaeche; + } + + public Spielfeld getNachbarfeld(int lage) { + return this.sflaeche.getSpielfeld(this.getNachbarpos(lage)); + } + + private Position getNachbarpos(int lage) { + Position nachbarpos = new Position(this.pos); + nachbarpos.x += mx[lage - 1]; + nachbarpos.y += my[lage - 1]; + return nachbarpos; + } + + } +} diff --git a/Spielfigur.cs b/Spielfigur.cs index 6343019..2a604c6 100644 --- a/Spielfigur.cs +++ b/Spielfigur.cs @@ -1,41 +1,41 @@ using System; -using System.Collections; - -namespace vampi -{ +using System.Collections; + +namespace vampi +{ public abstract class Spielfigur { public const int F_TYPE = 0; public const int TYPE_HUMAN = 1; public const int TYPE_VAMPIRE = 2; - + public const int F_AGE = 1; public const int F_MAXAGE = 2; public int[] props = new int[20]; - protected Spielfeld sfeld; + protected Spielfeld sfeld; - public int Age { + public int Age { get { return this.props[F_AGE]; } - } - - public int Typ { - get { return this.props[F_TYPE]; } - } - + } + + public int Typ { + get { return this.props[F_TYPE]; } + } + public Spielfigur(Spielfeld sfeld) { - sfeld.Sfigur = this; + sfeld.Sfigur = this; this.sfeld = sfeld; } - public virtual void runStep() { - this.props[F_AGE]++; - if (this.props[F_AGE] >= this.props[F_MAXAGE]) { - this.die(); - } - } - - public virtual void die() { - this.sfeld.Sfigur = null; - } - } + public virtual void runStep() { + this.props[F_AGE]++; + if (this.props[F_AGE] >= this.props[F_MAXAGE]) { + this.die(); + } + } + + public virtual void die() { + this.sfeld.Sfigur = null; + } + } } \ No newline at end of file diff --git a/Spielflaeche.cs b/Spielflaeche.cs index 0b675bd..ca77762 100644 --- a/Spielflaeche.cs +++ b/Spielflaeche.cs @@ -1,83 +1,83 @@ -using System; - -namespace vampi { - public class Spielflaeche { - private Spielfeld[,] sfeld_2dim; - private int groesse; - public int Groesse { - get { return sfeld_2dim.GetLength(0) * sfeld_2dim.GetLength(1); } - } - - public Spielflaeche(int groesse) { - this.groesse = groesse; - sfeld_2dim = new Spielfeld[groesse, groesse]; - for (int i = 0; i < groesse; i++) { - for (int j = 0; j < groesse; j++) { - sfeld_2dim[i, j] = new Spielfeld(new Position(i+1, j+1), this); - } - } - } - - public Spielflaeche(int groesse, int initBedeckung, int initVerhaeltnis) : this(groesse) { - initSpielfeld(initBedeckung, initVerhaeltnis); - } - - private void initSpielfeld(int initBedeckung, int initVerhaeltnis) { - // INFO: Andere Idee: Spielfeld linear befüllen, danach alle Felder durchgehen - // und das aktuelle Feld jeweils mit einem zufälligen Feld tauschen - int feldX, feldY; - int bedeckung = this.groesse * this.groesse * initBedeckung / 100; // groesse^2 * Prozent - int vampire = (int)Math.Round((double)bedeckung / (initVerhaeltnis+1)); // Felder / (Verhaeltnis+1) - - for (int i = bedeckung; i > 0; i--) { - // find an empty, random field - do { - feldX = Program.random.Next(0, this.groesse); - feldY = Program.random.Next(0, this.groesse); - } while (this.sfeld_2dim[feldX, feldY].Sfigur != null); - - // first set all vampires then set inhabitants - if (vampire > 0) { - new Vampir(this.sfeld_2dim[feldX, feldY]); - vampire--; - } else { - new Einwohner(this.sfeld_2dim[feldX, feldY]); - } - } - } - - public Spielfeld getSpielfeld(Position pos) { - if (pos.x > this.groesse) - pos.x -= this.groesse; // should be 1 - if (pos.x < 1) - pos.x += this.groesse; // should be this.groesse - - if (pos.y > this.groesse || pos.y < 1) { - // y-direction doesn't wrap - return null; - } - - return sfeld_2dim[pos.x-1, pos.y-1]; - } - - public int countTypeOccurrences(int typ) { - int result = 0; - for (int i = 0; i < this.groesse; i++) { - for (int j = 0; j < this.groesse; j++) { - if (this.sfeld_2dim[i, j].Sfigur != null && this.sfeld_2dim[i, j].Sfigur.Typ == typ) - result++; - } - } - return result; - } - - public void simulateStep() { - for (int i = 0; i < this.groesse; i++) { - for (int j = 0; j < this.groesse; j++) { - if (this.sfeld_2dim[i, j].Sfigur != null) - this.sfeld_2dim[i, j].Sfigur.runStep(); - } - } - } - } -} +using System; + +namespace vampi { + public class Spielflaeche { + private Spielfeld[,] sfeld_2dim; + private int groesse; + public int Groesse { + get { return sfeld_2dim.GetLength(0) * sfeld_2dim.GetLength(1); } + } + + public Spielflaeche(int groesse) { + this.groesse = groesse; + sfeld_2dim = new Spielfeld[groesse, groesse]; + for (int i = 0; i < groesse; i++) { + for (int j = 0; j < groesse; j++) { + sfeld_2dim[i, j] = new Spielfeld(new Position(i+1, j+1), this); + } + } + } + + public Spielflaeche(int groesse, int initBedeckung, int initVerhaeltnis) : this(groesse) { + initSpielfeld(initBedeckung, initVerhaeltnis); + } + + private void initSpielfeld(int initBedeckung, int initVerhaeltnis) { + // INFO: Andere Idee: Spielfeld linear befüllen, danach alle Felder durchgehen + // und das aktuelle Feld jeweils mit einem zufälligen Feld tauschen + int feldX, feldY; + int bedeckung = this.groesse * this.groesse * initBedeckung / 100; // groesse^2 * Prozent + int vampire = (int)Math.Round((double)bedeckung / (initVerhaeltnis+1)); // Felder / (Verhaeltnis+1) + + for (int i = bedeckung; i > 0; i--) { + // find an empty, random field + do { + feldX = Program.random.Next(0, this.groesse); + feldY = Program.random.Next(0, this.groesse); + } while (this.sfeld_2dim[feldX, feldY].Sfigur != null); + + // first set all vampires then set inhabitants + if (vampire > 0) { + new Vampir(this.sfeld_2dim[feldX, feldY]); + vampire--; + } else { + new Einwohner(this.sfeld_2dim[feldX, feldY]); + } + } + } + + public Spielfeld getSpielfeld(Position pos) { + if (pos.x > this.groesse) + pos.x -= this.groesse; // should be 1 + if (pos.x < 1) + pos.x += this.groesse; // should be this.groesse + + if (pos.y > this.groesse || pos.y < 1) { + // y-direction doesn't wrap + return null; + } + + return sfeld_2dim[pos.x-1, pos.y-1]; + } + + public int countTypeOccurrences(int typ) { + int result = 0; + for (int i = 0; i < this.groesse; i++) { + for (int j = 0; j < this.groesse; j++) { + if (this.sfeld_2dim[i, j].Sfigur != null && this.sfeld_2dim[i, j].Sfigur.Typ == typ) + result++; + } + } + return result; + } + + public void simulateStep() { + for (int i = 0; i < this.groesse; i++) { + for (int j = 0; j < this.groesse; j++) { + if (this.sfeld_2dim[i, j].Sfigur != null) + this.sfeld_2dim[i, j].Sfigur.runStep(); + } + } + } + } +} diff --git a/Vampir.cs b/Vampir.cs index 9d5365b..8f45fcc 100644 --- a/Vampir.cs +++ b/Vampir.cs @@ -1,76 +1,76 @@ -using System; - -namespace vampi -{ - class Vampir : Spielfigur { +using System; + +namespace vampi +{ + class Vampir : Spielfigur { public const int F_FILLER = 10; - - private static int count = 0; - public static int Count { - get { return Vampir.count; } - } - - public Vampir(Spielfeld sfeld) - : base(sfeld) { - this.props[F_TYPE] = TYPE_VAMPIRE; - this.props[F_MAXAGE] = Settings.vampireMaxAge; + + private static int count = 0; + public static int Count { + get { return Vampir.count; } + } + + public Vampir(Spielfeld sfeld) + : base(sfeld) { + this.props[F_TYPE] = TYPE_VAMPIRE; + this.props[F_MAXAGE] = Settings.vampireMaxAge; this.props[F_AGE] = Program.random.Next(0, Settings.vampireMaxInitAge); - this.props[F_FILLER] = Settings.vampireInitialFill; - Vampir.count++; - } - - public override void die() { - base.die(); - Vampir.count--; - } - - public override void runStep() { - base.runStep(); - this.props[F_FILLER] -= Settings.vampireDecreaseFillPerStep; - if (this.props[F_FILLER] <= 0) { - this.die(); - return; + this.props[F_FILLER] = Settings.vampireInitialFill; + Vampir.count++; + } + + public override void die() { + base.die(); + Vampir.count--; + } + + public override void runStep() { + base.runStep(); + this.props[F_FILLER] -= Settings.vampireDecreaseFillPerStep; + if (this.props[F_FILLER] <= 0) { + this.die(); + return; } - this.tryInfect(); + this.tryInfect(); } // runStep() protected void tryInfect() { - // count humans around me - int humans = 0; - for (int i = 1; i <= 8; i++) { - Spielfeld neighbor = this.sfeld.getNachbarfeld(i); - if (neighbor != null && neighbor.Sfigur != null) { - if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { - if (Settings.vampireInfectOnlyOneHuman) { - humans++; - } else { - ((Einwohner)neighbor.Sfigur).infect(); - this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; - // humans is still 0, so runStep() will return after this - } - } - } - } - - if (humans == 0) - return; - - // randomly infect one human - int infect = Program.random.Next(0, humans); - - for (int i = 1; i <= 8; i++) { - Spielfeld neighbor = this.sfeld.getNachbarfeld(i); - if (neighbor != null && neighbor.Sfigur != null && neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { - if (infect == 0) { - ((Einwohner)neighbor.Sfigur).infect(); - this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; - if (!Settings.vampireInfectOneOrMoreHumans) break; - } else { - infect--; - } - } - } - } - } - + // count humans around me + int humans = 0; + for (int i = 1; i <= 8; i++) { + Spielfeld neighbor = this.sfeld.getNachbarfeld(i); + if (neighbor != null && neighbor.Sfigur != null) { + if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { + if (Settings.vampireInfectOnlyOneHuman) { + humans++; + } else { + ((Einwohner)neighbor.Sfigur).infect(); + this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; + // humans is still 0, so runStep() will return after this + } + } + } + } + + if (humans == 0) + return; + + // randomly infect one human + int infect = Program.random.Next(0, humans); + + for (int i = 1; i <= 8; i++) { + Spielfeld neighbor = this.sfeld.getNachbarfeld(i); + if (neighbor != null && neighbor.Sfigur != null && neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { + if (infect == 0) { + ((Einwohner)neighbor.Sfigur).infect(); + this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; + if (!Settings.vampireInfectOneOrMoreHumans) break; + } else { + infect--; + } + } + } + } + } + } \ No newline at end of file