Esempio n. 1
0
void Player::choose()
{
    int type;
    int n_sectors = m_cake->get_size();
    float first_ev, second_ev;
    
    CakeCut *ck = m_cake->get_cake_cut(0);

    calculatePieceEvaluation(0, ck->get_cut_sector(), 0, ck->get_cut_point(), first_ev);
    cout << "Player " << m_id << " evaluates the first piece of cake as: "<< first_ev <<  endl;

    calculatePieceEvaluation(ck->get_cut_sector(), n_sectors - 1 , ck->get_cut_point(), 1, second_ev);
    cout << "Player " << m_id << " evaluates the second piece of cake as: "<< second_ev <<  endl;
    
    Piece *left_piece;
    Piece *right_piece;
    
    if(first_ev > second_ev)
    {
	left_piece = new Piece(this, new CakeCut(this, 0, 0), ck);
	set_piece(left_piece);
	m_cake->set_chosen(0);
    }
    else
    {
	right_piece = new Piece(this, ck, new CakeCut(this, n_sectors - 1, 1));
	set_piece(right_piece);
	m_cake->set_chosen(0);
    }
}
Esempio n. 2
0
int	ky_turn(t_program *tetris, t_tetrimino *tet)
{
  tet->rot = (tet->rot + 1);
  set_piece(tet);
  if (move_piece(tetris, tet, &tetris->posit))
    {
      tet->rot = (tet->rot - 1);
      set_piece(tet);
    }
  return (0);
}
Esempio n. 3
0
int		solve_map(t_map *map, t_list *list)
{
	int			x;
	int			y;
	t_etris		*tetri;

	if (list == NULL)
		return (1);
	y = 0;
	tetri = (t_etris *)(list->content);
	while (y < map->size - tetri->height + 1)
	{
		x = 0;
		while (x < map->size - tetri->width + 1)
		{
			if (place(tetri, map, x, y))
			{
				if (solve_map(map, list->next))
					return (1);
				else
					set_piece(tetri, map, point_new(x, y), '.');
			}
			x++;
		}
		y++;
	}
	return (0);
}
Esempio n. 4
0
	Board(Board *b) 
	{
	
		//  Allocate board memory
		board.resize(BOARD_SIZE);
		for (int i = 0; i < BOARD_SIZE; ++i) {
			for (int j = 0; j < BOARD_SIZE; ++j) {
				board[i].push_back(NULL);
			}
		}
		
		//  Initialize board to match argument board.
		for (int i = 0; i < BOARD_SIZE; ++i) {
			for (int j = 0; j < BOARD_SIZE; ++j) {
				set_piece(i, j, b->get_piece(i, j));
			}
		}
		
		//  Set parent board.
		set_parent(b);
		
		//  Set player move of current board.
		turn = !b->get_player();
			
	}
Esempio n. 5
0
void Player::take()
{	
  
    int n_sectors = m_cake->get_size();

    Piece *left_piece;
    Piece *right_piece;
    
    if(m_cake->get_chosen() == 1)
    {
	left_piece = new Piece(this, new CakeCut(this, 0, 0), m_cake->get_cake_cut(0));
	set_piece(left_piece);    }
    else
    {
	right_piece = new Piece(this, m_cake->get_cake_cut(0), new CakeCut(this, n_sectors - 1, 1));
	set_piece(right_piece); 
    }
}
Esempio n. 6
0
char *read_piece(piece_t *piece, unsigned long number) {
int c;
unsigned long rows, columns, cell, i, j;
	scanf("%lu", &rows);
	if (!rows) {
		return NULL;
	}
	scanf("%lu", &columns);
	if (!columns) {
		return NULL;
	}
	while (fgetc(stdin) != '\n');
	if (!set_piece(piece, number, rows, columns, 0UL)) {
		return NULL;
	}
	cell = 0;
	for (i = 0; i < piece->rows; i++) {
		j = 0;
		do {
			c = fgetc(stdin);
			if (c == '\n') {
				break;
			}
			else if (isblock(c)) {
				piece->blocks_n++;
				piece->cells[cell] = (char)c;
			}
			else {
				piece->cells[cell] = ' ';
			}
			cell++;
			j++;
		}
		while (j < piece->columns1);
		if (j < piece->columns1) {
			while (j < piece->columns1) {
				piece->cells[cell++] = ' ';
				j++;
			}
		}
		else {
			while (fgetc(stdin) != '\n');
		}
		piece->cells[cell++] = '\n';
	}
	piece->cells[cell] = 0;
	if (!piece->blocks_n || !set_piece_blocks(piece)) {
		FREE(piece->cells);
	}
	return piece->cells;
}
Esempio n. 7
0
char *flip_piece(piece_t *piece, piece_t *flipped) {
unsigned long i, j;
	if (!set_piece(flipped, piece->number, piece->rows, piece->columns1, piece->blocks_n)) {
		return NULL;
	}
	for (i = 0; i < flipped->cells_n; i += flipped->columns2) {
		for (j = 0; j < flipped->columns2; j++) {
			flipped->cells[i+j] = piece->cells[flipped->cells_n-flipped->columns2-i+j];
		}
	}
	flipped->cells[flipped->cells_n] = 0;
	if (!set_piece_blocks(flipped)) {
		FREE(flipped->cells);
	}
	return flipped->cells;
}
Esempio n. 8
0
char *rotate_piece(piece_t *piece, piece_t *rotated) {
unsigned long i, j;
	if (!set_piece(rotated, piece->number, piece->columns1, piece->rows, piece->blocks_n)) {
		return NULL;
	}
	for (i = 0; i < rotated->rows; i++) {
		for (j = 0; j < rotated->columns1; j++) {
			rotated->cells[i*rotated->columns2+j] = piece->cells[piece->cells_n-piece->columns2-j*piece->columns2+i];
		}
		rotated->cells[i*rotated->columns2+j] = '\n';
	}
	rotated->cells[rotated->cells_n] = 0;
	if (!set_piece_blocks(rotated)) {
		FREE(rotated->cells);
	}
	return rotated->cells;
}
Esempio n. 9
0
bool Board::drop_pieces(Tetris_Shape& shape, const bool to_bottom)
{
	//Move down
	shape.y(shape.y() + 1);

	//If bottom or there is a collison, stop, else continue
	if (shape.y() >= 0 && collision_below(shape) && shape.setable()) 
	{
		shape.y(shape.y() - 1);
		set_piece(shape);
		return false;
	}
	else if (shape.y() >= 0 && collision_below(shape) && !shape.setable())
	{
		shape.y(shape.y() - 1);
		return false;
	}

	if (to_bottom && shape.y() >= 0 && !collision_below(shape)) 
	{
		drop_pieces(shape, true);
	}
	return true;
}
Esempio n. 10
0
int game_t::stonify(struct cell_t *cell)
{
    int i;
    int len = 0;
    int x = cell->x;
    int y = cell->y;

    int left  = countline(cell, -1,  0);
    int right = countline(cell, +1,  0);
    int up    = countline(cell,  0, -1);
    int down  = countline(cell,  0, +1);

    int topleft  = countline(cell, -1, -1);
    int topright = countline(cell, +1, -1);
    int botleft  = countline(cell, -1, +1);
    int botright = countline(cell, +1, +1);

    if (left + right + 1 >= 5) {
        for (i = 0; i < left; i++) {
            set_piece(x - (i + 1), y, BOARD_STONE, DROP_FLOATER);
            len++;
        }
        for (i = 0; i < right; i++) {
            set_piece(x + (i + 1), y, BOARD_STONE, DROP_FLOATER);
            len++;
        }
    }

    if (up + down + 1 >= 5) {
        for (i = 0; i < up; i++) {
            set_piece(x, y - (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
        for (i = 0; i < down; i++) {
            set_piece(x, y + (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
    }

    if (topleft + botright + 1 >= 5) {
        for (i = 0; i < topleft; i++) {
            set_piece(x - (i + 1), y - (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
        for (i = 0; i < botright; i++) {
            set_piece(x + (i + 1), y + (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
    }

    if (topright + botleft + 1 >= 5) {
        for (i = 0; i < topright; i++) {
            set_piece(x + (i + 1), y - (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
        for (i = 0; i < botleft; i++) {
            set_piece(x - (i + 1), y + (i + 1), BOARD_STONE, DROP_FLOATER);
            len++;
        }
    }

    if (len > 0) {
        set_piece(x, y, BOARD_STONE, DROP_FLOATER);
        len++;
    }

    return len;
}
Esempio n. 11
0
// parse_fen_board
// Input:   board representation as a fen string
//          unpopulated board position struct
// Output:   index of where board description ends or 0 if parsing error
//          (populated) board position struct
static int parse_fen_board(position_t *p, char *fen) {
  // Invariant: square (f, r) is last square filled.
  // Fill from last rank to first rank, from first file to last file
  fil_t f = -1;
  rnk_t r = BOARD_WIDTH - 1;

  // Current and next characters from input FEN description
  char c, next_c;

  // Invariant: fen[c_count] is next character to be read
  int c_count = 0;

  // Loop also breaks internally if (f, r) == (BOARD_WIDTH-1, 0)
  while ((c = fen[c_count++]) != '\0') {
    int ori;
    ptype_t typ;

    switch (c) {
      // ignore whitespace until the end
      case ' ':
      case '\t':
      case '\n':
      case '\r':
        if ((f == BOARD_WIDTH - 1) && (r == 0)) {  // our job is done
          return c_count;
        }
        break;

        // digits
      case '1':
        if (fen[c_count] == '0') {
          c_count++;
          c += 9;
        }
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
        while (c > '0') {
          if (++f >= BOARD_WIDTH) {
            fen_error(fen, c_count, "Too many squares in rank.\n");
            return 0;
          }
          base_board.board[square_of(r, f)] = EMPTY_PIECE;
          c--;
        }
        break;

        // pieces
      case 'N':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'N') {  // White King facing North
          ori = NN;
          typ = KING;
        } else if (next_c == 'W') {  // White Pawn facing NW
          ori = NW;
          typ = PAWN;
        } else if (next_c == 'E') {  // White Pawn facing NE
          ori = NE;
          typ = PAWN;
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        set_piece(p, r, f, typ, ori, WHITE, &base_board);
        break;

      case 'n':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'n') {  // Black King facing North
          ori = NN;
          typ = KING;
        } else if (next_c == 'w') {  // Black Pawn facing NW
          ori = NW;
          typ = PAWN;
        } else if (next_c == 'e') {  // Black Pawn facing NE
          ori = NE;
          typ = PAWN;
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        set_piece(p, r, f, typ, ori, BLACK, &base_board);
        break;

      case 'S':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'S') {  // White King facing SOUTH
          ori = SS;
          typ = KING;
        } else if (next_c == 'W') {  // White Pawn facing SW
          ori = SW;
          typ = PAWN;
        } else if (next_c == 'E') {  // White Pawn facing SE
          ori = SE;
          typ = PAWN;
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        set_piece(p, r, f, typ, ori, WHITE, &base_board);
        break;

      case 's':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 's') {  // Black King facing South
          ori = SS;
          typ = KING;
        } else if (next_c == 'w') {  // Black Pawn facing SW
          ori = SW;
          typ = PAWN;
        } else if (next_c == 'e') {  // Black Pawn facing SE
          ori = SE;
          typ = PAWN;
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        set_piece(p, r, f, typ, ori, BLACK, &base_board);
        break;

      case 'E':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'E') {  // White King facing East
          set_piece(p, r, f, KING, EE, WHITE, &base_board);
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        break;

      case 'W':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'W') {  // White King facing West
        	set_piece(p, r, f, KING, WW, WHITE, &base_board);
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        break;

      case 'e':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'e') {  // Black King facing East
        	set_piece(p, r, f, KING, EE, BLACK, &base_board);
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        break;

      case 'w':
        if (++f >= BOARD_WIDTH) {
          fen_error(fen, c_count, "Too many squares in rank");
          return 0;
        }
        next_c = fen[c_count++];

        if (next_c == 'w') {  // Black King facing West
        	set_piece(p, r, f, KING, WW, BLACK, &base_board);
        } else {
          fen_error(fen, c_count+1, "Syntax error");
          return 0;
        }
        break;

        // end of rank
      case '/':
        if (f == BOARD_WIDTH - 1) {
          f = -1;
          if (--r < 0) {
            fen_error(fen, c_count, "Too many ranks");
            return 0;
          }
        } else {
          fen_error(fen, c_count, "Too few squares in rank");
          return 0;
        }
        break;

      default:
        fen_error(fen, c_count, "Syntax error");
        return 0;
        break;
    }  // end switch
  }  // end while

  if ((f == BOARD_WIDTH - 1) && (r == 0)) {
    return c_count;
  } else {
    fen_error(fen, c_count, "Too few squares specified");
    return 0;
  }
}
Esempio n. 12
0
int maxValue(QuartoPiece a, QuartoBoard *board, MinimaxRes *res, int numPly, int alpha, int beta)
{
	int local_alpha = -10000000;
	for(int i = 0; i < 4; i++){
		for(int j = 0; j < 4; j++){
			if(!is_valid_piece(&GET_PIECE(j, i, board->board))){
				QuartoBoard newB = *board;
				set_piece(&newB, j, i, &a);
				int won = quarto_win(&newB);
				if(newB.size == 16){
					//This placement filled up the board which means that
					//we can't go further down so we just update the x, j
					//and return the value of this end state
					res->x = j;
					res->y = i;
					return won*100;
				}else if(won){
					//This is the maximum we can get, which means that we won the game
					//no reason to go further down since this lead to a victory
					res->x = j;
					res->y = i;
					return 100;
				}else if(numPly == 0){
					//We have reached the bottom of the recursion
					//and we need only evaluate the possible placements
					//of the piece that we have gotten
					int quarto_value = quarto_herustic(&newB);
					if(quarto_value > local_alpha){
						local_alpha = quarto_value;
						res->x = j;
						res->y = i;
					}
					if(local_alpha >= beta) return local_alpha;
				}else{
					int pieces_left[16]; //Array with 0 or 1 to indicate if the pieces
					//in that index is available
					prep_available(&newB, pieces_left);
					for(int k = 0; k < 16; k++){
						if(pieces_left[k]){
							MinimaxRes r;
							int value = minValue(create_piece_from_int(k), 
									&newB, &r, numPly-1,
									local_alpha, beta);
							if(value > local_alpha){
								//The value we got from below is better
								//than what we have currently found
								//which means we need to keep this
								//position and update our alpha
								res->x = j;
								res->y = i;
								res->next_piece = k;
								local_alpha = value;
							}
							//If alpha is lager than beta there is
							//no use continuing because the min node above
							//will always chose the path that lead to beta
							if(local_alpha >= beta) return local_alpha;
						}
					}
				}
			}
		}
	}
	return local_alpha;
}
Esempio n. 13
0
int minValue(QuartoPiece a, QuartoBoard *board, MinimaxRes *res, int numPly, int alpha, int beta)
{
	int local_beta = 1000000;
	for(int i = 0; i < 4; i++){
		for(int j = 0; j < 4; j++){
			if(!is_valid_piece(&GET_PIECE(j, i, board->board))){
				QuartoBoard newB = *board;
				set_piece(&newB, j, i, &a);
				int won = quarto_win(&newB);
				if(newB.size == 16){
					//This placement filled up the board which means that
					//we can't go further down so we just update the x, j
					//and return the value of this end state
					res->x = j;
					res->y = i;
					return won*-100;
				}else if(won){
					//This is the maximum we can get, which means that we won the game
					//no reason to go further down since this lead to a victory
					res->x = j;
					res->y = i;
					return -100;
				}else if(numPly == 0){
					//We have reached the bottom of the recursion
					//and we need only evaluate the possible placements
					//of the piece that we have gotten
					int quarto_value = quarto_herustic(&newB)*(-1);
					if(quarto_value < local_beta){
						local_beta = quarto_value;
						res->x = j;
						res->y = i;
					}
					if(local_beta <= alpha) return local_beta;
				}else{
					int pieces_left[16]; //Array with 0 or 1 to indicate if the pieces
					//in that index is available
					prep_available(&newB, pieces_left);
					for(int k = 0; k < 16; k++){
						if(pieces_left[k]){
							MinimaxRes r;
							int value = maxValue(create_piece_from_int(k),
								       	&newB, &r, numPly-1, 
									alpha, local_beta);
							if(value < local_beta){
								//The value we got from below is smaller
								//than the best beta we have found which
								//means we want that, so we need to update
								//x, y and update beta
								res->x = j;
								res->y = i;
								res->next_piece = k;
								local_beta = value;
							}
							//Our beta value is smaller than alpha
							//which means that the max "node" above
							//will always chose the path which leads
							//to that alpha value and there is no use
							//in recursing any more
							if(local_beta <= alpha) return local_beta;
						}
					}
				}
			}
		}
	}
	return local_beta;
}