public void CompMove() { // For each combination of dice (because it can happen that using them in a different order has a different result // e.g. when a piece is taken with the first throw or only one dice is usable on the first throw for a piece List <List <int> > diceSeqPermut = new List <List <int> >(); // a list of sequences possible from the throw of the dice e.g. diceHi, diceLo or diceLo, diceHi; BGMoveSet thisMoveSet = new BGMoveSet(); // a moveSet to work with to find all the combinations bestMoveSet.Clear(); if (aBGRules.IsPossible(myBoard, theirBoard, dice1, dice2)) // check if there's at least one move { diceSeqPermut = aBGRules.GetDiceSeqPermut(dice1, dice2); // get dice sequences foreach (var diceSeq in diceSeqPermut) { GetPieceRecurse(thisMoveSet, diceSeq, myBoard, theirBoard); } foreach (BGMove move in bestMoveSet.BGMoves) { BoardUpdate(myBoard, theirBoard, move.FMove, move.TMove); // update board } } else { bestMoveSet.BGMoves.Add(new BGMove { FMove = 0, TMove = 0, PieceID = 0, SeqOrder = 0, Di = 0, MoveID = 0 }); } bestMoveSet.Di1 = dice1; bestMoveSet.Di2 = dice2; Message = bestMoveSet.GetMessage(GameOver); Notation = bestMoveSet.GetNotation(false); IWin = GameOver; // if the games ending now then this player must be the winner }
private void GetPieceRecurse(BGMoveSet moveset, List <int> diceSeq, int[] myBoard, int[] theirBoard) { //This routine is called recursively for each di in a sequence 2 or 4 (if doubles) //each piece is tried and the highest scoring sequence is remembered List <BGPiece> pieces = new List <BGPiece>(); // a list of pieces each piece has an id a position int[] myBoard1 = new int[26]; // declare an instance of the board int[] theirBoard1 = new int[26]; int i = moveset.BGMoves.Count(); // move index int f = 0; // piece.Position int t = 0; // to int d = 0; // dice to use Array.Copy(myBoard, myBoard1, 26); Array.Copy(theirBoard, theirBoard1, 26); // work with a copy of the board pieces = GetPieces(myBoard); // create alist of pieces if (i < diceSeq.Count) //there is an unused di in the sequence { bool moveFound = false; foreach (var piece in pieces) { f = piece.Position; d = diceSeq[i]; t = (f - d) < 0 ? 0 : f - d; // 0 represents bearing off; less than zero indicates you can bear off if (aBGRules.IsLegal(f, t, myBoard, theirBoard, d)) // if legal move { BGMove aBGMove = new BGMove(); // add the move to the list of moves aBGMove.PieceID = piece.PieceID; aBGMove.FMove = f; aBGMove.TMove = t; aBGMove.Di = d; aBGMove.SeqOrder = i; aBGMove.CheckerHit = (theirBoard[25 - t] == 1 && t != 0) ? true : false; moveset.BGMoves.Add(aBGMove); BoardUpdate(myBoard1, theirBoard1, f, t); // update board GetPieceRecurse(moveset, diceSeq, myBoard1, theirBoard1); // get another move Array.Copy(myBoard, myBoard1, 26); Array.Copy(theirBoard, theirBoard1, 26); // restore the board moveset.BGMoves.RemoveAt(i); moveFound = true; } } if (moveFound == false) // if no move is found it could be the first dice is unusable and the second may be { moveset.Score = ScoreBoard(myBoard1, theirBoard1); // nevertheless score the move so far and record it if ((moveset.Score > bestMoveSet.Score && moveset.BGMoves.Count == bestMoveSet.BGMoves.Count) || moveset.BGMoves.Count > bestMoveSet.BGMoves.Count) { // either the score is better or there's more move. bestMoveSet.Update(moveset); } } } else { moveset.Score = ScoreBoard(myBoard1, theirBoard1); // at the end of each set, score the moves if (moveset.Score > bestMoveSet.Score) { bestMoveSet.Update(moveset); } } }
public void Update(BGMoveSet moveSet) { BGMoves.Clear(); Di1 = moveSet.Di1; Di2 = moveSet.Di2; Score = moveSet.Score; foreach (BGMove move in moveSet.BGMoves) { BGMove bestMove = new BGMove(move); BGMoves.Add(bestMove); } }