Example #1
0
bool test_copy()
{
    piece p = new_piece_rh(0, 0, true, true);
    bool result = true;

    //the first one (set_up_1()) is with new_piece_rh (int x, int y, bool small, bool horizontal)
    set_up_1();
    for (int i = 0; i < NB_PIECES; i++) {
        copy_piece(pieces[i], p);
        result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height");
        result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width");
        result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x");
        result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y");
        result = result && test_equality_bool(is_horizontal(pieces[i]), is_horizontal(p), "copy is_horizontal");
        result = result && test_equality_bool(can_move_x(pieces[i]), can_move_x(p), "copy can_move_x");
        result = result && test_equality_bool(can_move_y(pieces[i]), can_move_y(p), "copy can_move_y");
    }
    tear_down();

    //the second one (set_up_2()) is with new_piece(int x, int y, int width, int height, bool move_x, bool move_y)
    set_up_2();
    for (int i = 0; i < NB_PIECES; i++) {
        copy_piece(pieces[i], p);
        result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height");
        result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width");
        result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x");
        result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y");
        result = result && test_equality_bool(is_horizontal(pieces[i]), is_horizontal(p), "copy is_horizontal");
        result = result && test_equality_bool(can_move_x(pieces[i]), can_move_x(p), "copy can_move_x");
        result = result && test_equality_bool(can_move_y(pieces[i]), can_move_y(p), "copy can_move_y");
    }
    tear_down();
    delete_piece(p);
    return result;
}
Example #2
0
void move_piece(piece p, dir d, int distance)
{
    if (!p) {
        failure("new_piece_rh p is NULL");
        return;
    }

    if (can_move_x(p) && !can_move_y(p)) {
        if (d == LEFT)
            p->position.x -= distance;
        if (d == RIGHT)
            p->position.x += distance;
    } else if (can_move_y(p) && !can_move_x(p)) {
        if (d == UP)
            p->position.y += distance;
        if (d == DOWN)
            p->position.y -= distance;
    } else if (can_move_y(p) && can_move_x(p)) {
        if (d == LEFT)
            p->position.x -= distance;
        if (d == RIGHT)
            p->position.x += distance;
        if (d == UP)
            p->position.y += distance;
        if (d == DOWN)
            p->position.y -= distance;
    }
}
Example #3
0
bool test_move() {
  bool result = true;
  
  piece p = new_piece(0, 0, 2, 1, true, false); //move_x-> TRUE, move_y->FALSE
  set_up();
  
  for (int dist = 1; dist < NB_PIECES; dist++)
    for (int i=0; i < NB_PIECES; i++) {

      // Test move LEFT
      copy_piece(pieces[i],p);
      move_piece(p, LEFT, dist);

      if (can_move_x(pieces[i]))
        result = result && test_equality_int(get_x(pieces[i])-dist,get_x(p),"move LEFT");
      else
        result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move LEFT");

      // Test move RIGHT
      copy_piece(pieces[i],p);
      move_piece(p, RIGHT, dist);
      
      if (can_move_x(pieces[i]))
        result = result && test_equality_int(get_x(pieces[i])+dist,get_x(p),"move RIGHT");
      else
        result = result && test_equality_int(get_x(pieces[i]),get_x(p),"move RIGHT");

      // Test move UP
      copy_piece(pieces[i],p);
      move_piece(p, UP, dist);
      
      if (can_move_y(pieces[i]))
        result = result && test_equality_int(get_y(pieces[i])+dist,get_y(p),"move UP");
      else
        result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move UP");

      // Test move DOWN
      copy_piece(pieces[i],p);
      move_piece(p, DOWN, dist);
      
      if (can_move_y(pieces[i]))
        result = result && test_equality_int(get_y(pieces[i])-dist,get_y(p),"move DOWN");
      else
        result = result && test_equality_int(get_y(pieces[i]),get_y(p),"move DOWN");

    }
  
  tear_down();
  delete_piece(p);
  
  return result;
}
Example #4
0
bool play_move(game g, int piece_num, dir d, int distance){
	if ((!can_move_y(g->pieces[piece_num]) && (d == 0 || d == 2))
			|| ((!can_move_x(g->pieces[piece_num])) && (d == 1 || d == 3))) return false;
	bool move_isAllowed = true;
	piece tmp_piece = new_piece(0,0,0,0,true,true); // Initialisation d'une pièce temporaire (mallocs)
	copy_piece(g->pieces[piece_num],tmp_piece); 
	
	for (int i = 0; i < distance; i++) { // On decompose le mouvement en déplacement de une case
		move_piece(g->pieces[piece_num], d, 1);
		if ((get_x(g->pieces[piece_num])+get_width(g->pieces[piece_num])-1 >= game_width(g) || get_x(g->pieces[piece_num]) < 0)
				|| (get_y(g->pieces[piece_num])+get_height(g->pieces[piece_num])-1 >= game_height(g) || get_y(g->pieces[piece_num]) < 0)) move_isAllowed = false;
		for(int p = 0; p < game_nb_pieces(g);++p) { // On verifie si le mouvement est valide (intersect+depassement grille)
			if (piece_num != p && intersect(g->pieces[piece_num], g->pieces[p])) move_isAllowed = false;
		}
	}
	if (move_isAllowed) {
		for(int p = 0; p < game_nb_pieces(g);++p) {
			if (piece_num != p && intersect(g->pieces[piece_num], g->pieces[p])) printf("Erreur\n");
		}
		g->nb_mouv += distance;
		delete_piece(tmp_piece);
		return true;
	}
	// si le mouvement n'est pas valide on remets la piece a sa place initiale
	copy_piece(tmp_piece, g->pieces[piece_num]);
	delete_piece(tmp_piece);
	return false;
}
Example #5
0
bool play_move(game g, int piece_num, dir d, int distance) {
    piece p=g->piece_list[piece_num];
    int travel=0;
    while (distance!=0) {
        move_piece(p, d, 1);
        g->nb_moves+=1;
        distance--;
        travel++;
        for (int i=0; i<game_nb_pieces(g); ++i) {
            if (p==g->piece_list[i]) {
                i++;
                if (i>=game_nb_pieces(g))
                    break;
            }
            if (intersect(p, game_piece(g, i))) {
                fprintf(stderr, "Unauthorized move: Piece %d is preventing %d from moving.\n\n", i, piece_num);
                move_piece(p, d, travel*-1);
                g->nb_moves-=travel;
                return false;
            }
            if (out_of_grid(p, g)) {
                fprintf(stderr, "Unauthorized move: %d would be out of bounds.\n\n", piece_num);
                move_piece(p, d, travel*-1);
                g->nb_moves-=travel;
                return false;
            }
            if (((d==LEFT||d==RIGHT)&&(!can_move_x(p)))||((d==UP||d==DOWN)&&(!can_move_y(p)))) {
                fprintf(stderr, "Unauthorized move: Piece orientation doesn't match move direction.\n\n");
                return false;
            }
        }
    }
    return true;
}
Example #6
0
piece copy_piece_for_solver(cpiece src){
  int x = get_x(src);
  int y = get_y(src);
  int width = get_width(src);
  int height = get_height(src);
  bool move_x = can_move_x(src);
  bool move_y = can_move_y(src);
  piece dst = new_piece (x, y, width, height, move_x, move_y);
  return dst;
}
Example #7
0
bool test_copy() {
  bool result = true;
  
  piece p = new_piece(0, 0, 2, 1, true, false);

  set_up();
  for (int i = 0 ; i < NB_PIECES; i++) {
    copy_piece(pieces[i],p);
    result = result && test_equality_int(get_height(pieces[i]), get_height(p), "copy get_height");
    result = result && test_equality_int(get_width(pieces[i]), get_width(p), "copy get_width");
    result = result && test_equality_int(get_x(pieces[i]), get_x(p), "copy get_x");
    result = result && test_equality_int(get_y(pieces[i]), get_y(p), "copy get_y");
    result = result && test_equality_bool(can_move_x(pieces[i]), can_move_x(p), "copy can_move_x");
    result = result && test_equality_bool(can_move_y(pieces[i]), can_move_y(p), "copy can_move_y");
  }
  
  tear_down();
  delete_piece(p);
  
  return result;
}
Example #8
0
void test_failure_piece(cpiece src)
{
    piece dst = NULL;
    copy_piece(src, dst);
    move_piece(dst, LEFT, 1);
    intersect(src, src);
    get_y(src);
    get_x(src);
    get_width(src);
    get_height(src);
    is_horizontal(src);
    can_move_x(src);
    can_move_y(src);
    delete_piece(dst);
}
Example #9
0
char* serialize(cpiece *arrPieces, int n)
{
    char *buf = malloc(n * 6 + 1);
    buf[0] = 0;
    for (int i = 0,j=0; i < n; i++,j+=6){
        buf[j] = get_x(arrPieces[i]) + '0';
        buf[j+1] = get_y(arrPieces[i]) + '0';
        buf[j+2] = get_width(arrPieces[i]) + '0';
        buf[j+3] = get_height(arrPieces[i]) + '0';
        buf[j+4] = can_move_x(arrPieces[i]) + '0';
        buf[j+5] = can_move_y(arrPieces[i]) + '0';
        buf[j+6] = '\0';
    }
    return buf;
}
Example #10
0
bool test_new_piece() {
  bool result = true;

  for(int y = 0; y < 5; y++){
    for(int x = 0; x < 5; x++){

      for(bool move_y = false; !move_y; move_y = !move_y){
	for(bool move_x = false; !move_x; move_x = !move_x){

	  int sizeX = 5 - x;
	  int sizeY = 5 - y;

	  piece p = new_piece(x, y, sizeX, sizeY, move_x, move_y);

	  // Test coordonnées de p
	  result = result && test_equality_int(x, get_x(p), "get_x");
	  result = result && test_equality_int(y, get_y(p), "get_y");

	  // Test hauteur/largeur de p
	  result = result && test_equality_int(sizeY, get_height(p), "get_height");
          result = result && test_equality_int(sizeX, get_width(p), "get_width");

	  // Test mouvement sur x
	  result = result && test_equality_bool(move_x, can_move_x(p), "move_x");

	  // Test mouvement sur y
	  result = result && test_equality_bool(move_y, can_move_y(p), "move_y");

	  delete_piece(p);
	}
      }
      
    }
  }
  return result;
}
Example #11
0
bool equals(cpiece p1, cpiece p2){
    if (p1==p2) return true;//if it's the same reference then it's the same piece.
    return (get_x(p1)==get_x(p2))&&(get_y(p1)==get_y(p2))&&(get_width(p1)==get_width(p2))&&(get_height(p1)==get_height(p2)&&(can_move_x(p1)==can_move_x(p2))&&(can_move_y(p1)==can_move_y(p2)));
}
Example #12
0
bool test_new_piece()
{
    bool result = true;
    for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 5; y++) {
            for (int compt_small = 0; compt_small < 2; ++compt_small) {
                for (int compt_horizontal = 0; compt_horizontal < 2; ++compt_horizontal) {
                    int size;
                    bool small;
                    bool horizontal;

                    if (compt_small == 0) {
                        small = true;
                        size = 2;
                    } else {
                        small = false;
                        size = 3;
                    }

                    if (compt_horizontal == 0)
                        horizontal = true;
                    else
                        horizontal = false;

                    piece p = new_piece_rh(x, y, small, horizontal);
                    result = result && test_equality_int(x, get_x(p), "get_x");
                    result = result && test_equality_int(y, get_y(p), "get_y");
                    if (horizontal) {
                        result = result && test_equality_int(1, get_height(p), "get_height");
                        result = result && test_equality_int(size, get_width(p), "get_width");
                        result = result && test_equality_bool(true, can_move_x(p), "can_move_x");
                        result = result && test_equality_bool(false, can_move_y(p), "can_move_y");
                    } else {
                        result = result && test_equality_int(size, get_height(p), "get_height");
                        result = result && test_equality_int(1, get_width(p), "get_width");
                        result = result && test_equality_bool(false, can_move_x(p), "can_move_x");
                        result = result && test_equality_bool(true, can_move_y(p), "can_move_y");
                    }
                    delete_piece(p);
                }
            }
            for (int width = 1; width < 4; width++) {
                for (int height = 1; height < 4; height++) {
                    for (int compt_move_x = 0; compt_move_x < 2; ++compt_move_x) {
                        for (int compt_move_y = 0; compt_move_y < 2; ++compt_move_y) {
                            bool move_x;
                            bool move_y;

                            if (compt_move_x == 0)
                                move_x = true;
                            else
                                move_x = false;

                            if (compt_move_y == 0)
                                move_y = true;
                            else
                                move_y = false;

                            piece p = new_piece(x, y, width, height, move_x, move_y);

                            result = result && test_equality_int(x, get_x(p), "get_x");
                            result = result && test_equality_int(y, get_y(p), "get_y");
                            result = result && test_equality_int(height, get_height(p), "get_height");
                            result = result && test_equality_int(width, get_width(p), "get_width");
                            if (move_x)
                                result = result && test_equality_bool(true, can_move_x(p), "can_move_x");
                            else
                                result = result && test_equality_bool(false, can_move_x(p), "not can_move_x");
                            if (move_y)
                                result = result && test_equality_bool(true, can_move_y(p), "can_move_y");
                            else
                                result = result && test_equality_bool(false, can_move_y(p), "not can_move_y");

                            delete_piece(p);
                        }
                    }
                }
            }
        }
    }
    return result;
}
Example #13
0
game new_game (int width, int height, int nb_pieces, piece *pieces){
	game g = malloc(sizeof(struct game_s));
	g->pieces = malloc(nb_pieces*sizeof(struct piece_s*));

	for(int i=0;i<nb_pieces;i++)
		g->pieces[i]= new_piece(get_x(pieces[i]),get_y(pieces[i]),get_width(pieces[i]),get_height(pieces[i]),can_move_x(pieces[i]), can_move_y(pieces[i]));

	g->nb_mouv = 0;
	g->nb_pieces = nb_pieces;
	g->w = width;
	g->h = height;
	return g;
}
Example #14
0
bool is_horizontal(cpiece p){
	return can_move_x(p) && !can_move_y(p);
}