#include "movement.h"

/*
typedef struct{
    int xdiff;
    int ydiff;
    int capturing;
    int op_pc;
    int my_pc;
    int my_type;
}dest_info;
*/

//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 int CheckMate(struct movement *move);
static int Can_Save_King(void);

int Valid_Source(struct movement *move){
	//check if valid/moveable/your-turn
	//move.valid_option = 0;
	//check if it's my turn
	if(!my_turn)
		return 0;
	int pcount;
/*
	int pcx, pcy;
	int pcount2;
	int check_ret;
	struct pieces back_my;
	struct pieces back_op;
	struct movement back_move;
	int back_c;
	int back_cm;
	int back_op_cm;
*/
	//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)
					return 1;
			}
		}
	}
	return 0;
}

static int Can_Save_King(void){
	int cx, cy;
	int pc;
	int check_ret;
	struct movement *move;
	//move->source_x = my_pieces[12] = 
	//move->source_y = 
	for(cx=0; cx<8; cx++){
		//move->dest_x = cx;
		for(cy=0; cy<8; cy++){
			//set destination
			//move->dest_y = cy;
			for(pc=0; pc<16; pc++){
				move->dest_x = cx;
				move->dest_y = cy;
				move->source_x = my_pieces[pc].position.x;
				move->source_y = my_pieces[pc].position.y;
				//verify valid
				if( Valid_Destination(my_pieces, oponent_pieces, move) ){

					check_ret = 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 0;
}

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

	//int is_in_check;
	//struct pieces *p2;
	//if(p1[0].color == my_team_color){

	//}

	//if(p1 == my_pieces){
	//	p2 = oponent_pieces;
		//is_in_check = check;
	//}else if(p1 == oponent_pieces){
	//	p2 = my_pieces;
		//is_in_check = op_check;
	//}//else{
		//is_in_check = Check(p1, p2, move);
	//}

printf("here 1\n");
	if(!my_turn)
		return 0;
	//dest_info vars;
	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
	//int my_type = 6;
	//int my_pcount;
	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;
				}
			}
		}
	}
	//if(check)
	//	if(move->my_type != 5)
	//		return 0;
printf("here 2\n");
printf("t: %d mypc: %d\n", move->my_type, move->my_pc);

	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
			//8 possible moves
			//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;
					}
				}
			}
			//int check_ret;
			//struct movement tmp;
			//if(   memcpy( (void *)&tmp, (const void *)move, sizeof(struct movement) ) == NULL  ){
			//	printf("memcpy error!\n");
			//	exit(1);
			//}
			//tmp.dest_x   = move->dest_x;
			//tmp.dest_y   = move->dest_y;
			//tmp.my_type  = move->my_type;
			//check not placing king in check

			//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
			check_ret = Check(p1, p2, move);
			if(check_ret == 1 || check_ret == 3)
					return 0;
			break;
		default:
			printf("ERROR: Incorrect Valid_Destination() usage! %d is not a chess piece\n", move->my_type);
			exit(1);
			break;
	}

	//if you've made it this far then move is valid
	//move.valid_option = 1;

	//if you were already in check fail if still in check
	return 1;
}

/*
int
	if(is_in_check == 1 || is_in_check == 3){
		check_ret = Check(p1, p2, move);
		if(check_ret == 1 || check_ret == 3)
			return 0
	}
	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;
	}
	//move->dest_x   = my_pieces[12].position.x;
	//move->dest_y   = my_pieces[12].position.y;
	check = Check(p1, p2, move);
	checkmate = CheckMate(move);
}

int Simulate_Is_Check(struct pieces *p1, struct pieces *p2, struct movement *move){
    //struct movement move;
    //(void)memcpy((void*)&move, (const void *)moveptr, sizeof(struct movement) );
	int ret_val;

	struct pieces *pa = malloc( sizeof(struct pieces) );
	struct pieces *pb = malloc( sizeof(struct pieces) );
	struct movement *mb =  malloc( sizeof(struct movement) );

	(void)memcpy((void *)pa,   (const void *)p1,   sizeof(struct pieces) );
	(void)memcpy((void *)pb,   (const void *)p2,   sizeof(struct pieces) );
	(void)memcpy((void *)mb,   (const void *)move, sizeof(struct movement) );
	//printf("px: %d\n", pa[0].first_turn);
printf("2 srcx: %d srcy: %d destx: %d desty: %d \n", move->source_x, move->source_y, move->dest_x, move->dest_y );
	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;
	}
	//mb->dest_x   = my_pieces[12].position.x;
	//mb->dest_y   = my_pieces[12].position.y;
printf("3 srcx: %d srcy: %d destx: %d desty: %d \n", mb->source_x, mb->source_y, mb->dest_x, mb->dest_y );
	ret_val = Check((struct pieces *)pa, (struct pieces *)pb, (struct movement *)mb);
	free(pa);
	free(pb);
	free(mb);
	return(ret_val);
	//checkmate = CheckMate(move);
}

void Setup_Pieces(void){
	int pcount;
	//0 pawn 1 rook 2 knight 3 bishop 4 queen 5 king
	//0-7     8/15   9/14     10/13    11       12	
	//while(my_turn == 0){
	//	sleep(1);
	//}
	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;
	}
}

/*
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;
		}
	}
}
*/


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
printf("2 t: %d mypc: %d\n", vars->my_type, vars->my_pc);

	//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
	}
printf("2 tc: %d\n", my_team_color);
printf("2 tcm: %d\n", my_pieces[0].color);
printf("2 tco: %d\n", oponent_pieces[0].color);
printf("2 p10: %d pc: %d\n", p1[0].color, piece_color);
	/*
	if(p1 == my_pieces){
		p2 = oponent_pieces;
		//is_my_piece = 1;
		piece_color = my_team_color;
	}else if(p1 == oponent_pieces){
		p2 = my_pieces;
		piece_color = ~my_team_color;
	}else{
		piece_color = my_team_color;
	}
	*/
	//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;
					}
				}
			}
		}
	}
printf("end\n");
	//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;
	//struct pieces *p2;
	//if(p1[0].color == my_team_color){
	//	piece_color = my_team_color;
	//}else{
	//	piece_color = ~my_team_color;
	//}
	/*
	if(p1 == my_pieces){
		p2 = oponent_pieces;
		piece_color = my_team_color;
	}else if(p1 == oponent_pieces){
		p2 = my_pieces;
		piece_color = ~my_team_color;
	}else{
		//problem
		piece_color = ~my_team_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){
		//if(vars->ydiff == 0){
			//moving sideways
			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){ 
		//&& vars->xdiff == 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;
	//struct pieces *p2;
	/*
	int piece_color;
	if(p1[0].color == my_team_color){
		piece_color = my_team_color;
	}else{
		piece_color = ~my_team_color;
	}

	if(p1 == my_pieces){
		p2 = oponent_pieces;
		piece_color = my_team_color;
	}else if(p1 == oponent_pieces){
		p2 = my_pieces;
		piece_color = ~my_team_color;
	}else{
		piece_color = ~my_team_color;
	}
	//check that move is diagonal
	//if(vars->xdiff != vars->ydiff)
	//	return;
	*/
	//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 Check(struct pieces *p1, struct pieces *p2, struct movement *moveptr){
	//0 nobody in check  1 i'm in check  2 opponent in check  3 both in check
	//

	struct movement move;
	(void)memcpy((void*)&move, (const void *)moveptr, sizeof(struct movement) );
	int pc;
	int tmp_check = 0;
	int my_check = 0;
	int op_check = 0;
	//struct pieces *p2;
	int piece_color;
	if(p1[0].color == 1){
		piece_color = 1;
	}else{
		piece_color = 0;
	}
	/*
	if(p1 == my_pieces){
		p2 = oponent_pieces;
		piece_color = my_team_color;
	}else if(p1 == oponent_pieces){
		p2 = my_pieces;
		piece_color = ~my_team_color;
	}else{
		piece_color = my_team_color;
	}
	//movement move;
	*/
	//determine location(destination) of kings
	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;
			tmp_check = Valid_Destination(p2, p1, &move);
			if(tmp_check)
				break;
		}
	}
	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 CheckMate(struct movement *moveptr){
	//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 0;
	}
	struct movement move;
	(void)memcpy( (void *)&move, (const void *)moveptr, sizeof(struct movement) );
	//struct pieces *p2;
	//p2 = (p1 == &my_pieces) ? &oponent_pieces : &my_pieces;

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

	if(!my_pieces[12].alive)
		return 1;

	int xc, yc;
	int kx = my_pieces[12].position.x;
	int ky = my_pieces[12].position.y;
	int check_ret;

	//if in check determine if any moves will get me out of check
	//if none then checkmated

	if(check){
		//set all possible king moves
		for(xc=-1; xc<2; xc++){
			//verify that x adjustment in 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;
								//verify is a valid destination
								if( Valid_Destination(my_pieces, oponent_pieces, &move) ){
									// if killing enemy king, then don't bother checking for check
									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;
									}
								}



							}
						}
					}
				}
			}
		}
		//unless another piece can save king checkmate happens here
printf("here a\n");
		return Can_Save_King();
printf("here b\n");
		//return 1;
	}
	return 0;
}


/*
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;
		}
	}
}
*/