/// <summary>method:UndoForm /// constructor2 /// </summary> /// <param name="mainForm"></param> /// <param name="cms"></param> public UndoForm(MainForm mainForm, CardMoveStore cms) { InitializeComponent(); this.mainForm = mainForm; this.cms = cms; }
/// <summary>method:cardInUndeal_Click /// event handler for card click in undeal panel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void cardInUndeal_Click(object sender, EventArgs e) { if (pnlTmp.Parent != null) { for (int i = (pnlTmp.Controls.Count - 1); i >= 0; i--) { Card card = pnlTmp.Controls[i] as Card; card.Location = card.Position; card.Parent = pnlTmp.Parent; card.BringToFront(); } pnlTmp.Parent = null; } Card self = sender as Card; self.CardDraw("unuse", form.unuse.Controls.Count * 2, form.unuse.Controls.Count * 2, 1); self.Parent = form.unuse; self.BringToFront(); // record move Card[] cardsArray = new Card[1]; cardsArray[0] = self; cardsArray[0].Position = cardsArray[0].Location; cardsArray[0].Click -= cardInUndeal_Click; cardsArray[0].Area = "unuse"; cardsArray[0].Name = cardsArray[0].Name.Substring(0, cardsArray[0].Name.Length - 1) + "1"; //cardsArray[0].Text = cardsArray[0].Name + "-"+cardsArray[0].Parent.Name; if (!AutoReplay.isAutoReplaying) { cms = RecordMove.RecordOneMove(cardsArray, form.undeal, form.unuse, form); lblCurrentStep.Text = cms.moves.Count.ToString(); } }
/// <summary>method:ReplayGame /// Performing operation when replay button is clicked. /// </summary> /// <param name="cms"></param> /// <param name="mainform"></param> public static void ReplayGame(CardMoveStore cms, MainForm mainform) { // Get the initial distributing of cards List <List <Card> > initCardsWithinAreaList = GetInitForReplay(cms, mainform); // Get the Movement records List <MoveNode> mnList = cms.moves; // Setup the inital distrubuting SetupInitalForReplay(initCardsWithinAreaList, mainform); // Start replaying(loop all the movement records and simulate the movement by using user32.dll and timer) StartReplay(mnList, mainform); }
/// <summary>method:GetInitForReplay /// tools method for setting up replay condition /// </summary> /// <param name="cms"></param> /// <param name="mainform"></param> /// <returns></returns> public static List <List <Card> > GetInitForReplay(CardMoveStore cms, MainForm mainform) { List <List <Card> > result = new List <List <Card> >(); int[][] initScreenshot = cms.game.init; for (int i = 0; i < initScreenshot.Length; i++) { List <Card> cardList = new List <Card>(); if (initScreenshot[i].Length > 0) { for (int j = 0; j < initScreenshot[i].Length; j++) { cardList.Add(Tools.ParseToCard(initScreenshot[i][j], i)); } } result.Add(cardList); } return(result); }
/// <summary>method:resetUndealFromUnuse_Click /// event handler for clicking reset icon in undeal panel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void resetUndealFromUnuse_Click(object sender, EventArgs e) { if (form.undeal.Controls.Count == 0) { if (pnlTmp.Parent != null) { for (int i = (pnlTmp.Controls.Count - 1); i >= 0; i--) { Card card = pnlTmp.Controls[i] as Card; card.Location = card.Position; card.Parent = pnlTmp.Parent; card.BringToFront(); } pnlTmp.Parent = null; } //MessageBox.Show("refreshed."); Card[] cardsArray = new Card[form.unuse.Controls.Count]; int count = form.unuse.Controls.Count - 1; for (int i = count; i >= 0; i--) { Card card = form.unuse.Controls[i] as Card; card.Area = "undeal"; card.Name = card.Name.Substring(0, card.Name.Length - 1) + "0"; //card.Text = card.Name + "-" + card.Area; card.Parent = form.undeal; card.Side = BackEnum.Back; card.Position = new Point(0, 0); card.CardDraw("undeal", 0, 0, -1); card.Click += cardInUndeal_Click; undealCards.Push(card); cardsArray[count - i] = card; } // record move if (!AutoReplay.isAutoReplaying) { cms = RecordMove.RecordOneMove(cardsArray, form.unuse, form.undeal, form); lblCurrentStep.Text = cms.moves.Count.ToString(); } } }
private void btnPerform_Click(object sender, EventArgs e) { // if (testScriptName == "storage-3.json" || testScriptName == "storage-7.json") { MessageBox.Show("Please test this requirement manually by referring the assotiated testcase."); return; } string stringJson; string jsonfile = System.Windows.Forms.Application.StartupPath + @"../../../" + @"/test/" + testScriptName; using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile)) { using (JsonTextReader reader = new JsonTextReader(file)) { stringJson = JToken.ReadFrom(reader).ToString().Replace("\r\n", ""); } } // set RecordMove.cms cmsForTest = JsonConvert.DeserializeObject <CardMoveStore>(stringJson); foreach (MoveNode move in cmsForTest.moves) { for (int i = 0; i < 13; i++) { Array.Reverse(move.screenshot[i]); } } foreach (int[] area in cmsForTest.game.init) { Array.Reverse(area); } // perfrom the test ReplayGame(cmsForTest, mainFormForTest); this.WindowState = FormWindowState.Minimized; }
/// <summary>method:slot_DragDrop /// event handeling for drag and drop to slot(table) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void slot_DragDrop(object sender, DragEventArgs e) { if (pnlTmp.Controls.Count == 0) { return; } pnlTmp = ((Panel)e.Data.GetData(typeof(Panel))); pnlTmp.Name = "pnlTmp"; Card topCard = ((Card)pnlTmp.Controls[pnlTmp.Controls.Count - 1]);//-1 Panel targetPnl = (Panel)sender; string sourcePnlName = topCard.Area; if (topCard.Area.ToString() == targetPnl.Name.ToString()) { if (pnlTmp.Parent != null) { for (int i = (pnlTmp.Controls.Count - 1); i >= 0; i--) { Card card = pnlTmp.Controls[i] as Card; card.Location = card.Position; card.Parent = pnlTmp.Parent; card.BringToFront(); } pnlTmp.Parent = null; } return; } ; //rules: When the slot is empty, then it is only allowed to put in the cards which the ACE as the top card. if (!IsCanMovedTo(topCard, targetPnl, "SlotEmpty")) { return; } //rules:When the card's number is continuous and the colours are alternated, then they can be dropped. if (!IsCanMovedTo(topCard, targetPnl, "ContinuousAndColoursAlternated")) { return; } int movedCardsAmount = pnlTmp.Controls.Count; Card[] cardsArray = new Card[movedCardsAmount]; for (int i = 0; i < movedCardsAmount; i++) { cardsArray[i] = (Card)pnlTmp.Controls[i]; } int count = targetPnl.Controls.Count; for (int i = cardsArray.Length - 1; i >= 0; i--) { cardsArray[i].Location = new Point(0, (count) * 30); cardsArray[i].Position = cardsArray[i].Location; cardsArray[i].Click -= cardInUndeal_Click; cardsArray[i].Parent = targetPnl; cardsArray[i].Area = targetPnl.Name; //cardsArray[i].Text = cardsArray[i].Name + "-" + cardsArray[i].Area; cardsArray[i].BringToFront(); count++; } pnlTmp.Size = new Size(0, 0); pnlTmp.Parent = null; pnlTmp.SendToBack(); //source movement Panel sourcePnl = (Panel)form.Controls.Find(sourcePnlName, true)[0]; if (sourcePnl.Controls.Count >= 1) { Card lastCardInPanelAfterMove = (Card)(sourcePnl.Controls[0]); lastCardInPanelAfterMove.CardDraw(1); lastCardInPanelAfterMove.Name = lastCardInPanelAfterMove.Name.Substring(0, lastCardInPanelAfterMove.Name.Length - 1) + "1"; //lastCardInPanelAfterMove.Text = lastCardInPanelAfterMove.Name + "-"+lastCardInPanelAfterMove.Parent.Name; } // record move if (!AutoReplay.isAutoReplaying) { cms = RecordMove.RecordOneMove(cardsArray, sourcePnl, targetPnl, form); lblCurrentStep.Text = cms.moves.Count.ToString(); } //Discriminate failure: if (IsFailure(form)) { if (MessageBox.Show("You have been unable to successfully complete the game.") == DialogResult.OK) { return; } } }
/// <summary>method:set_DragDrop /// event handeling for drag and drop to sets /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void set_DragDrop(object sender, DragEventArgs e) { pnlTmp = ((Panel)e.Data.GetData(typeof(Panel))); if (pnlTmp.Controls.Count == 0) { return; } pnlTmp.Name = "pnlTmp"; Card topCard = ((Card)pnlTmp.Controls[pnlTmp.Controls.Count - 1]); string sourcePnlName = topCard.Area; Panel targetPnl = (Panel)sender; if (topCard.Area.ToString() == targetPnl.Name.ToString()) { if (pnlTmp.Parent != null) { for (int i = (pnlTmp.Controls.Count - 1); i >= 0; i--) { Card card = pnlTmp.Controls[i] as Card; card.Location = card.Position; card.Parent = pnlTmp.Parent; card.BringToFront(); } pnlTmp.Parent = null; } return; } ; //Rules:It is only allowed to put on "A" when the current set is empty if (!IsCanMovedToSet(new Card[1] { topCard }, targetPnl, "SetEmpty")) { return; } //Rules: It is only allowed to put on the card which is with the same suit and with 1 bigger digit than the one existing in the current set when the set is setting with certain digit if (!IsCanMovedToSet(pnlTmp.Controls.Cast <Control>().ToArray(), targetPnl, "SetNotEmpty")) { return; } int movedCardsAmount = pnlTmp.Controls.Count; Card[] cardsArray = new Card[movedCardsAmount]; for (int i = 0; i < movedCardsAmount; i++) { cardsArray[i] = (Card)pnlTmp.Controls[i]; } int count = targetPnl.Controls.Count; for (int i = cardsArray.Length - 1; i >= 0; i--) { cardsArray[i].Location = new Point(0, count * 8); cardsArray[i].Position = cardsArray[i].Location;////////////////// cardsArray[i].Click -= cardInUndeal_Click; cardsArray[i].Parent = targetPnl; cardsArray[i].Area = targetPnl.Name; //cardsArray[i].Text = cardsArray[i].Name + "-" + cardsArray[i].Area; cardsArray[i].BringToFront(); count++; } pnlTmp.Size = new Size(0, 0); pnlTmp.Parent = null; pnlTmp.SendToBack(); //source movement Panel sourcePnl = (Panel)form.Controls.Find(sourcePnlName, true)[0]; if (sourcePnl.Controls.Count >= 1) { Card lastCardInPanelAfterMove = (Card)(sourcePnl.Controls[0]); lastCardInPanelAfterMove.CardDraw(1); lastCardInPanelAfterMove.Name = lastCardInPanelAfterMove.Name.Substring(0, lastCardInPanelAfterMove.Name.Length - 1) + "1"; //lastCardInPanelAfterMove.Text = lastCardInPanelAfterMove.Name + "-"+lastCardInPanelAfterMove.Parent.Name; } // record move if (!AutoReplay.isAutoReplaying) { cms = RecordMove.RecordOneMove(cardsArray, sourcePnl, targetPnl, form); lblCurrentStep.Text = cms.moves.Count.ToString(); } //Identification completed successfully:All the cards in each one of the 4 sets are startted from A and then listed orderly from 6 to K and being with the same suit. if (IsCompletedSuccessfully(form, "Sets")) { if (MessageBox.Show("You have successfully completed the game") == DialogResult.OK) { Button btnReplay = form.Controls.Find("btnReplay", true)[0] as Button; form.customedTimer1.Stop(); //btnReplay.Enabled = true; return; } } }
public TestForm(ref CardMoveStore cms, MainForm form) { InitializeComponent(); cmsForTest = cms; mainFormForTest = form; }
/// <summary>method:ReloadGame /// About getting local file ref: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.startuppath?view=netframework-4.8 /// </summary> /// <param name="mainform"></param> public static void ReloadGame(MainForm mainform) { // read the stored file into list string stringJson; string jsonfile = System.Windows.Forms.Application.StartupPath + @"../../../" + @"/test/" + STORAGE_FILE_NAME; using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile)) { using (JsonTextReader reader = new JsonTextReader(file)) { stringJson = JToken.ReadFrom(reader).ToString().Replace("\r\n", ""); } } // set the cms cmsForReload = JsonConvert.DeserializeObject <CardMoveStore>(stringJson); if ("1".Equals(cmsForReload.game.init[3][1].ToString().Substring(3, 1))) { foreach (MoveNode move in cmsForReload.moves) { for (int i = 0; i < 13; i++) { Array.Reverse(move.screenshot[i]); } } foreach (int[] area in cmsForReload.game.init) { Array.Reverse(area); } } RecordMove.cms = cmsForReload; // display cards onto the table // call game manager to undo the game(pass the "undoSeletedMoveNode" to mainform for perform the undo operation) List <List <Card> > undoCardsWithinAreaList = new List <List <Card> >(); int[][] undoSeletedScreenshot = new int[13][]; if (RecordMove.cms.moves.Count == 0) { undoSeletedScreenshot = RecordMove.cms.game.init; } else { undoSeletedScreenshot = ((RecordMove.cms.moves[RecordMove.cms.moves.Count - 1]) as MoveNode).screenshot; } for (int i = 0; i < undoSeletedScreenshot.Length; i++) { List <Card> cardList = new List <Card>(); if (undoSeletedScreenshot[i].Length > 0) { for (int j = 0; j < undoSeletedScreenshot[i].Length; j++) { cardList.Add(Tools.ParseToCard(undoSeletedScreenshot[i][j], i)); } } undoCardsWithinAreaList.Add(cardList); } undoGame(mainform, undoCardsWithinAreaList); }