public override void Update(GameTime gt) { if (map.moving) return; rootNode = new DecisionNode(X, Y, PlayerTurn.Player2); //rootNode.currState = ms; Watch.Reset(); Watch.Start(); DecisionNode solution = null; solution = DLSRoot(rootNode, -999999, 999999); //if (Watch.ElapsedMilliseconds >= 500) // break; Watch.Stop(); if (solution == null && rootNode.GetChildren().Count > 0) solution = rootNode.GetChildren()[0]; if (solution != null) { CardClass cc; if (solution.cardStart.X < 0) cc = hand.SelectCard((int)solution.cardStart.Y); else cc = map.map[(int)solution.cardStart.X, (int)solution.cardStart.Y]; map.MoveCard(cc, (int)solution.cardEnd.X, (int)solution.cardEnd.Y); } }
private int DLS(DecisionNode node, int depth, int alpha, int beta) { //if (Watch.ElapsedMilliseconds >= 500) // return node.Value; // no more time if (depth == maxDepth) { node.Value = NodeEval(map.map, node.pt, node.cardStart, node.cardEnd, depth); return node.Value; } if (TestWin(node)) { node.Value = NodeEval(map.map, node.pt, node.cardStart, node.cardEnd, depth); return node.Value; } if (node.GetChildren().Count == 0) { ExpandNode(node, depth); node.GetChildren().Sort(ChildNodeSort); } // Maximize! CardClass movedCard; if (node.GetChildren().Count == 0) { node.Value = NodeEval(map.map, node.pt, node.cardStart, node.cardEnd, depth); return node.Value; } int value = 0; foreach (DecisionNode n in node.GetChildren()) { if (n.cardStart.X == -1) { Turn activeTurn = map.GetTurn(node.pt); movedCard = activeTurn.SelectCard((int)n.cardStart.Y); } else movedCard = map.map[(int)n.cardStart.X, (int)n.cardStart.Y]; map.MoveCardAI(movedCard, (int)n.cardEnd.X, (int)n.cardEnd.Y, depth); value = -DLS(n, depth+1, -beta, -alpha); map.UndoMove(movedCard); if (value >= beta) { node.Value = beta; return beta; } if (value > alpha) alpha = value; } node.Value = alpha; return alpha; }
private DecisionNode DLSRoot(DecisionNode node, int alpha, int beta) { ExpandNode(node, 0); // Maximize! CardClass movedCard; if (node.GetChildren().Count == 0) { node.Value = NodeEval(map.map, node.pt, node.cardStart, node.cardEnd, 0); return null; } DecisionNode solution = null; int value = 0; foreach (DecisionNode n in node.GetChildren()) { if (n.cardStart.X == -1) { Turn activeTurn = map.GetTurn(node.pt); movedCard = activeTurn.SelectCard((int)n.cardStart.Y); } else movedCard = map.map[(int)n.cardStart.X, (int)n.cardStart.Y]; map.MoveCardAI(movedCard, (int)n.cardEnd.X, (int)n.cardEnd.Y, 0); value = -DLS(n, 1, -beta, -alpha); map.UndoMove(movedCard); if (value > alpha) { alpha = value; solution = n; } } node.Value = alpha; return solution; }