// free up a column by joining any SEQ that are joinable // or moves one suited sequence under another of proper rank // this only looks at the bottom card and tries to find an SEQ that can fit under it // of same suit public bool FreeSuitedSuits(ref board tb, ref List <card> LastSEQ) { bool bFound = false; column cCol; card Src = null; card Des = null; tb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_FSS); do { bFound = false; LastSEQ.Clear(); foreach (int e in tb.NonEmpties) { cCol = tb.ThisColumn[e]; LastSEQ.Add(cCol.ThisSeries.Last().topCard); } //LastSEQ.Sort(delegate(series s1, series s2) //{ // return Comparer<int>.Default.Compare(s1.topCard.rank, s2.topCard.rank); //}); // see if any suits can be combined foreach (card tm in tb.BottomMost) { foreach (card bm in LastSEQ) { if (tm.iStack == bm.iStack || tm.rank == 1) { continue; } if ((bm.rank + 1) == tm.rank && bm.suit == tm.suit) { bFound = true; Des = tm; Src = bm; break; } } if (bFound) { break; } } if (bFound) { tb.moveto(Src.iStack, Src.iCard, Des.iStack); tb.ReScoreBoard(); if (tb.NotifySuitJustCompleted) { tb.ExplainNoRescoreBoard("Free Suited Suits succeeded"); tb.NotifySuitJustCompleted = false; } } } while (bFound); tb.AssignCompletedID(); return(false); }
// this gets rid of as many facedown cards as possible // this should be run only if suits are locked out and only a few suits are remaining public int TryUnstackAll(ref board oldtb) { int Any; int Total = 0; int ne = oldtb.NumEmptyColumns; int ru = oldtb.UnexposedTypes.Count; int rs = oldtb.ComputeRemainingSuits(); if (rs > 3) { return(0); // the works best with only a few suits } if (ru > ne) { return(0); // need a bunch of empty columns for sure } if (oldtb.DealCounter < 5) { return(0); // only on last deal } board tb = new board(ref oldtb); tb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_USTK); //tb.ShowBoard(); Any = TryUnstackOne(ref tb, ref Total); //do //{ // Any = TryUnstackOne(ref tb); // if (Any > 0) // { // tb.ReScoreBoard(); // Total += Any; // } //} while (Any > 0); //tb.ExplainBoard("did unstack"); tb.AssignCompletedID(); NewBoards.Add(tb); return(Total); }
public bool ReduceSuits(ref board oldtb) { bool bSuccess = true; card cNext = null; card ExposedCard = null; int e, CardID, LastID; card CardExposed; board tb = new board(ref oldtb); tb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_RSI); OnCounter++; InitializeReduction(ref tb, ref cNext); Debug.Assert(cNext.rank == 13); GetPlaceholders(ref tb, cNext.iStack); if (bStackablesAbove(ref tb, ref cNext)) { bSuccess = UnstackSeriesBelow(ref tb, ref cNext); if (bSuccess) { e = tb.Empties[0]; tb.Empties.RemoveAt(0); CardExposed = tb.moveto(cNext.iStack, cNext.iCard, e); // have to move the king RecoverPlaceholder(ref tb, ref CardExposed); tb.tag = e; InitializeReduction(ref tb, ref cNext); } else { tb.AssignCompletedID(); return(false); // jys !!! may want to try a second or lower ranked card } } else { tb.tag = cNext.iStack; GetPlaceholders(ref tb, tb.tag); } // unstack below the last card (cNext) and the next card while (bSuccess) { bSuccess = UnstackSeriesBelow(ref tb, ref cNext); if (bSuccess) { if (StackableIDs.Count > 0) { CardID = StackableIDs[0]; // next card to gather in StackableIDs.RemoveAt(0); LastID = cNext.ID; cNext = utils.FindCardFromID(ref tb, CardID); bSuccess = UnstackSeriesBelow(ref tb, ref cNext); if (bSuccess) { ExposedCard = tb.moveto(cNext.iStack, cNext.iCard, tb.tag); RecoverPlaceholder(ref tb, ref ExposedCard); } else { tb.AssignCompletedID(); return(false); } } else { tb.ReScoreBoard(); cSC.Suitable.PerformSuitabilityScreening(ref tb); if (tb.bIsCompletable) { cSC.NextBoardSeries.Add(tb); } else { cSC.ThisBoardSeries.Add(tb); } tb.AssignCompletedID(); return(true); } } else { tb.AssignCompletedID(); return(false); } } tb.AssignCompletedID(); return(false); }
// exchange cards that are under the wrong suit public bool CombineLikeSuits(ref board tb) { column cCol = null; int n = 0; bool ChangedAnyBoards = false; int OriginalSrcStack = 0; card bm, Src = null, Des = null; bool bFound = false, bAny = false; List <card> LastSEQ = new List <card>(); // this holds the top card of the last SEQ series card BelowBM, cAbove; // Below Bm rank + 1 must equal to cAbove rank to swap (unless one card is top) tb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_COMBN); do { bFound = false; LastSEQ.Clear(); bAny = FreeSuitedSuits(ref tb, ref LastSEQ); if (tb.NumEmptyColumns == 0) { return(bAny); } //tb.ExplainBoard("before"); // locate the bottom card of the upper series, if any foreach (int e in tb.NonEmpties) { cCol = tb.ThisColumn[e]; n = cCol.ThisSeries.Count; if (n < 2) { continue; } bm = cCol.ThisSeries[n - 2].bottomCard; foreach (card tm in LastSEQ) { if (tm.iStack == bm.iStack) { continue; } if ((bm.rank - 1) == tm.rank && bm.suit == tm.suit) { OriginalSrcStack = tm.iStack; int iCardAbove = tm.iCard - 1; // this card must either be null (top of column) // or it must be 1 greater in rank if (iCardAbove > -1) // there is at least one card above us. It can be facedown too { cAbove = tb.ThisColumn[tm.iStack].Cards[iCardAbove]; if (cAbove.rank != (tm.rank + 1)) { continue; } BelowBM = cCol.Cards[bm.iCard + 1]; if (BelowBM.rank != (cAbove.rank - 1)) { continue; } } bFound = true; Src = tm; Des = bm; break; } } if (bFound == true) { break; } } if (bFound) { card MustMove = cCol.ThisSeries[n - 1].topCard; int nDest = GetBestMove(Src, ref tb); if (nDest < 0) { nDest = tb.Empties[0]; } tb.moveto(MustMove.iStack, MustMove.iCard, nDest); tb.moveto(Src.iStack, Src.iCard, Des.iStack); // do not bother moving from one top to another top when doing a swap if (!(tb.ThisColumn[nDest].Cards.Count == 1 && tb.ThisColumn[OriginalSrcStack].Cards.Count == 0)) { tb.moveto(nDest, MustMove.iCard, OriginalSrcStack); } tb.ReScoreBoard(); if (tb.NotifySuitJustCompleted) { tb.ExplainNoRescoreBoard("Combine Like Suits succeeded"); tb.NotifySuitJustCompleted = false; } ChangedAnyBoards = true; } } while (bFound == true); tb.AssignCompletedID(); //tb.ExplainBoard("after2"); return(ChangedAnyBoards); }
public int SOSExposeTop(ref board tb) { bool bAnySameSuit = false; List <series> TopSeries = new List <series>(); List <cPossibleMoves> ActualMoves = new List <cPossibleMoves>(); NewBoards.Clear(); TryUnstackAll(ref tb); tb.CopyWorkingCards(ref xBottomMost, GlobalClass.WorkingType.tBottomMost); foreach (column cCol in tb.ThisColumn) { if (cCol.iStack > 9) { break; } if (cCol.ThisSOS.Count == 1) { TopSeries.Add(cCol.ThisSOS[0]); } } foreach (card bm in xBottomMost) { foreach (series s in TopSeries) { card tm = s.topCard; if ((tm.rank + 1) == bm.rank) { card bbm = bm; cPossibleMoves cpm = new cPossibleMoves(ref tm, ref bbm); ActualMoves.Add(cpm); } } } foreach (cPossibleMoves cpm in ActualMoves) { if (cpm.bSameSuit) { bAnySameSuit = true; break; } } foreach (cPossibleMoves cpm in ActualMoves) { column cCol; card cCrd; board nb; series SOS; bool bCan; List <series> ListSEQ; List <int> yEmpties = new List <int>(); List <card> yBottomMost = new List <card>(); if (bAnySameSuit && !cpm.bSameSuit) { continue; } nb = new board(ref tb); nb.AssignCompletedID(GlobalClass.TypeCompletedBy.ID_SOSET); CardsToMove.Clear(); cCol = nb.ThisColumn[cpm.From.iStack]; SOS = cCol.ThisSOS[0]; ListSEQ = cCol.ThisSeries; // the top cards in the SEQ are the ones to move in the SOS for (int i = 0; i < SOS.NumSubSeries; i++) { cCrd = ListSEQ[i].topCard; CardsToMove.Add(cCrd); } nb.tag = cpm.To.iStack; nb.CopyWorkingInts(ref yEmpties, GlobalClass.WorkingType.tEmpties); nb.CopyWorkingCards(ref yBottomMost, GlobalClass.WorkingType.tBottomMost); bCan = DisAssemble.bCanDisassembleSOS(SOS.NumSubSeries, ref nb, ref yEmpties, ref yBottomMost, ref CardsToMove); if (bCan) { NewBoards.Add(nb); nb.AssignCompletedID(); } } return(NewBoards.Count); }
// 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; }