#1
23.04.2017 um 15:19 Uhr C#-Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Drunk { class Program { static void Main(string[] args) { HashSet<int> Count = new HashSet<int>(); WalkingGenerator DrunkMan = new WalkingGenerator(); int Position = 3; int number = 0; Random Step = new Random(); Console.WriteLine("Wieviel Durchläufe?"); int Calcnumber = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Calculating"); for (int j = 0; j < Calcnumber; j++) { while (DrunkMan.Check(Position)) { DrunkMan.Walk(ref Position, Step.Next(0, 8)); if (Position > 69) { Count.Add(number); number += 1; } } Position = 3; } float rate = (float)Count.Count / (Calcnumber/100); Console.WriteLine("Der Man hat eine Chance durchzukommen von {0}%", rate); Console.ReadLine(); } } class WalkingGenerator { public void Walk(ref int Coord, int Step) { switch(Step) { case 1: Coord -= 1; break; case 2: Coord += 1; break; case 3: Coord += 7; break; case 4: Coord -= 7; break; case 5: Coord += 6; break; case 6: Coord -= 6; break; case 7: Coord += 8; break; case 0: Coord -= 8; break; } } public bool Check(int Coord) { if (Coord % 7 == 0 || Coord % 7 == 6 || Coord <= 0 || Coord >= 69) { return false; } else { return true; } } } }