void game::gamestart(){ for(int i=0;i<17;i++){ b[i].position=i; b[i].number=0; setboard(b[i]); }//初始化 score=0; str=QString::number(score,10); ui->score->setText(str); setrandom(); setrandom(); ui->win->clear(); ui->lose->clear(); }
void game::keyPressEvent(QKeyEvent *event) { if(trigger){ if(event->key() == Qt::Key_Left){ for(int j=0;j<4;j++){ for(int i=2+j*4;i<=4+j*4;i++){ for(int k=i;k>1+(4*j);k--){ //跟前一個數字相同 if(b[k].number!=0&&b[k].number==b[k-1].number&&b[k-1].isAdded==false&&b[k].isAdded==false){ b[k-1].number=b[k-1].number+b[k-1].number; b[k].number=0; b[k-1].isAdded=true; //數字往前一格 b[k].isAdded=false; //原空格變為零 setboard(b[k-1]); setboard(b[k]); b[k-1].isMoved=true; b[k].isMoved=false; score=score+b[k-1].number; } //前一個空格為0 else if(b[k].number!=0&&b[k-1].number==0){ b[k-1].number=b[k].number; b[k].number=0; b[k-1].isMoved=true; b[k].isMoved=false; b[k-1].isAdded=b[k].isAdded; b[k].isAdded=false; setboard(b[k-1]); setboard(b[k]); } } } } } else if(event->key() == Qt::Key_Right){ for(int j=0;j<4;j++){ for(int i=3+j*4;i>=1+j*4;i--){ for(int k=i;k<(1+j)*4;k++){ if(b[k].number!=0&&b[k].number==b[k+1].number&&b[k+1].isAdded==false&&b[k].isAdded==false){ b[k+1].number=b[k+1].number+b[k+1].number; b[k].number=0; b[k+1].isAdded=true; b[k].isAdded=false; setboard(b[k+1]); setboard(b[k]); b[k+1].isMoved=true; b[k].isMoved=false; score=score+b[k+1].number; } else if(b[k].number!=0&&b[k+1].number==0){ b[k+1].number=b[k].number; b[k].number=0; b[k+1].isMoved=true; b[k].isMoved=false; b[k+1].isAdded=b[k].isAdded; b[k].isAdded=false; setboard(b[k+1]); setboard(b[k]); } } } } } else if(event->key() == Qt::Key_Up){ for(int j=0;j<4;j++){ for(int i=5+j;i<=13+j;i+=4){ for(int k=i;k>1+j;k-=4){ if(b[k].number!=0&&b[k].number==b[k-4].number&&b[k-4].isAdded==false&&b[k].isAdded==false){ b[k-4].number=b[k-4].number+b[k-4].number; b[k].number=0; b[k-4].isAdded=true; b[k].isAdded=false; setboard(b[k-4]); setboard(b[k]); b[k-4].isMoved=true; b[k].isMoved=false; score=score+b[k-4].number; } else if(b[k].number!=0&&b[k-4].number==0){ b[k-4].number=b[k].number; b[k].number=0; b[k-4].isMoved=true; b[k].isMoved=false; b[k-4].isAdded=b[k].isAdded; b[k].isAdded=false; setboard(b[k-4]); setboard(b[k]); } } } } } else if(event->key() == Qt::Key_Down){ for(int j=0;j<4;j++){ for(int i=9+j;i>=1+j;i-=4){ for(int k=i;k<13+j;k+=4){ if(b[k].number!=0&&b[k].number==b[k+4].number&&b[k+4].isAdded==false&&b[k].isAdded==false){ b[k+4].number=b[k+4].number+b[k+4].number; b[k].number=0; b[k+4].isAdded=true; b[k].isAdded=false; setboard(b[k+4]); setboard(b[k]); b[k+4].isMoved=true; b[k].isMoved=false; score=score+b[k+4].number; } else if(b[k].number!=0&&b[k+4].number==0){ b[k+4].number=b[k].number; b[k].number=0; b[k+4].isMoved=true; b[k].isMoved=false; b[k+4].isAdded=b[k].isAdded; b[k].isAdded=false; setboard(b[k+4]); setboard(b[k]); } } } } } if(AnyMoved()){ setrandom(); initialized(); score+=2; if(IsWined()){ trigger=false; ui->win->setPixmap(QPixmap(":/pic/win.png")); } if(Islosed()){ trigger=false; ui->win->setPixmap(QPixmap(":/pic/lose.png")); } str=QString::number(score,10); ui->score->setText(str); } } }
int main(int argc, char *argv[]) { int fd; #ifdef CHDIR char *dir; #endif hname = argv[0]; hackpid = getpid(); #ifdef CHDIR /* otherwise no chdir() */ /* * See if we must change directory to the playground. * (Perhaps hack runs suid and playground is inaccessible * for the player.) * The environment variable HACKDIR is overridden by a * -d command line option (must be the first option given) */ dir = getenv("HACKDIR"); if (argc > 1 && !strncmp(argv[1], "-d", 2)) { argc--; argv++; dir = argv[0] + 2; if (*dir == '=' || *dir == ':') dir++; if (!*dir && argc > 1) { argc--; argv++; dir = argv[0]; } if (!*dir) error("Flag -d must be followed by a directory name."); } #endif /* * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS * 2. Use $USER or $LOGNAME (if 1. fails) * 3. Use getlogin() (if 2. fails) * The resulting name is overridden by command line options. * If everything fails, or if the resulting name is some generic * account like "games", "play", "player", "hack" then eventually * we'll ask him. * Note that we trust him here; it is possible to play under * somebody else's name. */ { char *s; initoptions(); if (!*plname && (s = getenv("USER"))) strncpy(plname, s, sizeof(plname) - 1); if (!*plname && (s = getenv("LOGNAME"))) strncpy(plname, s, sizeof(plname) - 1); if (!*plname && (s = getlogin())) strncpy(plname, s, sizeof(plname) - 1); } /* * Now we know the directory containing 'record' and * may do a prscore(). */ if (argc > 1 && !strncmp(argv[1], "-s", 2)) { #ifdef CHDIR chdirx(dir, 0); #endif prscore(argc, argv); exit(0); } /* * It seems he really wants to play. * Remember tty modes, to be restored on exit. */ gettty(); setbuf(stdout, obuf); umask(007); setrandom(); startup(); cls(); u.uhp = 1; /* prevent RIP on early quits */ u.ux = FAR; /* prevent nscr() */ signal(SIGHUP, hangup); /* * Find the creation date of this game, * so as to avoid restoring outdated savefiles. */ gethdate(hname); /* * We cannot do chdir earlier, otherwise gethdate will fail. */ #ifdef CHDIR chdirx(dir, 1); #endif /* * Process options. */ while (argc > 1 && argv[1][0] == '-') { argv++; argc--; switch (argv[0][1]) { #ifdef WIZARD case 'D': wizard = TRUE; break; #endif #ifdef NEWS case 'n': flags.nonews = TRUE; break; #endif case 'u': if (argv[0][2]) strncpy(plname, argv[0] + 2, sizeof(plname) - 1); else if (argc > 1) { argc--; argv++; strncpy(plname, argv[0], sizeof(plname) - 1); } else printf("Player name expected after -u\n"); break; default: /* allow -T for Tourist, etc. */ strncpy(pl_character, argv[0] + 1, sizeof(pl_character) - 1); } } if (argc > 1) locknum = atoi(argv[1]); #ifdef MAX_NR_OF_PLAYERS if (!locknum || locknum > MAX_NR_OF_PLAYERS) locknum = MAX_NR_OF_PLAYERS; #endif #ifdef DEF_PAGER if (!(catmore = getenv("HACKPAGER")) && !(catmore = getenv("PAGER"))) catmore = DEF_PAGER; #endif #ifdef MAIL getmailstatus(); #endif #ifdef WIZARD if (wizard) strcpy(plname, "wizard"); else #endif if (!*plname || !strncmp(plname, "player", 4) || !strncmp(plname, "games", 4)) askname(); plnamesuffix(); /* strip suffix from name; calls askname() */ /* again if suffix was whole name */ /* accepts any suffix */ #ifdef WIZARD if (!wizard) { #endif /* * check for multiple games under the same name * (if !locknum) or check max nr of players (otherwise) */ signal(SIGQUIT, SIG_IGN); signal(SIGINT, SIG_IGN); if (!locknum) strcpy(lock, plname); getlock(); /* sets lock if locknum != 0 */ #ifdef WIZARD } else { char *sfoo; strcpy(lock, plname); if ((sfoo = getenv("MAGIC"))) while (*sfoo) { switch (*sfoo++) { case 'n': srandom(*sfoo++); break; } } if ((sfoo = getenv("GENOCIDED")) != NULL) { if (*sfoo == '!') { struct permonst *pm = mons; char *gp = genocided; while (pm < mons + CMNUM + 2) { if (!strchr(sfoo, pm->mlet)) *gp++ = pm->mlet; pm++; } *gp = 0; } else strncpy(genocided, sfoo, sizeof(genocided) - 1); strcpy(fut_geno, genocided); } } #endif setftty(); sprintf(SAVEF, "save/%d%s", getuid(), plname); regularize(SAVEF + 5); /* avoid . or / in name */ if ((fd = open(SAVEF, O_RDONLY)) >= 0 && (uptodate(fd) || unlink(SAVEF) == 666)) { signal(SIGINT, done1); pline("Restoring old save file..."); fflush(stdout); if (!dorecover(fd)) goto not_recovered; pline("Hello %s, welcome to %s!", plname, gamename); flags.move = 0; } else { not_recovered: fobj = fcobj = invent = 0; fmon = fallen_down = 0; ftrap = 0; fgold = 0; flags.ident = 1; init_objects(); u_init(); signal(SIGINT, done1); mklev(); u.ux = xupstair; u.uy = yupstair; inshop(); setsee(); flags.botlx = 1; makedog(); { struct monst *mtmp; if ((mtmp = m_at(u.ux, u.uy)) != NULL) mnexto(mtmp); /* riv05!a3 */ } seemons(); #ifdef NEWS if (flags.nonews || !readnews()) /* after reading news we did docrt() already */ #endif docrt(); /* give welcome message before pickup messages */ pline("Hello %s, welcome to %s!", plname, gamename); pickup(1); read_engr_at(u.ux, u.uy); flags.move = 1; } flags.moonphase = phase_of_the_moon(); if (flags.moonphase == FULL_MOON) { pline("You are lucky! Full moon tonight."); u.uluck++; } else if (flags.moonphase == NEW_MOON) pline("Be careful! New moon tonight."); initrack(); for (;;) { if (flags.move) { /* actual time passed */ settrack(); if (moves % 2 == 0 || (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) { movemon(); if (!rn2(70)) makemon(NULL, 0, 0); } if (Glib) glibr(); p_timeout(); ++moves; if (flags.time) flags.botl = 1; if (u.uhp < 1) { pline("You die..."); done("died"); } if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) { wailmsg = moves; if (u.uhp == 1) pline("You hear the wailing of the Banshee..."); else pline("You hear the howling of the CwnAnnwn..."); } if (u.uhp < u.uhpmax) { if (u.ulevel > 9) { if (Regeneration || !(moves % 3)) { flags.botl = 1; u.uhp += rnd((int)u.ulevel - 9); if (u.uhp > u.uhpmax) u.uhp = u.uhpmax; } } else if (Regeneration || (!(moves % (22 - u.ulevel * 2)))) { flags.botl = 1; u.uhp++; } } if (Teleportation && !rn2(85)) tele(); if (Searching && multi >= 0) dosearch(); gethungry(); invault(); amulet(); } if (multi < 0) { if (!++multi) { pline("%s", nomovemsg ? nomovemsg : "You can move again."); nomovemsg = 0; if (afternmv) (*afternmv)(); afternmv = NULL; } } find_ac(); #ifndef QUEST if (!flags.mv || Blind) #endif { seeobjs(); seemons(); nscr(); } if (flags.botl || flags.botlx) bot(); flags.move = 1; if (multi >= 0 && occupation) { if (monster_nearby()) stop_occupation(); else if ((*occupation)() == 0) occupation = NULL; continue; } if (multi > 0) { #ifdef QUEST if (flags.run >= 4) finddir(); #endif lookaround(); if (!multi) { /* lookaround may clear multi */ flags.move = 0; continue; } if (flags.mv) { if (multi < COLNO && !--multi) flags.mv = flags.run = 0; domove(); } else { --multi; rhack(save_cm); } } else if (multi == 0) { #ifdef MAIL ckmailstatus(); #endif rhack(NULL); } if (multi && multi % 7 == 0) fflush(stdout); } }
int submain ( struct vtx_data **graph, /* data structure for graph */ int nvtxs, /* number of vertices in full graph */ int nedges, /* number of edges in graph */ int using_vwgts, /* are vertex weights being used? */ int using_ewgts, /* are edge weights being used? */ int igeom, /* geometry dimension if using inertial method */ float **coords, /* coordinates of vertices if used */ char *outassignname, /* name of assignment output file */ char *outfilename, /* in which to print output metrics */ int *assignment, /* set number of each vtx (length n) */ double *goal, /* desired sizes for each set */ int architecture, /* 0=> hypercube, d=> d-dimensional mesh */ int ndims_tot, /* total number hypercube dimensions */ int mesh_dims[3], /* extent of mesh in 3 directions */ int global_method, /* global partitioning algorithm */ int local_method, /* local partitioning algorithm */ int rqi_flag, /* use RQI/Symmlq eigensolver? */ int vmax, /* if so, how many vtxs to coarsen down to */ int ndims, /* number of eigenvectors (2^d sets) */ double eigtol, /* tolerance on eigenvectors */ long seed /* for random graph mutations */ ) { extern int ECHO; /* controls output to file or screen */ extern int CHECK_INPUT; /* should I check input for correctness? */ extern int SEQUENCE; /* just generate spectal ordering? */ extern int OUTPUT_ASSIGN; /* print assignment to a file? */ extern int OUTPUT_METRICS; /* controls formatting of output */ extern int PERTURB; /* perturb matrix if quad/octasection? */ extern int NSQRTS; /* number of square roots to precompute */ extern int KL_METRIC; /* KL interset cost: 1=>cuts, 2=>hops */ extern int LANCZOS_TYPE; /* type of Lanczos to use */ extern int REFINE_MAP; /* use greedy strategy to improve mapping? */ extern int REFINE_PARTITION;/* number of calls to pairwise_refine to make */ extern int VERTEX_COVER; /* use matching to reduce vertex separator? */ extern int CONNECTED_DOMAINS; /* force subdomain connectivity at end? */ extern int INTERNAL_VERTICES; /* greedily increase internal vtxs? */ extern int DEBUG_INTERNAL; /* debug code about force_internal? */ extern int DEBUG_REFINE_PART; /* debug code about refine_part? */ extern int DEBUG_REFINE_MAP; /* debug code about refine_map? */ extern int DEBUG_MACH_PARAMS; /* print out computed machine params? */ extern int DEBUG_TRACE; /* trace main execution path */ extern int PRINT_HEADERS; /* print section headings for output? */ extern int TIME_KERNELS; /* benchmark some numerical kernels? */ extern double start_time; /* time code was entered */ extern double total_time; /* (almost) total time spent in code */ extern double check_input_time; /* time spent checking input */ extern double partition_time; /* time spent partitioning graph */ extern double kernel_time; /* time spent benchmarking kernels */ extern double count_time; /* time spent evaluating the answer */ extern double print_assign_time; /* time spent writing output file */ FILE *outfile; /* output file */ struct vtx_data **graph2; /* data structure for graph */ int hop_mtx[MAXSETS][MAXSETS]; /* between-set hop cost for KL */ double *vwsqrt; /* sqrt of vertex weights (length nvtxs+1) */ double time, time1; /* timing variables */ char *graphname, *geomname; /* names of input files */ char *inassignname; /* name of assignment input file */ int old_nsqrts; /* old value of NSQRTS */ int append; /* append output to existing file? */ int nsets; /* number of sets created by each divide */ int nsets_tot; /* total number of sets */ int bits; /* used in computing hops */ int flag; /* return code from check_input */ int old_perturb=0; /* saves original pertubation flag */ int i, j, k; /* loop counters */ double seconds(); void setrandom(long int seed); int check_input(), refine_part(); void connect_enforce(); void setrandom(), makevwsqrt(), balance(), countup(); void force_internal(), sequence(), reflect_input(); void machine_params(), assign_out(), refine_map(); void time_out(), time_kernels(), strout(); if (DEBUG_TRACE > 0) { printf("<Entering submain>\n"); } /* First check all the input for consistency. */ if (architecture == 1) mesh_dims[1] = mesh_dims[2] = 1; else if (architecture == 2) mesh_dims[2] = 1; /* Check for simple special case of 1 processor. */ k = 0; if (architecture == 0) k = 1 << ndims_tot; else if (architecture > 0) k = mesh_dims[0] * mesh_dims[1] * mesh_dims[2]; if (k == 1) { for (i = 1; i <= nvtxs; i++) assignment[i] = 0; if (OUTPUT_ASSIGN > 0 && outassignname != NULL) { time1 = seconds(); assign_out(nvtxs, assignment, k, outassignname); print_assign_time += seconds() - time1; } return(0); } graphname = Graph_File_Name; geomname = Geometry_File_Name; inassignname = Assign_In_File_Name; /* Turn of perturbation if using bisection */ if (ndims == 1) { old_perturb = PERTURB; PERTURB = FALSE; } if (ECHO < 0 && outfilename != NULL) { /* Open output file */ outfile = fopen(outfilename, "r"); if (outfile != NULL) { append = TRUE; fclose(outfile); } else append = FALSE; outfile = fopen(outfilename, "a"); if (append) { fprintf(outfile, "\n------------------------------------------------\n\n"); } } else { outfile = NULL; } Output_File = outfile; if (outfile != NULL && PRINT_HEADERS) { fprintf(outfile, "\n Chaco 2.0\n"); fprintf(outfile, " Sandia National Laboratories\n\n"); } if (CHECK_INPUT) { /* Check the input for inconsistencies. */ time1 = seconds(); flag = check_input(graph, nvtxs, nedges, igeom, coords, graphname, assignment, goal, architecture, ndims_tot, mesh_dims, global_method, local_method, rqi_flag, &vmax, ndims, eigtol); check_input_time += seconds() - time1; if (flag) { strout("ERROR IN INPUT.\n"); return (1); } } if (ECHO != 0) { reflect_input(nvtxs, nedges, igeom, graphname, geomname, inassignname, outassignname, outfilename, architecture, ndims_tot, mesh_dims, global_method, local_method, rqi_flag, vmax, ndims, eigtol, seed, outfile); } if (PRINT_HEADERS) { printf("\n\nStarting to partition ...\n\n"); if (Output_File != NULL ) { fprintf(Output_File, "\n\nStarting to partition ... (residual, warning and error messages only)\n\n"); } } time = seconds(); /* Perform some one-time initializations. */ setrandom(seed); machine_params(&DOUBLE_EPSILON, &DOUBLE_MAX); if (DEBUG_MACH_PARAMS > 0) { printf("Machine parameters:\n"); printf(" DOUBLE_EPSILON = %e\n", DOUBLE_EPSILON); printf(" DOUBLE_MAX = %e\n", DOUBLE_MAX); } nsets = (1 << ndims); old_nsqrts = NSQRTS; if (nvtxs < NSQRTS && !using_vwgts) { NSQRTS = nvtxs; } SQRTS = smalloc_ret((NSQRTS + 1) * sizeof(double)); if (SQRTS == NULL) { strout("ERROR: No space to allocate sqrts\n"); return(1); } for (i = 1; i <= NSQRTS; i++) SQRTS[i] = sqrt((double) i); if (using_vwgts && (global_method == 1 || global_method == 2)) { vwsqrt = smalloc_ret((nvtxs + 1) * sizeof(double)); if (vwsqrt == NULL) { strout("ERROR: No space to allocate vwsqrt\n"); sfree(SQRTS); NSQRTS = old_nsqrts; return(1); } makevwsqrt(vwsqrt, graph, nvtxs); } else vwsqrt = NULL; if (TIME_KERNELS) { time1 = seconds(); time_kernels(graph, nvtxs, vwsqrt); kernel_time += seconds() - time1; } if (SEQUENCE) { sequence(graph, nvtxs, nedges, using_ewgts, vwsqrt, LANCZOS_TYPE, rqi_flag, vmax, eigtol); goto End_Label; } /* Initialize cost function for KL-spiff */ if (global_method == 1 || local_method == 1) { for (i = 0; i < nsets; i++) { hop_mtx[i][i] = 0; for (j = 0; j < i; j++) { if (KL_METRIC == 2) { /* Count hypercube hops */ hop_mtx[i][j] = 0; bits = i ^ j; while (bits) { if (bits & 1) { ++hop_mtx[i][j]; } bits >>= 1; } } else if (KL_METRIC == 1) { /* Count cut edges */ hop_mtx[i][j] = 1; } hop_mtx[j][i] = hop_mtx[i][j]; } }
void fillmap() { FUNCTIONLIST *tmp; OBJECT *o; tmp = proclist; proclist = NULL; for (int i=0;i<playfield->w;i++) { o = objcol->new_obj(OBJ_RTILE); if (!o) out_of_mem("creating border"); playfield->set(i,0,o); o = objcol->new_obj(OBJ_RTILE); if (!o) out_of_mem("creating border"); playfield->set(i, playfield->h - 1,o); } for (int i=1;i<playfield->h - 1;i++) { o = objcol->new_obj(OBJ_RTILE); if (!o) out_of_mem("creating border"); playfield->set(0,i,o); o = objcol->new_obj(OBJ_RTILE); if (!o) out_of_mem("creating border"); playfield->set(playfield->w - 1,i,o); } if (get_config_int("MINER","background",0)) { for (int i=1;i<playfield->w -1 ;i++) for (int j=1;j<playfield->w -1 ;j++) playfield->set(i, j, objcol->new_obj(OBJ_BACKG)); } // for (int i=0;i<1695;i++) // setrandom(objcol->new_obj(OBJ_BOMAL)); playfield->set(2,2,objcol->new_obj(OBJ_STUT)); int placable_size = (playfield->w -10)* (playfield->h-11); // fprintf(stderr,"size %d\n",placable_size); // for (int i=0;i<(placable_size/400+1) ;i++) for (int i=0;i<(placable_size/60+1) ;i++) { setrandom(objcol->new_obj(OBJ_BOMAL)); setrandom(objcol->new_obj(OBJ_BOMB)); setrandom(objcol->new_obj(OBJ_ENERG)); setrandom(objcol->new_obj(OBJ_PSTUT)); setrandom(objcol->new_obj(OBJ_MONSTER)); } placable_size -= (placable_size/400 + 1) * 5 ; // fprintf(stderr,"bombs set size %d\n",placable_size); placable_size /= 20; // placable_size /=2; // fprintf(stderr,"loop size %d\n",placable_size); for (int i=0;i<placable_size;i++) { setrandom(objcol->new_obj(OBJ_BOX)); setrandom(objcol->new_obj(OBJ_BOX)); setrandom(objcol->new_obj(OBJ_BALLOON)); setrandom(objcol->new_obj(OBJ_LINKS)); setrandom(objcol->new_obj(OBJ_RECHTS)); setrandom(objcol->new_obj(OBJ_MINE)); setrandom(objcol->new_obj(OBJ_NE_RE)); setrandom(objcol->new_obj(OBJ_STER)); setrandom(objcol->new_obj(OBJ_GOUD)); setrandom(objcol->new_obj(OBJ_SCHEDEL)); setrandom(objcol->new_obj(OBJ_NE_LI)); setrandom(objcol->new_obj(OBJ_HEKOP)); setrandom(objcol->new_obj(OBJ_HEKNEER)); setrandom(objcol->new_obj(OBJ_BLOKAL)); setrandom(objcol->new_obj(OBJ_ENERG)); } for (int i=1;i<playfield->w -1 ;i++) for (int j=1;j<playfield->w -1 ;j++) if (playfield->is_empty(i,j)) { if (i > playfield->w/4 && i < playfield->w*3/4 && j > playfield->h/4 && j < playfield->h*3/4 ) playfield->set(i, j, objcol->new_obj(OBJ_BRICK2)); else playfield->set(i, j, objcol->new_obj(OBJ_BRICK)); } proclist = tmp; // playfield->set(10,2, objcol->new_obj(OBJ_GROEN)); // playfield->set(12,2, objcol->new_obj(OBJ_MONSTER)); // playfield->set(12,2, objcol->new_obj(OBJ_CROWN)); }