void board_move_tile(BoardPtr b, int index_origin, int index_destinazione) { if (board_get(b, index_origin) != NULL && board_get(b, index_destinazione) == NULL) { board_set(b, board_get(b, index_origin), index_destinazione); board_set(b, NULL, index_origin); board_update_freepos(b); } }
int live_or_die(board_t *self, int row, int col) { int curr = board_get(self, row, col); int top_row = CIRC(row - 1, self->num_rows); int bottom_row = (row + 1)%self->num_rows; int left_col = CIRC(col - 1, self->num_cols); int right_col = (col + 1)%self->num_cols; int neighbors = board_get(self, top_row, left_col) + board_get(self, top_row, col) + board_get(self, top_row, right_col) + board_get(self, row, left_col) + board_get(self, row, right_col) + board_get(self, bottom_row, left_col) + board_get(self, bottom_row, col) + board_get(self, bottom_row, right_col); if (curr == 1 && neighbors < 2) return 0; // dies of loneliness else if (curr == 1 && neighbors > 3) return 0; // dies of overcrowding else if (curr == 1 && (neighbors == 2 || neighbors == 3)) return 1; // stays alive else if (curr == 0 && neighbors == 3) return 1; // born return 0; // stays dead }
//Saves the current game static void save_game(gui_control* window, int slot) { FILE* f; char fileName[100]; int player_map[] = { 0, 1, -1 }; int i, j; game* g = game_get_current(); board* game_board = game_get_current_board(); int player = game_get_current_player(); sprintf(fileName, "saveload%d.data", slot); f = fopen(fileName, "w"); fprintf(f, "%s\n", g->name); fprintf(f, "%d\n", player); for (i = 0; i < game_board->rows; i++) { fprintf(f, "\n"); for (j = 0; j < game_board->cols; j++) { fprintf(f, "%d ", player_map[board_get(game_board, i, j)]); } } fclose(f); }
VALUE board_get_coord( VALUE self, VALUE c ) { if( c == Qnil ) { return Qnil; } return board_get( self, rb_funcall( c, id_x, 0 ), rb_funcall( c, id_y, 0 ) ); }
int moveUp(struct game * input ) { //forward columns int size = input->size; int col = 0, cell = 0, localScore = 0 , offset; bool hasMoved = false; for(col = 0; col < size ; col++){ offset = 0; //pack the cols to the top for(int i = 0 ; i < size; i++){ cell = board_get(input,col,i); if(cell != 0){ board_set(input,col,offset,cell); if(offset != i){ board_set(input,col,i,0); hasMoved = true; } offset++; } } //compact the row if possible for(int i = 0 ; i < size; i++){ cell = board_get(input,col,i); if (cell == board_get(input,col,i+1)) { if (cell != 0) { hasMoved = true; } board_set(input,col,i,cell*2); localScore += cell*2; //shift for(int j = i+1 ; j < size; j++) { board_set(input,col,j,board_get(input,col,j+1)); } } } } if (hasMoved == false) { localScore = -1; } return localScore; return -1; }
int moveLeft(struct game * input ) { //forward rows int size = input->size; int row = 0, cell = 0, localScore = 0 , offset; bool hasMoved = false; for(row = 0; row < size ; row++){ offset = 0; //pack the row to the left for(int i = 0 ; i < size; i++){ cell = board_get(input,i,row); if(cell != 0){ board_set(input,offset,row,cell); if(offset != i){ board_set(input,i,row,0); hasMoved = true; } offset++; } } //compact row if possible for(int i = 0 ; i < size; i++){ cell = board_get(input,i,row); if (cell == board_get(input,i+1,row)) { if(cell != 0) { hasMoved = true; } board_set(input,i,row,cell*2); localScore += cell*2; //shift for(int j = i+1 ; j < size; j++) { board_set(input,j,row,board_get(input,j+1,row)); } } } } if (hasMoved == false) { localScore = -1; } return localScore; }
int moveDown(struct game * input ) { //backwards columns int size = input->size; int col = 0, cell = 0, localScore = 0 , offset; bool hasMoved = false; for(col = 0; col < size ; col++){ offset = size-1; //pack the cols to the bottom for(int i = size-1 ; i >= 0 ; i--){ cell = board_get(input,col,i); if(cell != 0){ board_set(input,col,offset,cell); if(offset != i){ board_set(input,col,i,0); hasMoved = true; } offset--; } } //compact the row if possible for(int i = size-1 ; i >= 0; i--){ cell = board_get(input,col,i); if (cell == board_get(input,col,i-1)) { if(cell != 0) { hasMoved = true; } board_set(input,col,i,cell*2); localScore += cell*2; //shift for(int j = i-1 ; j >= 0; j--) { board_set(input,col,j,board_get(input,col,j-1)); } } } } if (hasMoved == false) { localScore = -1; } return localScore; }
int moveRight(struct game * input ) { //backwards rows int size = input->size; int row = 0, cell = 0, localScore = 0 , offset; bool hasMoved = false; for(row = 0; row < size ; row++){ offset = size-1; //pack the row to the right for(int i = size-1 ; i >= 0 ; i--){ cell = board_get(input,i,row); if(cell != 0){ board_set(input,offset,row,cell); if(offset != i){ board_set(input,i,row,0); hasMoved = true; } offset--; } } //compact the row if possible for(int i = size-1 ; i >= 0; i--){ cell = board_get(input,i,row); if (cell == board_get(input,i-1,row)) { if (cell != 0) { hasMoved = true; } board_set(input,i,row,cell*2); localScore += cell*2; //shift for(int j = i-1 ; j >= 0; j--) { board_set(input,j,row,board_get(input,j-1,row)); } } } } if (hasMoved == false) { localScore = -1; } return localScore; }
void insertNewNumber(struct game * input) { int row = 0, column = 0; int index, availableSquares = 0; int size = input->size; int value = 0; // count vacant squares for (row = 0; row < size; row = row + 1) { for (column = 0; column < size; column = column + 1) { value = board_get(input,row,column); if (value == 0) { availableSquares = availableSquares + 1; } } } if (availableSquares == 0) { //printf("Internal error no available square\n"); return; } // randomly pick a vacant square index = rand() % availableSquares; for (row = 0; row < size; row = row + 1) { for (column = 0; column < size; column = column + 1) { if (board_get(input,row,column) == 0) { if (index == 0) { if (rand() % 10 == 0) { board_set(input,row,column,4); } else { board_set(input,row,column,2); } return; } index = index - 1; } } } }
void print_board(board_t *self, int iteration) { printf("\n"); printf("Time Step: %d\n", iteration); printf("\n"); for (int i = 0; i < self->num_rows; ++i) { for (int j = 0; j < self->num_cols; ++j) { int alive = board_get(self, i, j); printf("%c ", (alive) ? '@' : '-'); } printf("\n"); } printf("\n"); }
VALUE board_subscript( int argc, VALUE *argv, VALUE self ) { if( argc == 2 && FIXNUM_P(argv[0]) && FIXNUM_P(argv[1]) ) { return board_get( self, argv[0], argv[1] ); } else if( argc == 1 ) { return board_get_coord( self, argv[0] ); } else { VALUE ary = rb_ary_new2( argc ); int i; for( i = 0; i < argc; i++ ) { rb_ary_push( ary, board_get_coord( self, argv[i] ) ); } return ary; } return Qnil; }
static int dm_test_board(struct unit_test_state *uts) { struct udevice *board; bool called_detect; char str[64]; int i; board_get(&board); ut_assert(board); board_get_bool(board, BOOL_CALLED_DETECT, &called_detect); ut_assert(!called_detect); board_detect(board); board_get_bool(board, BOOL_CALLED_DETECT, &called_detect); ut_assert(called_detect); board_get_str(board, STR_VACATIONSPOT, sizeof(str), str); ut_assertok(strcmp(str, "R'lyeh")); board_get_int(board, INT_TEST1, &i); ut_asserteq(0, i); board_get_int(board, INT_TEST2, &i); ut_asserteq(100, i); board_get_str(board, STR_VACATIONSPOT, sizeof(str), str); ut_assertok(strcmp(str, "Carcosa")); board_get_int(board, INT_TEST1, &i); ut_asserteq(1, i); board_get_int(board, INT_TEST2, &i); ut_asserteq(99, i); board_get_str(board, STR_VACATIONSPOT, sizeof(str), str); ut_assertok(strcmp(str, "Yuggoth")); return 0; }
int __init board_wifi_init(void) { #ifdef CONFIG_WIFI_CONTROL_FUNC int rc = 0; #endif board_wifi = 0; /* * add the case for the platform not here */ switch (machine_arch_type) { #ifdef CONFIG_ARCH_MSM7200A #ifdef CONFIG_MACH_HERO case MACH_TYPE_HERO: board_wifi = board_get("hero"); break; #endif #ifdef CONFIG_MACH_SAPPHIRE case MACH_TYPE_SAPPHIRE: board_wifi = board_get("sapphire"); break; #endif #ifdef CONFIG_MACH_HERO_ESPRESSO case MACH_TYPE_HERO_ESPRESSO: board_wifi = board_get("espresso"); break; #endif #endif #ifdef CONFIG_ARCH_MSM7201A #ifdef CONFIG_MACH_SAPPHIRE case MACH_TYPE_SAPPHIRE: board_wifi = board_get("sapphire"); break; #endif #ifdef CONFIG_MACH_TROUT case MACH_TYPE_TROUT: board_wifi = board_get("trout"); break; #endif #endif #ifdef CONFIG_ARCH_MSM7225 #ifdef CONFIG_MACH_MEMPHIS case MACH_TYPE_MEMPHIS: board_wifi = board_get("memphis"); break; #endif #ifdef CONFIG_MACH_JADE case MACH_TYPE_JADE: board_wifi = board_get("jade"); break; case MACH_TYPE_HTCJADE: board_wifi = board_get("jade"); break; #endif #ifdef CONFIG_MACH_BAHAMAS case MACH_TYPE_BAHAMAS: board_wifi = board_get("bahamas"); break; #endif #endif #ifdef CONFIG_ARCH_MSM7501A #ifdef CONFIG_MACH_HEROC case MACH_TYPE_HEROC: board_wifi = board_get("heroc"); break; #endif #ifdef CONFIG_MACH_DESIREC case MACH_TYPE_DESIREC: board_wifi = board_get("desirec"); break; #endif #ifdef CONFIG_MACH_HEROCT case MACH_TYPE_HEROCT: board_wifi = board_get("heroct"); break; #endif #endif default: printk(KERN_ERR "WiFi: unknown machine type %d\n", machine_arch_type); return -1; }; if (!board_wifi) printk(KERN_ERR "WiFi: seek vainly for machine type\n"); #ifdef CONFIG_WIFI_CONTROL_FUNC #ifdef CONFIG_WIFI_MEM_PREALLOC board_wifi->mem_prealloc = wifi_mem_prealloc; #endif wifi_ctrl_dev.dev.platform_data = board_wifi; rc = platform_device_register(&wifi_ctrl_dev); if (rc) printk(KERN_CRIT "WiFi: control device init failure(%d)\n", rc); printk(KERN_INFO "WiFi: control device registered.\n"); return rc; #endif return 0; }
void process(board *board) { FILE *out; if (debugflag) out = debugstream; else out = stdout; while (1) { char c = board_get(board, board->pointer->x, board->pointer->y); if (board->pointer->strmode && c != '"') { stack_push(board->pointer->stack, c); } else { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': stack_push(board->pointer->stack, (char) atoi(&c)); break; case '+': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, a + b); break; } case '-': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, b - a); break; } case '*': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, a * b); break; } case '/': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, b / a); break; } case '%': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, b % a); break; } case '!': stack_push(board->pointer->stack, !stack_pop(board->pointer->stack)); break; case '`': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, (b > a)); break; } case '>': board->pointer->dx = 1; board->pointer->dy = 0; break; case '<': board->pointer->dx = -1; board->pointer->dy = 0; break; case '^': board->pointer->dx = 0; board->pointer->dy = -1; break; case 'v': board->pointer->dx = 0; board->pointer->dy = 1; break; case '?': { srand((unsigned)time(NULL)); int r = rand() % 4; switch (r) { case 0: board->pointer->dx = 1; board->pointer->dy = 0; break; case 1: board->pointer->dx = -1; board->pointer->dy = 0; break; case 2: board->pointer->dx = 0; board->pointer->dy = -1; break; case 3: board->pointer->dx = 0; board->pointer->dy = 1; break; } break; } case '_': { char x = stack_pop(board->pointer->stack); if (x == 0) { board->pointer->dx = 1; board->pointer->dy = 0; } else { board->pointer->dx = -1; board->pointer->dy = 0; } break; } case '|': { char x = stack_pop(board->pointer->stack); if (x == 0) { board->pointer->dx = 0; board->pointer->dy = 1; } else { board->pointer->dx = 0; board->pointer->dy = -1; } break; } case '"': board->pointer->strmode = !board->pointer->strmode; break; case ':': stack_push(board->pointer->stack, stack_peek(board->pointer->stack)); break; case '\\': { char a = stack_pop(board->pointer->stack); char b = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, a); stack_push(board->pointer->stack, b); break; } case '$': stack_pop(board->pointer->stack); break; case '.': fprintf(out, "%i ", stack_pop(board->pointer->stack)); break; case ',': fprintf(out, "%c", stack_pop(board->pointer->stack)); break; case '#': pointer_move(board->pointer); break; case 'p': { int y = stack_pop(board->pointer->stack); int x = stack_pop(board->pointer->stack); char v = stack_pop(board->pointer->stack); board_put(board, x, y, v); break; } case 'g': { int y = stack_pop(board->pointer->stack); int x = stack_pop(board->pointer->stack); stack_push(board->pointer->stack, board_get(board, x, y)); break; } case '&': { int i; scanf("%d", &i); stack_push(board->pointer->stack, (char) i); break; } case '~': stack_push(board->pointer->stack, getchar()); break; case '@': return; } } pointer_move(board->pointer); if (board->pointer->x < 0) { board->pointer->x += BOARD_WIDTH; } if (board->pointer->x >= BOARD_WIDTH) { board->pointer->x -= BOARD_WIDTH; } if (board->pointer->y < 0) { board->pointer->y += BOARD_HEIGHT; } if (board->pointer->y >= BOARD_HEIGHT) { board->pointer->y -= BOARD_HEIGHT; } if (debugflag) { debug(board); } } }
void debug(board *board) { printf("\x1b[H\x1b[2J"); char direction; if (board->pointer->dx == 1) direction = '>'; else if (board->pointer->dx == -1) direction = '<'; else if (board->pointer->dy == 1) direction = 'v'; else if (board->pointer->dy == -1) direction = '^'; printf("Pointer: %d,%d %c %s\n", board->pointer->x, board->pointer->y, direction, (board->pointer->strmode)?"string mode":""); printf("Board:\n"); int y, x; for (y = 0; y < BOARD_HEIGHT; y++) { for (x = 0; x < BOARD_WIDTH; x++) { printf("%s%c\033[0m", (x == board->pointer->x && y == board->pointer->y)?"\033[41m":"", board_get(board, x, y)); } printf("\n"); } printf("Stack:\n"); stack_item *item = board->pointer->stack->top; while (item) { printf("(%d '%c') ", item->value, item->value); item = item->next; } printf("\n"); printf("Output:\n"); fseek(debugstream, 0, SEEK_SET); char c; while ((c = fgetc(debugstream)) != EOF) { printf("%c", c); } getchar(); }
int main(int argc, char *argv[]) { if(argc<2) { printf("Usage: %s [puzzle file] \n\n", argv[0]); return 0; } if(argc==3 && strcmp(argv[2], "debug")==0) debug=1; /* seed random number generator */ random_seed(); /* read num tiles */ int ntiles = input_get_ntiles(argv[1]); if(ntiles==0) { printf("input file error: %s\n", argv[1]); return 0; } /* create data structures */ e2_t e2; e2.board = malloc(ntiles*sizeof(int)); e2.tiles = malloc((ntiles+1)*sizeof(tile_t)); // ignore zero position e2.ntiles = ntiles; e2.side = sqrt(ntiles); printf("ntiles: %d\n", e2.ntiles); printf("side: %d\n", e2.side); /* set data */ input_read_tiles(&e2, argv[1]); board_set_tiles(&e2); /* print data structures */ //output_tiles(&e2); output_board_file(&e2, "/dev/stdout"); printf("------------------------------------------------------------\n"); printf("\n\n"); int merror, tmp_merror; int src_x, src_y, dst_x, dst_y; int max_lock = ntiles*ntiles; int lock=0; int nlocks=0; int it=0; int lmi_n=1; int lmi_me=ntiles*4; /* solved? */ while((merror = match_get_num_total_errors(&e2))!=0) { src_x = random_get(e2.side); src_y = random_get(e2.side); do { random_get_optimal_xy(&e2, src_x, src_y, &dst_x, &dst_y); } //do { random_get_xy_by_groups(&e2, src_x, src_y, &dst_x, &dst_y); } while(src_x==dst_x && src_y==dst_y); board_swap_tiles(&e2, src_x, src_y, dst_x, dst_y); rotation_auto(&e2, src_x, src_y); rotation_auto(&e2, dst_x, dst_y); tmp_merror = match_get_num_total_errors(&e2); if(tmp_merror < merror) { nlocks++; lock=0; merror = tmp_merror; printf("merror: %d, lmi_me: %d, lmi_n: %d, it: %d, nlocks: %d", merror, lmi_me, lmi_n, it, nlocks); if(debug) printf("\n"); else printf("\r"); if(debug) printf("swap src:%d(%d,%d) - dst:%d(%d,%d)\n", board_get(&e2, src_x, src_y), src_x, src_y, board_get(&e2, dst_x, dst_y), dst_x, dst_y); if(debug) { output_board_file(&e2, "/dev/stdout"); printf("------------------------------------------------------\n"); } if(it%100==0) { output_board_file(&e2, "/dev/stdout"); printf("------------------------------------------------------\n"); } } else { // undo board_swap_tiles(&e2, dst_x, dst_y, src_x, src_y); rotation_auto(&e2, src_x, src_y); rotation_auto(&e2, dst_x, dst_y); lock++; } // local minimal if(lock > max_lock) { if(merror < lmi_me) { if(lmi_n>1) lmi_n --; } else { if(lmi_n<1) lmi_n ++; } lmi_me = merror; int i; for(i=0; i<lmi_n; i++) { src_x = random_get(e2.side); src_y = random_get(e2.side); do { random_get_optimal_xy(&e2, src_x, src_y, &dst_x, &dst_y); } //do { random_get_xy_by_groups(&e2, src_x, src_y, &dst_x, &dst_y); } while(src_x==dst_x && src_y==dst_y); board_swap_tiles(&e2, src_x, src_y, dst_x, dst_y); rotation_auto(&e2, src_x, src_y); rotation_auto(&e2, dst_x, dst_y); } } it++; } printf("\n\n"); printf("------------------------------------------------------------\n"); printf("Solved: \n"); printf("------------------------------------------------------------\n"); output_board_file(&e2, "/dev/stdout"); /* free data sctructures */ free(e2.board); free(e2.tiles); return 0; }