public void InitializeReduction(ref board tb, ref card cNext) { int i, j = 13; tb.ReScoreBoard(); Stackables.Clear(); StackableIDs.Clear(); DoNotUse.Clear(); cNext = tb.SuitedSEQ[0].topCard; int suit = cNext.suit; foreach (series s in tb.SuitedSEQ) { DoNotUse.Add(s.iStack); // not a permanent placeholder till that card is moved (if it is moved) for (i = cNext.iCard; i <= s.bottom; i++) { column cCol = tb.ThisColumn[s.iStack]; Stackables.Add(cCol.Cards[i]); cCol.Cards[i].PartOfStackables = true; Debug.Assert(j == cCol.Cards[i].rank && suit == cCol.Cards[i].suit); j--; } cNext = s.AssemblyLink; // link to next card in "s" assembly if (cNext != null) { StackableIDs.Add(cNext.ID); } } cNext = tb.SuitedSEQ[0].topCard; }
// 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); }
public int TryUnstackOne(ref board tb, ref int total) { int Any; total = 0; do { Any = UnstackOneSeries(ref tb); total += Any; tb.ReScoreBoard(); } while (Any > 0); return(total); }
// this just checks empty columns for space right now // and only looks at SEQ public int ExposeTop(ref board tb) { int i, j, n, nT, nE; int sCost; // cost of all the series that need to be unstacked to expose the facedown card // is the sum of all the SEQ costs plus 1 int tlCount = 0; column cCol; series sCollapse = new series(); // out of sequence series we want to collapse to a series of series for (i = 0; i < 10; i++) { nE = tb.NumEmptyColumns; cCol = tb.ThisColumn[i]; n = cCol.Cards.Count; if (n < 2 || cCol.top == 0) { continue; } sCost = 1; for (j = 0; j < cCol.ThisSeries.Count; j++) { sCost += cCol.ThisSeries[j].nEmptiesToUnstack; } if (sCost <= nE) // series is moveable using empties { sCollapse.bottomCard = cCol.Cards[n - 1]; nT = cCol.top - 1; // point to first un-exposed card sCollapse.topCard = cCol.Cards[nT]; sCollapse.size = cCol.Cards.Count - nT; sCollapse.iStack = sCollapse.topCard.iStack; sCollapse.tag = nT; // this is destination if (utils.bRankable(ref sCollapse, ref tb)) { if (true) { if (!DisAssemble.TryExpose(ref sCollapse, ref tb)) { continue; } tlCount += 1; break; } } } } if (tlCount > 0) { tb.ReScoreBoard(); } return(tlCount); }
// this spins off up to 4 boards, one for each suit if that suit can be reduced // we spin into NextBoardSeries public void PerformSuitabilityScreening(ref board tb) { int i; CombineLikeSuits(ref tb); // this does a rescore which sets bIsCompletable and sets all rank bit for (i = 0; i < 4; i++) { if (tb.SuitStatus[i].bSuitsCompletable) { board nb = new board(ref tb); nb.ReScoreBoard(); nb.BuildingThisSuit = 1 + i; FormCostToComplete(ref nb, i); cSC.NextBoardSeries.Add(nb); } } }
// this joins suited sequences that can be swapped (ie: reversible moves) public bool JoinAllSuitedWhenRankable(ref board tb) { if (cSC.bTrigger) { Console.WriteLine(MethodBase.GetCurrentMethod().Name); } if (tb.NumEmptyColumns < 1) { return(false); } bool bAny = false, bResults = false; do { tb.BuildSortedSEQ(0xf, GlobalClass.eSortedSEQtype.SortByRank); bResults = cSC.JoinSuited.JoinSuitedWhenRankable(ref tb); bAny |= bResults; tb.ReScoreBoard(); } while (bResults); return(bAny); }
// this spins only a series of cards of same suit private bool SpinCardInto(ref board tb, int ColumnToFill) { int i; series s; Debug.Assert(tb.ThisColumn[ColumnToFill].Cards.Count == 0); // do not move a card into a column that contains something for (i = 0; i < 10; i++) { if (OEmpties.Contains(i)) { continue; } // do not move from empty column because it is either empty // or we just put something in it so as to make it not empty if (tb.ThisColumn[i].ThisSeries.Count == 1 && tb.ThisColumn[i].ThisSeries.Last().top == 0) { continue; } // do not create an empty column just to fill another one nb = new board(ref tb); nb.dead = false; s = nb.ThisColumn[i].ThisSeries.Last(); //stats[i]++; nb.moveto(i, s.top, ColumnToFill); //nb.ThisColumn[i].CalculateColumnValue(i); //nb.ThisColumn[ColumnToFill].CalculateColumnValue(ColumnToFill); nb.ReScoreBoard(); NewBoards.Add(nb); } return(false); }
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); }
/* * THIS DOES NOT WORK EXCEPT FOR VERY SIMPLE SEQ SERIES JYS !!!! need UnStack and to use patterns * This joins one SEQ series under another so as to concatanate the two series * */ /* * this just exposes the top card and check to see if it can be recovered * jys this needs to be able to expose all cards that are exposable * (8s,6h,7D) expose to 8S,7D,6H * * right now, the series underneath must be SEQ, use the SOS one for that, but it does not use placeholders * * */ public int ExposeOneTop(ref board tb) { int e, n; int LocAbove; // location of card just above the top card card CardAbove; int Any = 0; int OriginalSource; foreach (card cT in tb.TopMost) { LocAbove = cT.iCard - 1; if (LocAbove <= 0) { continue; // nothing to expose } CardAbove = tb.ThisColumn[cT.iStack].Cards[LocAbove]; if (CardAbove.bFaceUp) { continue; // already exposed } // we want only unexposed cards and the series underneath must be SEQ // we can expose this card by moving the one below it (our card cT) if (CardAbove.rank - 1 != cT.rank) { continue; // nope, wrong rank, we want to move it back } n = tb.ThisColumn[cT.iStack].Cards.Count - cT.iCard; // number of cards to move // the below does no error checking nor any statistics computation if (tb.NumEmptyColumns > 0) { e = tb.Empties[0]; OriginalSource = cT.iStack; tb.PerformMove(cT.iStack, cT.iCard, e, n); tb.PerformMove(e, 0, OriginalSource, n); Any++; } else { // we will have to find a card that can hold it temporarily foreach (card bC in tb.BottomMost) { if (bC.iStack == cT.iStack) { continue; } if (bC.rank - 1 == cT.rank) { OriginalSource = cT.iStack; n = tb.ThisColumn[cT.iStack].Cards.Count - cT.iCard; tb.PerformMove(cT.iStack, cT.iCard, bC.iStack, n); tb.PerformMove(bC.iStack, bC.iCard + 1, OriginalSource, n); Any++; } } } if (Any > 0) { tb.ThisColumn[cT.iStack].CalculateColumnValue(cT.iStack, tb.NumEmptyColumns, tb.BoardState()); } } if (Any > 0) { tb.ReScoreBoard(); // alternately CalcBoardScore and then GetSortedTops return(ExposeOneTop(ref tb)); } return(Any); }
public void AddNewBoard(ref board ThisBoard) { ThisBoard.ReScoreBoard(); ThisBoard.ID = cSC.ThisBoardSeries.Count; cSC.ThisBoardSeries.Add(ThisBoard); }
// 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; }