Archived
1
0

* converted line-endings to UNIX style

This commit is contained in:
2009-02-23 21:02:12 +01:00
parent 6a90788777
commit 66f1b5a757
10 changed files with 454 additions and 455 deletions

View File

@ -1,28 +1,28 @@
using System; using System;
namespace vampi namespace vampi
{ {
class Einwohner : Spielfigur { class Einwohner : Spielfigur {
public const int F_SEX = 10; public const int F_SEX = 10;
public const int SEX_MALE = 1; public const int SEX_MALE = 1;
public const int SEX_FEMALE = 2; public const int SEX_FEMALE = 2;
public const int F_INFECTED = 11; public const int F_INFECTED = 11;
private static int count = 0; private static int count = 0;
public static int Count { public static int Count {
get { return Einwohner.count; } get { return Einwohner.count; }
} }
public bool Infected { public bool Infected {
get { return (this.props[F_INFECTED] != 0); } get { return (this.props[F_INFECTED] != 0); }
} }
public string name; public string name;
public Einwohner(Spielfeld sfeld) public Einwohner(Spielfeld sfeld)
: base(sfeld) { : base(sfeld) {
this.props[F_TYPE] = TYPE_HUMAN; this.props[F_TYPE] = TYPE_HUMAN;
this.props[F_MAXAGE] = Settings.humanMaxAge; this.props[F_MAXAGE] = Settings.humanMaxAge;
this.props[F_AGE] = Program.random.Next(0, Settings.humanMaxInitAge); this.props[F_AGE] = Program.random.Next(0, Settings.humanMaxInitAge);
Einwohner.count++; Einwohner.count++;
this.props[F_SEX] = (Program.random.Next(0, 100) < 85)?SEX_MALE:SEX_FEMALE; this.props[F_SEX] = (Program.random.Next(0, 100) < 85)?SEX_MALE:SEX_FEMALE;
if (this.props[F_SEX] == SEX_MALE) { 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))]; this.name = Settings.namesFemale[Program.random.Next(0, Settings.namesFemale.GetLength(0))];
} }
// Console.WriteLine(this.name+" is born! (" + ((this.ismale)?"m":"f") + ")"); // Console.WriteLine(this.name+" is born! (" + ((this.ismale)?"m":"f") + ")");
} }
public void infect() { public void infect() {
if (this.Infected) if (this.Infected)
return; return;
// Console.WriteLine(this.name+" got infected!"); // Console.WriteLine(this.name+" got infected!");
this.props[F_INFECTED] = 1; 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; 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() { public override void die() {
Einwohner.count--; Einwohner.count--;
if (this.Infected) { if (this.Infected) {
int rvalue = Program.random.Next(0, 100); int rvalue = Program.random.Next(0, 100);
if (rvalue <= Settings.humanVampireConversionPercent) { if (rvalue <= Settings.humanVampireConversionPercent) {
new Vampir(this.sfeld); new Vampir(this.sfeld);
return; return;
} }
} }
base.die(); base.die();
} }
public override void runStep() { public override void runStep() {
base.runStep(); base.runStep();
this.tryMate(); this.tryMate();
} }
protected void tryMate() { protected void tryMate() {
if (this.Infected && !Settings.humanInfectedCanReproduceWithNormal) return; if (this.Infected && !Settings.humanInfectedCanReproduceWithNormal) return;
// search for constraints (empty field, partner to mate > 10 yrs) // search for constraints (empty field, partner to mate > 10 yrs)
Spielfeld birthplace = null; Spielfeld birthplace = null;
bool mateFound = false; bool mateFound = false;
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
Spielfeld neighbor = this.sfeld.getNachbarfeld(i); Spielfeld neighbor = this.sfeld.getNachbarfeld(i);
if (neighbor != null && neighbor.Sfigur != null) { if (neighbor != null && neighbor.Sfigur != null) {
if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) {
if (neighbor.Sfigur.Age >= Settings.humanLegalSexAge && (Settings.humanNormalCanReproduceWithInfected || !((Einwohner)neighbor.Sfigur).Infected)) { if (neighbor.Sfigur.Age >= Settings.humanLegalSexAge && (Settings.humanNormalCanReproduceWithInfected || !((Einwohner)neighbor.Sfigur).Infected)) {
if (neighbor.Sfigur.props[F_SEX] != this.props[F_SEX]) { if (neighbor.Sfigur.props[F_SEX] != this.props[F_SEX]) {
mateFound = true; mateFound = true;
} }
} }
} }
} else if (neighbor != null && neighbor.Sfigur == null) { } else if (neighbor != null && neighbor.Sfigur == null) {
birthplace = neighbor; birthplace = neighbor;
} }
} }
// reproduce! // reproduce!
if (mateFound && birthplace != null) { if (mateFound && birthplace != null) {
new Einwohner(birthplace); new Einwohner(birthplace);
} }
} }
} }
}//namespace }//namespace

View File

@ -7,23 +7,23 @@ namespace vampi {
public class Output_CLI : Output { public class Output_CLI : Output {
public Output_CLI() { public Output_CLI() {
Console.BackgroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.Black;
//Console.CursorVisible = false; //Console.CursorVisible = false;
Console.SetWindowSize(1, 1); Console.SetWindowSize(1, 1);
/* /*
Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight); Console.SetBufferSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
*/ */
if (System.Environment.OSVersion.Platform.ToString() != "Unix") { 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.SetWindowSize(Settings.size + 2, Settings.size + 8);
} }
Console.Title = "Vampi CLI --- ©2008 Markus Birth, FA76"; Console.Title = "Vampi CLI --- ©2008 Markus Birth, FA76";
Console.Clear(); Console.Clear();
} }
~Output_CLI() { ~Output_CLI() {
Console.Read(); Console.Read();
} }
public override void doOutput() { public override void doOutput() {
@ -31,82 +31,82 @@ namespace vampi {
this.drawStatistics(); this.drawStatistics();
} }
public void testSpielfeld() { public void testSpielfeld() {
Position pos; Position pos;
Spielfeld nachbarfeld; Spielfeld nachbarfeld;
//Schleife ueber alle Spielfelder //Schleife ueber alle Spielfelder
for (int y = 1; y <= Settings.size; y++) { for (int y = 1; y <= Settings.size; y++) {
for (int x = 1; x <= Settings.size; x++) { for (int x = 1; x <= Settings.size; x++) {
pos.x = x; pos.x = x;
pos.y = y; pos.y = y;
Console.SetCursorPosition(x, y); Console.SetCursorPosition(x, y);
Console.BackgroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Green;
Console.Write(' '); Console.Write(' ');
//Schleife ueber alle Nachbarfelder eines Spielfeldes //Schleife ueber alle Nachbarfelder eines Spielfeldes
for (int lage = 1; lage <= 8; lage++) { for (int lage = 1; lage <= 8; lage++) {
nachbarfeld = Program.sflaeche.getSpielfeld(pos).getNachbarfeld(lage); nachbarfeld = Program.sflaeche.getSpielfeld(pos).getNachbarfeld(lage);
if (nachbarfeld != null) { //Nachbarfeld existiert if (nachbarfeld != null) { //Nachbarfeld existiert
Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y);
Console.BackgroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Red;
Console.Write(' '); Console.Write(' ');
Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y); Console.SetCursorPosition(nachbarfeld.Pos.x, nachbarfeld.Pos.y);
System.Threading.Thread.Sleep(10); System.Threading.Thread.Sleep(10);
Console.BackgroundColor = ConsoleColor.Gray; Console.BackgroundColor = ConsoleColor.Gray;
Console.Write(' '); Console.Write(' ');
} }
} }
Console.SetCursorPosition(x, y); Console.SetCursorPosition(x, y);
Console.BackgroundColor = ConsoleColor.Gray; Console.BackgroundColor = ConsoleColor.Gray;
Console.Write(' '); Console.Write(' ');
}//for }//for
}//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 drawStatistics() { public void drawGameMap() {
int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); Position pos;
int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); Console.SetCursorPosition(0, 0);
Console.WriteLine("\n" + String.Format("Steps Done: {0:D5} ", Program.AnzSimDone)); for (pos.y = 1; pos.y <= Settings.size; pos.y++) {
Console.WriteLine(String.Format("Einwohner: {0:D} / Vampire: {1:D} ", Ecount, Vcount)); for (pos.x = 1; pos.x <= Settings.size; pos.x++) {
Console.WriteLine(String.Format("Verhältnis Vampire/Einwohner = 1/{0:N5} ", (double)Ecount / Vcount)); Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur;
Console.WriteLine(String.Format("Bedeckung: {0:N5} %", (double)(Ecount + Vcount) / Settings.size*Settings.size * 100) + " ");
} 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) + " ");
}
} }
} }

View File

@ -108,29 +108,29 @@ namespace vampi {
fp.Lock(); fp.Lock();
Position pos; Position pos;
Color c = Color.Black; Color c = Color.Black;
for (pos.y = 1; pos.y <= Settings.size; pos.y++) { for (pos.y = 1; pos.y <= Settings.size; pos.y++) {
for (pos.x = 1; pos.x <= Settings.size; pos.x++) { for (pos.x = 1; pos.x <= Settings.size; pos.x++) {
Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur; Spielfigur sf = Program.sflaeche.getSpielfeld(pos).Sfigur;
if (sf == null) c = Settings.guiColorEmpty; //Spielfeld leer if (sf == null) c = Settings.guiColorEmpty; //Spielfeld leer
else { //Spielfeld besetzt else { //Spielfeld besetzt
switch (sf.Typ) { switch (sf.Typ) {
case Spielfigur.TYPE_HUMAN: case Spielfigur.TYPE_HUMAN:
if (((Einwohner)sf).Infected) if (((Einwohner)sf).Infected)
c = getGradient(Settings.guiColorHumanInfected, 100-(double)(sf.Age-Settings.humanMaxAge+Settings.humanInfectedMaxAge)*100/(double)Settings.humanInfectedMaxAge); c = getGradient(Settings.guiColorHumanInfected, 100-(double)(sf.Age-Settings.humanMaxAge+Settings.humanInfectedMaxAge)*100/(double)Settings.humanInfectedMaxAge);
else 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); c = getGradient(Settings.guiColorHumanMale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge);
else else
c = getGradient(Settings.guiColorHumanFemale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge); c = getGradient(Settings.guiColorHumanFemale, 100-(double)sf.Age*100/(double)Settings.humanMaxAge);
break; break;
case Spielfigur.TYPE_VAMPIRE: case Spielfigur.TYPE_VAMPIRE:
c = getGradient(Settings.guiColorVampire, 100-(double)sf.Age*100/(double)Settings.vampireMaxAge); c = getGradient(Settings.guiColorVampire, 100-(double)sf.Age*100/(double)Settings.vampireMaxAge);
break; break;
} }
} }
fp.SetPixel(pos.x-1, pos.y-1, c); fp.SetPixel(pos.x-1, pos.y-1, c);
} }
} }
fp.Unlock(true); fp.Unlock(true);
fg.DrawImage(this.field, 0, Output_GUI.statsHeight, Settings.size * this.scale, Settings.size * this.scale); 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); g.Clear(Color.White);
int lineSpc = 10; int lineSpc = 10;
int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER); int Ecount = Einwohner.Count; // sflaeche.countTypeOccurrences(Typliste.EINWOHNER);
int Vcount = Vampir.Count; // sflaeche.countTypeOccurrences(Typliste.VAMPIR); 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("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("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); g.DrawString(String.Format("C{0:N} D{1:N}", Program.lastCalcTime, Program.lastStatsTime), Settings.guiFont, Settings.guiFontBrush, 200, 0);

View File

@ -1,19 +1,19 @@
using System; using System;
namespace vampi { namespace vampi {
public struct Position { public struct Position {
public int x; public int x;
public int y; public int y;
public Position(int x, int y) { public Position(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public Position(Position pos) { public Position(Position pos) {
this.x = pos.x; this.x = pos.x;
this.y = pos.y; this.y = pos.y;
} }
} }
} }

View File

@ -1,23 +1,22 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Text;
using System.Text;
using System.Timers; using System.Timers;
using System.Diagnostics; using System.Diagnostics;
namespace vampi { namespace vampi {
class Program { class Program {
private static int anz_sim = 100000; private static int anz_sim = 100000;
private static int anz_sim_done = 0; private static int anz_sim_done = 0;
public static int AnzSimDone { public static int AnzSimDone {
get { return Program.anz_sim_done; } 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 Spielflaeche sflaeche = new Spielflaeche(Settings.size, Settings.coveragePercent, Settings.vampireRatio);
public static double lastCalcTime = 0; public static double lastCalcTime = 0;
public static double lastStatsTime = 0; public static double lastStatsTime = 0;
public static double lastOverallTime = 0; public static double lastOverallTime = 0;
static void Main(string[] args) { static void Main(string[] args) {
Output output = new Output_GUI(); Output output = new Output_GUI();
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
@ -28,10 +27,10 @@ namespace vampi {
sw2.Stop(); sw2.Stop();
Program.lastOverallTime = sw2.Elapsed.TotalMilliseconds/Settings.drawEveryNthStep; Program.lastOverallTime = sw2.Elapsed.TotalMilliseconds/Settings.drawEveryNthStep;
sw2.Reset(); sw2.Reset();
sw2.Start(); sw2.Start();
sw.Reset(); sw.Reset();
sw.Start(); sw.Start();
Program.lastCalcTime /= Settings.drawEveryNthStep; Program.lastCalcTime /= Settings.drawEveryNthStep;
output.doOutput(); output.doOutput();
sw.Stop(); sw.Stop();
Program.lastCalcTime = 0; Program.lastCalcTime = 0;
@ -39,11 +38,11 @@ namespace vampi {
} }
if (output.requestAbort) break; if (output.requestAbort) break;
sw.Reset(); sw.Reset();
sw.Start(); sw.Start();
sflaeche.simulateStep(); sflaeche.simulateStep();
sw.Stop(); sw.Stop();
Program.lastCalcTime += sw.Elapsed.TotalMilliseconds; Program.lastCalcTime += sw.Elapsed.TotalMilliseconds;
} }
}//Main }//Main
}//class }//class
}//namespace }//namespace

View File

@ -1,16 +1,16 @@
using System; using System;
using System.Drawing; using System.Drawing;
namespace vampi { namespace vampi {
public abstract class Settings { public abstract class Settings {
public const int size = 120; public const int size = 120;
public const int coveragePercent = 77; public const int coveragePercent = 77;
public const int vampireRatio = 3; public const int vampireRatio = 3;
public const int drawEveryNthStep = 1; public const int drawEveryNthStep = 1;
public const ConsoleColor colorHuman = ConsoleColor.Green; public const ConsoleColor colorHuman = ConsoleColor.Green;
public const ConsoleColor colorHumanInfected = ConsoleColor.DarkMagenta; public const ConsoleColor colorHumanInfected = ConsoleColor.DarkMagenta;
public const ConsoleColor colorVampire = ConsoleColor.Red; public const ConsoleColor colorVampire = ConsoleColor.Red;
public const ConsoleColor colorEmpty = ConsoleColor.Gray; public const ConsoleColor colorEmpty = ConsoleColor.Gray;
/* public static Color guiColorHuman = Color.LimeGreen; /* public static Color guiColorHuman = Color.LimeGreen;
@ -25,22 +25,22 @@ namespace vampi {
public static Font guiFont = new Font("sans-serif", 8); public static Font guiFont = new Font("sans-serif", 8);
public static Brush guiFontBrush = Brushes.Black; public static Brush guiFontBrush = Brushes.Black;
public const int humanLegalSexAge = 10; public const int humanLegalSexAge = 10;
public const int humanVampireConversionPercent = 5; public const int humanVampireConversionPercent = 5;
public const int humanMaxInitAge = 10; public const int humanMaxInitAge = 10;
public const int humanMaxAge = 80; public const int humanMaxAge = 80;
public const int humanInfectedMaxAge = 20; public const int humanInfectedMaxAge = 20;
public const bool humanInfectedCanReproduceWithNormal = false; public const bool humanInfectedCanReproduceWithNormal = false;
public const bool humanNormalCanReproduceWithInfected = false; public const bool humanNormalCanReproduceWithInfected = false;
public const int vampireInitialFill = 100; public const int vampireInitialFill = 100;
public const int vampireMaxInitAge = 60; public const int vampireMaxInitAge = 60;
public const int vampireMaxAge = 500; public const int vampireMaxAge = 500;
public const int vampireDecreaseFillPerStep = 3; public const int vampireDecreaseFillPerStep = 3;
public const int vampireIncreaseFillPerBite = 1; public const int vampireIncreaseFillPerBite = 1;
public const bool vampireInfectOnlyOneHuman = false; public const bool vampireInfectOnlyOneHuman = false;
public const bool vampireInfectOneOrMoreHumans = false; public const bool vampireInfectOneOrMoreHumans = false;
public static string[] namesMale = { 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", "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", "Yolande", "Yvette", "Yerba", "Yvonne",
"Zoe", "Zenja", "Zancara", "Zarah" "Zoe", "Zenja", "Zancara", "Zarah"
}; };
} }
} }

View File

@ -1,38 +1,38 @@
using System; using System;
namespace vampi namespace vampi
{ {
public class Spielfeld { public class Spielfeld {
private Position pos; private Position pos;
private Spielflaeche sflaeche; private Spielflaeche sflaeche;
private Spielfigur sfigur = null; private Spielfigur sfigur = null;
private int[] mx = {-1, 0, 1, 1, 1, 0, -1, -1}; private int[] mx = {-1, 0, 1, 1, 1, 0, -1, -1};
private int[] my = {-1, -1, -1, 0, 1, 1, 1, 0}; private int[] my = {-1, -1, -1, 0, 1, 1, 1, 0};
public Spielfigur Sfigur { public Spielfigur Sfigur {
get { return this.sfigur; } get { return this.sfigur; }
set { this.sfigur = value; } set { this.sfigur = value; }
} }
public Position Pos { public Position Pos {
get { return pos; } get { return pos; }
} }
public Spielfeld(Position pos, Spielflaeche sflaeche) { public Spielfeld(Position pos, Spielflaeche sflaeche) {
this.pos = pos; this.pos = pos;
this.sflaeche = sflaeche; this.sflaeche = sflaeche;
} }
public Spielfeld getNachbarfeld(int lage) { public Spielfeld getNachbarfeld(int lage) {
return this.sflaeche.getSpielfeld(this.getNachbarpos(lage)); return this.sflaeche.getSpielfeld(this.getNachbarpos(lage));
} }
private Position getNachbarpos(int lage) { private Position getNachbarpos(int lage) {
Position nachbarpos = new Position(this.pos); Position nachbarpos = new Position(this.pos);
nachbarpos.x += mx[lage - 1]; nachbarpos.x += mx[lage - 1];
nachbarpos.y += my[lage - 1]; nachbarpos.y += my[lage - 1];
return nachbarpos; return nachbarpos;
} }
} }
} }

View File

@ -1,41 +1,41 @@
using System; using System;
using System.Collections; using System.Collections;
namespace vampi namespace vampi
{ {
public abstract class Spielfigur { public abstract class Spielfigur {
public const int F_TYPE = 0; public const int F_TYPE = 0;
public const int TYPE_HUMAN = 1; public const int TYPE_HUMAN = 1;
public const int TYPE_VAMPIRE = 2; public const int TYPE_VAMPIRE = 2;
public const int F_AGE = 1; public const int F_AGE = 1;
public const int F_MAXAGE = 2; public const int F_MAXAGE = 2;
public int[] props = new int[20]; public int[] props = new int[20];
protected Spielfeld sfeld; protected Spielfeld sfeld;
public int Age { public int Age {
get { return this.props[F_AGE]; } get { return this.props[F_AGE]; }
} }
public int Typ { public int Typ {
get { return this.props[F_TYPE]; } get { return this.props[F_TYPE]; }
} }
public Spielfigur(Spielfeld sfeld) { public Spielfigur(Spielfeld sfeld) {
sfeld.Sfigur = this; sfeld.Sfigur = this;
this.sfeld = sfeld; this.sfeld = sfeld;
} }
public virtual void runStep() { public virtual void runStep() {
this.props[F_AGE]++; this.props[F_AGE]++;
if (this.props[F_AGE] >= this.props[F_MAXAGE]) { if (this.props[F_AGE] >= this.props[F_MAXAGE]) {
this.die(); this.die();
} }
} }
public virtual void die() { public virtual void die() {
this.sfeld.Sfigur = null; this.sfeld.Sfigur = null;
} }
} }
} }

View File

@ -1,83 +1,83 @@
using System; using System;
namespace vampi { namespace vampi {
public class Spielflaeche { public class Spielflaeche {
private Spielfeld[,] sfeld_2dim; private Spielfeld[,] sfeld_2dim;
private int groesse; private int groesse;
public int Groesse { public int Groesse {
get { return sfeld_2dim.GetLength(0) * sfeld_2dim.GetLength(1); } get { return sfeld_2dim.GetLength(0) * sfeld_2dim.GetLength(1); }
} }
public Spielflaeche(int groesse) { public Spielflaeche(int groesse) {
this.groesse = groesse; this.groesse = groesse;
sfeld_2dim = new Spielfeld[groesse, groesse]; sfeld_2dim = new Spielfeld[groesse, groesse];
for (int i = 0; i < groesse; i++) { for (int i = 0; i < groesse; i++) {
for (int j = 0; j < groesse; j++) { for (int j = 0; j < groesse; j++) {
sfeld_2dim[i, j] = new Spielfeld(new Position(i+1, j+1), this); sfeld_2dim[i, j] = new Spielfeld(new Position(i+1, j+1), this);
} }
} }
} }
public Spielflaeche(int groesse, int initBedeckung, int initVerhaeltnis) : this(groesse) { public Spielflaeche(int groesse, int initBedeckung, int initVerhaeltnis) : this(groesse) {
initSpielfeld(initBedeckung, initVerhaeltnis); initSpielfeld(initBedeckung, initVerhaeltnis);
} }
private void initSpielfeld(int initBedeckung, int initVerhaeltnis) { private void initSpielfeld(int initBedeckung, int initVerhaeltnis) {
// INFO: Andere Idee: Spielfeld linear bef<65>llen, danach alle Felder durchgehen // INFO: Andere Idee: Spielfeld linear bef<65>llen, danach alle Felder durchgehen
// und das aktuelle Feld jeweils mit einem zuf<75>lligen Feld tauschen // und das aktuelle Feld jeweils mit einem zuf<75>lligen Feld tauschen
int feldX, feldY; int feldX, feldY;
int bedeckung = this.groesse * this.groesse * initBedeckung / 100; // groesse^2 * Prozent int bedeckung = this.groesse * this.groesse * initBedeckung / 100; // groesse^2 * Prozent
int vampire = (int)Math.Round((double)bedeckung / (initVerhaeltnis+1)); // Felder / (Verhaeltnis+1) int vampire = (int)Math.Round((double)bedeckung / (initVerhaeltnis+1)); // Felder / (Verhaeltnis+1)
for (int i = bedeckung; i > 0; i--) { for (int i = bedeckung; i > 0; i--) {
// find an empty, random field // find an empty, random field
do { do {
feldX = Program.random.Next(0, this.groesse); feldX = Program.random.Next(0, this.groesse);
feldY = Program.random.Next(0, this.groesse); feldY = Program.random.Next(0, this.groesse);
} while (this.sfeld_2dim[feldX, feldY].Sfigur != null); } while (this.sfeld_2dim[feldX, feldY].Sfigur != null);
// first set all vampires then set inhabitants // first set all vampires then set inhabitants
if (vampire > 0) { if (vampire > 0) {
new Vampir(this.sfeld_2dim[feldX, feldY]); new Vampir(this.sfeld_2dim[feldX, feldY]);
vampire--; vampire--;
} else { } else {
new Einwohner(this.sfeld_2dim[feldX, feldY]); new Einwohner(this.sfeld_2dim[feldX, feldY]);
} }
} }
} }
public Spielfeld getSpielfeld(Position pos) { public Spielfeld getSpielfeld(Position pos) {
if (pos.x > this.groesse) if (pos.x > this.groesse)
pos.x -= this.groesse; // should be 1 pos.x -= this.groesse; // should be 1
if (pos.x < 1) if (pos.x < 1)
pos.x += this.groesse; // should be this.groesse pos.x += this.groesse; // should be this.groesse
if (pos.y > this.groesse || pos.y < 1) { if (pos.y > this.groesse || pos.y < 1) {
// y-direction doesn't wrap // y-direction doesn't wrap
return null; return null;
} }
return sfeld_2dim[pos.x-1, pos.y-1]; return sfeld_2dim[pos.x-1, pos.y-1];
} }
public int countTypeOccurrences(int typ) { public int countTypeOccurrences(int typ) {
int result = 0; int result = 0;
for (int i = 0; i < this.groesse; i++) { for (int i = 0; i < this.groesse; i++) {
for (int j = 0; j < this.groesse; j++) { for (int j = 0; j < this.groesse; j++) {
if (this.sfeld_2dim[i, j].Sfigur != null && this.sfeld_2dim[i, j].Sfigur.Typ == typ) if (this.sfeld_2dim[i, j].Sfigur != null && this.sfeld_2dim[i, j].Sfigur.Typ == typ)
result++; result++;
} }
} }
return result; return result;
} }
public void simulateStep() { public void simulateStep() {
for (int i = 0; i < this.groesse; i++) { for (int i = 0; i < this.groesse; i++) {
for (int j = 0; j < this.groesse; j++) { for (int j = 0; j < this.groesse; j++) {
if (this.sfeld_2dim[i, j].Sfigur != null) if (this.sfeld_2dim[i, j].Sfigur != null)
this.sfeld_2dim[i, j].Sfigur.runStep(); this.sfeld_2dim[i, j].Sfigur.runStep();
} }
} }
} }
} }
} }

138
Vampir.cs
View File

@ -1,76 +1,76 @@
using System; using System;
namespace vampi namespace vampi
{ {
class Vampir : Spielfigur { class Vampir : Spielfigur {
public const int F_FILLER = 10; public const int F_FILLER = 10;
private static int count = 0; private static int count = 0;
public static int Count { public static int Count {
get { return Vampir.count; } get { return Vampir.count; }
} }
public Vampir(Spielfeld sfeld) public Vampir(Spielfeld sfeld)
: base(sfeld) { : base(sfeld) {
this.props[F_TYPE] = TYPE_VAMPIRE; this.props[F_TYPE] = TYPE_VAMPIRE;
this.props[F_MAXAGE] = Settings.vampireMaxAge; this.props[F_MAXAGE] = Settings.vampireMaxAge;
this.props[F_AGE] = Program.random.Next(0, Settings.vampireMaxInitAge); this.props[F_AGE] = Program.random.Next(0, Settings.vampireMaxInitAge);
this.props[F_FILLER] = Settings.vampireInitialFill; this.props[F_FILLER] = Settings.vampireInitialFill;
Vampir.count++; Vampir.count++;
} }
public override void die() { public override void die() {
base.die(); base.die();
Vampir.count--; Vampir.count--;
} }
public override void runStep() { public override void runStep() {
base.runStep(); base.runStep();
this.props[F_FILLER] -= Settings.vampireDecreaseFillPerStep; this.props[F_FILLER] -= Settings.vampireDecreaseFillPerStep;
if (this.props[F_FILLER] <= 0) { if (this.props[F_FILLER] <= 0) {
this.die(); this.die();
return; return;
} }
this.tryInfect(); this.tryInfect();
} // runStep() } // runStep()
protected void tryInfect() { protected void tryInfect() {
// count humans around me // count humans around me
int humans = 0; int humans = 0;
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
Spielfeld neighbor = this.sfeld.getNachbarfeld(i); Spielfeld neighbor = this.sfeld.getNachbarfeld(i);
if (neighbor != null && neighbor.Sfigur != null) { if (neighbor != null && neighbor.Sfigur != null) {
if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { if (neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) {
if (Settings.vampireInfectOnlyOneHuman) { if (Settings.vampireInfectOnlyOneHuman) {
humans++; humans++;
} else { } else {
((Einwohner)neighbor.Sfigur).infect(); ((Einwohner)neighbor.Sfigur).infect();
this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite;
// humans is still 0, so runStep() will return after this // humans is still 0, so runStep() will return after this
} }
} }
} }
} }
if (humans == 0) if (humans == 0)
return; return;
// randomly infect one human // randomly infect one human
int infect = Program.random.Next(0, humans); int infect = Program.random.Next(0, humans);
for (int i = 1; i <= 8; i++) { for (int i = 1; i <= 8; i++) {
Spielfeld neighbor = this.sfeld.getNachbarfeld(i); Spielfeld neighbor = this.sfeld.getNachbarfeld(i);
if (neighbor != null && neighbor.Sfigur != null && neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) { if (neighbor != null && neighbor.Sfigur != null && neighbor.Sfigur.props[F_TYPE] == TYPE_HUMAN) {
if (infect == 0) { if (infect == 0) {
((Einwohner)neighbor.Sfigur).infect(); ((Einwohner)neighbor.Sfigur).infect();
this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite; this.props[F_FILLER] += Settings.vampireIncreaseFillPerBite;
if (!Settings.vampireInfectOneOrMoreHumans) break; if (!Settings.vampireInfectOneOrMoreHumans) break;
} else { } else {
infect--; infect--;
} }
} }
} }
} }
} }
} }