Advertisement
Guest User

/r/dailyprogrammer 186 Intermediate C# solution by /u/Davipb

a guest
Nov 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7.  
  8. namespace CodingDead
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             #region VARIABLES
  16.             List<int> Map = new List<int>(20 * 20);
  17.             for (int i = 0; i < Map.Capacity; i++)
  18.                 Map.Add(0);
  19.  
  20.             Random RNG = new Random();
  21.  
  22.             ulong GLOBAL_ZombiesMoved = 0;
  23.             ulong GLOBAL_HuntersMoved = 0;
  24.             ulong GLOBAL_VictimsMoved = 0;
  25.             ulong GLOBAL_HunterSingleKills = 0;
  26.             ulong GLOBAL_HunterDoubleKills = 0;
  27.             ulong GLOBAL_BitHunters = 0;
  28.             ulong GLOBAL_BitVictims = 0;
  29.  
  30.             bool Draw_Maps = false;
  31.             int Draw_Scale = 10;
  32.  
  33.             int[] numbs = new int[3];
  34.             int TotalTicks = 0;
  35.  
  36.             #endregion
  37.  
  38.             #region XYZT INPUT
  39.         start:
  40.             Console.WriteLine("Enter Zombies Hunters Victims Ticks:");
  41.  
  42.             string[] line = Console.ReadLine().Split(' ');
  43.  
  44.             if (line.Length != 4)
  45.             {
  46.                 Console.WriteLine("\nPlease enter a properly formatted input\n");
  47.                 goto start;
  48.             }
  49.  
  50.             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))
  51.             {
  52.                 Console.WriteLine("\nOnly integer numbers are accepted\n");
  53.                 goto start;
  54.             }
  55.  
  56.             if (numbs.Sum() > Map.Capacity)
  57.             {
  58.                 Console.WriteLine("\nx + y + z must be lower than {0}\n", Map.Capacity);
  59.                 goto start;
  60.             }
  61.             #endregion
  62.  
  63.             #region DRAW INPUT
  64.         askDraw:
  65.             Console.WriteLine("\nDraw map every tick? (y\\n)\n");
  66.             string ans = Console.ReadLine().ToLower();
  67.             if (ans == "y")
  68.                 Draw_Maps = true;
  69.             else if (ans == "n")
  70.                 Draw_Maps = false;
  71.             else
  72.                 goto askDraw;
  73.  
  74.             if (Draw_Maps)
  75.             {
  76.             askScale:
  77.                 Console.WriteLine("Enter map scale (1:1 = 1 tile, 1 pixel):");
  78.                 ans = Console.ReadLine();
  79.                 if (!int.TryParse(ans, out Draw_Scale))
  80.                     goto askScale;
  81.             }
  82.             #endregion
  83.  
  84.             #region INITIALIZE
  85.             for (int n = 0; n < numbs.Length; n++)
  86.                 for (int i = numbs[n]; i > 0; i--)
  87.                 {
  88.                     // 0 = Empty, 1 = Zombie, 2 = Hunter, 3 = Victim
  89.                     int[] available = GetAvailable(Map).ToArray();
  90.                     Map[available[RNG.Next(available.Length)]] = n+1;
  91.                 }
  92.             #endregion
  93.  
  94.             #region DRAW STARTING MAP
  95.             if (!Draw_Maps)
  96.             {
  97.             askStartDraw:
  98.                 Console.WriteLine("\nDraw starting map? (y\\n)\n");
  99.                 ans = Console.ReadLine().ToLower();
  100.                 if (ans == "y")
  101.                     DrawMap(Map, 10, "START.png");
  102.                 else if (ans == "n")
  103.                 { }
  104.                 else
  105.                     goto askStartDraw;
  106.             }
  107.             else
  108.                 DrawMap(Map, Draw_Scale, "START.png");
  109.  
  110.             #endregion
  111.  
  112.             // Main loop
  113.             for (int tick = 0; tick < TotalTicks; tick++)
  114.             {
  115.                 if (Draw_Maps)
  116.                     DrawMap(Map, Draw_Scale, "Maps/Tick" + tick.ToString() + ".png");
  117.  
  118.                 //Debug.WriteLine("Ticking, tick: {0}", tick+1);
  119.                 List<int> ClonedList = new List<int>(Map);
  120.  
  121.                 #region MOVE
  122.                 // Move
  123.                 for (int i = 0; i < ClonedList.Count; i++)
  124.                 {
  125.                     if (ClonedList[i] == 0)
  126.                         continue;
  127.  
  128.                     if (ClonedList[i] != 3 || CheckSidesFor(Map, i, 1, true))
  129.                     {
  130.                         //Debug.WriteLine("Moving index {0}, it's a {1}", i, ClonedList[i]);
  131.  
  132.                         int[] pos = IndexToPos(i);
  133.                         int[] newpos = pos;
  134.                         List<int> notUsed = Enumerable.Range(0, ClonedList[i] == 1 ? 4 : 8).ToList();
  135.  
  136.                         do
  137.                         {
  138.                             int dir = notUsed[RNG.Next(notUsed.Count)];
  139.                             newpos = DirToPos(dir, pos);
  140.  
  141.                             notUsed.Remove(dir);
  142.                             notUsed.Sort();;
  143.  
  144.                         } while (newpos[0] == -1);
  145.  
  146.                         if (pos[0] < 20 && pos[1] < 20 && pos[0] >= 0 && pos[1] >= 0)
  147.                             if (Map[PosToIndex(pos)] == 0)
  148.                             {
  149.                                 Map[i] = 0;
  150.                                 Map[PosToIndex(pos)] = ClonedList[i];
  151.  
  152.                                 if (ClonedList[i] == 1)
  153.                                     GLOBAL_ZombiesMoved++;
  154.                                 else if (ClonedList[i] == 2)
  155.                                     GLOBAL_HuntersMoved++;
  156.                                 else if (ClonedList[i] == 3)
  157.                                     GLOBAL_VictimsMoved++;
  158.  
  159.                             }
  160.                     }
  161.  
  162.                 }
  163.  
  164.                 #endregion
  165.  
  166.                 ClonedList = new List<int>(Map);
  167.  
  168.                 #region SLAY
  169.                 // Slaying
  170.                 for (int i = 0; i < ClonedList.Count; i++)
  171.                 {
  172.                     if (ClonedList[i] != 2)
  173.                         continue;
  174.  
  175.                     if (!CheckSidesFor(Map, i, 1, true))
  176.                         continue;
  177.  
  178.                     //Debug.WriteLine("Slaying, Hunter is at {0}", i);
  179.  
  180.                     int[] pos = IndexToPos(i);
  181.  
  182.                     int slayed = 0;
  183.  
  184.                     List<int> notUsed = Enumerable.Range(0, 8).ToList();
  185.  
  186.                     for (int d = 0; d < 8; d++)
  187.                     {
  188.                         if (slayed == 2)
  189.                             break;
  190.  
  191.                         int dir = notUsed[RNG.Next(notUsed.Count)];
  192.  
  193.                         if (DirToPos(dir, pos)[0] != -1)
  194.                         {
  195.                             if (Map[PosToIndex(DirToPos(dir, pos))] == 1)
  196.                             {
  197.                                 Map[PosToIndex(DirToPos(dir, pos))] = 0;
  198.                                 slayed++;
  199.                             }
  200.                         }
  201.  
  202.                         notUsed.Remove(dir);
  203.                         notUsed.Sort();
  204.                     }
  205.  
  206.                     if (slayed == 2)
  207.                         GLOBAL_HunterDoubleKills++;
  208.                     else
  209.                         GLOBAL_HunterSingleKills++;
  210.                 }
  211.                 #endregion
  212.  
  213.                 ClonedList = new List<int>(Map);
  214.  
  215.                 #region BITE
  216.                 //Biting
  217.                 for (int i = 0; i < ClonedList.Count; i++)
  218.                 {
  219.                     if (ClonedList[i] != 1)
  220.                         continue;
  221.  
  222.                     if (!CheckSidesFor(Map, i, 2))
  223.                         continue;
  224.  
  225.                     if (!CheckSidesFor(Map, i, 3))
  226.                         continue;
  227.  
  228.                     //Debug.WriteLine("Biting, Zombie is at {0}", i);
  229.  
  230.                     int[] pos = IndexToPos(i);
  231.  
  232.                     List<int> notUsed = Enumerable.Range(0, 4).ToList();
  233.  
  234.                     for (int d = 0; d < 4; d++)
  235.                     {
  236.                         int dir = notUsed[RNG.Next(notUsed.Count)];
  237.  
  238.                         if (DirToPos(dir, pos)[0] != -1)
  239.                         if (Map[PosToIndex(DirToPos(dir, pos))] == 2)
  240.                         {
  241.                             Map[PosToIndex(DirToPos(dir, pos))] = 1;
  242.                             GLOBAL_BitHunters++;
  243.                             break;
  244.                         }
  245.                         else if (Map[PosToIndex(DirToPos(dir, pos))] == 3)
  246.                         {
  247.                             Map[PosToIndex(DirToPos(dir, pos))] = 1;
  248.                             GLOBAL_BitVictims++;
  249.                             break;
  250.                         }
  251.      
  252.                         notUsed.Remove(dir);
  253.                         notUsed.Sort();
  254.                     }
  255.                 }
  256.                 #endregion
  257.  
  258.             }
  259.  
  260.             #region DRAW FINAL MAP
  261.             if (!Draw_Maps)
  262.             {
  263.             askEndDraw:
  264.                 Console.WriteLine("\nDraw final map? (y\\n)\n");
  265.                 ans = Console.ReadLine().ToLower();
  266.                 if (ans == "y")
  267.                     DrawMap(Map, 10, "END.png");
  268.                 else if (ans == "n")
  269.                 { }
  270.                 else
  271.                     goto askEndDraw;
  272.             }
  273.             else
  274.                 DrawMap(Map, Draw_Scale, "END.png");
  275.             #endregion
  276.  
  277.             #region FINAL CALCULATION
  278.             int finalVictims = 0;
  279.             int finalHunters = 0;
  280.             int finalZombies = 0;
  281.  
  282.             var finalScore = Map.GroupBy(x => x);
  283.             foreach (var group in finalScore)
  284.             {
  285.                 if (group.Key == 1)
  286.                     finalZombies += group.Count();
  287.                 if (group.Key == 2)
  288.                     finalHunters += group.Count();
  289.                 if (group.Key == 3)
  290.                     finalVictims += group.Count();
  291.             }
  292.  
  293.             int zombiedecay = numbs[0] - finalZombies;
  294.             int humandecay = (numbs[1] + numbs[2]) - (finalHunters + finalVictims);
  295.             #endregion
  296.  
  297.  
  298.             Console.Clear();
  299.             Console.WriteLine("\n------------------FINISHED!-------------------\n");
  300.             Console.WriteLine("Units moved by zombies: \t{0}", GLOBAL_ZombiesMoved);
  301.             Console.WriteLine("Units moved by hunters: \t{0}", GLOBAL_HuntersMoved);
  302.             Console.WriteLine("Units moved by victims: \t{0}", GLOBAL_VictimsMoved);
  303.             Console.WriteLine();
  304.             Console.WriteLine("Single kills by hunters: \t{0}", GLOBAL_HunterSingleKills);
  305.             Console.WriteLine("Double kills by hunters: \t{0} ({1} zombies)", GLOBAL_HunterDoubleKills, GLOBAL_HunterDoubleKills * 2);
  306.             Console.WriteLine("Total kills by hunters: \t{0}", GLOBAL_HunterSingleKills + GLOBAL_HunterDoubleKills * 2);
  307.             Console.WriteLine();
  308.             Console.WriteLine("Victims bitten: \t\t{0}", GLOBAL_BitVictims);
  309.             Console.WriteLine("Hunters bitten: \t\t{0}", GLOBAL_BitHunters);
  310.             Console.WriteLine("Total bitten: \t\t\t{0}", GLOBAL_BitHunters + GLOBAL_BitVictims);
  311.             Console.WriteLine();
  312.             Console.WriteLine("STARTING POPULATION");
  313.             Console.WriteLine("Victims: \t\t\t{0}", numbs[2]);
  314.             Console.WriteLine("Hunters: \t\t\t{0}", numbs[1]);
  315.             Console.WriteLine("Zombies: \t\t\t{0}", numbs[0]);
  316.             Console.WriteLine();
  317.             Console.WriteLine("FINAL POPULATION");
  318.             Console.WriteLine("Victims: \t\t\t{0}", finalVictims);
  319.             Console.WriteLine("Hunters: \t\t\t{0}", finalHunters);
  320.             Console.WriteLine("Zombies: \t\t\t{0}", finalZombies);
  321.             Console.ReadKey();
  322.             Console.Clear();
  323.             Console.WriteLine("------------------RESULT-------------------");
  324.  
  325.             if (zombiedecay > humandecay)
  326.                 Console.WriteLine("HUMANS WON!");
  327.             else if (humandecay > zombiedecay)
  328.                 Console.WriteLine("ZOMBIES WON!");
  329.             else if (humandecay == zombiedecay)
  330.                 Console.WriteLine("TIE!");
  331.  
  332.             Console.WriteLine("Zombie decay: \t{0}", zombiedecay);
  333.             Console.WriteLine("Human decay: \t{0}", humandecay);
  334.            
  335.  
  336.            
  337.  
  338.         }
  339.  
  340.         static int[] DirToPos(int dir, int[] startpos)
  341.         {
  342.             int[] pos = startpos;
  343.  
  344.             switch (dir)
  345.             {
  346.                 case 0:
  347.                     pos[0]--; break;
  348.                 case 1:
  349.                     pos[1]--; break;
  350.                 case 2:
  351.                     pos[0]++; break;
  352.                 case 3:
  353.                     pos[1]++; break;
  354.                 case 4:
  355.                     pos[0]--; pos[1]--; break;
  356.                 case 5:
  357.                     pos[0]++; pos[1]--; break;
  358.                 case 6:
  359.                     pos[0]--; pos[1]++; break;
  360.                 case 7:
  361.                     pos[0]++; pos[1]++; break;
  362.             }
  363.  
  364.             if (pos[0] < 0 || pos[0] > 19 || pos[1] < 0 || pos[1] > 19)
  365.                 return new int[] { -1, -1 };
  366.  
  367.             return pos;
  368.         }
  369.  
  370.         static IEnumerable<int> GetAvailable(List<int> map)
  371.         {
  372.             for (int i = 0; i < map.Count; i++)
  373.                 if (map[i] == 0)
  374.                     yield return i;
  375.         }
  376.  
  377.         static bool CheckSidesFor(List<int> map, int index, int checkfor, bool diag = false)
  378.         {
  379.             int[] pos = IndexToPos(index);
  380.  
  381.             if (pos[0] > 0 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] })] == checkfor)
  382.                 return true;
  383.             if (pos[0] < 19 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] })] == checkfor)
  384.                 return true;
  385.             if (pos[1] > 0 && map[PosToIndex(new int[] { pos[0], pos[1] - 1 })] == checkfor)
  386.                 return true;
  387.             if (pos[1] < 19 && map[PosToIndex(new int[] { pos[0], pos[1] + 1 })] == checkfor)
  388.                 return true;
  389.  
  390.             if (diag)
  391.             {
  392.                 if (pos[0] > 0 && pos[1] > 0 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] - 1 })] == checkfor)
  393.                     return true;
  394.                 if (pos[0] < 19 && pos[1] > 0 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] - 1 })] == checkfor)
  395.                     return true;
  396.                 if (pos[0] > 0 && pos[1] < 19 && map[PosToIndex(new int[] { pos[0] - 1, pos[1] + 1 })] == checkfor)
  397.                     return true;
  398.                 if (pos[0] < 19 && pos[1] < 19 && map[PosToIndex(new int[] { pos[0] + 1, pos[1] + 1 })] == checkfor)
  399.                     return true;
  400.             }
  401.  
  402.             return false;
  403.         }
  404.  
  405.         static int[] IndexToPos(int index)
  406.         {
  407.             return new int[] {index % 20, (index - (index % 20)) / 20};
  408.         }
  409.  
  410.         static int PosToIndex(int[] pos)
  411.         {
  412.             return pos[0] + pos[1] * 20;
  413.         }
  414.  
  415.         static void DrawMap(List<int> map, int scale, string name)
  416.         {
  417.             Bitmap image = new Bitmap(20 * scale, 20 * scale);
  418.  
  419.             for (int i = 0; i < map.Count; i++)
  420.             {
  421.                 int[] pos = IndexToPos(i);
  422.                 Color pixColor = map[i] == 0 ? Color.White : map[i] == 1 ? Color.Green : map[i] == 2 ? Color.Red : Color.Salmon;
  423.  
  424.                 for (int z = 0; z < scale * scale; z++)
  425.                     image.SetPixel((pos[0] * scale) + (z % scale), (pos[1] * scale) + ((z - (z % scale)) / scale), pixColor);
  426.             }
  427.  
  428.             image.Save(name);
  429.         }
  430.     }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement