Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Created June 2, 2017 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomrockdsouza/d5b978a9d24456ca294e6304f8222dc0 to your computer and use it in GitHub Desktop.
Save tomrockdsouza/d5b978a9d24456ca294e6304f8222dc0 to your computer and use it in GitHub Desktop.
C Implementation of adversarial search ( MIN-MAX )
/**
* Implementation of adversarial search
* Example To Use Program :https://s15.postimg.org/yyh4lhdsr/Adversial.png
*
* Email: engineer.rc1@gmail.com
* @author Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017
* @copyright GNU General Public License v3.0
* No reproduction in whole or part without maintaining this copyright notice
* and imposing this condition on any subsequent users.
*/
#include<stdio.h>
#include<conio.h>
int poop[20], lol[20][20], p = 0;
//This Function Creates the tree.
void admat() {
int i, j, k;
for (i = 0; i <= p; i++) {
printf("Enter number of daughter nodes of node%d: ", i);
scanf("%d", & j);
if (j != 0) {
printf("Nodes Assigned to node %d: ", i);
for (k = 0; k < j; k++) {
p++;
lol[i][p] = 1;
printf("%d ", p);
}
printf("\n");
}
}
}
//This Function Decides the Highest or lowest From the Sub-Branches based on the Level
int minmax(int a, int b, int s) {
int d;
if (s == 0) {
if (a < b) {
d = a;
} else {
d = b;
}
} else if (s == 1) {
if (a < b) {
d = b;
} else {
d = a;
}
} else d = a;
return d;
}
//This Function parses the tree and askes the user value of a node if it has no children
int trav(int n, int a, int s) {
int i, b = 0, c = 0, e = 0;
s = (s + 1) % 2;
//This loop iterates through all child nodes 1 by 1
for (i = 0; i < n; i++) {
if (lol[a][i] == 1) {
e = 1;
if (b == 0) {
//This Value is assigned 2 because it's a single node
c = minmax(trav(n, i, s), c, 2);
b = 1;
} else {
c = minmax(trav(n, i, s), c, s);
}
}
}
if (e == 0) {
printf("Enter value of node %d:", a);
scanf("%d", & c);
poop[a] = c;
return c;
} else {
poop[a] = c;
return c;
}
}
int main() {
int i, j;
admat();
trav(p+1, 0, 0);
//Print The Values of The Nodes After The adversarial search is Complete
for (i = 0; i <=p; i++) {
}
printf("\nOUTPUT\n-------------------------\n");
printf("Value of Root Node0 is %d\n\n",poop[0]);
for (i = 0; i <=p; i++) {
for (j = 0; j <=p; j++) {
if(lol[i][j]){
printf("Node%d => Node%d Value %d\n", i,j, poop[j]);
}
}
printf("\n");
}
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment