public MoveTuple SuggestNextMove(Int64 Sig) { MoveTuple mv = null; if (uniqueGameStates.ContainsKey(Sig)) { GameNode gameNode = uniqueGameStates[Sig]; GameNode gameNodeChild = gameNode.Children.Find(delegate(GameNode gn) { if (gn.BestScore == gameNode.BestScore) { return(true); } else { return(false); } }); if (gameNodeChild != null) { //mv = gameNodeChild.Game.GetLastMove; mv = game.Diff(gameNode.Game, gameNodeChild.Game); //if (Sig != gameNodeChild.Game.Signature) //{ // System.Diagnostics.Debugger.Break(); //} } } return(mv); }
private void SuggestMove_Click(object sender, System.EventArgs e) { //int oldPosition = 0; //int newPosition = 0; MoveTuple mv = gameDomain.SuggestNextMove(theGame.Signature); //if (theGame.SuggestedMove(ref oldPosition, ref newPosition)) if (mv != null) { if (MessageBox.Show(string.Format("Move peg at position {0} to position {1}", mv.original + 1, mv.destination + 1), "Suggested Move is:", System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) { theGame.Move(mv.original, mv.destination); if (theGame.GameOver) { setTheBoard(); MessageBox.Show("there are no moves left"); } pegToMove = -1; pegDestination = -1; setTheBoard(); } } else { MessageBox.Show("There are no suggestions."); } }
public void Undo() { if (pastMoves.Count == 0) { throw new Exception("The are no moves to undo."); } else { MoveTuple lastMove = pastMoves.Pop(); board[lastMove.original] = true; board[lastMove.jumped] = true; board[lastMove.destination] = false; } }
void setTheBoard() { for (int idx = 0; idx < 15; idx++) { ((System.Windows.Forms.PictureBox)holes[idx]).Image = GetPegImage(idx); } try { MoveTuple lastMove = theGame.GetLastMove; message.Text = string.Format("last move was from {0} to {1}", lastMove.original, lastMove.destination); bestPossible.Text = string.Format("{0}", gameDomain.BestPossibleScore(theGame.Signature)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); message.Text = string.Format("unable to determine last move"); } }
public bool SuggestedMove(ref int oldPosition, ref int newPosition) { bool found = false; if (!GameOver) { Random rand = new Random(System.DateTime.Now.Millisecond); System.Collections.Generic.List <MoveTuple> moves = AvailableMoves; if (moves.Count > 0) { int idx = rand.Next(moves.Count - 1); MoveTuple suggestedMove = moves[idx] as MoveTuple; oldPosition = suggestedMove.original; newPosition = suggestedMove.destination; found = true; } } return(found); }
public static MoveTuple Diff(game BeforeMoveBoard, game AfterMoveBoard) { if (BeforeMoveBoard.PegsLeft - 1 != AfterMoveBoard.PegsLeft) { throw new ArgumentException("The BeforeMoveBoard must have only one more peg on board than the AfterMoveBoard", "BeforeMoveBoard"); } Int64 b4Sig = BeforeMoveBoard.Signature; Int64 aftSig = AfterMoveBoard.Signature; MoveTuple mt = null; for (int originalPosition = 0; originalPosition <= BeforeMoveBoard.board.GetUpperBound(0); originalPosition++) { if (BeforeMoveBoard.board[originalPosition] != AfterMoveBoard.board[originalPosition]) { if (!AfterMoveBoard.board[originalPosition]) { // Since originalPosition is empty, it could be the original position for (int destinationPosition = 0; destinationPosition <= BeforeMoveBoard.board.GetUpperBound(0); destinationPosition++) { // if once a blank is found now look for possible leagl moves if (BeforeMoveBoard.IsValidMove(originalPosition, destinationPosition)) { int jumpedSpace = BeforeMoveBoard.JumpedSpace(destinationPosition, originalPosition); if (aftSig == b4Sig + Convert.ToInt64(Math.Pow(2, destinationPosition) - Math.Pow(2, jumpedSpace) - Math.Pow(2, originalPosition))) { mt = new MoveTuple(originalPosition, jumpedSpace, destinationPosition); break; } } } } if (mt != null) { break; } } } return(mt); }