Ejemplo n.º 1
0
int Game::minMove(int depth, int alpha, int beta) {
	
	std::vector<int> allMoves = getAvailableMoves();
	int winner = checkWinner();

	if ((allMoves.size() <= 0 || winner > 0) || depth <= 0) {
		return evaluateState(2, winner);
	}
	
	int* bestMove = NULL;
	int bestScore = INT_MAX;
	
	for (std::vector<int>::const_iterator it = allMoves.begin(); it != allMoves.end(); it++) {
		setCell(*it, 1);
		int score = maxMove(depth - 1, alpha, beta);
		undoMove(*it);
		if (score < bestScore) {
			bestScore = score;
			beta = score;
		}
		if (beta < alpha) {
			return bestScore;
		}
	}
	return bestScore;
}
Ejemplo n.º 2
0
/* This function should return the image frame and actions to be performed by this call
 * returns the animation number corresponding to this frame */
short stateUpdate(tKey* key, tObject* kid,tRoom* room) {
	/* TODO: check this function, it may not work in 64 bits architectures*/
	tState* current=&(kid->action);
	/*static float step;
	static float acumLocation;*/
	short flags;
	short steps;
	
	current->frame--;
	
	current->image=current->animation[current->frame];
	flags         =current->flags    [current->frame];
	steps         =current->steps    [current->frame];
	current->imgoffx=current->offsx    [current->frame];
	current->mirror=flags&STATES_FLAG_M?1:0;
	
	/* BEGIN DEBUGSTATES */
#ifdef DEBUGSTATES
	printf("stateUpdate: animation=%d steps=%d ",current->image,steps);
	debugShowFlag(flags);
#endif
	/* END DEBUGSTATES */
	
	if (!current->frame) {
		int action;
		free(current->animation);
		free(current->flags);
		free(current->steps);
		free(current->offsx);
		/* Find matching action */
		action=evaluateState(current->currentState,key,kid,room);

		/* Performs the events called by this action */
			/* Remember the animation and flags for the next current->frame frames */
		state_GetAnimation(action,current);
			/* Remember the state where we are now */
		current->currentState=statesActionList[action].nextStateId;
#ifdef DEBUGSTATES
		printf("NEW STATE: action=%d next=%d\n",action,current->currentState);
#endif
			/* Move the kid (turn+traslate) */
		switch(statesActionList[action].moveType) {
		case STATES_MOVETYPES_ABSOLUTEONSTART:
			/* AbsoluteOnStart (x)
			 * Moves the kid x step units from the first forward tile change
			 * and starts the animation there
			 */

			/* 1) move current location to the left tileChange */
			kid->location-=(kid->location%STATES_STEPS_PER_TILE);
			/* 2) if looking right add one tile to reach the right tileChange
			 * 3) if looking right add x, if looking left substract x */
			if (kid->direction!=DIR_LEFT)
				kid->location+=STATES_STEPS_PER_TILE+statesActionList[action].moveOffset;
			else
				kid->location-=statesActionList[action].moveOffset;
			break;
		case STATES_MOVETYPES_ABSOLUTEONSTOP: {
			/* AbsoluteOnStop (x)
			 * Deletes frames (in the middle) to make sure that, at the end of
			 * the animation, the kid had moved only x step units from the first
			 * forward tile change
			 * if there is a lack of movements by frame it stops before reaching it.
			 */
			/* 1) calculate the number of frames the guy will move */
			int accumulate=0;
			int i,j;
			int from,to;
			int moveTo=statesActionList[action].moveOffset;
			
			if (kid->direction!=DIR_LEFT)
				moveTo+=STATES_STEPS_PER_TILE-(kid->location%STATES_STEPS_PER_TILE);
			else
				moveTo+=kid->location%STATES_STEPS_PER_TILE;
			
			/* First iteration: determine i=number of frames not cropped */
			for (i=0;(i<current->frame)&&(accumulate<=moveTo);i++) {
				accumulate+=current->steps[alternate(i,current->frame)];
			}
			for (j=0;j<i;j++) {
				from=alternate(j,current->frame);
				to=alternate(j,i);
				if (j%2) {
					/* the first frames are ok, so I'll fix the last frames */
#ifdef DEBUGSTATES
					printf("from=%d to=%d ok\n",from,to);
#endif
					current->animation[to]=current->animation[from];
					current->flags[to]=current->flags[from];
					current->steps[to]=current->steps[from];
				}
			}
#ifdef DEBUGSTATES
			printf("total frames=%d number of frames to be used=%d. wanted movement=%d movement to be performed=%d\n",current->frame,i,statesActionList[action].moveOffset,accumulate);
#endif
			/* force at least one animation */
			if (!i) i=1;
			current->frame=i; /* now the last frames are moved, crop the animation */
			break;
			}
		case STATES_MOVETYPES_RELATIVETURN:
			/* relative but turning */
			kid->direction=(kid->direction==DIR_LEFT)?DIR_RIGHT:DIR_LEFT;
			kid->location+=(kid->direction==DIR_LEFT)?
				-statesActionList[action].moveOffset:
				statesActionList[action].moveOffset;
			break;
		case STATES_MOVETYPES_RELATIVE:
				kid->location+=(kid->direction==DIR_LEFT)?
				-statesActionList[action].moveOffset:
				statesActionList[action].moveOffset;
			break;
		}
	}
	
	kid->location+=(kid->direction==DIR_LEFT)?-steps:steps;

	if ((current->frame==1) && (current->currentState<0))
		return current->currentState; /* if this is the last frame of the last state, return exit code */
	return flags;
}
Ejemplo n.º 3
0
double AI::chooseShot(fizGTableState state, int depth, shot& bestShot, turn_T turn){
	//MC search

	if(depth == 0){
		return evaluateState(state, turn);
	}

	set<shot> moves;

	generateMoves(state, moves, turn);

	if(moves.empty()) {
		//if no shots were found return -2
		return -2;
	}

	set<shot>::iterator iter = moves.begin();

	//for each possible shot
	//evaluate score of that shot (generate all moves, take 3 easiest moves)
	//choose best scoring shot

	double num_samples = 10;

	double bestScore = -1;
	//shot bestShot = 0;

	fizGShot theShot;
	bestShot = (shot)*iter;

	for(;iter != moves.end(); iter++){
		double sum = 0;
		for(int j = 0; j < num_samples; j++){
			shot nextShot = (shot)*iter;
			perturbShot(nextShot);
			fizGTableState nextState = state;

			//double x = nextState.getBall(CUE).getPos().x;
			ShotStatus status = theShot.execute(*theTable, nextState, nextShot.a, nextShot.b, nextShot.theta, nextShot.phi, nextShot.v);
			//theShot.save("shotViz_shot.txt", false);
			//theShot.saveDynamics(.1, "dynamics.txt");
			//x = nextState.getBall(CUE).getPos().x;
			//x = nextState.getBall(TWO).getPos().x;
			BallState bs = nextState.getBall(TWO).getState();

			if(status != OK) continue;
			if(!shotSuccess(nextState, *iter)) continue;
			shot tempShot;

			double stateScore = chooseShot(nextState, depth - 1, tempShot, turn);
			sum += (stateScore == -2 ? 0:stateScore);
		}

		double score = sum / num_samples * iter->probability;
		if(score > bestScore){
			bestScore = score;
			bestShot = *iter;
		}
	}

	//maybe increase search depth after this works
	return bestScore; //just for now
}