#include "movement.h"

//0-7     8/15   9/14     10/13    11       12
int type_conv[] = {0, 0 ,0 ,0 ,0 ,0 ,0 ,0, 1, 2, 3, 4, 5, 3, 2, 1};

static int Pawn_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars);
static int One_Axis_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars);
static int Diagonal_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars);
//static int Check(struct pieces *p1, struct pieces *p2, struct movement *move);
static void CheckMate(void);
//static int Simulate_CheckMate(void);
static int Can_Save_King(struct pieces *p1, struct pieces *p2);
static int Can_Kill_King(struct pieces *p1, struct pieces *p2, struct movement *move);
static int Is_In_Check(struct pieces *p1, struct pieces *p2, struct movement *moveptr);
static int Simulate_Is_Check(struct pieces *p1, struct pieces *p2, struct movement *move);
static int Can_Piece_Save_King(int piece);

int Valid_Source(struct movement *move){
	//check if valid/moveable/your-turn

	//check if it's my turn EDIT
	//if(!my_turn)
	//	return 0;
	int pcount;
	//check if one of my piece is on the spot clicked
	for(pcount=0; pcount<16; pcount++){
		//only check alive pieces
		if(my_pieces[pcount].alive){
			if(my_pieces[pcount].position.x == move->source_x){
				if(my_pieces[pcount].position.y == move->source_y){
					if(check){
						if( Can_Piece_Save_King(pcount) )
							return 1;
						else
							printf("Invalid: move does not remove your king from check!\n");
					}else{
						return 1;
					}
				}
			}
		}
	}
	return 0;
}

static int Can_Save_King(struct pieces *p1, struct pieces *p2){
	//called when king is in check
	// 1 a move can get king out of check, 0 no moves can save king
	int cx, cy;
	int pc;
	//int check_ret;
	struct movement move;
	for(cx=0; cx<8; cx++){
		for(cy=0; cy<8; cy++){
			for(pc=0; pc<16; pc++){
				move.dest_x = cx;
				move.dest_y = cy;
				move.source_x = p1[pc].position.x;
				move.source_y = p1[pc].position.y;
				move.caller = 1;
				if( Valid_Destination(p1, p2, &move) ){
					if( !Simulate_Is_Check(p1, p2, &move) ){
					//if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
						//verify valid
						//if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
						//if( Can_Kill_King(my_pieces, oponent_pieces, &move) ){
						//	return 1;
						//}else{
						//return( ~Simulate_Is_Check(my_pieces, oponent_pieces, &move) );
						//Make_Move(&back_my, &back_op, &back_move);
						//check_ret = Check(&back_my, &back_op, &back_move);
						//if(check_ret != 1){
						//	if(check_ret != 3){
						//		return 1;
						//	}
						//}
						return 1;
					}
					//if( Can_Kill_King(p1, p2, &move) )
					//	return 1;
					continue;
				}
			}
		}
	}
	return 0;
}

static int Can_Piece_Save_King(int piece){
	int cx, cy;
	struct movement move;
	move.source_x = my_pieces[piece].position.x;
	move.source_y = my_pieces[piece].position.y;
	for(cx=0; cx<8; cx++){
		for(cy=0; cy<8; cy++){
			move.dest_x = cx;
			move.dest_y = cy;
			if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
				if( !Simulate_Is_Check(my_pieces, oponent_pieces, &move) ){
					//if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
					return 1;

					//if( Can_Kill_King(my_pieces, oponent_pieces, &move) ){
					//return 1;
					//}else{
					//return( ~Simulate_Is_Check(my_pieces, oponent_pieces, &move) );
				}
				//if( Can_Kill_King(my_pieces, oponent_pieces, &move) )
				//	return 1;
				continue;
			}
		}
	}
	return 0;
}
static int Can_Kill_King(struct pieces *p1, struct pieces *p2, struct movement *move){
	//if move results in killing enemy king
	if(p2[12].position.x == move->dest_x){
		if(p2[12].position.y == move->dest_y)
			return 1;
	}
	return 0;
}

int Valid_Destination(struct pieces *p1, struct pieces *p2, struct movement *move){
	//check if valid/not-occupied/your-turn/equals-src/etc.
	//if(!my_turn)
	//	return 0;
	//int check_ret;

	move->xdiff = abs(move->dest_x - move->source_x);
	move->ydiff = abs(move->dest_y - move->source_y);

	//make sure we aren't moving to same destination as source
	if(move->xdiff == 0){
		if(move->ydiff == 0)
			return 0;
	}
	int pcount;
	move->capturing = 0;
	//int piece_to_capture;
	for(pcount=0; pcount<16; pcount++){
		//only check alive pieces
		if(p1[pcount].alive){
			//make sure one of my peices isn't already on the destination
			if(p1[pcount].position.x == move->dest_x){
				if(p1[pcount].position.y == move->dest_y)
					return 0;
			}
		}
		if(p2[pcount].alive){
			//see if opponent is on destination
			if(p2[pcount].position.x == move->dest_x){
				if(p2[pcount].position.y == move->dest_y){
					move->capturing=1;
					move->op_pc = pcount;
				}
			}
		}
	}
	/*check type limitations for move
	  -pawns move 1 square forward(to empty destinations) or forward angle(if oponent can be captured)
	  -rook move right/left/up/down to empty destination or oponent occupied long as not blocked by team
	  -knight move in L shape to empty or oponent occupied
	  -bishop move at angles to empty or oponent occupied long as not blocked by team
          -queen move right/left/up/down/angle long as not blocked by team
	  -king move 1 square right/left/up/down/angle long as not blocked by team
	  ---remember to implement castling maneuver
	*/
	//check what type I am
	for(pcount=0; pcount<16; pcount++){
		if(p1[pcount].alive){
			if(p1[pcount].position.x == move->source_x){
				if(p1[pcount].position.y == move->source_y){
					move->my_pc = pcount;
					move->my_type = p1[pcount].type;
					break;
				}
			}
		}
	}

	switch(move->my_type){
		case 0: //pawn
			if( Pawn_Move_Fail(p1, p2, move) )
				return 0;
			break;
		case 1: //rook
			//check not moving diagonal
			if(move->xdiff > 0){
				if(move->ydiff > 0)
					return 0;
			}
			if( One_Axis_Move_Fail(p1, p2, move) )
				return 0;
			break;
		case 2: //knight
			//check that L shaped move is happening
			if(move->xdiff != 2){
				if(move->ydiff != 2)
					return 0;
			}
			if(move->xdiff == 2){
				if(move->ydiff != 1)
					return 0;
			}
			if(move->ydiff == 2){
				if(move->xdiff != 1)
					return 0;
			}
			break;
		case 3: //bishop
			//check that move is diagonal
			if(move->xdiff != move->ydiff)
				return 0;
			if( Diagonal_Move_Fail(p1, p2, move) )
				return 0;
			break;
		case 4: //queen
			if(move->xdiff != move->ydiff){ //if not diagonal
				if(move->xdiff != 0){ //if x moved then y can't
					if(move->ydiff != 0)
						return 0;
				}
				if(move->ydiff != 0){ //if y moved then x can't
					if(move->xdiff != 0)
						return 0;
				}
				if( One_Axis_Move_Fail(p1, p2, move) )
					return 0;
			}
			if(move->xdiff == move->ydiff){ // if diagonal
				if( Diagonal_Move_Fail(p1, p2, move) )
					return 0;
			}
			break;
		case 5: //king
			//check not moving more than 1 space
			if(move->xdiff > 1 || move->ydiff > 1)
				return 0;
			//check not moving to spot occupied by own piece
			for(pcount=0; pcount<16; pcount++){
				if(p1[pcount].alive){
					if(p1[pcount].position.x == move->dest_x){
						if(p1[pcount].position.y == move->dest_y)
							return 0;
					}
				}
			}
			//Can_Kill_King(p1, p2, move);

			//if move results in killing enemy king then it's ok
			//if(p2[12].position.x == move->dest_x){
			//	if(p2[12].position.y == move->dest_y)
			//		return 1;
			//}
			//check not moving to a spot that puts me in check
			if( Simulate_Is_Check(p1, p2, move) ){
				if( Can_Kill_King(p1, p2, move) )
					return 1;
				else
					return 0;
			}
			break;
		default:
			printf("ERROR: Incorrect Valid_Destination() usage! %d is not a chess piece\n", move->my_type);
			printf("Calling Function: %d\n", move->caller);
			exit(1);
			break;
	}

	//if you've made it this far then move is valid
	return 1;
}

void Make_Move(struct pieces *p1, struct pieces *p2, struct movement *move){
	p1[move->my_pc].position.x = move->dest_x;
	p1[move->my_pc].position.y = move->dest_y;
	p1[move->my_pc].first_turn = 0;
	if(move->capturing){
		p2[move->op_pc].alive = 0;
		printf("%d set to dead\n", p2[move->op_pc].type);
	}
	move->caller = 0;
	check = Is_In_Check(p1, p2, move);
	move->caller = 1;
	op_check = Is_In_Check(p2, p1, move);
	//check = Check(p1, p2, move);
	//CheckMate();
	//if(op_check){
	//	op_checkmate = ~Can_Save_King(p2, p1);
	//}
	if(op_check){
		//printf("opponent can save king: %d\n", Can_Save_King(p2, p1) );
		op_checkmate = Can_Save_King(p2, p1) ? 0 : 1;
		printf("opponent can save king: %d\n", op_checkmate ? 0 : 1);
	}
	//if(check){
	//
	//	printf("you can save king: %d\n", Can_Save_King(p1, p2) );
	//}
}

static int Simulate_Is_Check(struct pieces *p1, struct pieces *p2, struct movement *move){
	//Tells if potential move would result in p1's king being in check
	int ret_val;
	struct pieces *pa = malloc( sizeof(struct pieces)*16 );
	struct pieces *pb = malloc( sizeof(struct pieces)*16 );
	struct movement *mb =  malloc( sizeof(struct movement) );

	(void)memcpy((void *)pa,   (const void *)p1,   sizeof(struct pieces)*16 );
	(void)memcpy((void *)pb,   (const void *)p2,   sizeof(struct pieces)*16 );
	(void)memcpy((void *)mb,   (const void *)move, sizeof(struct movement) );
	pa[move->my_pc].position.x = move->dest_x;
	pa[move->my_pc].position.y = move->dest_y;
	pa[move->my_pc].first_turn = 0;
	if(move->capturing){
		pb[move->op_pc].alive = 0;
	}
	move->caller = 2;
	ret_val = Is_In_Check( pa, pb, mb );
	free(pa);
	free(pb);
	free(mb);
	return(ret_val);
}

void Setup_Pieces(void){
	int pcount;
	int pcount2 = 0;
	const int black_pawn_row = 6;
	const int white_pawn_row = 1;

	for(pcount=0; pcount<16; pcount++){
		my_pieces[pcount].alive         = 1;
		my_pieces[pcount].type          = type_conv[pcount];
		my_pieces[pcount].first_turn    = 1;
		pcount2 = (pcount > 7) ? pcount - 8 : pcount;
		my_pieces[pcount].color         = my_team_color;

		my_pieces[pcount].position.x    = pcount2;
		if(my_team_color) //i'm the black team
			my_pieces[pcount].position.y = (pcount<8) ? black_pawn_row : black_pawn_row+1;
		else
			my_pieces[pcount].position.y = (pcount<8) ? white_pawn_row : white_pawn_row-1;
	}

	for(pcount=0; pcount<16; pcount++){
		oponent_pieces[pcount].alive         = 1;
		oponent_pieces[pcount].first_turn    = 1;
		oponent_pieces[pcount].type          = type_conv[pcount];
		pcount2 = (pcount > 7) ? pcount - 8 : pcount;
		oponent_pieces[pcount].position.x    = pcount2;
		oponent_pieces[pcount].color         = my_team_color ? 0 : 1;
		if(my_team_color) //i'm the black team
			oponent_pieces[pcount].position.y = (pcount<8) ? white_pawn_row : white_pawn_row-1;
		else
			oponent_pieces[pcount].position.y = (pcount<8) ? black_pawn_row : black_pawn_row+1;
	}
}

static int Pawn_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars){
	//pawn
	//check that move is no more than 1 space in correct direction (cept first turn)  
	// remember En Passant
	//has 4 posible moves from any position

	//check verticle movement, must move up/down
	if(vars->ydiff == 0)
		return 1;

	//int is_my_piece = 0;
	int piece_color;
	//struct pieces *p2;
	if(p1[0].color == 0){
		piece_color = 0; //p1 is white team bottom
	}else{
		piece_color = 1; //p1 is black team top
	}

	//check not moving backwards
	if(piece_color){ //black top
		if(vars->dest_y > vars->source_y)
			return 1;
	}else{ //white bottom
		if(vars->dest_y < vars->source_y)
			return 1;
	}

	//check that x movement is no greater than 1
	if(vars->xdiff > 1)
		return 1;
	//check that y is no greater than 1, or 2 if first turn ... 
	if(vars->ydiff > 1){
		if(vars->ydiff != 2 || p1[vars->my_pc].first_turn != 1)
			return 1;
		//check that not moving thru an occupied space (can only happen on first turn)
		int pcount;
		for(pcount=0; pcount<16; pcount++){
			if(p2[pcount].alive){
				if(p2[pcount].position.x == vars->dest_x){
					if(piece_color){ //if p1 is black
						if(p2[pcount].position.y == vars->dest_y+1)
							return 1;
					}else{ //white
						if(p2[pcount].position.y == vars->dest_y-1)
							return 1;
					}
				}
			}
			if(pcount == vars->my_pc)
				continue;
			if(p1[pcount].alive){
				if(p1[pcount].position.x == vars->dest_x){
					if(piece_color){ //if black
						if(p1[pcount].position.y == vars->dest_y+1)
							return 1;
					}else{ //white
						if(p1[pcount].position.y == vars->dest_y-1)
							return 1;
					}
				}
			}
		}
	}

	//check that enemy is not on space if moving forward
	if(vars->capturing){
		if(vars->xdiff == 0)
			return 1;
	}
	//check that enemy is on space if moving diag
	if(vars->xdiff == vars->ydiff){
		if(!vars->capturing)
			return 1;
	}
	return 0;
}

static int One_Axis_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars){
	//rook
	int pcount;
	//int piece_color;
	//implement castling later

	//check not moving diagonal
	//if(vars->xdiff > 0){
	//	if(vars->ydiff > 0)
	//		return 1;
	//}

	//check not moving thru a piece
	if(vars->xdiff > 0){
			for(pcount=0; pcount<16; pcount++){
				if(p2[pcount].alive){
					if(p2[pcount].position.y == vars->dest_y){
						if(p2[pcount].position.x < vars->dest_x){
							if(p2[pcount].position.x > vars->source_x)
								return 1;
						}
						if(p2[pcount].position.x > vars->dest_x){
							if(p2[pcount].position.x < vars->source_x)
								return 1;
						}
					}
				}
				if(p1[pcount].alive){
					if(p1[pcount].position.y == vars->dest_y){
						if(p1[pcount].position.x < vars->dest_x){
							if(p1[pcount].position.x > vars->source_x)
								return 1;
						}
						if(p1[pcount].position.x > vars->dest_x){
							if(p1[pcount].position.x < vars->source_x)
								return 1;
						}
					}
				}
			}
	}
	if(vars->ydiff > 0){ 
		//moving up/down
		for(pcount=0; pcount<16; pcount++){
			if(p2[pcount].alive){
				if(p2[pcount].position.x == vars->dest_x){
					if(p2[pcount].position.y < vars->dest_y){
						if(p2[pcount].position.y > vars->source_y)
							return 1;
					}
					if(p2[pcount].position.y > vars->dest_y){
						if(p2[pcount].position.y < vars->source_y)
							return 1;
					}
				}
			}
			if(p1[pcount].alive){
				if(p2[pcount].position.x == vars->dest_x){
					if(p1[pcount].position.y < vars->dest_y){
						if(p1[pcount].position.y > vars->source_y)
							return 1;
					}
					if(p1[pcount].position.y > vars->dest_y){
						if(p1[pcount].position.y < vars->source_y)
							return 1;
					}
				}
			}
		}
	}
	return 0;
}

static int Diagonal_Move_Fail(struct pieces *p1, struct pieces *p2, struct movement *vars){
	//bishop
	int pcount;

	//check that not moving thru a piece
	for(pcount=0; pcount<16; pcount++){
		if(vars->capturing){
			if(pcount == vars->op_pc){
				continue;
			}
		}
		if(!p2[pcount].alive)
			continue;
		//(x2-x1)*(y3-y1) = (x3-x1)*(y2-y1)
		if( (vars->dest_x - vars->source_x) * (p2[pcount].position.y - vars->source_y) == 
		    (p2[pcount].position.x - vars->source_x) * (vars->dest_y - vars->source_y) ){
			if(p2[pcount].position.x > vars->source_x){
				if(p2[pcount].position.x < vars->dest_x)
					return 1;
			}
			if(p2[pcount].position.x < vars->source_x){
				if(p2[pcount].position.x > vars->dest_x)
					return 1;
			}
		}
	}
	for(pcount=0; pcount<16; pcount++){
		if(!p1[pcount].alive)
			continue;
		if(pcount == vars->my_pc)
			continue;
		if( (vars->dest_x - vars->source_x) * (p1[pcount].position.y - vars->source_y) ==
		    (p1[pcount].position.x - vars->source_x) * (vars->dest_y - vars->source_y) ){
			if(p1[pcount].position.x > vars->source_x){
				if(p1[pcount].position.x < vars->dest_x)
					return 1;
			}
			if(p1[pcount].position.x < vars->source_x){
				if(p1[pcount].position.x > vars->dest_x)
					return 1;
			}
		}
	}
	return 0;
}


static int Is_In_Check(struct pieces *p1, struct pieces *p2, struct movement *moveptr){
	//1 p1's king is in check, 0 not in check

	struct movement move;
	(void)memcpy((void*)&move, (const void *)moveptr, sizeof(struct movement) );
	int pc;
	printf("Is_In_Check(): called by %d\n", move.caller);

	//determine location(destination) of king
	move.dest_x   = p1[12].position.x;
	move.dest_y   = p1[12].position.y;
	//am I in check?
	for(pc=0; pc<16; pc++){
		if(p2[pc].alive){
			move.source_x = p2[pc].position.x;
			move.source_y = p2[pc].position.y;
			move.caller = 2;
			if( Valid_Destination(p2, p1, &move) )
				return 1;
		}
	}
	return 0;
}

/*
	if(tmp_check == 1 && piece_color == 1)
		my_check = 1;
	if(tmp_check == 1 && piece_color == 0)
		op_check = 1;
	tmp_check = 0;
	//determine location(destination) of kings
	move.dest_x   = p2[12].position.x;
	move.dest_y   = p2[12].position.y;
	//are you in check
	for(pc=0; pc<16; pc++){
		if(p1[pc].alive){
			move.source_x = p1[pc].position.x;
			move.source_y = p1[pc].position.y;
			tmp_check = Valid_Destination(p1, p2, &move);
			if(tmp_check)
				break;
		}
	}
	if(tmp_check == 1 && piece_color == 1)
		op_check = 1;
	if(tmp_check == 1 && piece_color == 0)
		my_check = 1;

	if(my_check){
		if(op_check){
			return 3;
		}
		return 1;
	}
	if(op_check){
		return 2;
	}
	return 0;
}
*/

//static int Simulate_CheckMate(void){
//	return 1;
//}

static void CheckMate(void){
	//1 game over 0 alive
	//
	//if enemy king is dead go ahead and set and return
	if(!oponent_pieces[12].alive){
		op_checkmate = 1;
		return;
	}
	//struct movement move;
	//(void)memcpy( (void *)&move, (const void *)moveptr, sizeof(struct movement) );

	//if(!oponent_pieces[12].alive)
	//	op_checkmate = 1;

	if(!my_pieces[12].alive){
		checkmate = 1;
		return;
	}
	struct movement move;
	int xc, yc;
	int kx = oponent_pieces[12].position.x;
	int ky = oponent_pieces[12].position.y;
	//int check_ret;

	//if in check determine if king can move to save himself
	if(op_check){
		//set all possible king moves
		for(xc=-1; xc<2; xc++){
			//verify that x adjustment is on the board
			if(kx+xc > -1){
				if(kx+xc<8){
					for(yc=-1; yc<2; yc++){
						//verify that y adjustment is on the board
						if(ky+yc > -1){
							if(ky+yc<8){
								move.source_x = kx;
								move.source_y = ky;
								move.dest_x = kx+xc;
								move.dest_y = ky+yc;
								move.caller = 3;
								//verify is a valid destination

								if( Valid_Destination(oponent_pieces, my_pieces, &move) ){
									if( !Simulate_Is_Check(oponent_pieces, my_pieces, &move) )

									//if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
									// if killing enemy king, then don't bother checking for check
									//if(Can_Kill_King(move) ){
									//		op_checkmate = 1;
									//		return;
									//}
									//if(move.dest_x == oponent_pieces[12].position.x){
									//	if(move.dest_y == oponent_pieces[12].position.y){
									//		op_checkmate = 1;
									//		return 0;
									//	}
									//}
									//verify that this move won't keep king in check
									//check_ret = Check(my_pieces, oponent_pieces, &move);
									//if(check_ret == 0 || check_ret == 2){
									//		return 0;
									//}
										//not checkmate
										return;
								//if( Can_Kill_King(oponent_pieces, my_pieces, &move) )
								//	return;
								}
							}
						}
					}
				}
			}
		}
		//unless another piece can save king checkmate happens here
		op_checkmate = ~Can_Save_King(oponent_pieces, my_pieces);
		return;
		//return 1;
	}
	return;
}



/*
int Queen_Move_Fail(dest_info *vars){
	//queen
	//check that move is either: diagonal, up/down, or right/left
	if(vars->xdiff != vars->ydiff){ //if not diagonal
		if(vars->xdiff != 0){ //if x moved then y can't
			if(vars->ydiff != 0)
				return;
			//moving sideways
			for(pcount=0; pcount<16; pcount++){
				if(oponent_pieces[pcount].alive){
					if(oponent_pieces[pcount].position.y == move.destination.y){
						if(oponent_pieces[pcount].position.x < move.destination.x && oponent_pieces[pcount].position.x > move.source.x)
							return;
						if(oponent_pieces[pcount].position.x > move.destination.x && oponent_pieces[pcount].position.x < move.source.x)
							return;
					}
				}
				if(my_pieces[pcount].alive){
					if(my_pieces[pcount].position.y == move.destination.y){
						if(my_pieces[pcount].position.x < move.destination.x && my_pieces[pcount].position.x > move.source.x)
							return;
						if(my_pieces[pcount].position.x > move.destination.x && my_pieces[pcount].position.x < move.source.x)
							return;
					}
				}
			}
		}
		if(vars->ydiff != 0){ //if y moved then x can't
			if(vars->xdiff != 0)
				return;
			//moving up/down
			for(pcount=0; pcount<16; pcount++){
				if(oponent_pieces[pcount].alive){
					if(oponent_pieces[pcount].position.x == move.destination.x){
						if(oponent_pieces[pcount].position.y < move.destination.y && oponent_pieces[pcount].position.y > move.source.y)
							return;
						if(oponent_pieces[pcount].position.y > move.destination.y && oponent_pieces[pcount].position.y < move.source.y)
							return;
					}
				}
				if(my_pieces[pcount].alive){
					if(oponent_pieces[pcount].position.x == move.destination.x){
						if(my_pieces[pcount].position.y < move.destination.y && my_pieces[pcount].position.y > move.source.y)
							return;
						if(my_pieces[pcount].position.y > move.destination.y && my_pieces[pcount].position.y < move.source.y)
							return;
					}W
				}
			}
		}
	}

	//check that not moving thru a piece
	for(pcount=0; pcount<16; pcount++){
		if(oponent_pieces[pcount].alive){
			//slope of 1
			if( move.source.y-oponent_pieces[pcount].position.y / move.source.x-oponent_pieces[pcount].position.x ){
                                               //check if between source/dest x values
                                               if(oponent_pieces[pcount].position.x > move.source.x && oponent_pieces[pcount].position.x < move.destination.x){
                                                       //check if shares the same y intercept
                                                       if(move.source.y-move.source.x == oponent_pieces[pcount].position.y-oponent_pieces[pcount].position.x){
                                                               //don't count piece we are capturing
                                                                if(pcount != vars->op_pc)
                                                                        return;
                                                        }
                                                }
                                               if(oponent_pieces[pcount].position.x < move.source.x && oponent_pieces[pcount].position.x > move.destination.x){
                                                        if(move.source.y-move.source.x == oponent_pieces[pcount].position.y-oponent_pieces[pcount].position.x){
                                                                if(pcount != vars->op_pc)
                                                                        return;
                                                        }
                                                }
                                        }
                                }
                                if(my_pieces[pcount].alive){
                                        if( move.source.y-my_pieces[pcount].position.y / move.source.x-my_pieces[pcount].position.x ){
                                                if(my_pieces[pcount].position.x > move.source.x && my_pieces[pcount].position.x < move.destination.x){
                                                        if(move.source.y-move.source.x == my_pieces[pcount].position.y-my_pieces[pcount].position.x){
                                                                //don't count self
                                                                if(pcount != vars->my_pc)
                                                                        return;
                                                        }
                                                }
                                                if(my_pieces[pcount].position.x < move.source.x && my_pieces[pcount].position.x > move.destination.x){
                                                        if(move.source.y-move.source.x == my_pieces[pcount].position.y-my_pieces[pcount].position.x){
                                                                if(pcount != vars->my_pc)
                                                                        return;
                                                        }
                                                }
                                        }
                                }
                        }
	return 0;
}


void Adjust_Bounds(void){
	int xc, yc;
	float lx, ty, rx, by;
	float eighth_x, eighth_y;

	lx = win_x-board_x-(win_x-board_x)/2;
	ty = win_y-board_y-(win_y-board_y)/2;
	by = ty + board_y;
	rx = lx + board_x;

	eighth_x = board_x/8;
	eighth_y = board_y/8;

	for(xc=0; xc<8; xc++){
		for(yc=0; yc<8; yc++){
			//.x1 .y1 = left bottom  x2 y2 = right top
			bounds[xc][yc].x1 = lx + xc*eighth_x;
			bounds[xc][yc].y1 = by - yc*eighth_y;
			bounds[xc][yc].x2 = lx + eighth_x + xc*eighth_x;
			bounds[xc][yc].y2 = by - eighth_y - yc*eighth_y;
		}
	}
}
*/