/* 判断一个皇后的位置是否安全 */
bool is_safe(int a[],position k,int col)
{
  for(int i = 0;i <= col-1;++i){
    if( is_same_row(a[i],k) ||  is_same_diagonal(a[i],i,k,col))
      return false;
  }
  return true;
}
Esempio n. 2
0
/* Requirement 6 - tests to see whether a move is valid or not */
BOOLEAN is_valid_move(struct move move, enum cell_contents board[][BOARD_WIDTH], enum move_validity *move_validity)
{
	struct position jump_pos;

	/* check both positions are valid */
	if (!is_valid(move.start.x, move.start.y, board))
	{
		if (move_validity != NULL) *move_validity = INVALID_START_POSITION;
		return FALSE;
	}
	if (!is_valid(move.end.x, move.end.y, board))
	{
		if (move_validity != NULL) *move_validity = INVALID_END_POSITION;
		return FALSE;
	}
	
	/* check start is peg and end is hole */
	if (!is_peg(move.start.x, move.start.y, board))
	{
		if (move_validity != NULL) *move_validity = START_NOT_PEG;
		return FALSE;
	}
	if (!is_hole(move.end.x, move.end.y, board))
	{
		if (move_validity != NULL) *move_validity = END_NOT_HOLE;
		return FALSE;
	}
	
	/* check positions are on the same row/column and separated by 1 cell */
	if (!is_same_row(move.start, move.end, 2) && !is_same_column(move.start, move.end, 2))
	{
		if (move_validity != NULL) *move_validity = NOT_SAME_ROW_COLUMN;
		return FALSE;
	}

	/* final check: there is a peg in between the positions */
	jump_pos = get_jumped_position(move.start, move.end, board);
	if (board[jump_pos.y][jump_pos.x] == PEG)
	{
		if (move_validity != NULL) *move_validity = VALID;
		return TRUE;
	}
	else
	{
		if (move_validity != NULL) *move_validity = NO_PEG_TO_JUMP;
		return FALSE;
	}
}
Esempio n. 3
0
/* returns the position between 2 positions, this function makes the assumption
   that is_valid_move() has been passed */
struct position get_jumped_position(struct position p1, struct position p2, enum cell_contents board[][BOARD_HEIGHT])
{
	struct position pos;
	pos.x = p1.x;
	pos.y = p1.y;

	if (is_same_row(p1, p2, 2))
	{
		if (p1.y > p2.y)
			pos.y = p1.y - 1;
		else
			pos.y = p1.y + 1;
	}
	else if (is_same_column(p1, p2, 2))
	{
		if (p1.x > p2.x)
			pos.x = p1.x - 1;
		else
			pos.x = p1.x + 1;
	}

	return pos;
}