private int AlfaBeta(Model.TreeNode node, bool isMax, int alfa, int beta) { if (node.isLeaf(node)) { node.Value = node.getFunctionResult(node.ContaintData.initialH, node.ContaintData.initialM, node.ContaintData.finalH, node.ContaintData.finalM); return(node.Value); } if (isMax) { foreach (Model.TreeNode t in node.Child) { alfa = Math.Max(AlfaBeta(t, t.ContaintData.playerTurn == 0 ? true : false, alfa, beta), alfa); if (alfa >= beta) { node.Value = beta; return(beta); } } node.Value = alfa; return(alfa); } else { foreach (Model.TreeNode t in node.Child) { beta = Math.Min(AlfaBeta(t, t.ContaintData.playerTurn == 0 ? true : false, alfa, beta), beta); if (beta <= alfa) { node.Value = alfa; return(alfa); } } node.Value = beta; return(beta); } }
private int Minimax(Model.TreeNode node, bool isMax) { if (node.isLeaf(node)) { node.Value = node.getFunctionResult(node.ContaintData.initialH, node.ContaintData.initialM, node.ContaintData.finalH, node.ContaintData.finalM); return(node.Value); } switch (node.Child.Length) { case 1: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), -8); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), 15); } return(node.Value); case 2: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false)); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false)); } return(node.Value); case 3: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false))); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false))); } return(node.Value); case 4: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false)))); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false)))); } return(node.Value); case 5: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false))))); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false))))); } return(node.Value); case 6: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[5], node.Child[5].ContaintData.playerTurn == 0 ? true : false)))))); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[5], node.Child[5].ContaintData.playerTurn == 0 ? true : false)))))); } return(node.Value); default: if (isMax) { node.Value = Math.Max(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false), Math.Max(Minimax(node.Child[5], node.Child[5].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[6], node.Child[6].ContaintData.playerTurn == 0 ? true : false))))))); } else { node.Value = Math.Min(Minimax(node.Child[0], node.Child[0].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[1], node.Child[1].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[2], node.Child[2].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[3], node.Child[3].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[4], node.Child[4].ContaintData.playerTurn == 0 ? true : false), Math.Min(Minimax(node.Child[5], node.Child[5].ContaintData.playerTurn == 0 ? true : false), Minimax(node.Child[6], node.Child[6].ContaintData.playerTurn == 0 ? true : false))))))); } return(node.Value); } }