/// <summary> /// This method swaps values 'a' and 'b' in Sequence property of BarleyModel class /// </summary> /// <param name="barleyModel">the instance of class to swap values</param> /// <param name="a">first value to swap</param> /// <param name="b">second value to swap</param> private void Swap(ref BarleyModel barleyModel, int a, int b) { //swap int temp = barleyModel.Sequence[a]; barleyModel.Sequence[a] = barleyModel.Sequence[b]; barleyModel.Sequence[b] = temp; }
/// <summary> /// check if the game is over and you won /// otherwise return game View /// </summary> /// <returns></returns> public ActionResult Index() { BarleyModel sequence = TempData["sequence"] as BarleyModel; if (sequence != null) { if (sequence.Sequence.SequenceEqual(template)) { return(RedirectToAction("Congratulations")); } return(View(sequence)); } return(View(staticModel)); }
public ActionResult Move(BarleyModel barleyModel) { int zeroPosition = barleyModel.Sequence.IndexOf(0); int btnPos = barleyModel.Sequence.IndexOf(barleyModel.Button); if ((zeroPosition + 1) % square != 0) { if (btnPos - zeroPosition == 1) { Swap(ref barleyModel, zeroPosition, btnPos); swaped = true; } else if ((zeroPosition + 1 <= (barleyModel.Sequence.Count - square) && btnPos - zeroPosition == square) || (zeroPosition + 1 > square && zeroPosition - btnPos == square)) { Swap(ref barleyModel, zeroPosition, btnPos); swaped = true; } } if (!swaped) { if (zeroPosition + 4 % square != 0) { if (zeroPosition - btnPos == 1) { Swap(ref barleyModel, zeroPosition, btnPos); } else if ((zeroPosition + 1 <= (barleyModel.Sequence.Count - square) && btnPos - zeroPosition == square) || (zeroPosition + 1 > square && zeroPosition - btnPos == square)) { Swap(ref barleyModel, zeroPosition, btnPos); } } } swaped = false; TempData["sequence"] = barleyModel; staticModel = barleyModel; return(RedirectToAction("Index", "Home")); }