public bool RunFilter(int BoardsToSave) { TimeSpan ElapsedR; cSC.CountLimit = 0; BoardBeingWorked = 0; cSC.bSignalSpinDone = false; cSC.bExceededCountLimit = false; cSC.bExceededONEcolLimit = false; cSC.bExceededTWOcolLimit = false; cSC.bExceededThreecolLimit = false; cSC.bOutOfSpaceInSTLOOKUP = false; cSC.bGotOneSuitAtLeast = false; cSC.SortedScores.Clear(); cSC.BestScoreIndex.Clear(); cSC.TimeDealStarted = TimeLastBest; TimeLastBest = DateTime.Now; // in event we never get a "best" TimeSinceFilterRan = TimeLastBest; OriginalCount = cSC.ThisBoardSeries.Count; while (BoardBeingWorked < cSC.NextBoardSeries.Count) { board nb = cSC.NextBoardSeries[BoardBeingWorked]; if (nb.bIsCompletable) { ReduceSuits(ref nb); } else { cSC.ThisBoardSeries.Add(nb); // pass back for AllPossibleMoves } BoardBeingWorked++; if (0 == (BoardBeingWorked % 100) && ((GlobalClass.TraceBits & 4) > 0)) { ElapsedR = DateTime.Now.Subtract(TimeLastBest); int iSecs = Convert.ToInt32(ElapsedR.TotalSeconds); nb.ShowBoard(); Console.WriteLine("R(" + nb.score + ") " + " BBW:" + BoardBeingWorked); } } // we did all boards cSC.NextBoardSeries.Clear(); return(false); }
static void SolveBoard(ref cSpinControl cSC, string strSpiderBin) { int m = GlobalClass.MAX_FILTERED_BOARDS; GlobalClass.StrategyType gcst = GlobalClass.StrategyType.CONTINUE_SPINNING; board InitialBoard = new board(); cSC.Deck = new cBuildDeck(strSpiderBin, XMLtoRead, ref cSC); MakeAllPossibleMoves = new cSpinAllMoves(ref cSC); cSC.Deck.GetBoardFromSpiderSave(ref InitialBoard); if (InitialBoard.NumEmptyColumns > 0) { GlobalClass.bFoundFirstColunn = false; // started with an empty column, no need to display it. } InitialBoard.ShowBoard(); cSC.cMXF = new cMergeXmlFile(); if (cSC.bJustReadXML) { Console.WriteLine(" Did you mean to read a pre-existing XML file?\n - close window to abort\n - or press N to delete the XML file and abort\n - or press Y to continue"); if (cSC.EventList.Count > 0) { Console.WriteLine(" There are " + cSC.EventList.Count + " events in the event list"); Console.WriteLine(cSC.Deck.strEventInfoPrompt); } ConsoleKeyInfo cki = Console.ReadKey(); Console.WriteLine(""); if (cki.Key.ToString().ToLower() == "n") { File.Delete(cSC.XML_Diag_filename); Environment.Exit(0); } else { string strA = cki.KeyChar.ToString(); int A = -1; int.TryParse(strA, out A); if (A >= 1 && A < 7) { int RequiredOffset = 0; int RequiredDeal = A; string strCmd = Console.ReadLine().ToLower(); strA = strCmd.Substring(0, 1); InitialBoard = null; if (strA == "m") //Format is 2m34 or 3M45 { strCmd = strCmd.Substring(1); RequiredOffset = Convert.ToInt32(strCmd); bool bResult = cSC.Deck.AdvanceToPosition(RequiredDeal, RequiredOffset, out InitialBoard); CallExit(); } else { if (strA == "s") // format is 2s 3S , etc .. show all best scores { bool bResult = cSC.Deck.ShowAllBestScores(RequiredDeal, out InitialBoard); CallExit(); } else if (strA == "t") // format is 2t45 3t9 , etc .. trace using stlookup { // deals 2, 3, and set breakpoint in debugger at the callout for the 45, 9, etc RequiredOffset = Convert.ToInt32(strCmd.Substring(1)); bool bResult = cSC.Deck.AddBoardsToLookup(RequiredDeal, RequiredOffset); CallExit(); } else { Console.WriteLine("Unknown key code. One of M,S,m,s only"); CallExit(); } } } else { bool bResult = cSC.Deck.AdvanceSaveBoard(cki.Key.ToString().ToLower(), out InitialBoard); Debug.Assert(false); } } } //Suitable.CheckSuitability(ref InitialBoard); // the above is not critical to the program as yet, just good info about unexposed cards //cSC.Suitable.CombineLikeSuits(ref InitialBoard); MakeAllPossibleMoves.AddInitialBoard(ref InitialBoard, GlobalClass.FIRST_CARD, ref Suitable); //cSC.JoinSuited.SpinSuitedJoinables(ref cSC.ThisBoardSeries); //if (InitialBoard.bIsCompletable) PerformReduceSuits(ref cSC, 20); //Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) //{ // e.Cancel = true; // Program.keepRunning = false; //}; while (true) // (Program.keepRunning) { RunAway--; if (RunAway < 0) { Console.WriteLine("hit any key except x: TBS:" + cSC.ThisBoardSeries.Count + " NBS:" + cSC.NextBoardSeries.Count); ConsoleKeyInfo cki = Console.ReadKey(); if (cki.KeyChar == 'x') { Environment.Exit(0); } } bool b1 = MakeAllPossibleMoves.RunFilter(m); // m is max to allow after applying filter bool bSameSeed = cSC.PrevSeed.bSameSeed(ref cSC.ThisSeed); if (cSC.bOutOfSpaceInSTLOOKUP) { Console.WriteLine("ran out of space and had to clear stlookup"); cSC.stlookup.Clear(); cSC.bOutOfSpaceInSTLOOKUP = false; } if (cSC.bSpinDidAllBoards) { int iAny = 0; for (int i = 0; i < cSC.ThisBoardSeries.Count; i++) { board nb = cSC.ThisBoardSeries[i]; if (cSC.Suitable.CombineLikeSuits(ref nb)) { iAny++; } } if (iAny > 0) { cSC.bSpinDidAllBoards = false; m = cSC.ThisBoardSeries.Count; Console.WriteLine("Cannot deal but found " + iAny.ToString() + " new boards using join suited" + cSC.bSpinDidAllBoards); } } if (cSC.bSpinDidAllBoards || bSameSeed || cSC.bStopThisRun) { m = HandleDeal(ref cSC, false); Console.WriteLine("Handled deal with m:" + m.ToString() + " bSpin:" + cSC.bSpinDidAllBoards + " SameSeed:" + bSameSeed, " StopRun:" + cSC.bStopThisRun); Console.WriteLine("TBS: " + cSC.ThisBoardSeries.Count + " NBS: " + cSC.NextBoardSeries.Count); cSC.bTrace = true; } else { // unable to finish all boards -or- didnt generate any boards other then the same seeds if (cSC.SortedScores.Count == 0) { // no larger scores were found during the last run: use ThisSeed m = HandleDeal(ref cSC, true); Console.WriteLine("Handled deal from SortedScores.count=0 m:" + m.ToString()); } else { gcst = utils.UseThisManyBoards(ref cSC, ref m); Console.WriteLine("HNS: " + m + " type " + gcst.ToString()); if (gcst != GlobalClass.StrategyType.CONTINUE_SPINNING) { Console.WriteLine("Seem we are to reduce:" + m); HandleNewStrategy(gcst, ref cSC, ref m); } } } } //if (!Program.keepRunning) //{ // cSC.BestBoard.ShowBoard(); // 11nov2012 added this ctrl-c stuff //} }
// return true if was able to create a new board public bool SpinOneCard(ref board tb, int SrcCol, int SrcCard, int DesCol, int DesCard) { if (cSC.bTrigger) { Console.WriteLine(MethodBase.GetCurrentMethod().Name); } int iSecs = 0; TimeSpan TimeSinceLastBest; tb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_BS); tb.moveto(SrcCol, SrcCard, DesCol); tb.ReScoreBoard(); if (tb.NotifySuitJustCompleted) { NumSuitsWritten++; bool WriteOnce = true; string strJustCompleted = "_"; int j = 1; for (int i = 0; i < 4; i++) { if ((j & tb.bitJustCompleted) > 0) { strJustCompleted += GlobalClass.cSuits[i] + "_"; SuitsCompleted[i]++; if (SuitsCompleted[i] > 1) { WriteOnce = false; } } j = j << 1; } if (WriteOnce) { string strName = cSC.Deck.SaveBoardAsBin(ref tb, eSavedType.eSUIT); cSC.Deck.SaveBoardMoves(ref tb, strName); } } if (tb.score >= cSC.BestScore) { if (tb.score == cSC.BestScore) { LastDuplicateBoard = tb; } else { cSC.BestScore = tb.score; cBestScore cBS = new cBestScore(cSC.ThisBoardSeries.Count, tb.score, tb.NumEmptyColumns, tb.NumCompletedSuits, tb.bOnLastDeal); if (tb.NotifySuitJustCompleted) { cBS.SuitJustCompleted |= tb.bitJustCompleted; } cSC.SortedScores.Add(cBS); cSC.BestBoard = tb; LastDuplicateBoard = null; } } else { if (tb.bOnLastDeal && false) { cBestScore cBS = new cBestScore(cSC.ThisBoardSeries.Count, tb.score, tb.NumEmptyColumns, tb.NumCompletedSuits, tb.bOnLastDeal); if (tb.NotifySuitJustCompleted) { cBS.SuitJustCompleted |= tb.bitJustCompleted; } cSC.SortedScores.Add(cBS); } } tb.NotifySuitJustCompleted = false; //tb.from = tb.ID; // board was not spun off here tb.ID = cSC.ThisBoardSeries.Count; tb.AssignCompletedID(); if ((GlobalClass.TraceBits & 8) > 0 || cSC.bTrigger) { tb.ShowRawBoard(); } cSC.ThisBoardSeries.Add(tb); cSC.CountLimit++; TimeSinceLastBest = DateTime.Now.Subtract(TimeLastBest); if (cSC.bSignalSpinDone) { return(false); } if (TimeSinceLastBest.TotalSeconds > cSC.MaxValues.TimeOutBest) { Console.WriteLine("Aborting due to timeing out"); if ((GlobalClass.TraceBits & 2) > 0) { Console.WriteLine("Best1: " + cSC.BestScore + " dups:" + cSC.NumberDuplicates + " BoardsScored:" + cSC.CountLimit + " LastBoardSpun:" + BoardBeingWorked); Console.WriteLine("Suits made (dchs) " + SuitsCompleted[0].ToString() + " " + SuitsCompleted[1].ToString() + " " + SuitsCompleted[2].ToString() + " " + SuitsCompleted[3].ToString() + " StringSuits:" + tb.strSuitsRemoved); if (OriginalCount > BoardBeingWorked) { Console.WriteLine("Did not visit " + (OriginalCount - BoardBeingWorked) + " boards from last deal"); } } cSC.bSpinTimedOut = true; cSC.bSignalSpinDone = true; return(false); } if (tb.score > GlobalClass.SUIT_WEIGHT) { Num3000++; if (tb.NotifySuitJustCompleted) { tb.NotifySuitJustCompleted = false; utils.SetSuitableValues(ref cSC, ref tb); //cSC.bSignalSpinDone = true; cSC.bGotOneSuitAtLeast = true; //Console.WriteLine("Completed Suit"); //return true; } } else if (tb.NumEmptyColumns > 2) { NumThreeOrBetter++; if (NumThreeOrBetter > 100 && Num3000 == 0) { cSC.bSignalSpinDone = true; cSC.bExceededThreecolLimit = true; Console.WriteLine("ABORT: Too many Three Column events: " + NumThreeOrBetter); return(false); } } else if (tb.NumEmptyColumns > 1) { NumTwo++; // if (NumTwo > (cSC.MaxValues.MaxInserts / (1000 - 198 * tb.DealCounter)) && NumThreeOrBetter == 0) if (NumTwo > (1 + tb.DealCounter) * 200 && NumThreeOrBetter == 0) { cSC.bSignalSpinDone = true; cSC.bExceededTWOcolLimit = true; Console.WriteLine("ABORT: Too many Two Column events: " + NumTwo); return(false); } } else if (tb.NumEmptyColumns > 0) { NumOne++; //if (NumOne > ((1 + tb.DealCounter) * 1000) && NumTwo == 0) if (NumTwo == 0) { if (NumOne > (tb.DealCounter < 2 ? GlobalClass.SUIT_WEIGHT : (2 * GlobalClass.SUIT_WEIGHT))) { cSC.bSignalSpinDone = true; cSC.bExceededONEcolLimit = true; Console.WriteLine("ABORT: Too many One Column events: " + NumOne); return(false); } } } if (0 == (cSC.CountLimit % 5000) && ((GlobalClass.TraceBits & 4) > 0)) { dtNow = DateTime.Now; TimeSpan tc = dtNow.Subtract(tb.TimeOfLastDeal); string strSinceLD = ""; if (tc.TotalSeconds < 60) { strSinceLD = Convert.ToInt32(tc.TotalSeconds).ToString("0") + "s"; } else { strSinceLD = Convert.ToInt32(tc.TotalMinutes).ToString("0") + "m"; } dtDiff = dtNow.Subtract(dtLast); dtLast = dtNow; iSecs = Convert.ToInt32(dtDiff.TotalSeconds); tb.ShowBoard(); Console.WriteLine("Q(" + cSC.SortedScores.Count + ") " + cSC.BestScore + " dups:" + cSC.NumberDuplicates + " limit:" + cSC.CountLimit / 1000 + "k i:" + BoardBeingWorked + " et:" + strSinceLD + " dif:" + (cSC.CountLimit - BoardBeingWorked) + " 1c:" + NumOne + " 2c:" + NumTwo + " 3+" + NumThreeOrBetter + " " + strSpinType); cSC.stlookup.ShowBufferStats(); } // if (MoveValue == board.DEALT_A_CARD) return bCreatedBoard; // no need to check timouts, etc as we just want to get this board into the system if (cSC.CountLimit >= cSC.MaxValues.MaxInserts) { cSC.bSignalSpinDone = true; cSC.bExceededCountLimit = true; Console.WriteLine("Aborting! Exceeded limit when counting"); } if (cSC.bSignalSpinDone && (GlobalClass.TraceBits & 2) > 0) { dtNow = DateTime.Now; dtDiff = dtNow.Subtract(dtLast); dtLast = dtNow; iSecs = Convert.ToInt32(dtDiff.TotalSeconds); tb.ShowBoard(); Console.WriteLine("SpinDone - Best: " + cSC.BestScore + " dups:" + cSC.NumberDuplicates + " limit:" + cSC.CountLimit / 1000 + "k i:" + BoardBeingWorked + " et:" + iSecs + " dif:" + (cSC.CountLimit - BoardBeingWorked) + " 1c:" + NumOne + " 2c:" + NumTwo + " 3+" + NumThreeOrBetter); return(false); } if (cSC.bSignalSpinDone) { return(false); } LastCount = cSC.ThisBoardSeries.Count; if (cSC.BestScore > GlobalClass.COL_WEIGHT) { if (cSC.BestScore > LastBest) { if ((GlobalClass.TraceBits & 2) > 0) { Console.WriteLine(TraceBoard(LastCount - 1)); dtDiff = DateTime.Now.Subtract(dtFirstBoard); tb.ShowBoard(); TimeSinceFilter = DateTime.Now.Subtract(TimeSinceFilterRan); Console.WriteLine("Extra:(" + tb.ExtraPoints + ") " + cSC.BestScore + " dups:" + cSC.NumberDuplicates + " limit:" + cSC.CountLimit + " i:" + BoardBeingWorked + " TO: " + cSC.MaxValues.TimeOutBest + " (Sec) MaxIter:" + cSC.MaxValues.MaxInserts + " TimeSinceFilterRan(sec):" + TimeSinceFilter.TotalSeconds.ToString("0")); } } LastBest = cSC.BestScore; } return(true); // bSignalSpinDone; }