public static void TryRobbing(List <Robber> robbers, List <Citizen> citizens, out int robbedCitizens) { robbedCitizens = 0; foreach (Citizen citizen in citizens) { bool multipleRobbersCanRobTheSameCitizen = true; if (!citizen.Belongings.Any()) // om inte medborgaren har något på sig kan han inte bli rånad { continue; } if (multipleRobbersCanRobTheSameCitizen) { foreach (Robber r in robbers) { if (citizens.First().Equals(citizen)) { GameField.AddMarker(citizen.VerticalPosition, citizen.HorizontalPosition, 'H'); ConsoleFunctions.ShowLocations(); } r.StealFrom(citizen); r.PeopleRobbed++; robbedCitizens++; citizen.TimesRobbed++; Event.SomeoneRobbed(citizen, r); } } else { if (citizens.First().Equals(citizen)) { GameField.AddMarker(citizen.VerticalPosition, citizen.HorizontalPosition, 'H'); ConsoleFunctions.ShowLocations(); } Robber robber = robbers[Initialize.rand.Next(robbers.Count)]; // en slumpad rånare rånar personen robber.StealFrom(citizen); robber.PeopleRobbed++; citizen.TimesRobbed++; robbedCitizens++; Event.SomeoneRobbed(citizen, robber); } } }
public static void SomeoneRobbed(Citizen citizen, Robber robber) { int robberClocks = robber.StolenGoods.OfType <Item>().Where(x => x.ItemName == "Clock").Count(); int robberCash = robber.StolenGoods.OfType <Item>().Where(x => x.ItemName == "Cash").Count(); int robberKeys = robber.StolenGoods.OfType <Item>().Where(x => x.ItemName == "Keys").Count(); int robberPhone = robber.StolenGoods.OfType <Item>().Where(x => x.ItemName == "Phone").Count(); int citizenClocks = citizen.Belongings.OfType <Item>().Where(x => x.ItemName == "Clock").Count(); int citizenCash = citizen.Belongings.OfType <Item>().Where(x => x.ItemName == "Cash").Count(); int citizenKeys = citizen.Belongings.OfType <Item>().Where(x => x.ItemName == "Keys").Count(); int citizenPhone = citizen.Belongings.OfType <Item>().Where(x => x.ItemName == "Phone").Count(); int offsetFromTop = 4; int middlePositionLeft = GameField.GameWidth / 2; string[] toPrint = { "========================", " A robber has stolen ", " from a citizen! ", " This is their current ", " inventories. ", "======ROBBER============", $" Clock x {robberClocks}", $" Cash x {robberCash}", $" Keys x {robberKeys}", $" Phone x {robberPhone}", " ", "======CITIZEN===========", $" Clock x {citizenClocks}", $" Cash x {citizenCash}", $" Keys x {citizenKeys}", $" Phone x {citizenPhone}", " ", "========================" }; int lastLine = offsetFromTop + toPrint.Length; int lastColumn = middlePositionLeft + toPrint[0].Length + 1; ConsoleFunctions.PrintWho(citizen); //visar en pil brevid personen som det hände något med ConsoleFunctions.PrintInfoStatScreen(toPrint, offsetFromTop, middlePositionLeft); Thread.Sleep(5000); ConsoleFunctions.ResetPartialScreen(offsetFromTop, middlePositionLeft, lastLine, lastColumn); Prison.PrisonTimerTick(); Console.SetCursorPosition(0, GameField.GameHeight + 1); }
public static void CheckCollision(out int robbed, out int caught) { robbed = 0; caught = 0; //räknar igenom alla personer och sätter deras vanliga bokstav först, så det inte är halvfyllt när kollisioner ska kollas.. //det var jag inte nöjd med helt enkelt :P foreach (Person p in TownPeople) { GameField.RegularLocationMarkers(p); } foreach (Robber prisoner in Prison.Jail) { GameField.AddMarker(prisoner.VerticalPosition, prisoner.HorizontalPosition, 'F'); } ConsoleFunctions.ShowLocations(); List <Person> listOfAlreadyCheckedPersons = new List <Person>(); foreach (Person person in TownPeople) { List <Cop> copsAtLocation = CopsOnSpot(TownPeople, person); List <Robber> robbersAtLocation = RobbersOnSpot(TownPeople, person); List <Citizen> citizensAtLocation = CitizensOnSpot(TownPeople, person); if (listOfAlreadyCheckedPersons.Contains(person)) // om personen redan har kollats kan vi kolla nästa direkt { continue; } if (robbersAtLocation.Any() && citizensAtLocation.Any()) // om det finns en eller flera rånare/vanliga medborgare ska rånarna ta en sak av varje medborgare { Event.TryRobbing(robbersAtLocation, citizensAtLocation, out int robbedPeople); robbed += robbedPeople; } if (copsAtLocation.Any() && robbersAtLocation.Any()) // och om en polis eller rånare möts ska polisen ta hans saker { Event.TryToCatchRobber(robbersAtLocation, copsAtLocation, out int caughtRobbers); caught += caughtRobbers; } listOfAlreadyCheckedPersons.AddRange(copsAtLocation); listOfAlreadyCheckedPersons.AddRange(robbersAtLocation); listOfAlreadyCheckedPersons.AddRange(citizensAtLocation); } }
static void PrintHiScores(int offset, Citizen mostRobbed, Robber mostRobberies, Robber mostTimesCaught, Robber robberMostItems, Cop copMostItems, Cop mostRobbersBusted) { int i = 0; int placeOnRight = Console.WindowWidth - offset; while (i <= 10) { if (i == 0) { ConsoleFunctions.ColoredText("[C10]Most robbed", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {mostRobbed.ID}[C15], score:[C12] {mostRobbed.TimesRobbed} ", placeOnRight, i + 1); } else if (i == 2) { ConsoleFunctions.ColoredText("[C10]Most robberies", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {mostRobberies.ID}[C15], score:[C12] {mostRobberies.PeopleRobbed} ", placeOnRight, i + 1); } else if (i == 4) { ConsoleFunctions.ColoredText("[C10]Most caught", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {mostTimesCaught.ID}[C15], score:[C12] {mostTimesCaught.TimesCaught} ", placeOnRight, i + 1); } else if (i == 6) { ConsoleFunctions.ColoredText("[C10]Most items [C12](robber)", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {robberMostItems.ID}[C15], score:[C12] {robberMostItems.StolenGoods.Count} ", placeOnRight, i + 1); } else if (i == 8) { ConsoleFunctions.ColoredText("[C10]Most items [C9](cop)", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {copMostItems.ID}[C15], score:[C12] {copMostItems.SiezedItems.Count} ", placeOnRight, i + 1); } else if (i == 10) { ConsoleFunctions.ColoredText("[C10]Most robbers busted", placeOnRight, i); ConsoleFunctions.ColoredText($"[C6]ID:[C10] {mostRobbersBusted.ID}[C15], score:[C12] {mostRobbersBusted.RobbersBusted} ", placeOnRight, i + 1); } i += 2; // plussar på med 2, så nästa utskrift hamnar under id, score (dvs 2 rader under den förra) } }
static void Main() { int numberOfRobbedCitizens = 0; int numberOfRobbersCaught = 0; Console.CursorVisible = false; Initialize.Start(); //List<Person> persons = People.TownPeople; //lite info Console.WriteLine("Om en rånare blir tagen av en polis blir det ett 'A'nhållen\n" + "Om en invånare blir rånad blir det ett 'H'old-up\n" + "Om rånare blir tagen blir det ett stillastående 'F'ånge\n" + "Efter 20 händelser kommer rånaren tillbaks in i spelet\n" + "'P'oliser, 'I'nvånare, 'R'ånare\n" + "Tryck vart som helst för att fortsätta.."); Console.ReadKey(); Console.Clear(); //MoveDirection(persons, out int[] oldDirections); while (true) { //MoveDirection(People.TownPeople, out int[] directions); //CheckDirection(oldDirections, directions); Hiscores.GetScores(); //hämtar highscores Prison.CheckPrison(); //kollar fängelset om det är någon rånare som ska läggas till i spelplanen People.MoveAround(); //alla utom dom i fängelset ska röra sig People.CheckCollision(out int robbed, out int caught); // kollar om någon står på samma plats som någon annan, och skickar tillbaks hur många som blivit rånade/tagna ConsoleFunctions.ShowLocations(); //visar vart alla står numberOfRobbedCitizens += robbed; numberOfRobbersCaught += caught; ConsoleFunctions.PrintInfo(GameField.GameHeight + 2, 0, $"Number of Robbers caught: {numberOfRobbersCaught}\n" + $"Number of Citizens robbed: {numberOfRobbedCitizens}, Robbers in prison: {(Prison.Prisoners>0 ? $"{Prison.Prisoners}, next release: {Prison.NextRelease()/5}" : "none")} \n" + $"Number of robbers not in prison: {People.RobbersIngame} "); Thread.Sleep(300); ConsoleFunctions.ResetDrawnGameMap(); // istället för console clear skriver programmet ett " " där det finns en bokstav (mindre flicker) GameField.ResetField(); // nollställer 2d arrayen så de gamla bokstäverna inte skrivs ut igen } }
public static void TryToCatchRobber(List <Robber> robbers, List <Cop> cops, out int robbersCaught) { robbersCaught = 0; foreach (Robber robber in robbers) { if (!robber.StolenGoods.Any()) // om rånaren inte har något i sin inventory är han ännu inte en rånare (eller har avtjänat sitt straff) { continue; } Cop cop = cops[Initialize.rand.Next(cops.Count)]; cop.TakeGoodsFromRobber(robber); // en slumpad polis tar rånarens saker (om det är flera på samma ställe) robber.TimesCaught++; cop.RobbersBusted++; GameField.AddMarker(robber.VerticalPosition, robber.HorizontalPosition, 'A'); ConsoleFunctions.ShowLocations(); SomeoneBusted(cop, robber); //RobberCaughtPrint(gameField, cop, robber, width, height); //gammal kod! robbersCaught++; } }