Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Drawing;
- namespace CodingDead
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region VARIABLES
- List<int> Map = new List<int>(20 * 20);
- for (int i = 0; i < Map.Capacity; i++)
- Map.Add(0);
- Random RNG = new Random();
- ulong GLOBAL_ZombiesMoved = 0;
- ulong GLOBAL_HuntersMoved = 0;
- ulong GLOBAL_VictimsMoved = 0;
- ulong GLOBAL_HunterSingleKills = 0;
- ulong GLOBAL_HunterDoubleKills = 0;
- ulong GLOBAL_BitHunters = 0;
- ulong GLOBAL_BitVictims = 0;
- bool Draw_Maps = false;
- int Draw_Scale = 10;
- int[] numbs = new int[3];
- int TotalTicks = 0;
- #endregion
- #region XYZT INPUT
- start:
- Console.WriteLine("Enter Zombies Hunters Victims Ticks:");
- string[] line = Console.ReadLine().Split(' ');
- if (line.Length != 4)
- {
- Console.WriteLine("\nPlease enter a properly formatted input\n");
- goto start;
- }
- if (!int.TryParse(line[0], out numbs[0]) || !int.TryParse(line[1], out numbs[1]) || !int.TryParse(line[2], out numbs[2]) || !int.TryParse(line[3], out TotalTicks))
- {
- Console.WriteLine("\nOnly integer numbers are accepted\n");
- goto start;
- }
- if (numbs.Sum() > Map.Capacity)
- {
- Console.WriteLine("\nx + y + z must be lower than {0}\n", Map.Capacity);
- goto start;
- }
- #endregion
- #region DRAW INPUT
- askDraw:
- Console.WriteLine("\nDraw map every tick? (y\\n)\n");
- string ans = Console.ReadLine().ToLower();
- if (ans == "y")
- Draw_Maps = true;
- else if (ans == "n")
- Draw_Maps = false;
- else
- goto askDraw;
- if (Draw_Maps)
- {
- askScale:
- Console.WriteLine("Enter map scale (1:1 = 1 tile, 1 pixel):");
- ans = Console.ReadLine();
- if (!int.TryParse(ans, out Draw_Scale))
- goto askScale;
- }
- #endregion
- #region INITIALIZE
- for (int n = 0; n < numbs.Length; n++)
- for (int i = numbs[n]; i > 0; i--)
- {
- // 0 = Empty, 1 = Zombie, 2 = Hunter, 3 = Victim
- int[] available = GetAvailable(Map).ToArray();
- Map[available[RNG.Next(available.Length)]] = n+1;
- }
- #endregion
- #region DRAW STARTING MAP
- if (!Draw_Maps)
- {
- askStartDraw:
- Console.WriteLine("\nDraw starting map? (y\\n)\n");
- ans = Console.ReadLine().ToLower();
- if (ans == "y")
- DrawMap(Map, 10, "START.png");
- else if (ans == "n")
- { }
- else
- goto askStartDraw;
- }
- else
- DrawMap(Map, Draw_Scale, "START.png");
- #endregion
- // Main loop
- for (int tick = 0; tick < TotalTicks; tick++)
- {
- if (Draw_Maps)
- DrawMap(Map, Draw_Scale, "Maps/Tick" + tick.ToString() + ".png");
- //Debug.WriteLine("Ticking, tick: {0}", tick+1);
- List<int> ClonedList = new List<int>(Map);
- #region MOVE
- // Move
- for (int i = 0; i < ClonedList.Count; i++)
- {
- if (ClonedList[i] == 0)
- continue;
- if (ClonedList[i] != 3 || CheckSidesFor(Map, i, 1, true))
- {
- //Debug.WriteLine("Moving index {0}, it's a {1}", i, ClonedList[i]);
- int[] pos = IndexToPos(i);
- int[] newpos = pos;
- List<int> notUsed = Enumerable.Range(0, ClonedList[i] == 1 ? 4 : 8).ToList();
- do
- {
- int dir = notUsed[RNG.Next(notUsed.Count)];
- newpos = DirToPos(dir, pos);
- notUsed.Remove(dir);
- notUsed.Sort();;
- } while (newpos[0] == -1);
- if (pos[0] < 20 && pos[1] < 20 && pos[0] >= 0 && pos[1] >= 0)
- if (Map[PosToIndex(pos)] == 0)
- {
- Map[i] = 0;
- Map[PosToIndex(pos)] = ClonedList[i];
- if (ClonedList[i] == 1)
- GLOBAL_ZombiesMoved++;
- else if (ClonedList[i] == 2)
- GLOBAL_HuntersMoved++;
- else if (ClonedList[i] == 3)
- GLOBAL_VictimsMoved++;
- }
- }
- }
- #endregion
- ClonedList = new List<int>(Map);
- #region SLAY
- // Slaying
- for (int i = 0; i < ClonedList.Count; i++)
- {
- if (ClonedList[i] != 2)
- continue;
- if (!CheckSidesFor(Map, i, 1, true))
- continue;
- //Debug.WriteLine("Slaying, Hunter is at {0}", i);
- int[] pos = IndexToPos(i);
- int slayed = 0;
- List<int> notUsed = Enumerable.Range(0, 8).ToList();
- for (int d = 0; d < 8; d++)
- {
- if (slayed == 2)
- break;
- int dir = notUsed[RNG.Next(notUsed.Count)];
- if (DirToPos(dir, pos)[0] != -1)
- {
- if (Map[PosToIndex(DirToPos(dir, pos))] == 1)
- {
- Map[PosToIndex(DirToPos(dir, pos))] = 0;
- slayed++;
- }
- }
- notUsed.Remove(dir);
- notUsed.Sort();
- }
- if (slayed == 2)
- GLOBAL_HunterDoubleKills++;
- else
- GLOBAL_HunterSingleKills++;
- }
- #endregion
- ClonedList = new List<int>(Map);
- #region BITE
- //Biting
- for (int i = 0; i < ClonedList.Count; i++)
- {
- if (ClonedList[i] != 1)
- continue;
- if (!CheckSidesFor(Map, i, 2))
- continue;
- if (!CheckSidesFor(Map, i, 3))
- continue;
- //Debug.WriteLine("Biting, Zombie is at {0}", i);
- int[] pos = IndexToPos(i);
- List<int> notUsed = Enumerable.Range(0, 4).ToList();
- for (int d = 0; d < 4; d++)
- {
- int dir = notUsed[RNG.Next(notUsed.Count)];
- if (DirToPos(dir, pos)[0] != -1)
- if (Map[PosToIndex(DirToPos(dir, pos))] == 2)
- {
- Map[PosToIndex(DirToPos(dir, pos))] = 1;
- GLOBAL_BitHunters++;
- break;
- }
- else if (Map[PosToIndex(DirToPos(dir, pos))] == 3)
- {
- Map[PosToIndex(DirToPos(dir, pos))] = 1;
- GLOBAL_BitVictims++;
- break;
- }
- notUsed.Remove(dir);
- notUsed.Sort();
- }
- }
- #endregion
- }
- #region DRAW FINAL MAP
- if (!Draw_Maps)
- {
- askEndDraw:
- Console.WriteLine("\nDraw final map? (y\\n)\n");
- ans = Console.ReadLine().ToLower();
- if (ans == "y")
- DrawMap(Map, 10, "END.png");
- else if (ans == "n")
- { }
- else
- goto askEndDraw;
- }
- else
- DrawMap(Map, Draw_Scale, "END.png");
- #endregion
- #region FINAL CALCULATION
- int finalVictims = 0;
- int finalHunters = 0;
- int finalZombies = 0;
- var finalScore = Map.GroupBy(x => x);
- foreach (var group in finalScore)
- {
- if (group.Key == 1)
- finalZombies += group.Count();
- if (group.Key == 2)
- finalHunters += group.Count();
- if (group.Key == 3)
- finalVictims += group.Count();
- }
- int zombiedecay = numbs[0] - finalZombies;
- int humandecay = (numbs[1] + numbs[2]) - (finalHunters + finalVictims);
- #endregion
- Console.Clear();
- Console.WriteLine("\n------------------FINISHED!-------------------\n");
- Console.WriteLine("Units moved by zombies: \t{0}", GLOBAL_ZombiesMoved);
- Console.WriteLine("Units moved by hunters: \t{0}", GLOBAL_HuntersMoved);
- Console.WriteLine("Units moved by victims: \t{0}", GLOBAL_VictimsMoved);
- Console.WriteLine();
- Console.WriteLine("Single kills by hunters: \t{0}", GLOBAL_HunterSingleKills);
- Console.WriteLine("Double kills by hunters: \t{0} ({1} zombies)", GLOBAL_HunterDoubleKills, GLOBAL_HunterDoubleKills * 2);
- Console.WriteLine("Total kills by hunters: \t{0}", GLOBAL_HunterSingleKills + GLOBAL_HunterDoubleKills * 2);
- Console.WriteLine();
- Console.WriteLine("Victims bitten: \t\t{0}", GLOBAL_BitVictims);
- Console.WriteLine("Hunters bitten: \t\t{0}", GLOBAL_BitHunters);
- Console.WriteLine("Total bitten: \t\t\t{0}", GLOBAL_BitHunters + GLOBAL_BitVictims);
- Console.WriteLine();
- Console.WriteLine("STARTING POPULATION");
- Console.WriteLine("Victims: \t\t\t{0}", numbs[2]);
- Console.WriteLine("Hunters: \t\t\t{0}", numbs[1]);
- Console.WriteLine("Zombies: \t\t\t{0}", numbs[0]);
- Console.WriteLine();
- Console.WriteLine("FINAL POPULATION");
- Console.WriteLine("Victims: \t\t\t{0}", finalVictims);
- Console.WriteLine("Hunters: \t\t\t{0}", finalHunters);
- Console.WriteLine("Zombies: \t\t\t{0}", finalZombies);
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine("------------------RESULT-------------------");
- if (zombiedecay > humandecay)
- Console.WriteLine("HUMANS WON!");
- else if (humandecay > zombiedecay)
- Console.WriteLine("ZOMBIES WON!");
- else if (humandecay == zombiedecay)
- Console.WriteLine("TIE!");
- Console.WriteLine("Zombie decay: \t{0}", zombiedecay);
- Console.WriteLine("Human decay: \t{0}", humandecay);
- }
- static int[] DirToPos(int dir, int[] startpos)
- {
- int[] pos = startpos;
- switch (dir)
- {
- case 0:
- pos[0]--; break;
- case 1:
- pos[1]--; break;
- case 2:
- pos[0]++; break;
- case 3:
- pos[1]++; break;
- case 4:
- pos[0]--; pos[1]--; break;
- case 5:
- pos[0]++; pos[1]--; break;
- case 6:
- pos[0]--; pos[1]++; break;
- case 7:
- pos[0]++; pos[1]++; break;
- }
- if (pos[0] < 0 || pos[0] > 19 || pos[1] < 0 || pos[1] > 19)
- return new int[] { -1, -1 };
- return pos;
- }
- static IEnumerable<int> GetAvailable(List<int> map)
- {
- for (int i = 0; i < map.Count; i++)
- if (map[i] == 0)
- yield return i;
- }
- static bool CheckSidesFor(List<int> map, int index, int checkfor, bool diag = false)
- {
- int[] pos = IndexToPos(index);
- if (pos[0] > 0 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] })] == checkfor)
- return true;
- if (pos[0] < 19 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] })] == checkfor)
- return true;
- if (pos[1] > 0 && map[PosToIndex(new int[] { pos[0], pos[1] - 1 })] == checkfor)
- return true;
- if (pos[1] < 19 && map[PosToIndex(new int[] { pos[0], pos[1] + 1 })] == checkfor)
- return true;
- if (diag)
- {
- if (pos[0] > 0 && pos[1] > 0 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] - 1 })] == checkfor)
- return true;
- if (pos[0] < 19 && pos[1] > 0 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] - 1 })] == checkfor)
- return true;
- if (pos[0] > 0 && pos[1] < 19 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] + 1 })] == checkfor)
- return true;
- if (pos[0] < 19 && pos[1] < 19 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] + 1 })] == checkfor)
- return true;
- }
- return false;
- }
- static int[] IndexToPos(int index)
- {
- return new int[] {index % 20, (index - (index % 20)) / 20};
- }
- static int PosToIndex(int[] pos)
- {
- return pos[0] + pos[1] * 20;
- }
- static void DrawMap(List<int> map, int scale, string name)
- {
- Bitmap image = new Bitmap(20 * scale, 20 * scale);
- for (int i = 0; i < map.Count; i++)
- {
- int[] pos = IndexToPos(i);
- Color pixColor = map[i] == 0 ? Color.White : map[i] == 1 ? Color.Green : map[i] == 2 ? Color.Red : Color.Salmon;
- for (int z = 0; z < scale * scale; z++)
- image.SetPixel((pos[0] * scale) + (z % scale), (pos[1] * scale) + ((z - (z % scale)) / scale), pixColor);
- }
- image.Save(name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement