public string CupsAfterOne() { int[] list = new int[8]; var iOne = Cups.IndexOf(1) + 1; for (int i = 0; i < list.Length; i++) { int idx = (iOne + i) % 9; list[i] = Cups[idx]; } string val = string.Join("", list.Select(i => i.ToString()).ToArray()); return(val); }
public int FindDestination(int maxValue) { int destCup = CurrentCup; do { destCup--; if (destCup < 1) { destCup = maxValue; } } while (Cups.IndexOf(destCup) == -1); return(destCup); }
public void Play(int until = 10) { for (int move = 1; move <= until; move++) { if (PRINT) { Console.WriteLine($"-- move {move} --"); } if (PRINT) { PrintCups(); } // the current cup int currentCup = Cups[CurrcentCupIndex]; // pick up the three cups that are immediately clockwise of the current cup List <int> cupsInHand = PickupCups(); // select a destination cup. the cup with a label equal to the current cup's label minus one // can only be on the board to search. if not found lowest, go up and search from the highest again int destinationCup = SelectDestinationCup(currentCup); // insert the cup from the hands in the right position after the destination cup var destinationCupIndex = Cups.IndexOf(destinationCup); Cups.InsertRange(destinationCupIndex + 1, cupsInHand); // make sure the holden cup is at the same index as before! otherwise shift everything while (currentCup != Cups[CurrcentCupIndex]) { var firstCup = Cups.FirstOrDefault(); Cups.RemoveAt(0); Cups.Add(firstCup); } // select a new current cup, the cup which is immediately clockwise of the current cup CurrcentCupIndex = GetNextCupIndex(CurrcentCupIndex); } if (PRINT) { Console.WriteLine($"-- final --"); } if (PRINT) { PrintCups(); } }
public int[] Extract3Cups() { var idx = Cups.IndexOf(CurrentCup); int[] cups3 = new int[3]; var count = (Cups.Count - (idx + 1)) >= 3 ? 3 : (Cups.Count - (idx + 1)); Cups.CopyTo(idx + 1, cups3, 0, count); Cups.RemoveRange(idx + 1, count); if (count < 3) { count = 3 - count; Cups.CopyTo(0, cups3, 3 - count, count); Cups.RemoveRange(0, count); } return(cups3); }
protected List <int> PickupCups() { var indexesToPick = new List <int>() { 0, 1, 2 }.Select(i => GetNextCupIndex(CurrcentCupIndex + i)).ToList(); var cupsToPickUp = indexesToPick.Select(i => Cups[i]).ToList(); if (PRINT) { Console.WriteLine($"pick up: {String.Join(", ", cupsToPickUp)}"); } // remove the cups from the circle foreach (var cupToRemove in cupsToPickUp) { var index = Cups.IndexOf(cupToRemove); Cups.RemoveAt(index); } return(cupsToPickUp); }
public void UpdateCurrentCup() { var idx = Cups.IndexOf(CurrentCup) + 1; CurrentCup = (idx < Cups.Count) ? Cups[idx] : Cups[0]; }
public void InsertCups(int[] cups3, int destCup) { Cups.InsertRange(Cups.IndexOf(destCup) + 1, cups3); }