void TetrisForm::move() { if(yspeed>0) { bool new_block = false; for(unsigned int i=0; i < all_blocks.size(); i++) { for(int a=0; a<=3; a++) { for(int b=0; b<=3; b++) { if((col_y(rects[a], all_blocks[i].rects[b]) == 1) and (id != all_blocks[i].id) and all_blocks[i].in_game) { if((rects[a].w == BLOCK_SIZE) and (all_blocks[i].rects[b].w == BLOCK_SIZE)){ //bug 1 //std::cout << "id: " << id << " col with id: "<< all_blocks[i].id << " a:" <<a<<" b:"<<b<<" i:"<<i <<std::endl; //std::cout << "active block: (" << xpos << ", " << ypos<< ") rect: (" << rects[a].x << ", " << rects[a].y<<") W:"<< rects[a].w<<" collides with: (" << all_blocks[i].rects[b].x<<", "<<all_blocks[i].rects[b].y <<") "<<std::endl; new_block=true; yspeed = 0; } } if(ypos>SCREEN_HEIGHT){ below=true; } } } } /*if((ypos > SCREEN_HEIGHT) and in_game) { new_block = true; in_game = false; }*/ if(new_block and in_game){ TetrisForm temp(round(random_pos(), BLOCK_SIZE), 0, true, random_block()); all_blocks.push_back(temp); } else if (below and in_game) { in_game = false; TetrisForm temp(round(random_pos(), BLOCK_SIZE), 0, true, random_block()); all_blocks.push_back(temp); } } ypos += yspeed; if(yspeed == 0) { ypos = round(ypos, BLOCK_SIZE); } }
/* * try ITERN random program iterations. */ static void stress(table_t *tab_p, const int iter_n, const int mmaping_b) { void *data, *key; int which = 0, mode, weight_total; int iter_c, pnt_c, free_c, ret, ksize, dsize; entry_t *grid, *free_p, *grid_p, *last_p; int linear_b = 0, linear_eof_b = 0; (void)printf("Performing stress tests with %d iterations:\n", iter_n); (void)fflush(stdout); grid = malloc(sizeof(entry_t) * MAX_ENTRIES); if (grid == NULL) { (void)printf("problems allocating space for %d entries.\n", MAX_ENTRIES); exit(1); } /* initialize free list */ free_p = grid; for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) { grid_p->en_free_b = 1; grid_p->en_key = NULL; grid_p->en_key_size = 0; grid_p->en_data = NULL; grid_p->en_data_size = 0; grid_p->en_next_p = grid_p + 1; } /* redo the last next pointer */ (grid_p - 1)->en_next_p = NULL; free_c = MAX_ENTRIES; #if 0 /* load the list */ if (mmaping_b) { for (ret = table_first(tab_p, (void **)&key_p, NULL, (void **)&data_p, NULL); ret == TABLE_ERROR_NONE; ret = table_next(tab_p, (void **)&key_p, NULL, (void **)&data_p, NULL)) { } } #endif /* total the weights */ weight_total = 0; for (mode = 0; mode < MODE_MAX; mode++) { weight_total += mode_weights[mode]; } for (iter_c = 0; iter_c < iter_n;) { int weight; /* decide what to do */ weight = RANDOM_VALUE(weight_total) + 1; for (mode = 0; mode < MODE_MAX; mode++) { weight -= mode_weights[mode]; if (weight <= 0) { break; } } /* out of bounds */ if (mode >= MODE_MAX) { continue; } switch (mode) { case MODE_CLEAR: if (mmaping_b || large_b) { continue; } call_c++; table_clear(tab_p); /* re-init free list */ free_p = grid; for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) { if (! grid_p->en_free_b) { if (grid_p->en_key != NULL) { free(grid_p->en_key); } if (grid_p->en_data != NULL) { free(grid_p->en_data); } } grid_p->en_free_b = 1; grid_p->en_next_p = grid_p + 1; } /* redo the last next pointer */ (grid_p - 1)->en_next_p = NULL; free_c = MAX_ENTRIES; linear_b = 0; linear_eof_b = 0; iter_c++; if (verbose_b) { (void)printf("table cleared.\n"); fflush(stdout); } break; case MODE_INSERT: if (mmaping_b) { continue; } if (free_c > 0) { which = RANDOM_VALUE(free_c); last_p = NULL; grid_p = free_p; for (pnt_c = 0; pnt_c < which && grid_p != NULL; pnt_c++) { last_p = grid_p; grid_p = grid_p->en_next_p; } if (grid_p == NULL) { (void)printf("reached end of free list prematurely\n"); exit(1); } do { key = random_block(&ksize); } while (key == NULL); data = random_block(&dsize); call_c++; ret = table_insert(tab_p, key, ksize, data, dsize, NULL, 0); if (ret == TABLE_ERROR_NONE) { if (verbose_b) { (void)printf("stored in pos %d: %d, %d bytes of key, data\n", grid_p - grid, ksize, dsize); fflush(stdout); } grid_p->en_free_b = 0; grid_p->en_key = key; grid_p->en_key_size = ksize; grid_p->en_data = data; grid_p->en_data_size = dsize; /* shift free list */ if (last_p == NULL) { free_p = grid_p->en_next_p; } else { last_p->en_next_p = grid_p->en_next_p; } grid_p->en_next_p = NULL; free_c--; iter_c++; } else { for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) { if (grid_p->en_free_b) { continue; } if (grid_p->en_key_size == ksize && memcmp(grid_p->en_key, key, ksize) == 0) { break; } } /* if we did not store it then error */ if (grid_p >= grid + MAX_ENTRIES) { (void)fprintf(stderr, "ERROR storing #%d: %s\n", which, table_strerror(ret)); } if (key != NULL) { free(key); } if (data != NULL) { free(data); } } } break; case MODE_OVERWRITE: if (mmaping_b) { continue; } if (free_c < MAX_ENTRIES) { which = RANDOM_VALUE(MAX_ENTRIES); if (grid[which].en_free_b) { continue; } data = random_block(&dsize); call_c++; ret = table_insert(tab_p, grid[which].en_key, grid[which].en_key_size, data, dsize, NULL, 1); if (ret == TABLE_ERROR_NONE) { if (verbose_b) { (void)printf("overwrite pos %d with data of %d bytes\n", which, dsize); fflush(stdout); } grid[which].en_free_b = 0; if (grid[which].en_data != NULL) { free(grid[which].en_data); } grid[which].en_data = data; grid[which].en_data_size = dsize; grid[which].en_next_p = NULL; free_c--; iter_c++; } else { (void)fprintf(stderr, "ERROR overwriting #%d: %s\n", which, table_strerror(ret)); free(data); } } break; case MODE_RETRIEVE: if (free_c < MAX_ENTRIES) { which = RANDOM_VALUE(MAX_ENTRIES); if (grid[which].en_free_b) { continue; } call_c++; ret = table_retrieve(tab_p, grid[which].en_key, grid[which].en_key_size, (void **)&data, &dsize); if (ret == TABLE_ERROR_NONE) { if (grid[which].en_data_size == dsize && memcmp(grid[which].en_data, data, dsize) == 0) { if (verbose_b) { (void)printf("retrieved key #%d, got data of %d bytes\n", which, dsize); fflush(stdout); } } else { (void)fprintf(stderr, "ERROR: retrieve key #%d: data (%d bytes) didn't " "match table (%d bytes)\n", which, grid[which].en_data_size, dsize); } iter_c++; } else { (void)fprintf(stderr, "error retrieving key #%d: %s\n", which, table_strerror(ret)); } } break; case MODE_DELETE: if (mmaping_b) { continue; } if (free_c >= MAX_ENTRIES) { continue; } which = RANDOM_VALUE(MAX_ENTRIES); if (grid[which].en_free_b) { continue; } call_c++; ret = table_delete(tab_p, grid[which].en_key, grid[which].en_key_size, (void **)&data, &dsize); if (ret == TABLE_ERROR_NONE) { if (grid[which].en_data_size == dsize && memcmp(grid[which].en_data, data, dsize) == 0) { if (verbose_b) { (void)printf("deleted key #%d, got data of %d bytes\n", which, dsize); fflush(stdout); } } else { (void)fprintf(stderr, "ERROR deleting key #%d: data didn't match table\n", which); } grid[which].en_free_b = 1; if (grid[which].en_key != NULL) { free(grid[which].en_key); } if (grid[which].en_data != NULL) { free(grid[which].en_data); } grid[which].en_next_p = free_p; free_p = grid + which; free_c++; if (free_c == MAX_ENTRIES) { linear_b = 0; linear_eof_b = 0; } iter_c++; if (data != NULL) { free(data); } } else { (void)fprintf(stderr, "ERROR deleting key %d: %s\n", which, table_strerror(ret)); } break; case MODE_DELETE_FIRST: /* * We have a problem here. This is the only action routine * which modifies the table and is not key based. We don't have * a way of looking up the key in our local data structure. */ break; case MODE_FIRST: call_c++; ret = table_first(tab_p, (void **)&key, &ksize, (void **)&data, &dsize); if (ret == TABLE_ERROR_NONE) { linear_b = 1; linear_eof_b = 0; if (verbose_b) { (void)printf("first entry has key, data of %d, %d bytes\n", ksize, dsize); fflush(stdout); } iter_c++; } else if (free_c == MAX_ENTRIES) { if (verbose_b) { (void)printf("no first in table\n"); fflush(stdout); } } else { (void)fprintf(stderr, "ERROR: first in table: %s\n", table_strerror(ret)); } break; case MODE_NEXT: call_c++; ret = table_next(tab_p, (void **)&key, &ksize, (void **)&data, &dsize); if (ret == TABLE_ERROR_NONE) { if (verbose_b) { (void)printf("next entry has key, data of %d, %d\n", ksize, dsize); fflush(stdout); } iter_c++; } else if (ret == TABLE_ERROR_LINEAR && (! linear_b)) { if (verbose_b) { (void)printf("no first command run yet\n"); fflush(stdout); } } else if (ret == TABLE_ERROR_NOT_FOUND) { if (verbose_b) { (void)printf("reached EOF with next in table: %s\n", table_strerror(ret)); fflush(stdout); } linear_b = 0; linear_eof_b = 1; } else { (void)fprintf(stderr, "ERROR: table_next reports: %s\n", table_strerror(ret)); linear_b = 0; linear_eof_b = 0; } break; case MODE_THIS: call_c++; ret = table_this(tab_p, (void **)&key, &ksize, (void **)&data, &dsize); if (ret == TABLE_ERROR_NONE) { if (verbose_b) { (void)printf("this entry has key,data of %d, %d bytes\n", ksize, dsize); fflush(stdout); } iter_c++; } else if (ret == TABLE_ERROR_LINEAR && (! linear_b)) { if (verbose_b) { (void)printf("no first command run yet\n"); fflush(stdout); } } else if (ret == TABLE_ERROR_NOT_FOUND || linear_eof_b) { if (verbose_b) { (void)printf("table linear already reached EOF\n"); fflush(stdout); } } else { (void)fprintf(stderr, "ERROR: this table: %s\n", table_strerror(ret)); linear_b = 0; linear_eof_b = 0; } break; case MODE_INFO: { int buckets, entries; call_c++; ret = table_info(tab_p, &buckets, &entries); if (ret == TABLE_ERROR_NONE) { if (verbose_b) { (void)printf("table has %d buckets, %d entries\n", buckets, entries); fflush(stdout); } iter_c++; } else { (void)fprintf(stderr, "ERROR: table info: %s\n", table_strerror(ret)); } } break; case MODE_ADJUST: { int buckets, entries; if (mmaping_b || auto_adjust_b || large_b) { continue; } call_c++; ret = table_info(tab_p, &buckets, &entries); if (ret == TABLE_ERROR_NONE) { if (entries == 0) { if (verbose_b) { (void)printf("cannot adjusted table, %d entries\n", entries); fflush(stdout); } } else if (buckets == entries) { if (verbose_b) { (void)printf("no need to adjust table, %d buckets and entries\n", buckets); fflush(stdout); } } else { ret = table_adjust(tab_p, entries); if (ret == TABLE_ERROR_NONE) { (void)printf("adjusted table from %d to %d buckets\n", buckets, entries); iter_c++; } else { (void)printf("ERROR: table adjust to %d buckets: %s\n", entries, table_strerror(ret)); } } } else { (void)fprintf(stderr, "ERROR: table info: %s\n", table_strerror(ret)); } } break; default: (void)printf("unknown mode %d\n", which); break; } } /* run through the grid and free the entries */ for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) { if (! grid_p->en_free_b) { if (grid_p->en_key != NULL) { free(grid_p->en_key); } if (grid_p->en_data != NULL) { free(grid_p->en_data); } } } /* free used pointers */ free(grid); }