Ejemplo n.º 1
0
void E_scene_main_game::UpdatePossibleMoves(unsigned char posX, unsigned char posZ)
{
	for(int i=0;i<32;i++)
		possible_moves[i]=99;
	
	int j=0;
	
	char piece_pos[2];
	
	// convert pouetChess coordinates to FAILE coordinates
	piece_pos[0]=(unsigned char)('h'-posX);
	piece_pos[1]=(unsigned char)(posZ+'1');

	
	move_s moves[MOVE_BUFF];
	int num_moves, i, ep_temp;
	char comp_move[6];
	d_long temp_hash;

	ep_temp = ep_square;
	temp_hash = cur_pos;
	num_moves = 0;
	gen (&moves[0], &num_moves);

	// for all possible moves for all pieces
	for (i = 0; i < num_moves; i++) 
	{
		comp_to_coord (moves[i], comp_move);
		
		// if the move is from the piece we want
		if ((comp_move[0]==piece_pos[0]) && (comp_move[1]==piece_pos[1]))
		{
			// test if the move is legal
			make (&moves[0], i);
			if (check_legal (&moves[0], i)) 
			{
				// convert from FAILE coords to pouetChess coords
				unsigned char new_pos=(unsigned char)(('h'-comp_move[2])+8*(comp_move[3]-'1'));
				// add this move to the possible moves array
				possible_moves[j++]=new_pos;	
			}
			unmake (&moves[0], i);
			ep_square = ep_temp;
			cur_pos = temp_hash;
		}
	}
}
Ejemplo n.º 2
0
void condition(int a, int b){
	
		 int i;
    if(x[a][b]==0){
		 for(i=1;i<=9;i++){
		 	if(check_legal(a,b,i)){
		 		x[a][b] = i;
		 		if(a==8 && b==8){
		 			print_x();
		 			total++;
                    return;
		 		}
		 		else{
		 			if(b==8){
		 				condition(a+1,0);
		 			}
		 			else{
		 				condition(a,b+1);
		 			}
		 		}
		 		x[a][b]=0;
		 	}
		 }
	}
	else{
		if(a==8&&b==8){
			print_x();
			total++;
			return;
		}
		else if(b==8){
		 	condition(a+1,0);
		}
		else{
		 	condition(a,b+1);
		}
	}

    return ;
	
}
Ejemplo n.º 3
0
void CheckBadFlow(bool reset)
{
  move_s hismoves[MOVE_BUFF];
  move_s ourmoves[MOVE_BUFF];
  int his_num_moves, our_num_moves, j, i, ic, icc;

  bool othermove = FALSE;

  int 
    pawnmates = FALSE, 
    knightmates = FALSE, 
    bishopmates = FALSE, 
    rookmates = FALSE, 
    queenmates = FALSE;
  
  static int 
    pawnmated = FALSE, 
    knightmated = FALSE, 
    bishopmated = FALSE, 
    rookmated = FALSE, 
    queenmated = FALSE;

  bool
    pawnwarn = FALSE,
    knightwarn = FALSE,
    bishopwarn = FALSE,
    rookwarn = FALSE,
    queenwarn = FALSE;

  if (reset)
    {
      pawnmated = FALSE; 
      knightmated = FALSE; 
      bishopmated = FALSE; 
      rookmated = FALSE; 
      queenmated = FALSE;
      return;
    }

  ic = in_check();

  if (!holding[!white_to_move][(white_to_move ? wpawn : bpawn)])
    {
  
      DropaddHolding((white_to_move ? wpawn : bpawn) , !white_to_move);
      
      gen(&hismoves[0]);
      his_num_moves = numb_moves;
      
      for(i = 0; (i < his_num_moves) && (pawnmates == FALSE); i++)
	{
	  make(&hismoves[0], i);
	  
	  if (check_legal(&hismoves[0], i, ic))
	    {
	      pawnmates = CANCEL_THRESH;

	      icc = in_check();
	      
	      gen(&ourmoves[0]); 
	      our_num_moves = numb_moves;
	      
	      for (j = 0; (j < our_num_moves) && (pawnmates != FALSE); j++)
		{
		  
		  make(&ourmoves[0], j);
		  
		  if (check_legal(&ourmoves[0], j, icc))
		    pawnmates = FALSE;
		  
		  unmake(&ourmoves[0], j);
		  
		}
	    }
	  unmake(&hismoves[0], i);	  
	}
      DropremoveHolding((white_to_move ? wpawn : bpawn), !white_to_move);
    }
  
  if (!holding[!white_to_move][(white_to_move ? wknight : bknight)])
    {
      
      DropaddHolding((white_to_move ? wknight : bknight) , !white_to_move);
      
      gen(&hismoves[0]); 
      his_num_moves = numb_moves;
      
      for(i = 0; (i < his_num_moves) && (knightmates == FALSE); i++)
	{
	  
	  make(&hismoves[0], i);
	  
	  if (check_legal(&hismoves[0], i, ic))
	    {
	      knightmates = CANCEL_THRESH;

	      icc = in_check();
	      
	      gen(&ourmoves[0]); 
	      our_num_moves = numb_moves;
	      
	      for (j = 0; (j < our_num_moves) && (knightmates != FALSE); j++)
		{
		  make(&ourmoves[0], j);
		  
		  if (check_legal(&ourmoves[0], j, icc))
		    knightmates = FALSE;
		  
		  unmake(&ourmoves[0], j);
		}
	    }
	  unmake(&hismoves[0], i);
	}
      DropremoveHolding((white_to_move ? wknight : bknight), !white_to_move);
    }
  
  if (!holding[!white_to_move][(white_to_move ? wbishop : bbishop)])
    {      
      
      DropaddHolding((white_to_move ? wbishop : bbishop) , !white_to_move);
      
      gen(&hismoves[0]); 
      his_num_moves = numb_moves;
      
      for(i = 0; (i < his_num_moves) && (bishopmates == FALSE); i++)
	{
	  make(&hismoves[0], i);
	  
	  if (check_legal(&hismoves[0], i, ic))
	    {
	      bishopmates = CANCEL_THRESH;
	      
	      icc = in_check();

	      gen(&ourmoves[0]);
	      our_num_moves = numb_moves;
	      
	      for (j = 0; (j < our_num_moves) && (bishopmates != FALSE); j++)
		{
		  make(&ourmoves[0], j);
		  
		  if (check_legal(&ourmoves[0], j, icc))
		    bishopmates = FALSE;
		  
		  unmake(&ourmoves[0], j);
		}
	    }
	  unmake(&hismoves[0], i);
	}
      DropremoveHolding((white_to_move ? wbishop : bbishop), !white_to_move);  
    }
  
  if (!holding[!white_to_move][(white_to_move ? wrook : brook)])
    {
      
      DropaddHolding((white_to_move ? wrook : brook) , !white_to_move);
      
      gen(&hismoves[0]);
      his_num_moves= numb_moves;
      
      for(i = 0; (i < his_num_moves) && (rookmates == FALSE); i++)
	{
	  make(&hismoves[0], i);
	  
	  if (check_legal(&hismoves[0], i, ic))
	    {
	      rookmates = CANCEL_THRESH;

	      icc = in_check();
	      
	      gen(&ourmoves[0]);
	      our_num_moves = numb_moves;
	      
	      for (j = 0; (j < our_num_moves) && (rookmates != FALSE); j++)
		{
		  make(&ourmoves[0], j);
		  
		  if (check_legal(&ourmoves[0], j, icc))
		    rookmates = FALSE;
		  
		  unmake(&ourmoves[0], j); 
		}
	    }
	  unmake(&hismoves[0], i);
	}
      DropremoveHolding((white_to_move ? wrook : brook), !white_to_move); 
    }
  
  if (!holding[!white_to_move][(white_to_move ? wqueen : bqueen)])
    {
      
      DropaddHolding((white_to_move ? wqueen : bqueen) , !white_to_move);

      gen(&hismoves[0]);
      his_num_moves= numb_moves;
      
      for(i = 0; (i < his_num_moves) && (queenmates == FALSE); i++)
	{
	  make(&hismoves[0], i);
	  
	  if (check_legal(&hismoves[0], i, ic))
	    {
	      queenmates = CANCEL_THRESH;

	      icc = in_check();
	      
	      gen(&ourmoves[0]);
	      our_num_moves = numb_moves;
	      
	      for (j = 0; (j < our_num_moves) && (queenmates != FALSE); j++)
		{
		  make(&ourmoves[0], j);
		  
		  if (check_legal(&ourmoves[0], j, icc))
		    queenmates = FALSE;
		  
		  unmake(&ourmoves[0], j); 
		}
	    }
	  unmake(&hismoves[0], i);
	}
      DropremoveHolding((white_to_move ? wqueen : bqueen), !white_to_move);
    }

  /* order in which we tell things is important if we partner ourselves */
  
  /* only update if changed */
  if (pawnmates != pawnmated)
    {
      if (pawnmates == CANCEL_THRESH)
	  pawnwarn = TRUE;
      else if (pawnmates == 0 && pawnmated == 0)
	{
	  printf("tellics ptell p doesn't mate me anymore\n");
	  othermove = TRUE;
	}
    }
  
  if (knightmates != knightmated)
    {
      if (knightmates == CANCEL_THRESH)
	  knightwarn = TRUE;
      else if (knightmates == 0 && knightmated == 0)
	{
	  printf("tellics ptell n doesn't mate me anymore\n");
	  othermove = TRUE;
	}
    }  

  if (bishopmates != bishopmated)
    {
      if (bishopmates == CANCEL_THRESH)
	  bishopwarn = TRUE;
      else if (bishopmates == 0 && bishopmated == 0)
	{
	  printf("tellics ptell b doesn't mate me anymore\n");
	  othermove = TRUE;
	}
    }  
  if (rookmates != rookmated)
    {
      if (rookmates == CANCEL_THRESH)
	  rookwarn = TRUE;
      else if (rookmates == 0 && rookmated == 0)
	{
	  printf("tellics ptell r doesn't mate me anymore\n");
	  othermove = TRUE;
	}
    }
  if (queenmates != queenmated)
    {
      if (queenmates == CANCEL_THRESH)
	  queenwarn = TRUE;
      else if (queenmates == 0 && queenmated == 0)
	{
	  printf("tellics ptell q doesn't mate me anymore\n");
	  othermove = TRUE;
	} 
    }

  if (pawnwarn)
	printf("tellics ptell ---p\n");
  if (knightwarn)
	printf("tellics ptell ---n\n");
  if (bishopwarn)
	printf("tellics ptell ---b\n");
  if (rookwarn)
	printf("tellics ptell ---r\n");
  if (queenwarn)
	printf("tellics ptell ---q\n");

  /* if other sjeng had to sit because of piece-loss, he
     may be able to go now */
  
  if (piecedead && othermove)
    {
      piecedead = FALSE;
      printf("tellics ptell x\n");
      printf("tellics ptell go\n");
      go_fast = FALSE;
    }

  (pawnmates) ? (pawnmated = pawnmates) : (pawnmated--);
  (bishopmates) ? (bishopmated = bishopmates) : (bishopmated--);
  (rookmates) ? (rookmated = rookmates) : (rookmated--);
  (queenmates) ? (queenmated = queenmates) : (queenmated--);
  (knightmates) ? (knightmated = knightmates) : (knightmated--);

  return;
}
Ejemplo n.º 4
0
Archivo: expr.c Proyecto: ShijianXu/ICS
uint32_t eval(int p,int q)
{
	expr_ok=true;

	int dominant=0;
//printf("%d %d\n",p,q);
	if(p>q){
		printf("This is a bad expression\n");
		expr_ok=false;
		return 0;
	}
	else if(p==q){
		if(tokens[p].type==NUMBER)
			return atoi(tokens[p].str);
		else if(tokens[p].type==HEX)
		{
			char *hexnum=tokens[p].str+2;
			uint32_t sum=0,temp=0;
			int i;
			for(i=0;hexnum[i]!='\0';i++)
			{
				if(hexnum[i]>='0' && hexnum[i]<='9')
					temp=hexnum[i]-'0';
				else if(hexnum[i]>='a' && hexnum[i]<='f')
					temp=hexnum[i]-'a'+10;
				sum=sum*16+temp;
			}
			return sum; 
		}

		/*************** VAR NOT DONE **********************/
		else if(tokens[p].type==VAR)
		{
			uint32_t addr=0;
			addr=find_addr(tokens[p].str,&is_obj);
			if(!is_obj)
				printf("NO SUCH OBJECT!\n");
			return addr;
//printf("test %2x\n",addr);
		}
		/*************** VAR NOT DONE **********************/

		else if(tokens[p].type==REG)
		{

			reg_right=true;

			if(strcmp(tokens[p].str,"$eax")==0)
				return cpu.eax;
			else if(strcmp(tokens[p].str,"$ecx")==0)
				return cpu.ecx;
			else if(strcmp(tokens[p].str,"$edx")==0)
				return cpu.edx;
			else if(strcmp(tokens[p].str,"$ebx")==0)
				return cpu.ebx;
			else if(strcmp(tokens[p].str,"$esp")==0)
				return cpu.esp;
			else if(strcmp(tokens[p].str,"$ebp")==0)
				return cpu.ebp;
			else if(strcmp(tokens[p].str,"$esi")==0)
				return cpu.esi;
			else if(strcmp(tokens[p].str,"$edi")==0)
				return cpu.edi;
			else if(strcmp(tokens[p].str,"$eip")==0)
				return cpu.eip;

			else 
			{
				printf("NO SUCH REGISTER!\n");
				expr_ok=false;
				reg_right=false;
				return 0;
			}
		}
		else if(tokens[p].type==SREG)
		{
			if(strcmp(tokens[p].str,"$ES")==0)
				return cpu.sreg[0].Sreg;
			else if(strcmp(tokens[p].str,"$CS")==0)
				return cpu.sreg[1].Sreg;
			else if(strcmp(tokens[p].str,"$SS")==0)
				return cpu.sreg[2].Sreg;
			else if(strcmp(tokens[p].str,"$DS")==0)
				return cpu.sreg[3].Sreg;
			else
			{
				printf("A Bad expression!\n");
				expr_ok=false;
				return 0;
	        }
		}
		else if(tokens[p].type==EFLAGS)
		{
			if(strcmp(tokens[p].str,"$OF")==0)
				return cpu.OF;
			else if(strcmp(tokens[p].str,"$SF")==0)
				return cpu.SF;
			else if(strcmp(tokens[p].str,"$ZF")==0)
				return cpu.ZF;
			else if(strcmp(tokens[p].str,"$AF")==0)
				return cpu.AF;
			else if(strcmp(tokens[p].str,"$PF")==0)
				return cpu.PF;
			else if(strcmp(tokens[p].str,"$CF")==0)
				return cpu.CF;
		}
		else
		{
			printf("A Bad expression!\n");
			expr_ok=false;
			return 0;
		}
	}
	bool check=check_parentheses(p,q);
	if(!check)
	{
		printf("The parentheses not match!\n");
		expr_ok=false;
		return 0;
	}
	else if(check)
	{
			bool legal;
			legal=check_legal(p,q);
		if(legal)
			return eval(p+1,q-1);		
		else
		{
			dominant=find_domiop(p,q);
			if(tokens[dominant].type==LOGNOT)
			{
				int val3=eval(dominant+1,q);
				return !val3;
			}
			else if(tokens[dominant].type==DEREF)
			{
				int val4=eval(dominant+1,q);
				uint32_t value=swaddr_read(val4,4,3);
				return value;
			}
			else
			{		
				int val1=eval(p,dominant-1);
				if(expr_ok==false)
					return 0;
				int val2=eval(dominant+1,q);
				if(expr_ok==false)
					return 0;
				switch(tokens[dominant].type)
				{
					case '+':return val1+val2;
					case '-':return val1-val2;
					case '*':return val1*val2;
					case '/':return val1/val2;
					case EQ:return val1==val2;
					case NEQ:return val1!=val2;
					case LOGAND:return val1&&val2;
					case LOGOR:return val1||val2;
					default: assert(0);
				}
			}
		}

	}

	return 0;
}
Ejemplo n.º 5
0
void E_scene_main_game::update_result()
{
	
	/* check for draw by the 50 move rule: */
	if (fifty > 100) 
	{
		result = draw_by_fifty;
		return;		
	}
	else if (is_draw ()) 
	{
		result = draw_by_rep;
		return;
	}
	
	
	move_s moves[MOVE_BUFF];
	int num_moves, i, ep_temp;
	d_long temp_hash;

	ep_temp = ep_square;
	temp_hash = cur_pos;
	num_moves = 0;
	gen (&moves[0], &num_moves);
	
	// for all possible moves for all pieces
	for (i = 0; i < num_moves; i++) 
	{
		// if the move is from the side of the current player
		bool color=moves[i].from%2;
		if (current_color==WHITE)
			color=!color;
		
		// if the selected piece is not owned by the current player, continue
		if (!color) 
			continue;
		
		// test if the move is legal
		make (&moves[0], i);
		bool legal=check_legal (&moves[0], i); 
		
		unmake (&moves[0], i);
		ep_square = ep_temp;
		cur_pos = temp_hash;
		
		// if we find a legal move, game can continue
		if (legal)
			return;
	}	
	
	
	// if we are here, it means that there is no
	// move avaible for the current player
	if (in_check ())
	{
		if (white_to_move == 1)
			result = white_is_mated;
		else 
			result = black_is_mated;
	}
	else 
	{
		result = stalemate;
	}
	
}