/* ----- [ check ] ---------------------------------------------------------- */ void check(struct Var_conf *config) { int sum = 0; int timer = 1; // Fix bug of timer // Disable timer config->state = J_PAUSE; if(!config->dropping && !SDL_RemoveTimer(config->timer)) { fprintf(stderr, "[ERROR] Cannot remove timer in %s at line %d.\n", __FILE__,__LINE__); timer = 0; } sum = remove_lines(config, config->model, 1); disp_gamepanel(config); // Update score, lines and level config->lines += sum; config->level = config->lines / LN_BY_LVL; config->score += SC_COEF * sum * sum * (config->level + 1); config->interv = INTERV_NEW; disp_scores(config); // Enable timer config->state = J_PLAY; if(timer) { if( !config->dropping && (config->timer = SDL_AddTimer(config->interv,step,config)) == NULL) fprintf(stderr, "[ERROR] Cannot initialize timer in %s at line %d.\n", __FILE__,__LINE__); } }
/* ----- [ ai_best_pos ] ---------------------------------------------------- */ void ai_best_pos(struct Var_conf *config,struct model *model_tmp, SDL_Rect *position, int *turning) { enum Piece_bloc piece_id = config->pc_cur_id; enum Piece_bloc piece_next_id = config->pc_next_id; SDL_Rect pos; SDL_Rect next_pos; int score_tmp; int i = 0, j = 0, k = 0, l = 0; int sum = 0; int max_score = 0; // Init table of scores int scores[4*4][((int)PNL_LB+4)*((int)PNL_LB+4)]; for(i = 0;i < 4*4;i++) for(j = 0;j < (PNL_LB+4)*(PNL_LB+4);j++) scores[i][j] = 0; // Init position pos.x = 0; pos.y = config->piece_pos.y; // Checking each configuration // checking each position // checking holes // checking lines // checking each rotation // checking holes // checking lines for(i = 0;i < 4;i++) { for(j = 0;j < PNL_LB+4;j++) { // Update position pos.x = j-4; pos.y = config->piece_pos.y; score_tmp = ai_score_pos(config,model_tmp,piece_id,pos); for(k = 0;k < 4;k++) for(l = 0;l < PNL_LB+4;l++) scores[i*4+k][(int)(j*(PNL_LB+4)+l)] = score_tmp; // Get dropped position if(check_pc(config,model_tmp,config->pieces[piece_id],pos)) { while(check_pc(config,model_tmp,config->pieces[piece_id],pos) && pos.y != PNL_HB) pos.y++; pos.y--; // Take off piece in temporary model takeoff(config,model_tmp,&piece_id,pos,0); // Remove lines remove_lines(config,model_tmp,0); // Get next piece piece_next_id = config->pc_next_id; // For all turns for(k = 0;k < 4;k++) { // For all positions for(l = 0;l < (PNL_LB+4);l++) { // Get new position next_pos.x = l-4; next_pos.y = 1; // Calculate score for new piece position score_tmp = ai_score_pos(config,model_tmp, piece_next_id,next_pos); scores[i*4+k][(int)(j*(PNL_LB+4)+l)] += score_tmp; } // Turn new piece next_pos.x = config->piece_pos.x; turn(config,model_tmp,&piece_next_id,&next_pos,HORLO,0,0); } // Copy model for(k = 0;k < PNL_HB;k++) for(l = 0;l < PNL_LB;l++) model_set(model_tmp,k,l,model_get(config->model,k,l)); } } // Turn turn(config,model_tmp,&piece_id,&(config->piece_pos), HORLO,0,0); } // Get best position pos.x = 0; *turning = 0; max_score = scores[0][0]; for(i = 0;i < 4;i++) { for(j = 0;j < PNL_LB+4;j++) { for(k = 0;k < 4;k++) for(l = 0;l < (PNL_LB+4);l++) //printf("%d ",scores[i][j]); if(scores[i*4+k][(int)(j*(PNL_LB+4)+l)] > max_score) { max_score = scores[i*4+k][(int)(j*(PNL_LB+4)+l)]; pos.x = j-4; *turning = i; } } //printf("\n"); } // Print scores table /*for(i = 0;i < 4;i++) { for(j = 0;j < PNL_LB+4;j++) { for(k = 0;k < 4;k++) { for(l = 0;l < PNL_LB+4;l++) { if( 3 < j & j < 10) printf("%d ",scores[i*4+k][(int)(j*(PNL_LB+4)+l)]); } if( 3 < j & j < 10) printf("\n"); } if( 3 < j & j < 10) printf("\n\n"); } if( 3 < j & j < 10) printf("\n\n"); }*/ position->x = pos.x; //printf("i = %d j = %d \n",pos->x,*turning); }
int dpf(int argc, char *argv[], int retc, char *retv[]) /*************/ { int groups; int noisemult_p = 0; int numlines,maxlines; int i; int donll = TRUE; int noneg = FALSE; int top = FALSE; char command[128]; char name[64]; (void) retc; (void) retv; Wturnoff_buttons(); leader_len = 20.0; axish = FALSE; axisp = FALSE; axisp_freq = 1.0; if (argc > 1) { for (i=1; i<argc; i++) { if (strcmp(argv[i],"off") == 0) { set_dpf_flag(0,""); redo_dpf_dpir(); RETURN; } else if (strcmp(argv[i],"turnoff") == 0) { set_dpf_flag(0,""); RETURN; } else if (strcmp(argv[i],"noll") == 0) donll = FALSE; else if (strcmp(argv[i],"pos") == 0) noneg = TRUE; else if (strcmp(argv[i],"top") == 0) top = TRUE; else if (strcmp(argv[i],"axish") == 0) axish = TRUE; else if (strcmp(argv[i],"axisp") == 0) { axisp = TRUE; P_getreal(PROCESSED,"sfrq",&axisp_freq,1); } else if (strcmp(argv[i],"leader") == 0) { if (((i+1) < argc) && isReal(argv[i+1]) ) { i++; leader_len = stringReal(argv[i]); } } else if (isReal(argv[i])) noisemult_p = i; } } if (donll) /* if not donll, dpf uses last previous line listing */ { if (noisemult_p != 0) { if (noneg) strcpy(command,"nll('dpf','pos',"); else strcpy(command,"nll('dpf',"); strcat(command,argv[noisemult_p]); strcat(command,")\n"); } else { if (noneg) strcpy(command,"nll('dpf','pos')\n"); else strcpy(command,"nll('dpf')\n"); } execString(command); } if(P_getreal(CURRENT, "dpf_sc2",&dpf_sc2,1)) dpf_sc2=sc2; if(P_getreal(CURRENT, "dpf_wc2",&dpf_wc2,1)) dpf_wc2=wc2; if(dpf_wc2>wc2) dpf_wc2=wc2; if(dpf_sc2<sc2) dpf_sc2=sc2; /* if (init2d(1,1)) return(ERROR); */ scale = vs; if (normflag) scale *= normalize; /* Wscrprintf("normflag=%d normalize=%g\n",normflag,normalize); */ plot = (argv[0][0] == 'p'); /*select_init(get_rev, dis_setup, fdimname, doheaders, docheck2d, dospecpars, doblockpars, dophasefile)*/ /*if (init2d(0,plot + 1)) return(ERROR); */ if (select_init( 0, plot+1, NO_FREQ_DIM, NO_HEADERS, DO_CHECK2D, DO_SPECPARS, NO_BLOCKPARS, NO_PHASEFILE )) return(ERROR); if ((numlines = getlines(noneg)) == 0) RETURN; if (!plot) { setwindows(); dispcalib = (float) (mnumypnts-ymin) / (float) wc2max; getOptName(PEAK_MARK,name); set_line_thickness(name); getOptName(PEAK_NUM,name); set_graphics_font(name); } else { double size = G_getCharSize("PeakNum"); charsize(size); //charsize((double)0.7); dispcalib = ppmm / ymultiplier; } CharSize = ycharpixels + ycharpixels / 5; maxlines = dnpnt / CharSize; if (maxlines < numlines) remove_lines(maxlines,&numlines); if (numlines > 0) { //color(PARAM_COLOR); color(PEAK_MARK_COLOR); groups = groupcheck(numlines); if (debug1) { Wscrprintf("index high low newhigh newlow highlimit lowlimit\n"); for (i = 1; i <= groups; i++) Wscrprintf("%d %d %d %d %d %d %d\n",i,ga[i].high,ga[i].low, ga[i].newhigh,ga[i].newlow,ga[i].highlimit,ga[i].lowlimit); } label_proc = (top) ? (PFV) label_top : (PFV) label_bot; for (i = 1; i <= groups; i++) label_group(i); if (!plot) { // construct the command, and set the flag and command char cmd[64]; if(argc > 1) { if (isReal(argv[1]) ) sprintf(cmd,"%s(%s",argv[0],argv[1]); else sprintf(cmd,"%s('%s'",argv[0],argv[1]); for(i=2;i<argc;i++) { if (isReal(argv[i]) ) { strcat(cmd,","); strcat(cmd,argv[i]); } else { strcat(cmd,",'"); strcat(cmd,argv[i]); strcat(cmd,"'"); } } strcat(cmd,")\n"); } else sprintf(cmd,"%s\n",argv[0]); Wsetgraphicsdisplay("ds"); set_dpf_flag(1, cmd); } } releaseAllWithId("dpf"); endgraphics(); disp_status(" "); set_graphics_font("Default"); RETURN; }