Beispiel #1
0
void remove_snake_tail(snake * cur_snake, board * cur_board)
/*! Mark the cell on the board corresponding to the snake's last segment
    as open and remove the last segment from the snake.  Update the snake
    data structure to reflect this removal.
    
    If this removal results in the snake having no segments, then set the
    snake's tail and head to NULL to indicate a nonexistent snake.
*/
{
    if(cur_snake->length == 1){
        return;
    }
    snake_segment * SegPointer1 = cur_snake->tail;
    snake_segment * SegPointer2 = cur_snake->tail->next;

    //free the tail
    *board_cell(cur_board, SegPointer1->row, SegPointer1->col) = CELL_OPEN; //change cell status
    free(SegPointer1);//free the last node
    cur_snake->tail = SegPointer2; //update tail
    if(cur_snake->tail == cur_snake->head || cur_snake->length == 0){
        cur_snake->tail = NULL;
        cur_snake->head = NULL;
        
    }
    cur_snake->length -= 1;
}
Beispiel #2
0
void randomly_add_food(board * cur_board, float probability)
/*! Given a board and a probability, perform a random binary test
    that passes with the given probability (the random function will
    be useful for this, see "random" man page).  If the test passes, select
    a random cell on the board (each cell should have an equal chance
    of being selected) and change that cell to food if it is currently
    an open cell.  If the selected cell is not open, do nothing.

    We recommend that the default seed be used with the random number
    library, in order to produce deterministic results that make bugs
    easier to track down.  (i.e. just use random without calling srandom)
*/
{
    //pinpoints where the thing will be generated by specifing which cell to place in
	//first figure out where to generate fooda
	
    float num = ((float)rand() / ((float)RAND_MAX));
    if (num < probability){
    	cell * pCell = board_cell(cur_board, rand() % (cur_board->rows), rand() % (cur_board->cols));
        
    	if(*pCell == CELL_OPEN){
    		
    		*pCell = CELL_FOOD; //either food or no food
            
    	}
    }
}
Beispiel #3
0
void update_snake_head(snake * cur_snake, board * cur_board, int growth_per_food)
/*! Attempt to move the head of the given snake one cell (by appending to the snake)
    according to the snake's heading (north is up).  The head can move in the given
    direction if the destination cell is either open or food.  If the cell is food,
    add the given growth per food to the snake's growth counter.  If the destination
    cell is a snake or wall, do nothing.
*/
{

/* do nothing if it has no pieces */
   if(cur_snake->head == NULL)
	{
	}
   else
   {
       int col = cur_snake->head->col;
       int row = cur_snake->head->row;


       switch(cur_snake->heading)
       {
        case NORTH:
	    row--;
	    break;
	case SOUTH:
 	    row++;
	    break;
	case EAST:
	    col++;
	    break;
	case WEST:
	    col--;
	    break;
        }


      if( *(board_cell(cur_board, row, col)) == CELL_OPEN || *(board_cell(cur_board, row, col)) == CELL_FOOD )
      {
          if( *(board_cell(cur_board, row, col)) == CELL_FOOD )
	          cur_snake->growth += growth_per_food;

          append_snake_head(cur_snake, cur_board, row, col);
      }
    }

}
Beispiel #4
0
snake * create_snake(board * cur_board, int row, int col, direction heading, int initial_growth)
/*! Create a single-segment snake (See game.h for data structure specification)
    at the given row and column, with the given heading and given growth.
    Needed memory should be dynamically allocated with the malloc family of
    functions.  Mark the corresponding cell on the given board as occupied by
    a snake, and return a pointer to the newly created snake instance */
{
    /* This code is complete; you should not need to modify it */
    snake_segment * segment = malloc(sizeof(snake_segment));
    segment->row = row;
    segment->col = col;
    segment->next = NULL;

    snake * new_snake = malloc(sizeof(snake));
    new_snake->head = segment;
    new_snake->tail = segment;
    new_snake->heading = heading;
    new_snake->growth = initial_growth;

    *board_cell(cur_board, row, col) = CELL_SNAKE;
    return new_snake;
}
Beispiel #5
0
void update_snake_head(snake * cur_snake, board * cur_board, int growth_per_food)
/*! Attempt to move the head of the given snake one cell (by appending to the snake)
    according to the snake's heading (north is up).  The head can move in the given
    direction if the destination cell is either open or food.  If the cell is food,
    add the given growth per food to the snake's growth counter.  If the destination
    cell is a snake or wall, do nothing.
*/

{
    int temp_row = cur_snake->head->row;
    int temp_col = cur_snake->head->col;
    cell * pCell;
    switch (cur_snake->heading){
        
        case NORTH:
            temp_row--;
        break;
        case SOUTH:
            temp_row++;
        break;
        case EAST:
            temp_col++;
        break;
        case WEST:
            temp_col--;
        break;
    }
//append the new head
    pCell = board_cell(cur_board, temp_row, temp_col);
        if(*pCell == CELL_OPEN || *pCell == CELL_FOOD){
            if(*pCell == CELL_FOOD){
                cur_snake->growth += growth_per_food;

                cur_snake->food += 1;
            }
        append_snake_head(cur_snake, cur_board, temp_row, temp_col);
        }
 
}
Beispiel #6
0
void append_snake_head(snake * cur_snake, board * cur_board, int row, int col)
/*! Add a new snake segment to the given snake with the given row and column.
    This segment is the new snake head; update the snake data structure to integrate
    the new segment.  Needed memory should be allocated with the malloc family of
    functions.  Also mark the corresponding cell on the given board as being occupied
    by a snake.
*/
{
    snake_segment * SegPointer1 = cur_snake->head;
    snake_segment * SegPointer2 = malloc(sizeof(snake_segment)); //create a new snake head

    //filling out the information for both the snake and board

    SegPointer2->row = row; //fill segment location
    SegPointer2->col = col; 
    *board_cell(cur_board, row, col) = CELL_SNAKE; //change cell status
    SegPointer2->next = NULL;
    SegPointer1->next = SegPointer2; //link the head

    cur_snake->head = SegPointer2; //update head
    cur_snake->length += 1; //update snake data

}
Beispiel #7
0
void remove_snake_tail(snake * cur_snake, board * cur_board)
/*! Mark the cell on the board corresponding to the snake's last segment
    as open and remove the last segment from the snake.  Update the snake
    data structure to reflect this removal.

    If this removal results in the snake having no segments, then set the
    snake's tail and head to NULL to indicate a nonexistent snake.
*/
{
    /*mark corresponding cell to open */
    cell *pCell = board_cell(cur_board, cur_snake->tail->row, cur_snake->tail->col);
    *pCell = CELL_OPEN;

    /* store the 2nd to last piece of the snake which will be the new tail */
    snake_segment *pSeg = cur_snake->tail->next;
    /* free the current tail */
    free(cur_snake->tail);
    /* put in the new tail */
    cur_snake->tail = pSeg;

    if(pSeg == NULL)
	cur_snake->head = NULL;

}
Beispiel #8
0
void board_draw(board_t* b, int anim_i, int anim_N)
{
  setTextColor(0, 0xff);
  setIntFont(&Font_7x8);
  if (b->n_moves < 3)
    DoString(0, 0, "try to get to 'b'!");
  else
    DoString(0, 0, IntToStr(b->n_moves, 6, 0));

  bool blocks = (b->font == FONT_NONE);
  if (blocks) {
    b->cell_w = RESX / b->w;
    b->cell_h = (RESY - 8) / b->h;
  }
  else {
    setExtFont(font_list[b->font]);
    b->cell_w = b->cell_h = getFontHeight();
  }

  uint centeringx = (RESX - (b->cell_w * b->w)) / 2;
  if (centeringx >= RESX)
    centeringx = 0;

  uint centeringy = (RESY - (b->cell_h * b->h)) / 2;
  if (centeringy >= RESY)
    centeringy = 0;
  if (centeringy < 8)
    centeringy = 8;

  int x, y;
  int xx, yy;

  for (x = 0; x < b->w; x ++) {
    for (y = 0; y < b->h; y ++) {
      cell_t* c = board_cell(b, x, y);

      xx = x * b->cell_w;
      yy = y * b->cell_h;
      if (anim_i >= anim_N) {
        // animation ended.
        c->val = c->anim_newval;
        c->anim_to = -1;
      }
      else
      if (c->anim_to >= 0) {
        int from_x = xx;
        int from_y = yy;
        int to_x = (c->anim_to % b->w) * b->cell_w;
        int to_y = (c->anim_to / b->w) * b->cell_h;
        xx = from_x + anim_i * (to_x - from_x) / anim_N;
        yy = from_y + anim_i * (to_y - from_y) / anim_N;
      }

      {
        const color_t* col = cell_col(c);
        setTextColor(col->bg, col->fg);
      }

      xx += centeringx;
      yy += centeringy;
      if ((xx >= 0) && (xx < RESX) && (yy >= 0) && (yy < RESY)) {
        if (blocks) {
          if (c->val) // don't draw empty blocks
            DoRect(xx, yy, b->cell_w, b->cell_h);
        }
        else
          DoChar(xx, yy, cell_chr(c));
      }
    }
  }

}