예제 #1
0
파일: newMinmax.c 프로젝트: noeh/Michael
/******************************************************************************\
 * Max  (4,4,0,0,0,0,b,r,r,s,b,s,s,s,s,s,s,s,s,s,s,s)                                                                     *
 \******************************************************************************/
int Max(int depth, int alpha, int beta)
{
    /* int best = -INFINITY;                                                      */
	int val;
	int *legal_columns;
	int *legal_pieces;
	int max_legal;
	int i;
    
    /*check win*/
    val = checkComp(evaluated_column, total_rows, board);
    if (val != 0) // if finds win, return
    {
#ifdef ZZZ
        printf("FOUND WIN: column%d val:%d\n", evaluated_column, val);

        dispboard();
#endif
        return val;
    }
    
	if (depth <= 0)
    {
        val = Evaluate(); // reached the terminal without finding a win. apply heuristics.
#ifdef ZZZ
         printf("Depth %d: did not find win, Column:%d val:%d\n",maxdepthval, evaluated_column, val);
#endif
        if (val>0)
        {
            val = val * -1;
        }
		return val;
    }
    
    /* GenerateLegalMoves();                                                      */
	max_legal = 0;
	legal_columns = (int*)calloc(columns*2,sizeof(int));
	if (legal_columns == NULL)
		return alpha;
    
	legal_pieces = (int*)calloc(columns*2,sizeof(int));
    
	if (legal_pieces == NULL)
	{
		free(legal_columns);
		return alpha;
	}

        
	for (i = 0; i < columns; i++)
    {
		if (board[order[i] * total_rows + rows - 1] == SPACE)
		{
			legal_columns[max_legal] = order[i];
			legal_pieces[max_legal++] = BLUE;
			legal_columns[max_legal] = order[i];
			legal_pieces[max_legal++] = GREEN;
		}
    }
    
    if(max_legal == 0)// full board
        return alpha;
    
    /* while (MovesLeft()) {                                                      */
	for (i = 0; i < max_legal; i++) {
        
        /*    MakeNextMove();                                                         */
		AddToBoard(legal_columns[i], legal_pieces[i]);
        
        /*    val = Min(depth - 1);                                                   */
        evaluated_column = legal_columns[i];
        
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
        static int count = 0;
#ifdef ZZZ
        printf("\t\tMAX called %d times.\n", ++count);

        dispboard();
#endif
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
        
		val = Min(depth - 1, alpha, beta);
#ifdef ZZZ
        printf("MAX::Depth:%d column:%d piece:%d val:%d best:%d\n", maxdepthval - depth, evaluated_column, legal_pieces[i], val, alpha);
#endif

        
        /*    UnmakeMove();                                                           */
		RemoveFromBoard(legal_columns[i]);
        
        /*    if (val > best)                                                         */
        /*       best = val;
         */
		if (val > alpha)
		{
			alpha = val;
			if (depth == maxdepthval)
			{
                
				piece = legal_pieces[i];
				column = legal_columns[i];
#ifdef ZZZ
                printf("MAX::Best has changed:%d, column:%d, piece:%d \n", alpha, column, piece);
#endif
			}
		}
        /* }                                                                          */
	}
    
	free(legal_columns);
	free(legal_pieces);
    
    /* return best;                                                               */
	return alpha;
} /* Max */
예제 #2
0
파일: newMinmax.c 프로젝트: noeh/Michael
int AlphaBeta(int depth, int alpha, int beta, int turn)// turn=1 if blue; else 0;
{
    int val;
    int * legal_columns;
    int * legal_pieces;
    int max_legal;
    int i;
    
    
    /*check win*/
    val = checkComp(evaluated_column, total_rows, board);
    if (val != 0) // if finds win, return
    {
#ifdef ZZZ
        printf("FOUND WIN: column%d val:%d\n", evaluated_column, val);
        
        dispboard();
#endif
        return val;
    }

    if(depth == 0)
    {
        val = Evaluate();
        return val;
    }
    /* GenerateLegalMoves();                                                      */
	max_legal = 0;
	legal_columns = (int*)calloc(columns*2,sizeof(int));
	if (legal_columns == NULL)
		return alpha;
    
	legal_pieces = (int*)calloc(columns*2,sizeof(int));
    
	if (legal_pieces == NULL)
	{
		free(legal_columns);
		return alpha;
	}
    
    if (turn == 1) 
    {
        for (i = 0; i < columns; i++)
        {
            if (board[order[i] * total_rows + rows - 1] == SPACE)
            {
                legal_columns[max_legal] = order[i];
                legal_pieces[max_legal++] = BLUE;
                legal_columns[max_legal] = order[i];
                legal_pieces[max_legal++] = GREEN;
            }
        }
    }
    else
    {
        for (i = 0; i < columns; i++)
        {
            if (board[order[i] * total_rows + rows - 1] == SPACE)
            {
                legal_columns[max_legal] = order[i];
                legal_pieces[max_legal++] = RED;
                legal_columns[max_legal] = order[i];
                legal_pieces[max_legal++] = GREEN;
            }
        }
    }

    turn = turn==1?0:1; // swithch turn
    if(max_legal == 0)// full board
        return alpha;
    
    
    /* while (MovesLeft()) {                                                      */
	for (i = 0; i < max_legal; i++) {
        
        /*    MakeNextMove();                                                         */
		AddToBoard(legal_columns[i], legal_pieces[i]);
        
        /*    val = Min(depth - 1);                                                   */
        evaluated_column = legal_columns[i];
        
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
        static int count = 0;
#ifdef ZZZ
        printf("\t\tMAX called %d times.\n", ++count);
        
        dispboard();
#endif
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
        
		val = -AlphaBeta(depth-1, -beta, -alpha, turn);
#ifdef ZZZ
        printf("MAX::Depth:%d column:%d piece:%d val:%d best:%d\n", maxdepthval - depth, evaluated_column, legal_pieces[i], val, alpha);
#endif
        
        
        /*    UnmakeMove();                                                           */
		RemoveFromBoard(legal_columns[i]);
        
        /*    if (val > best)                                                         */
        /*       best = val;
         */
		if (val > alpha)
		{
			alpha = val;
			if (depth == maxdepthval)
			{
                
				piece = legal_pieces[i];
				column = legal_columns[i];
//                if(best == 1000)// blocking case
//                    piece = BLUE;
#ifdef ZZZ
                printf("MAX::Best has changed:%d, column:%d, piece:%d \n", alpha, column, piece);
#endif
			}
		}
        /* }                                                                          */
	}
    free(legal_columns);
	free(legal_pieces);
    return alpha;
    
}
int main(int argc, char **argv)
{
    char configFile[0xff+1] = "clAmfConfig.xml";
    char nodename[0xff+1] = "ControllerI0";
    char nodetype[0xff+1] = {0};
    char **comps = NULL;
    char **seenComps = NULL;
    int numSeen = 0;
    int numComps = 0;
    int i;
    if(argc != 2 && argc != 3 && argc != 4)
    {
        fprintf(stderr, "%s [clAmfConfig.xml] [ asp node name] [ asp node type]\n", argv[0]);
        exit(127);
    }
    if(argc > 1)
    {
        strncpy(configFile, argv[1], sizeof(configFile)-1);
    }
    if(argc > 2)
    {
        strncpy(nodename, argv[2], sizeof(nodename)-1);
    }
    if(argc > 3)
    {
        strncpy(nodetype, argv[3], sizeof(nodetype)-1);
    }
    if(!nodetype[0])
    {
        /*  
         *find node type. check for nodenameN or nodenameIN where N is a digit.
         */
        char *s = nodename;
        s += strlen(nodename)-1;
        while(s > nodename && isdigit(*s)) --s;
        if(*s == 'I' && s != nodename) --s;
        strncpy(nodetype, nodename, (s-nodename)+1); 
        fprintf(stderr, "Inferred nodetype [%s] for nodename [%s]\n", nodetype, nodename);
    }
    if(parseConfigFile(configFile, nodename, nodetype, &comps, &numComps) < 0)
    {
        fprintf(stderr, "Error parsing file [%s] for node [%s]\n", configFile, nodename);
        exit(127);
    }
    seenComps = calloc(numComps, sizeof(*seenComps));
    assert(seenComps);
    fprintf(stderr, "Dumping components matching node [%s], nodetype [%s]\n", nodename, nodetype);
    printf("safplus_amf\n");
    for(i = 0; i < numComps; ++i)
    {
        if(!checkComp(comps[i], seenComps, numSeen))
        {
            printf("%s\n", comps[i]);
            seenComps[numSeen++] = comps[i];
        }
    }
    for(i = 0; i < numComps; ++i)
        free(comps[i]);
    if(comps) free(comps);
    if(seenComps) free(seenComps);
    return 0;
}
예제 #4
0
파일: newMinmax.c 프로젝트: noeh/Michael
/******************************************************************************\
 * Min                                                                          *
 \******************************************************************************/
int Min(int depth, int alpha, int beta)
{
    /* int best = INFINITY;  // <-- Note that this is different than in "Max".    */
	int val;
	int *legal_columns;
	int *legal_pieces;
	int max_legal;
	int i;
    
    
    /*check win*/
    val = checkComp(evaluated_column, total_rows, board);
    if (val != 0) // if finds win, return
    {

#ifdef ZZZ
        printf("FOUND WIN: column%d val:%d\n", evaluated_column, val);
        dispboard();
#endif
        return val;
    }
    
    /* if (depth <= 0)                                                            */
    /*    return Evaluate();                                                      */
	if (depth <= 0)
    {
        val = Evaluate(); // reached the terminal without finding a win
                            // heuristics to apply
#ifdef ZZZ
        printf("Depth %d: did not find win, Column:%d val:%d\n",maxdepthval, evaluated_column, val);
        dispboard();
#endif
		return val;
    }
    
    /* GenerateLegalMoves();                                                      */
	max_legal = 0;
	legal_columns = (int*)calloc(columns*2,sizeof(int));
	if (legal_columns == NULL)
		return beta;
	legal_pieces = (int*)calloc(columns*2,sizeof(int));
	if (legal_pieces == NULL)
	{
		free(legal_columns);
		return beta;
	}
	for (i = 0; i < columns; i++)
    {
		if (board[order[i] * total_rows + rows - 1] == SPACE)
		{
			legal_columns[max_legal] = order[i];
			legal_pieces[max_legal++] = RED;
			legal_columns[max_legal] = order[i];
			legal_pieces[max_legal++] = GREEN;
		}
    }
    if(max_legal == 0)// full board
        return beta;
    /* while (MovesLeft()) {                                                      */
	for (i = 0; i < max_legal; i++) {
        
        /*    MakeNextMove();                                                         */
		AddToBoard(legal_columns[i], legal_pieces[i]);
        
        /*    val = Max(depth - 1);                                                   */
        evaluated_column = legal_columns[i];
        
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
        static int min_count = 0;
#ifdef ZZZ
        printf("\t\tMIN called %d times.\n", ++min_count);        
        dispboard();
        printf("\n");
        printf("\n");
        printf("\n");
        /* FOR DEBUGGING */
        /* FOR DEBUGGING */
#endif        
		val = Max(depth - 1, alpha, beta);
#ifdef ZZZ
        printf("MIN::Depth:%d column:%d piece:%d val:%d best:%d\n", maxdepthval - depth, evaluated_column, legal_pieces[i], val, beta);
#endif

        //        printf("val:%d\n", val);
        
        /*    UnmakeMove();                                                           */
		RemoveFromBoard(legal_columns[i]);
        
        /*    if (val < best)  // <-- Note that this is different than in "Max".      */
        /*       best = val;                                                          */
        
		if (val < beta){
			beta = val;
#ifdef ZZZ
            printf("MIN::Best has changed:%d\n", beta);
#endif
        }

        /* }                                                                          */
	}
    
	free(legal_columns);
	free(legal_pieces);
    
	return beta;
    
} /* Min */
예제 #5
0
void playGame(){
	cardDeck = fopen("/dev/cards", "r");
	int stateOfGame = 1;
	int compCurrCard, currCard;
	char input[10];
	int playerHandTotal = 0;
	int playerAceTotal = 0;
	int compHandTotal = 0;
	int compAceTotal = 0;
	compHandTotal = cardDraw(&compAceTotal);
	compCurrCard = cardDraw(&compAceTotal);
	playerHandTotal = cardDraw(&playerAceTotal);
	while(stateOfGame == 1){
		printf("The dealer:\n? + %d\n\n", compCurrCard);
		currCard = cardDraw(&playerAceTotal);
		if(currCard == 11){
			currCard=changeAce(&playerHandTotal, &playerAceTotal);
		}
		checkTotal(&playerHandTotal, &playerAceTotal);
		printf("You:\n%d + %d = %d ", playerHandTotal, currCard, playerHandTotal+currCard, playerAceTotal);
		playerHandTotal+=currCard;
		stateOfGame = checkTotal(&playerHandTotal, &playerAceTotal);
		if(stateOfGame == -1){
			printf("BUSTED!");
		}
		printf("\n\n");
		if(stateOfGame == 1){
			printf("Do you want to \"hit\" or \"stand\"?");
			scanf("%s", input);
			if(strcmp(input, "hit"))
				stateOfGame = 0;
		}

	}

	if(stateOfGame == -1){
		printf("You busted, dealer wins\n");
	}

	else{
		printf("The dealer:\n%d + %d = %d\n\n",compHandTotal, compCurrCard, compHandTotal+compCurrCard);
		compHandTotal += compCurrCard;
		do{
			compCurrCard = cardDraw(&compAceTotal);
			if(compCurrCard == 11){
				compCurrCard=changeAce(&compHandTotal, &compAceTotal);
			}
			printf("The dealer:\n%d + %d = %d\n\n",compHandTotal, compCurrCard, compHandTotal+compCurrCard);
			compHandTotal += compCurrCard;
			stateOfGame = checkComp(&compHandTotal, &compAceTotal);
		}
		while(stateOfGame == 1);
		if(stateOfGame == 0){
			(playerHandTotal > compHandTotal)? printf("You won, the dealer drew %d and you drew %d", compHandTotal, playerHandTotal):printf("You Lost, the dealer drew %d and you drew %d", compHandTotal, playerHandTotal);
		}
		else{

		checkTotal(&playerHandTotal, &playerAceTotal);
			printf("Dealer Busted, you win!");
		}
	}
}