Exemplo n.º 1
0
    void Board::applyMove(const DoubleMove* d)
    {
//        fprintf(stderr, "F: "); d->first()->printMove();
        applyMove(d->first());
//        fprintf(stderr, "S: "); d->second()->printMove();
        applyMove(d->second());
    }
Exemplo n.º 2
0
treeNode* myTree(treeNode *head,char inBoard[BOARDROWS][BOARDCOLS],char pColour, char cColour){
    char boardHolder[BOARDROWS][BOARDCOLS];
    int numChildren;
    node* ptr;
    if (head->board[0][0] != 'W' && head->board[0][0] != 'B' && head->board[0][0] != 'O' ){  
        copyBoard(inBoard,head->board);
    }
    if (head->numChildren == 0){
        node* moves = getMoves(head->board,cColour,pColour);
        numChildren = getListSize(moves);
        head->numChildren = numChildren;
        //printf("Generated %i moves from the head node.\n", numChildren);
        head->children = malloc(sizeof(treeNode) * numChildren);
        for(int i=0;i<numChildren;i++){
	  // printf("for\n");
            copyBoard(head->board,boardHolder);
            applyMove(moves,boardHolder);
	    //printBoard(boardHolder);
            head->children[i] = malloc(sizeof(treeNode));
            copyBoard(boardHolder,head->children[i]->board);
            head->children[i]->numChildren = 0;
	    ptr = moves->next;
	    free(moves);
	    moves = ptr;
        }
    }else{//this should happen when there are already children in the tree
      for(int i=0;i<head->numChildren;i++){
	head->children[i] = myTree(head->children[i],head->children[i]->board,cColour,pColour);
      }
    }
    return head;
}
Exemplo n.º 3
0
Arquivo: gui.c Projeto: tuval10/Chess
/*
	gets a game where the next player is the AI. show loading Image until the computer makes it's move and then make the move on board
	PRECONDITON: DeleteWidgets(&(screen->widgetChildren)) was made
	throws : malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), 
	SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError() 
*/
void GUImakeAITurn(){
	move m; 
	mainGame->isLoadingAIMove=1;
	showGame();
	if(standart_fail){return;}
	m=get_AI_Move(mainGame->gameInfo);
	if(standart_fail){return;}
	mainGame->isCheckMate=applyMove(mainGame->gameInfo,&m);
	if(standart_fail){return;}
	DeleteWidgets(&screen->widgetChildren);
	mainGame->isLoadingAIMove=0;
	showGame();
}
Exemplo n.º 4
0
Arquivo: gui.c Projeto: tuval10/Chess
/*
*	handle clicking on the board
*	throws calloc errors, malloc__error(), calloc_error(),SDL_LoadBMPError(), SDL_DisplayFormatError(), 
*   SDL_SetColorKeyError(), SDL_BlitSurfaceError(),SDL_FlipError()
*/
void handleBoardSelection(Widget *widget, int xClicked, int yClicked)
{
	int row = 0, col = 0;
	move moveToBeApplied={0};
	if(mainGame->isCheckMate==2)
		return;
	yClicked-=20;
	xClicked-=20;
	row = BOARD_SIZE - ( yClicked / PIECE_SIZE ) - 1;
	col =  ( xClicked / PIECE_SIZE );
	if(mainGame->pieceWasClicked){
		moveToBeApplied.row = mainGame->clickedPieceRow;
		moveToBeApplied.col = mainGame->clickedPieceCol;
		moveToBeApplied.newRow = row;
		moveToBeApplied.newCol = col;
		moveToBeApplied.promoteTo = mainGame->promoteTo;
		graphicMoveToCastle(&moveToBeApplied);
		if(isMoveLegal(mainGame->gameInfo,&moveToBeApplied)){
			if(standart_fail) return;
			mainGame->isCheckMate=applyMove(mainGame->gameInfo, &moveToBeApplied);
			if(standart_fail) return;
		}
		mainGame->pieceWasClicked = 0; /* need to reset the flag saying if we clicked */
		mainGame->promoteTo=0; /* reseting the option to promote */
		DeleteWidgets(&(screen->widgetChildren));
		
		/*PvsAI and AI Turn*/
		if( (mainGame->isCheckMate!=2) && (isAITurn(mainGame->gameInfo)) )
			GUImakeAITurn();
		else
			showGame();
	}

	else
	{   /*we might be clicking on a piece for movement and its with our color (player color) */
		if( occupier_color(row, col , mainGame->gameInfo->boardPointer) == mainGame->gameInfo->nextTurnPlayerColor )
		{
			mainGame->pieceWasClicked = 1;/* set the flag saying "we clicked!" */
			mainGame->clickedPieceCol = col;/* save coordinates of the click */
			mainGame->clickedPieceRow = row;
			if(isPawnBeforePromotionMarked()){
				if(standart_fail) return;
				mainGame->promoteTo=getQueenChar(mainGame->gameInfo->nextTurnPlayerColor);
			}
			/*color the appropriate square - do a label on the label place a button - we put the piece */
            DeleteWidgets(&(screen->widgetChildren));
	        showGame();
		}
	}
}
Exemplo n.º 5
0
void Game::receiveMove(const Move& move)
{
    if (validator_->validate(move, currentPlayer_->type()))
    {
        currentPlayer_->responseAck(true);
        if (!applyMove(move))
            return;

        prepareNextMoveRequest();
    }
    else
    {
        currentPlayer_->responseAck(false);
        requestMove();
    }
}
void PhysicsComponent::update(double delta) {

    timeDelta = delta;
    if (useGravity) applyGravity();
    if (useFriction) applyFriction();
    
    
    force = checkForce(force);
    
    movement = (impulse+force)*timeDelta;

    applyMove(movement, true);
    
    impulse = Vec2(0,0);

}
Exemplo n.º 7
0
treeNode * makeTree(node * list, int layer){
  char boardHolder[BOARDROWS][BOARDCOLS];
  node* ptr;
  if(layer==2) return NULL; //We should put the evaluation function here
  layer++;
  
  treeNode * temp = malloc(sizeof(treeNode));
  int listLength = getListSize(list);
  temp->numChildren = listLength;
  temp->children = malloc(sizeof(treeNode) * listLength);
  temp->nodePtr = malloc(sizeof(node));
  for(int i = 0; i < listLength; i++){
    applyMove(list,boardHolder);
    temp->children[i] = makeTree(getMoves(boardHolder, computerColour, playerColour), layer);
    setNode(temp->nodePtr, ptr->origin.x, ptr->origin.y, ptr->jumpDest.x, ptr->jumpDest.y, ptr->dir, ptr->jumps);
    printf("made %d child\n", i+1);
    ptr = list->next;
    free(list);
    list = ptr;
  }
  return temp;
}
Exemplo n.º 8
0
void Board::applyExternalMove(const Move extMove) {
  // moves that come from the outside don't have all the handy flags
  // so analyze it and set the proper flags
  Square sourceSq(extMove.getSource());
  Square targetSq(extMove.getTarget());
  Piece movingPiece = Pawn;
  Piece capturedPiece = Pawn;
  Piece promotionPiece = Pawn;
  bool capturing = false;
  bool promoting = false;
  bool doublePushing = false;
  bool enPassanting = false;
  int enPassantTargetFile = -1;
  bool castling = false;
  bool castlingDirection = false;

  const BitBoard sourceBB(1LL << sourceSq);
  const BitBoard targetBB(1LL << targetSq);

  bool sourceDirtied = !(_dirty & sourceBB);
  bool targetDirtied = !(_dirty & targetBB);

  for (size_t i = 0; i < 6; ++i) {
    if (_pieces[i] & sourceBB) movingPiece = Piece(i);
    if (_pieces[i] & targetBB) {
      capturedPiece = Piece(i);
      capturing = true;
    }
  }

  if (movingPiece == Pawn) {
    const int diff = sourceSq - targetSq;
    if (abs(diff) == 16) {
      doublePushing = true;
      const int file = sourceSq & 7;
      enPassantTargetFile = file;
    } else if (targetBB & 0xFF000000000000FF) {
      promoting = true;
      promotionPiece = extMove.getPromotionPiece();
    } else if (((diff & 1) == 1) && !capturing) {
      enPassanting = true;
      capturing = true;
      capturedPiece = Pawn;
    }
  }

  if (movingPiece == King) {
    const int diff = sourceSq - targetSq;
    if (2 == diff) {
      castling = true;
      castlingDirection = true;
    } else if (-2 == diff) {
      castling = true;
      castlingDirection = false;
    }
  }

  _moves.clear();
  int temp = _draw100Counter.top();
  _draw100Counter.clear();
  _draw100Counter.push(temp);

  applyMove(Move(sourceSq, targetSq, movingPiece, capturedPiece, promotionPiece,
                 promoting, capturing, doublePushing, enPassanting,
                 enPassantTargetFile, castling, castlingDirection,
                 sourceDirtied, targetDirtied));
}
Exemplo n.º 9
0
ReturnValue alphaBeta(Configuration v, int alpha, int beta, int depth){
// 	int counter=0;
	if (DEBUG_STACK){
		printstack();
		printf("alphabeta a=%d b=%d d=%d\n", alpha, beta, depth);
// 		printBoardNonBlock(v);
	}
	++stackcount;
	HashRetVal s;
	s=retrieve(v);
	ReturnValue ret;
	ReturnValue temp;
	ret.alpha=alpha;
	ret.beta=beta;
	if (s!=NULL) {
		switch (s->type){
			case EXACT:
				if (s->lowerbound>=beta) {
					ret.value=s->lowerbound;
					ret.move=s->mv;
					--stackcount;
					if (DEBUG_STACK){
						printstack();
						printf("hit exact 1\n");
					}
					return ret;
				}
				if (s->upperbound<=alpha) {
					ret.value=s->upperbound;
					ret.move=s->mv;
					--stackcount;
					if (DEBUG_STACK){
						printstack();
						printf("hit exact 2\n");
					}
					return ret;
				}
				ret.alpha=max(alpha, s->lowerbound);
				ret.beta=min(beta, s->upperbound);
				ret.move=s->mv;
				break;
			case FAIL_LOW:
				if (s->upperbound<=alpha) {
					ret.value=s->upperbound;
					ret.move=s->mv;
					--stackcount;
					if (DEBUG_STACK){
						printstack();
						printf("hit fail low\n");
					}
					return ret;
				}
				break;
			case FAIL_HIGH:
				if (s->lowerbound>=beta) {
					ret.value=s->lowerbound;
					ret.move=s->mv;
					--stackcount;
					if (DEBUG_STACK){
						printstack();
						printf("hit fail high\n");
					}
					return ret;
				}
				break;
			default:
				break;
		}
		free(s);
	}
	if (depth==0) {
		ChildIterator itr=getExpansion(v);
		ret.value=getCurrentValue(itr);
		ret.move=getCurrent(itr);
		releaseChildIterator(itr);
		if (getType(v)==MAXNODE)
			updateMoveHeuristic(v, ret.move.x, ret.move.y, ret.value);
		else
			updateMoveHeuristic(v, ret.move.x, ret.move.y, -ret.value);
		if (DEBUG_STACK){
			printBoardNonBlock(v);
			printstack();
			printf("eval %d\n", ret.value);
			printstack();
			printf("(%d, %d)\n", ret.move.x, ret.move.y);
		}
	}
	else if (getType(v)==MAXNODE) {
//		printf("*\n");
		ChildIterator itr=getExpansion(v);
//		printf("e: %d %d %d\n", getCurrent(itr).x, getCurrent(itr).y, depth);
		int a=alpha;
		ret.value=-FIVE_SCORE;
		ret.move=getCurrent(itr);
		if (getCurrentValue(itr)>=FIVE_SCORE){
			ret.value=FIVE_SCORE;
		}
		else {
			while (itr!=NULL && ret.value<beta) {
	//			printf("depth=%d, move=%d %d\n",
	//				depth, getCurrent(itr).x, 
	//				getCurrent(itr).y);
				if (tickTimer()==0)
					break;
				applyMove(v, getCurrent(itr));
	//			printBoardNonBlock(v);
				
				if (DEBUG_STACK){
					printstack();
					printf("black trying %d %d\n", 
						getCurrent(itr).x, getCurrent(itr).y);
				}
				
				switch (getChildrenCount(itr)){
// 					case 1:
/*						++quadcount;
						temp=alphaBeta(v, a, beta, 
								depth-((doublecount & 3)==0));*/
					case 1:
// 					case 2:
						++doublecount;
						temp=alphaBeta(v, a, beta,
								depth-1);
						--doublecount;
						break;
					default:
						temp=alphaBeta(v, a, beta, depth-1);
						break;
				}
				updateMoveHeuristic(v, temp.move.x,
									temp.move.y, temp.value);
				if (DEBUG_STACK){
					printstack();
					printf("black try %d %d, result=%d\n", 
						getCurrent(itr).x, getCurrent(itr).y,
									temp.value);
				}
				
				if (temp.value>ret.value) {
					ret.value=temp.value;
					ret.move=getCurrent(itr);
	//				printf("current black move = %d %d\n", ret.move.x, ret.move.y);
				}
				undoMove(v, getCurrent(itr));
	//			printBoard(v);
				if (a<ret.value){
					a=ret.value;
				}
	//			printf("a=%d ret.value=%d\n", a, ret.value);
				// TODO to be verified
	/*			if (temp.value<=-INFINITY){
					++counter;
				}
				if (counter>2)
					break;*/
				getNext(&itr);
	//			printf("e: %d %d\n", itr->current.x, itr->current.y);
			}
			if (DEBUG_STACK){
				if (ret.value>=beta){
					printstack();
					printf("pruned\n");
				}
			}
		}
		releaseChildIterator(itr);
	}
	else {
//		printf("-\n");
		ChildIterator itr=getExpansion(v);
//		printf("n: %d %d %d\n", getCurrent(itr).x, getCurrent(itr).y, depth);
		int b=beta;
		ret.value=FIVE_SCORE;
		ret.move=getCurrent(itr);
		if (getCurrentValue(itr)<=-FIVE_SCORE){
			ret.value=-FIVE_SCORE;
		}
		else {
			while (itr!=NULL && ret.value>alpha) {
				if (tickTimer()==0)
					break;
				applyMove(v, getCurrent(itr));
				
				if (DEBUG_STACK){
					printstack();
					printf("white trying %d %d\n", 
						getCurrent(itr).x, getCurrent(itr).y);
				}
				
				if (getChildrenCount(itr)<=1){
					++doublecount;
					temp=alphaBeta(v, alpha, b, 
							depth-1);
					--doublecount;
				}
				else {
					temp=alphaBeta(v, alpha, b, depth-1);
				}
				updateMoveHeuristic(v, temp.move.x,
									temp.move.y, -temp.value);
				
				if (DEBUG_STACK){
					printstack();
					printf("white try %d %d, result=%d\n", 
						getCurrent(itr).x, getCurrent(itr).y,
									temp.value);
					printf("retval=%d\n", ret.value);
				}
				
				if (temp.value<ret.value){
					ret.value=temp.value;
					ret.move=getCurrent(itr);
		//			printf("current white move = %d %d\n", ret.move.x, ret.move.y);
				}
				undoMove(v, getCurrent(itr));
				if (b>ret.value){
					b=ret.value;
				}
	//			printf("a=%d ret.value=%d\n", b, ret.value);
/*				if (ret.value<=-INFINITY-10)
					break;*/
				// TODO to be verified
	/*			if (temp.value>=INFINITY){
					++counter;
				}
				if (counter>2)
					break;*/
				getNext(&itr);
			}
			if (DEBUG_STACK){
				if (ret.value<=alpha){
					printstack();
					printf("pruned\n");
				}
			}
		}
		releaseChildIterator(itr);
	}

	if (ret.value<=alpha && getType(v)==MINNODE) {
		/* fail low */
		v->upperbound=ret.value;
		
		if (depth>0)
			store(v, ret.move, FAIL_LOW);
		
		/* Fail low result implies an upper bound */
	}
	else if (ret.value>alpha && ret.value<beta) {
		/* accurate window */
		v->upperbound=v->lowerbound=ret.value;
		if (depth>0)
			store(v, ret.move, EXACT);
	}
	else if (getType(v)==MINNODE && ret.value>=MAXNODE){
		/* fail high */
		v->lowerbound=ret.value;
		
		if (depth>0)
			store(v, ret.move, FAIL_HIGH);
		
	}
/*	printstack();
	printf("ab return %d (%d,%d))\n", ret.value, ret.move.x,
		  ret.move.y);*/
//	getchar();
	--stackcount;
	return ret;
}
void ClientUnit::advance()
{
    find1TargetUnit();
    applyMove();
    applyFire();
}
Exemplo n.º 11
0
void Rubik::applyMoves(std::vector<std::string>& m) {
	int size = m.size();
	for (int i = 0; i < size; i++) {
		applyMove(m[i]);
	}
}