Skip to content

Commit 13933eb

Browse files
committed
2 parents 0661e29 + 58b891d commit 13933eb

4 files changed

Lines changed: 40 additions & 33 deletions

File tree

.vs/CellSimulator/v15/.suo

4.5 KB
Binary file not shown.

Cell.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Cell
1818
public int maxEnergy = 100;
1919

2020
public string lastAction = "Born";
21-
21+
2222
public Cell(int cellId)
2323
{
2424
id = cellId;
@@ -27,15 +27,15 @@ public Cell(int cellId)
2727
public bool Eat(Random rand)
2828
{
2929
bool didEat = false;
30-
if(rand.Next(0, 101) > 75)
30+
if (rand.Next(0, 101) > 75)
3131
{
3232
didEat = true;
3333
}
3434
if (didEat)
3535
{
3636
food += 40;
3737
}
38-
if(food > maxFood)
38+
if (food > maxFood)
3939
{
4040
food = maxFood;
4141
}
@@ -44,10 +44,10 @@ public bool Eat(Random rand)
4444
public bool Split(Random rand)
4545
{
4646
bool didSplit = false;
47-
if(rand.Next(0, 101) > 75)
47+
if (rand.Next(0, 101) > 75)
4848
{
4949
didSplit = true;
50-
}
50+
}
5151
return didSplit;
5252
}
5353
}

CellOverseer.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CellOverseer
1515

1616
public CellOverseer(int startingCells)
1717
{
18-
for(int i = 0; i < startingCells; ++i)
18+
for (int i = 0; i < startingCells; ++i)
1919
{
2020
AddNewCell(cells);
2121
}
@@ -28,13 +28,13 @@ public void AddNewCell(List<Cell> cellList)
2828
public void SimulateNext()
2929
{
3030
List<Cell> stillAliveCells = new List<Cell>();
31-
foreach(Cell cell in cells)
31+
foreach (Cell cell in cells)
3232
{
33-
if(cell.energy <= 0 || cell.food <= 0)
33+
if (cell.energy <= 0 || cell.food <= 0)
3434
{
3535
continue;
3636
}
37-
if(cell.food >= 60)
37+
if (cell.food >= 60)
3838
{
3939
if (cell.Split(rand))
4040
{
@@ -58,8 +58,8 @@ public void SimulateNext()
5858
else
5959
{
6060
cell.lastAction = "FailEat";
61-
}
62-
}
61+
}
62+
}
6363
cell.energy -= 5;
6464
cell.food -= 5;
6565
if (cell.energy <= 0 || cell.food <= 0)
@@ -74,7 +74,7 @@ public void SimulateNext()
7474
public void SaveToFile()
7575
{
7676
List<string> linesToAppend = new List<string>();
77-
foreach(Cell cell in cells)
77+
foreach (Cell cell in cells)
7878
{
7979
string lineToAppend = cell.id + "|" + cell.age + "|" + cell.food + "|" + cell.maxFood + "|" + cell.energy + "|" + cell.maxEnergy + "|" + cell.lastAction;
8080
linesToAppend.Add(lineToAppend);
@@ -83,15 +83,15 @@ public void SaveToFile()
8383
File.AppendAllLines("CellList-" + nameAppender + ".txt", linesToAppend);
8484
}
8585
public bool LoadFromFile(string fileName)
86-
{
87-
if(!File.Exists(fileName))
86+
{
87+
if (!File.Exists(fileName))
8888
{
8989
return false;
9090
}
9191

9292
List<Cell> listOfCells = new List<Cell>();
9393
string[] dataLines = File.ReadAllLines(fileName);
94-
foreach(string dataLine in dataLines)
94+
foreach (string dataLine in dataLines)
9595
{
9696
string[] splitDataLine = dataLine.Split('|');
9797
Cell tempCell = new Cell(cellIdCounter)

Program.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13+
bool showCellInfo = true;
1314
CellOverseer overseer = new CellOverseer(1);
1415
while (true)
1516
{
16-
foreach(Cell cell in overseer.cells)
17+
if (showCellInfo)
1718
{
18-
Console.WriteLine("ID:" + cell.id + "|AGE:" + cell.age + "|FOOD:" + cell.food + "|ENERGY:" + cell.energy + "|ACTION:" + cell.lastAction);
19+
foreach (Cell cell in overseer.cells)
20+
{
21+
Console.WriteLine("ID:" + cell.id + "|AGE:" + cell.age + "|FOOD:" + cell.food + "|ENERGY:" + cell.energy + "|ACTION:" + cell.lastAction);
22+
}
1923
}
2024
Console.WriteLine("ALIVE:" + overseer.cells.Count());
21-
string keyPressed = Console.ReadLine();
22-
if(keyPressed != "")
25+
string commandEntered = Console.ReadLine().ToLower();
26+
if (!string.IsNullOrEmpty(commandEntered))
2327
{
24-
if(keyPressed == "save")
25-
{
26-
overseer.SaveToFile();
27-
continue;
28-
}
29-
else if(keyPressed == "load")
28+
switch (commandEntered)
3029
{
31-
string fileName = Console.ReadLine();
32-
if(fileName == "")
33-
{
30+
case "save":
31+
overseer.SaveToFile();
3432
continue;
35-
}
36-
else
37-
{
38-
overseer.LoadFromFile(fileName);
39-
}
33+
case "load":
34+
string fileName = Console.ReadLine();
35+
if (string.IsNullOrEmpty(fileName))
36+
{
37+
continue;
38+
}
39+
else
40+
{
41+
overseer.LoadFromFile(fileName);
42+
}
43+
break;
44+
case "switchshowinfo":
45+
showCellInfo = !showCellInfo;
46+
break;
4047
}
4148
}
4249

0 commit comments

Comments
 (0)