To perform adversarial search using Min Max search > Java Program
To perform adversarial search using Min Max search > Java Program
Artificial Intelligence
Program:
import java.util.Scanner;
public class MinMax {
public static void main(String[] args) {
new MinMax().go();
System.out.println("\n");
}
void go() {
Scanner sc = new Scanner(System.in);
int[] values = new int[20];
int[] row = new int[10];
boolean chk = true;
System.out.println("Enter the no. of levels:");
int lvl = sc.nextInt(), n = 0;
for (int i = 0; i < lvl; i++) {
row[i] = (int) Math.pow(2, i);
n = n + row[i];
}
System.out.println("Enter the value of leaf nodes:");
for (int i = row[lvl - 1] - 1; i < n; i++) {
values[i] = sc.nextInt();
}
int count = row[lvl - 2];
for (int i = row[lvl - 2]; i >= 0; i--) {
if (count == 0) {
lvl--;
count = row[lvl];
}
if ((lvl - 2) % 2 == 1) {
values[i] = (int) Math.min(values[2 * i + 1], values[2 * i + 2]);
} else if ((lvl - 2) % 2 == 0) {
values[i] = (int) Math.max(values[2 * i + 1], values[2 * i + 2]);
}
count--;
}
for (int i = 0; i < n; i++) {
System.out.print(values[i] + " ");
}
}
}
/*Output:
Enter the no. of levels:
3
Enter the value of leaf nodes:
10
5
9
8
8 5 8 10 5 9 8
*/
Comments
Post a Comment