Example #1
0
int itera(struct gol *w)
{
    int i, j, vecinas, cell;

// Cambiamos el array - tablero según las reglas de nuestro juego
// a través de un puntero
  for (i = 0; i < w->tam; i++)
    for (j = 0; j < w->tam; j++) {
       vecinas = vivas(w,i,j);
       cell = vecina(w,i,j);
       if ((cell) && (vecinas >= 2) && (vecinas <= 3))
         set_cell(w,i,j,1);
        else if (cell)
          set_cell(w,i,j,0);
        else if (vecinas == 3)
          set_cell(w,i,j,1);
        else
          set_cell(w,i,j,0); 
    }

// Cambiamos los punteros
  int *aux = w->w2;
  w->w2 = w->w1;
  w->w1 = aux;
  
  return 0;
}
Example #2
0
 void fill(double p, R rng)
 {
     #pragma omp parallel for
     for(int i=0; i<L; ++i)
     {
         for(int j=0; j<L; ++j)
         {
             if(rng() < p) set_cell(j, i, 1);
             else set_cell(j, i, 0);
         }
     }
 }
Example #3
0
File: world.c Project: FarK/CursoC
void world_init(struct world *w)
{
	memset(w->cells[0], 0, w->size_x * w->size_y * sizeof(bool));
	memset(w->cells[1], 0, w->size_x * w->size_y * sizeof(bool));
	w->current_buf = 0;

	/* Glider */
	set_cell(w, 0, 0, 1, true);
	set_cell(w, 0, 1, 2, true);
	set_cell(w, 0, 2, 0, true);
	set_cell(w, 0, 2, 1, true);
	set_cell(w, 0, 2, 2, true);
}
Example #4
0
//creates random board
void set_random_board() {
  int i, j;
  int r = 0;
  //RAND_MAX = NB_COLORS;
  for (i = 0; i< BOARD_SIZE; i++){
    for (j=0; j < BOARD_SIZE; j++) {
      r = rand() % NB_COLORS;
      set_cell(i,j,r,board);
    }
  }
  set_cell(0,BOARD_SIZE-1, color1,board);
  set_cell(BOARD_SIZE-1,0, color2,board);
}
Example #5
0
//update, given player, choice and board
void update_board(char player, char color, char * b) {
  int i,j;
  int change = 0;
  for (i=0; i<BOARD_SIZE; i++) {
    for (j=0; j<BOARD_SIZE; j++) {
      if (get_cell(i,j,b) == color) {
	if (in_board(i-1,j)) {if (get_cell(i-1,j,b) == player) {set_cell(i,j,player,b); change = 1;}}
	if (in_board(i+1,j)) {if (get_cell(i+1,j,b) == player) {set_cell(i,j,player,b); change = 1;}}
	if (in_board(i,j-1)) {if (get_cell(i,j-1,b) == player) {set_cell(i,j,player,b); change = 1;}}
	if (in_board(i,j+1)) {if (get_cell(i,j+1,b) == player) {set_cell(i,j,player,b); change = 1;}}
      }
    }
  }
  if (change) {update_board(player, color, b );}
}
Example #6
0
// Loads table content from a file
void Table::load(std::string fname)
{
    const std::vector<char> delims = {delim};
    std::ifstream in(fname);
    if (in.fail()) throw FileOpenException();

    // Delete old table
    data.clear();

    int k = 0;

    while (!in.eof())
    {
        k++;
        std::string line;
        // Load a line
        getline(in, line);
        // Split a line by cell delimiter
        std::vector<std::string> parsed = split_string(line, delims, false, true);
        int l = 0;
        for (auto it = parsed.begin(); it < parsed.end(); it++)
        {
            l++;
            // Remove spaces
            trim(*it, ' ');
            // Dont put empty cells
            if (*it == "") continue;
            set_cell(CellReference(k, l), *it);
        }
    }
}
Example #7
0
void TileMap::_set_tile_data(const DVector<int>& p_data) {

	int c=p_data.size();
	DVector<int>::Read r = p_data.read();


	for(int i=0;i<c;i+=2) {

		const uint8_t *ptr=(const uint8_t*)&r[i];
		uint8_t local[8];
		for(int j=0;j<8;j++)
			local[j]=ptr[j];

#ifdef BIG_ENDIAN_ENABLED


		SWAP(local[0],local[3]);
		SWAP(local[1],local[2]);
		SWAP(local[4],local[7]);
		SWAP(local[5],local[6]);
#endif
		int x = decode_uint16(&local[0]);
		int y = decode_uint16(&local[2]);
		uint32_t v = decode_uint32(&local[4]);
		bool flip_h = v&(1<<29);
		bool flip_v = v&(1<<30);
		v&=(1<<29)-1;

//		if (x<-20 || y <-20 || x>4000 || y>4000)
//			continue;
		set_cell(x,y,v,flip_h,flip_v);
	}

}
Example #8
0
File: main.c Project: photz/conway
int main(int argc, char* argv[])
{
  
  board_t b, bb;

  memset(&b, 0, sizeof(b));
  memset(&bb, 0, sizeof(bb));

  const pos_t p = {0, 3};

  set_cell(&b, W, p);
  set_cell(&b, W, (pos_t) {0,2});
  set_cell(&b, B, (pos_t) {0,1});

  set_cell(&b, W, (pos_t) {5, 5});
  set_cell(&b, B, (pos_t) {6, 5});
  set_cell(&b, B, (pos_t) {6, 6});
  set_cell(&b, B, (pos_t) {4, 5});


  // Why the heck is the old version equally fast?
  

  printf("black as max yields a rating of %d\n", get_rating(&b, B));


  return 0;
}
Example #9
0
//copying board into test_board
void copy_board() {
  int i;
  int j;
  for (i = 0; i<BOARD_SIZE; i++) {
    for (j=0; j<BOARD_SIZE; j++) {
      set_cell(i,j,get_cell(i,j,board),test_board);
    }
  }
}
Example #10
0
File: world.c Project: FarK/CursoC
void world_iterate(struct world *w)
{
	int neighbors;

	for (int i = 0; i < w->size_x; i++) {
		for (int j = 0; j < w->size_y; j++) {
			neighbors = count_neighbors(w, i, j);
			if (world_get_cell(w, i, j))
				set_cell(w, !w->current_buf, i, j,
					 neighbors == 2 || neighbors == 3);
			else
				set_cell(w, !w->current_buf, i, j,
					 neighbors == 3);
		}
	}

	w->current_buf = !w->current_buf;
}
Example #11
0
//creates symetric board
void set_sym_board() {
  int i, j;
  int r = 0;
  //first half
  for (i = 0; i< BOARD_SIZE; i++){
    for (j=i; j < BOARD_SIZE; j++) {
      r = rand() % NB_COLORS;
      set_cell(i,j,r,board);
    }
  }
  //second half
  for (i = 0; i<BOARD_SIZE; i++) {
    for (j = 0; j<i; j++) {
      set_cell(i,j,get_cell(j,i,board),board);
    }
  }
  set_cell(0,BOARD_SIZE-1, color1,board);
  set_cell(BOARD_SIZE-1,0, color2,board);
}
Example #12
0
end_edit(TableUI *tui) {
    if (is_editing(tui)) {
        char buf[65536];           /* An EDIT control's limit */
        int len = GetWindowTextLength(tui->edit);
        GetWindowTextA (tui->edit, buf, len + 1);
        set_cell(tui->table, tui->cur_row, tui->cur_col, buf, len);
        cancel_edit(tui);
        redraw_rows(tui, tui->cur_row, tui->cur_row);
    }
}
Example #13
0
 void apply_down_right(F fkt)
 {
     for(int i=0; i<L; ++i)
     {
         for(int j=0; j<L; ++j)
         {
             set_cell(j, i, fkt(get_cell(j, i)));
         }
     }
 }
Example #14
0
/* Tries to fill the cell (i, j) with the next available number.
Returns a flag to indicate if it succeeded. */
bool advance_cell(int i, int j)
{
    int n = clear_cell(i, j);
    while (++n <= 9) {
        if (is_available(i, j, n)) {
            set_cell(i, j, n);
            return true;
        }
    }
    return false;
}
Example #15
0
/* miss_handler handles miss case. It passes client's request
 * to server and passes server's response to client. If the 
 * response satisfies size requirements, store the response 
 * into cache. */
void miss_handler(int clientfd, struct client_request *request)
{
	int serverfd, length, response_size;
	char buf[MAXBUF], object_buf[MAX_OBJECT_SIZE];
	struct cache_cell *cell;
	rio_t rio_server;
	
	response_size = length = 0;
	
	/* acts as a client and writes request to server */
	Pthread_mutex_lock(&open_mutex);
	serverfd = open_serverfd_h(request, clientfd);
	Pthread_mutex_unlock(&open_mutex);
	rio_readinitb(&rio_server, serverfd);
	
	if (rio_writen(serverfd, request->request, request->request_length) 
					!= request->request_length)
	{
		write(2, "write error\n", strlen("write error\n"));
		close_connection(request, clientfd, serverfd);
	}
	
	/* passes server's response to client */
	while (1)
	{
		if ((length = rio_readnb(&rio_server, buf, MAXBUF)) < 0)
			close_connection(request, clientfd, serverfd);
		if (response_size + length <= MAX_OBJECT_SIZE)
			memcpy(object_buf + response_size, buf, length);
		response_size += length;
		if (rio_writen(clientfd, buf, length) < length)
			break;
		if (length != MAXBUF)
			break;
	}
	
	/* if response satisfies size requirement, store the response 
	 * into cache */
	if (response_size <= MAX_OBJECT_SIZE)
	{
		/* need a mutex to prevent inserting the same cell twice 
		 * into cache in race condition */
		Pthread_mutex_lock(&dup_mutex);
		if (search_cell_variant(request->request_line) == 0)
		{
			cell = allocate_cell();
			set_cell(cell, request->request_line, object_buf, response_size);
			add_to_list(cell);
		}
		Pthread_mutex_unlock(&dup_mutex);
	}
	close_connection(request, clientfd, serverfd);
}
Example #16
0
void
set_str(CHAR_INFO * buffer, char *str, int x, int y, unsigned char bg,
	unsigned char fg)
{
	int cx;
	for (cx = 0; cx < BUFFER_CX; cx++) {
		char c = str[cx];
		if (c == 0)
			break;
		set_cell(buffer, c, x + cx, y, bg, fg);
	}
}
/**
 * Read a file of (i,j) pairs specifying an initial set of live cells
 */
void read_board(problem_t* problem)
{
    FILE* fp = fopen(problem->init, "r");
    int i, j;
    if (fp == NULL) {
        fprintf(stderr, "Could not open board file: %s\n", problem->init);
        exit(-2);
    }
    create_board(problem);
    while (fscanf(fp, "%d %d", &i, &j) == 2)
        set_cell(problem, i, j);
    fclose(fp);
}
Example #18
0
matrix *
create_random_matrix(size_t rows, size_t cols)
{
    matrix *mm = create_matrix(rows, cols);

    for (size_t ii = 0; ii < rows; ++ii) {
        for (size_t jj = 0; jj < cols; ++jj) {
            set_cell(mm, ii, jj, 10 * rand48());
        }
    }

    return mm;
}
Example #19
0
matrix *
create_identity_matrix(size_t rows, size_t cols)
{
    matrix *mm = create_matrix(rows, cols);

    for (size_t ii = 0; ii < rows; ++ii) {
        if (ii >= cols)
            break;

        set_cell(mm, ii, ii, ONE);
    }

    return mm;
}
Example #20
0
insert_datetime(TableUI *tui, int include_time) {
    char timestr[32];
    SYSTEMTIME time;
    GetLocalTime(&time);
    if (include_time)
        sprintf(timestr, "%04d-%02d-%02d %02d:%02d:%02d",
            time.wYear, time.wMonth, time.wDay,
            time.wHour, time.wMinute, time.wSecond);
    else
        sprintf(timestr, "%04d-%02d-%02d",
            time.wYear, time.wMonth, time.wDay);
    set_cell(tui->table, tui->cur_row, tui->cur_col, timestr, strlen(timestr));
    redraw_rows(tui, tui->cur_row, tui->cur_row);
}
Example #21
0
/* Processes the program arguments. Each argument is assumed to be a string
with three digits row-col-number, 1-based, representing the known cells in the
Sudoku. For example, "123" means there is a 3 in the cell (0, 1). */
void init_known(size_t count, char** cells)
{
    for (int c = 0; c < count; ++c) {
        char* cell = cells[c];
        int i, j, n;
        if (sscanf(cell, "%1d%1d%1d", &i, &j, &n)) {
            set_cell(i-1, j-1, n);
            known[i-1][j-1] = 1;
        } else {
            printf("bad input token: %s\n", cell);
            exit(EXIT_FAILURE);
        }
    }
}
Example #22
0
static PyObject * get_primitive(PyObject *self, PyObject *args)
{
  int num_atom, num_atom_prim;
  double symprec;
  PyArrayObject* lattice_vectors;
  PyArrayObject* atomic_positions;
  PyArrayObject* atom_types;
  PyObject *array;

  double *p_lattice;
  double *p_positions;
  double lattice[3][3];
  double (*positions)[3];
  int *types_int;
  int *types;

  if (!PyArg_ParseTuple(args,
			"OOOd",
			&lattice_vectors,
			&atomic_positions,
			&atom_types,
			&symprec)) {
    return NULL;
  }

  p_lattice = (double(*))lattice_vectors->data;
  p_positions = (double(*))atomic_positions->data;
  types_int = (int*)atom_types->data;

  num_atom = atom_types->dimensions[0];
  positions = (double(*)[3]) malloc(sizeof(double[3]) * num_atom);
  types = (int*) malloc(sizeof(int) * num_atom);
  set_spglib_cell(lattice, positions, types, num_atom,
		  p_lattice, p_positions, types_int);
  num_atom_prim = spg_find_primitive(lattice, positions, types, num_atom,
				     symprec);
  array = set_cell(num_atom_prim, lattice, positions, types);

  free(types);
  free(positions);

  return array;
}
Example #23
0
/*
 * Solves a board starting with the given cell. Returns 1 if the board could be
 * solved, 0 if not.
 */
int solve_board(struct board *b, int r, int c)
{
	int prev;
	int val;

	assert_(b != NULL &&
	       r >= MIN_NUM && r <= MAX_NUM &&
	       c >= MIN_NUM && c <= MAX_NUM);

	/* Base case: board solved, print it. */
	if (b->unset_cells == 0) {
		print_board(b);
		return 1;
	}

	/* Find the next unset cell. */
	while (is_set(b, r, c) && next_cell(&r, &c))
	       ;

	/* This should never happen. */
	if (is_set(b, r, c))
		return 1;

	/* Try every possible cell value until the board can be solved. */
	prev = MIN_NUM;
	while (1) {
		val = find_common_free(b->cells[r][c].row_candidates,
				       b->cells[r][c].col_candidates,
				       b->cells[r][c].square_candidates,
				       prev);
		if (val == -1)
			break;

		set_cell(b, r, c, val);
		if (solve_board(b, r, c))
			return 1;
		unset_cell(b, r, c, val);

		prev = val+1;
	}

	return 0;
}
Example #24
0
/*
 * Reads a board from the given file. Format: a digit represents a cell value,
 * a dot represents an empty cell. Cells should be given from top to bottom and
 * left to right. All other characters are ignored.
 *
 * Example:
 *
 *     5 3 . . 7 . . . .
 *     6 . . 1 9 5 . . .
 *     . 9 8 . . . . 6 .
 *     8 . . . 6 . . . 3
 *     4 . . 8 . 3 . . 1
 *     7 . . . 2 . . . 6
 *     . 6 . . . . 2 8 .
 *     . . . 4 1 9 . . 5
 *     . . . . 8 . . 7 9
 *
 */
void read_board(FILE *f, struct board *b)
{
	int row;
	int col;
	int c;

	assert_(f != NULL && b != NULL);

	row = MIN_NUM;
	col = MIN_NUM;

	while (! feof(f)) {
		c = fgetc(f);
		if ((isdigit(c) && c != '0') || c == '.') {
			if (c != '.')
				set_cell(b, row, col, (c - '0'));
			if (! next_cell(&row, &col))
				break;
		}
	}
}
Example #25
0
void NPietTest::simpleTest()
{
    QImage image( "nhello.png" );
    image = image.convertToFormat( QImage::Format_RGB32 );
    qDebug() << image.bits()  << image.width() << image.height();
    QRgb* cells = (QRgb*) image.bits();
    set_image( image.width(), image.height());
    for( int i = 0; i < image.height(); ++i ) {
      QRgb* lineptr = (QRgb*) image.scanLine( i );
      for( int j = 0; j < image.width(); ++j ) {
        int r = qRed( lineptr[j] );
        int g = qGreen( lineptr[j] );
        int b = qBlue( lineptr[j] );
        int col = ((r * 256 + g) * 256) + b;
        int col_idx = get_color_idx (col);
        if (col_idx < 0) {
          /* set to black or white: */
          col_idx = (/*unknown_color*/1 == 0 ? c_black : c_white);
        }
        set_cell (j, i, col_idx);
      }
    }
    qDebug() << "result:" << piet_run();
}
Example #26
0
void browse(int type, int nc, int nr, void *in)
{
  WINDOW *pdlscr, *wmenu, *wscroll, *warray, *whlab, *wvlab, *wtmp;
  char s[CHBUF],echobuf[CHBUF],line[CHBUF];
  chtype ch;
  int i,j,eps,ioff,joff,iecho;
  int ncols, nrows, mycols;
  extern int colwid, dcols, drows, width[];

  pdlscr = initscr();  /* sets LINES, COLS (which aren't macro constants...) */
  clear();  /* Clear the screen before we start drawing */

  colwid = width[type];
  ncols = (COLS-HLAB)/colwid;
  dcols = MIN(nc,ncols);
  mycols = dcols*colwid;

  nrows = LINES-3;
  drows = MIN(nr,nrows);

  cbreak();
  noecho();
  nonl();
  intrflush(pdlscr,FALSE);
  keypad(pdlscr,TRUE);
  /* Menu bar */
  wmenu  = subwin(pdlscr,1,COLS,0,0);
  wvlab  = subwin(pdlscr,1,mycols,1,HLAB);
  wscroll= subwin(pdlscr,drows,mycols+HLAB,2,0);
  warray = subwin(wscroll,drows,mycols,2,HLAB);
  whlab  = subwin(wscroll,drows,HLAB,2,0);

  keypad(warray,TRUE);
  scrollok(pdlscr,TRUE);
  scrollok(wscroll,TRUE);

  wmenu  = subwin(pdlscr,1,COLS,0,0);

  sprintf(s,"Perldl data browser: type %d, (%d,%d), type q to quit\n",
	  type,nc,nr);
  mvwaddstr(wmenu,0,10,s);
  wrefresh(wmenu);

  for (i=0;i<dcols;i++) {
    update_vlab(wvlab,i,0);
  }
  wrefresh(wvlab);

  for (j=0;j<drows;j++) {
    update_hlab(whlab,j,0);
  }
  wrefresh(whlab);

  for (j=0;j<drows;j++) {
    update_row(warray,j,0,0,type,nc,in);
  }

  i = j = eps = 0;
  ioff = joff = 0;
  while (tolower(ch=mvwgetch(warray,j-joff,(i-ioff)*colwid+
		      MIN(eps,colwid-2))) != 'q') {
    /* #define ECHOCH */
#ifdef ECHOCH
    sprintf(echobuf,"%8o",ch);
    mvwaddstr(wmenu,0,iecho,echobuf);
    iecho = (iecho < 72) ? iecho+8 :0;
    wrefresh(wmenu);
#endif
    switch (ch) {
    case KEY_LEFT:
    case KEY_RIGHT:
    case KEY_UP:
    case KEY_DOWN:
    case '\t':
    case '\015':
      if (eps) {
	line[eps] = '\0';
	set_value(i,j,type,nc,in,line);
      }
      set_cell(warray,i,j,ioff,joff,type,nc,in);
      eps = 0;
      wrefresh(warray);
      break;
    case '\b':
    case KEY_DL:
    case 0177:
      if (eps) {
	eps--;
	mvwaddch(warray,j-joff,(i-ioff)*colwid+MIN(eps,colwid-2),' ');
	wrefresh(warray);
      }
      continue;
    default:
      if (!eps && ch >= 32 && ch <= 127) {
	clear_cell(warray,i-ioff,j-joff);
	wrefresh(warray);
      }
      mvwaddch(warray,j-joff,(i-ioff)*colwid+MIN(eps,colwid-2),ch|A_UNDERLINE);
      line[eps++]=ch;
      continue;
    }

    switch (ch) {
    case KEY_LEFT:
      i = (i<2)?0:i-1;
      if (i-ioff == -1) {
	ioff--;
	wtmp = newwin(1,mycols-colwid,1,HLAB);
	overwrite(wvlab,wtmp);
	mvwin(wtmp,1,HLAB+colwid);
	overwrite(wtmp,wvlab);
	delwin(wtmp);
	update_vlab(wvlab,0,ioff);
	wtmp = newwin(drows,mycols-colwid,2,HLAB);
	overwrite(warray,wtmp);
	mvwin(wtmp,2,HLAB+colwid);
	overwrite(wtmp,warray);
	delwin(wtmp);
	update_col(warray,0,ioff,joff,type,nc,in);
	wrefresh(warray);
	wrefresh(wvlab);
      }
      break;
    case KEY_RIGHT:
    case '\t':
      i = (i>nc-2)?nc-1:i+1;
      if (i-ioff == dcols) {
	ioff++;
	wtmp = newwin(1,mycols-colwid,1,HLAB+colwid);
	overwrite(wvlab,wtmp);
	mvwin(wtmp,1,HLAB);
	overwrite(wtmp,wvlab);
	delwin(wtmp);
	update_vlab(wvlab,dcols-1,ioff);
	wtmp = newwin(drows,mycols-colwid,2,HLAB+colwid);
	overwrite(warray,wtmp);
	mvwin(wtmp,2,HLAB);
	overwrite(wtmp,warray);
	delwin(wtmp);
	update_col(warray,dcols-1,ioff,joff,type,nc,in);
	wrefresh(warray);
	wrefresh(wvlab);
      }
      break;
    case KEY_UP:
      j = (j<2)?0:j-1;
      if (j-joff == -1) {
	joff--;
	wscrl(wscroll,-1);
	wrefresh(wscroll);
	update_hlab(whlab,0,joff);
	wrefresh(whlab);
	update_row(warray,0,ioff,joff,type,nc,in);
	wrefresh(warray);
      }
      break;
    case KEY_DOWN:
    case '\015':
      j = (j>nr-2)?nr-1:j+1;
      if (j-joff == drows) {
	joff++;
	wscrl(wscroll,1);
	wrefresh(wscroll);
	update_hlab(whlab,drows-1,joff);
	wrefresh(whlab);
	update_row(warray,drows-1,ioff,joff,type,nc,in);
	wrefresh(warray);
      }
      break;
    }
  }
  nl();
  echo();
  nocbreak();
  endwin();
}
Example #27
0
//корень зла
game::game(): grp_ {nullptr}, map_ {nullptr}, count_treasure_ {0}, max_count_{100}
{
    set_cell = [this](std::string chr) {
        map_->get_cell(ents_[chr]->get_x(), ents_[chr]->get_y())->chr_ = ents_[chr];
    };

    draw_cell = [this](cell* cell_)
    {
        if(cell_->get_ter_draw() != "")
        {
            grp_->draw(cell_->get_x()*cell_->get_w(),
                       cell_->get_y()*cell_->get_h(),
                       cell_->get_h(), cell_->get_w(),
                       cell_->get_ter_draw().c_str());
        }
        if(cell_->get_chr_draw() != "")
        {
            grp_->draw(cell_->get_x()*cell_->get_w(),
                       cell_->get_y()*cell_->get_h(),
                       cell_->get_h(), cell_->get_w(),
                       cell_->get_chr_draw().c_str());
        }
        grp_->draw(cell_->get_x()*cell_->get_w(),
                   cell_->get_y()*cell_->get_h(),
                   cell_->get_h(), cell_->get_w(),
                   cell_->get_lgt_draw());

        std::string collected = "collected  " + std::to_string(count_treasure_);
        grp_->draw(0, 0, 20, 140, collected.c_str(), {white, 255});
        std::string remaining = "remaining  " + std::to_string(max_count_ - count_treasure_);
        grp_->draw(500, 0, 20, 140, remaining.c_str(), {white, 255});
    };

    move_chr_to = [this](std::string chr, int x, int y)->int
    {
        if(map_->move_second_ent_to(ents_[chr]->get_x(),
        ents_[chr]->get_y(),
        ents_[chr]->get_x() + x,
        ents_[chr]->get_y() + y) == 0)
        {
            ents_[chr]->set_x(ents_[chr]->get_x() + x);
            ents_[chr]->set_y(ents_[chr]->get_y() + y);
            return 1;
        }
        return 0;
    };

    check_ter_chr_ = [this](std::string chr, int x, int y)->int
    {
        return map_->get_cell(ents_[chr]->get_x() + x, ents_[chr]->get_y() + y)->is_pass();
    };

    check_lgt_chr_ = [this](std::string chr, int x, int y)->int
    {
        return map_->get_cell(ents_[chr]->get_x() + x, ents_[chr]->get_y() + y)->is_light();
    };

    grp_ = new graphic {640, 640, 30};
    event_ = new event {};
    quit_ = false;

    ents_.insert(std::make_pair("empty", new empty {}));
    ents_.insert(std::make_pair("wall", new wall {})); //another brick in the wall
    ents_.insert(std::make_pair("light", new light {}));
    ents_.insert(std::make_pair("dark", new dark {}));

    map_ = new board {32, 32, ents_["wall"], ents_["empty"]};

    int x, y;
    do
    {
        x = rand() % 32;
        y = rand() % 32;
    } while(!(map_->get_cell(x, y)->is_pass() && map_->get_cell(x, y)->chr_ == nullptr));
    ents_.insert(std::make_pair("player", new player {x,y}));
    set_cell("player");

    do
    {
        x = rand() % 32;
        y = rand() % 32;
    } while(!(map_->get_cell(x, y)->is_pass() && map_->get_cell(x, y)->chr_ == nullptr));
    ents_.insert(std::make_pair("monster" + std::to_string(0), new monster {x,y}));
    set_cell("monster" + std::to_string(0));

    for(int i = 0; i < max_count_; ++i)
    {
        do
        {
            x = rand() % 32;
            y = rand() % 32;
        } while(!(map_->get_cell(x, y)->is_pass() && map_->get_cell(x, y)->chr_ == nullptr));

        ents_.insert(std::make_pair("treasure" + std::to_string(i), new treasure {x,y}));
        set_cell("treasure" + std::to_string(i));
    }

    collision_player_ = [this](std::string ent, int x, int y)
    {
        if(map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->get_id() == "treasure")
        {
            map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->chr_ = nullptr;
            ++count_treasure_;
        }
        else if(map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->get_id() == "monster")
        {
            event_->execute_config("quit");
        }

        if(count_treasure_ == max_count_)
        {
            event_->execute_config("quit");
        }
    };


    collision_monster_ = [this](std::string ent, int x, int y)
    {
        if(map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->get_id() == "treasure")
        {
            map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->chr_ = nullptr;
            --max_count_;
        }
        else if(map_->get_cell(ents_[ent]->get_x() + x, ents_[ent]->get_y() + y)->get_id() == "player")
        {
            event_->execute_config("quit");
        }
    };


    event_->set_config("quit", [this] {this->quit_ = true;});
    event_->set_config("up", [this] {collision_player_("player", 0, -1); move_chr_to("player", 0, -1);});
    event_->set_config("down", [this] {collision_player_("player", 0, 1); move_chr_to("player", 0, 1);});
    event_->set_config("left", [this] {collision_player_("player", -1, 0); move_chr_to("player", -1, 0);});
    event_->set_config("right", [this] {collision_player_("player", 1, 0); move_chr_to("player", 1, 0);});
    event_->set_config("m0_up", [this] {collision_monster_("monster0", 0, -1); move_chr_to("monster0", 0, -1);});
    event_->set_config("m0_down", [this] {collision_monster_("monster0", 0, 1); move_chr_to("monster0", 0, 1);});
    event_->set_config("m0_left", [this] {collision_monster_("monster0", -1, 0); move_chr_to("monster0", -1, 0);});
    event_->set_config("m0_right", [this] {collision_monster_("monster0", 1, 0); move_chr_to("monster0", 1, 0);});

    aim0_ = new ai
    {
        [this](int x, int y)->int {return check_ter_chr_("monster0", x, y);},
        [this](int x, int y)->int {return check_lgt_chr_("monster0", x, y);},
        2
    };
}
Example #28
0
void TileMap::set_cellv(const Vector2& p_pos,int p_tile,bool p_flip_x,bool p_flip_y,bool p_transpose) {

	set_cell(p_pos.x,p_pos.y,p_tile,p_flip_x,p_flip_y,p_transpose);
}
Example #29
0
/** Program entry point */
int main() 
{
   printf("\n\n  Welcome to the 7 wonders of the world of the 7 colors\n"
	      "  *****************************************************\n\n"
	 "Current board state:\n");
   srand(time(NULL));
   print_board();

   int i = 0;
   int j = 0;
   
   int victories[5][5]; // victories[i][j] contains the number of victories of i over j
   for(i = 0 ; i < 5 ; i++){
	for(j = 0 ; j < 5 ; j++){
		victories[i][j] = 0;
	}
   }
   

   long int start = clock();

   void (*strats[5])(char) = {improved_random_play, spider, greedy, double_greedy, mix};
   char* names[5] = {"Improved Random","Spider","Greedy","Double Greedy","Mix"};
   int k = 0;
   float score1 = 0;
   float score2 = 0;


   for(i = 0 ; i < 4 ; i++){
	for(j = i+1 ; j < 5 ; j++){

		for(k = 0 ; k < NB_SIMULATIONS ; k++){
			printf("%s VS %s :\n", names[i], names[j]);
			random_filling();
			copy_board();
     		 	run_game(PLAYER1,strats[i],strats[j]);

			score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE);
			score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE);
			if(score1 > score2){(victories[i][j])++;}
			else{(victories[j][i])++;}

			get_saved_board();
			set_cell(0,BOARD_SIZE-1,PLAYER2);
			set_cell(BOARD_SIZE-1,0,PLAYER1);
			run_game(PLAYER2,strats[i],strats[j]);

			score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE);
			score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE);
			if(score1 > score2){(victories[i][j])++;}
			else{(victories[j][i])++;}
		}
	}
   }


   printf("Total execution time : %ld", (clock()-start)/CLOCKS_PER_SEC);
   printf("\n\n");

   for(i = 0 ; i < 4 ; i++){
	for(j = i+1 ; j < 5 ; j++){
		printf("%s VS %s :\n", names[i], names[j]);
		printf("%s : %d victories\n", names[i], victories[i][j]);
		printf("%s : %d victories\n", names[j], victories[j][i]);
		printf("\n\n");
	}
   }


   return 0; // Everything went well
}
Example #30
0
int
main(int argc, char *argv[])
{
    parse_options(argc, argv);
    if (difficulty < MIN_DIFFICULTY) {
        choose_difficulty();
    }

    calculate_sudoku();
    print_grid();
    return 0;

#ifdef CURSED
    int num = 0;

    printf("f**k\n");
    /* initialize your non-curses data structures here */
    //printgrid();

    (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */

    initscr();      /* initialize the curses library */
    draw_grid();
    //(void) newterm();
    keypad(stdscr, TRUE);  /* enable keyboard mapping */
    (void) nonl();         /* tell curses not to do NL->CR/NL on output */
    (void) cbreak();       /* take input chars one at a time, no wait for \n */
#if 0
    (void) echo();         /* echo input - in color */
#else
    (void) noecho();
#endif

    if (has_colors()) {
        start_color();

        /*
         * Simple color assignment, often all we need.  Color pair 0 cannot
         * be redefined.  This example uses the same value for the color
         * pair as for the foreground color, though of course that is not
         * necessary:
         */
        init_pair(1, COLOR_RED,     COLOR_BLACK);
        init_pair(2, COLOR_GREEN,   COLOR_BLACK);
        init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
        init_pair(4, COLOR_BLUE,    COLOR_BLACK);
        init_pair(5, COLOR_CYAN,    COLOR_BLACK);
        init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
        init_pair(7, COLOR_WHITE,   COLOR_BLACK);
    }
    init_pair(8, COLOR_WHITE, COLOR_BLACK);
    bkgd(COLOR_PAIR(8));//wbkgd();

    set_cell(GY_0, GX_0);

    for (;;)
    {
        int c = getch();     /* refresh, accept single keystroke of input */
        handle_ch(c);
        attrset(COLOR_PAIR(num % 8));
        num++;

        /* process the command keystroke */
    }

    finish(0);               /* we're done */
#endif /* CURSED */
}