/* **获取某人的所有可能步法,并储存 */ void get_all(int who) { int i, j; moves_stack_block[moves_stack_next] = 0; for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) if(legal(i, j, who)) { moves_x[moves_next] = i; moves_y[moves_next] = j; moves_next++; moves_stack_block[moves_stack_next]++; } moves_stack_next++; }
/**************************************************************************** * Emulator Options Menu * ****************************************************************************/ int Emu_options () { int quit = 0; int ret; int prevmenu = menu; int mcardcount = 6; char mcardmenu[6][20] = { {"Video Options"}, {"SRAM Manager"}, {"STATE Manager"}, {"Game Infos"}, {"View Credits"}, {"Return to previous"} }; menu = 0; while (quit == 0) { strcpy (menutitle, "Emulator Options"); ret = DoMenu(&mcardmenu[0], mcardcount); switch (ret) { case -1: case 5: quit = 1; break; case 0: // Video Options OptionMenu(); break; case 1: // File Manager case 2: if (loadsavemenu(ret-1)) return 1; break; case 3: // ROM Information RomInfo(); break; case 4: // view credits legal(); break; } } menu = prevmenu; return 0; }
/* Fonction qui simule les coup possible et les enregistre dans le damier */ void calculerPossibilites(Othellier *dame, Coup *coup) { int position = coup->position; int i; /* on commence par la premiere case */ for(i = 10; i < MAX_CASE; i++){ /* si le coup est légal on affiche dans la case le symbole et on retourne vrai */ coup->position = i; if(legal(dame, coup)){ dame->damier[coup->position] = POSSIBLE; } } coup->position = position; }
int search(void) { int x0,y0,cout,action,i; type p,t; memset(array,0,sizeof(array)); p=malloc(sizeof(struct node)); p->x=x1; p->y=y1; p->cout=0; p->action=0; array[p->x][p->y][p->action]=1; createqueue(1000); makeempty(Q); enqueue(p,Q); while( !isempty(Q) ) { p=dequeue(Q); if( p->x==x2&&p->y==y2) return p->cout; for(i=0;i<5;++i) { x0=p->x+direction[i][0]; y0=p->y+direction[i][1]; cout=p->cout+1; action=p->action+1; if(legal(x0,y0,action)&&map[x0][y0]!='*'&&x0>=0&&x0<m&&y0>=0&&y0<n) { if(!array[x0][y0][action]&&!(x0==dogpoint[action].x&&y0==dogpoint[action].y)) { array[x0][y0][action]=1; t=malloc(sizeof(struct node)); t->x=x0; t->y=y0; if(action==D) action=0; t->action=action; t->cout=cout; enqueue(t,Q); } } } } return -1; }
int main() { int i =0; int j =0; for(i=0;i<8;i++) for(j=0;j<8;j++) { scanf("%d",&board[i][j]); } for(j=0;j<8;j++) for(i=0;i<8;i++) { if(legal(i,j)) { printf("%d %d\n",i,j); return 1; } } }
int solve(int i,int j) //Recursive Backtracking Function to find the legal value at a position {int k; if(i==9) { printsudoku(); exit(0); } while(A[i][j]!= -1) { if(j==8 ) { if(i<8) { i=i+1; j=0; } else { printsudoku(); exit(0); } } else j++; } if(A[i][j]==-1) {for(k=1;k<=9;k++) { if(legal(i,j,k)) { A[i][j]=k; if(j==8) solve(i+1,0); else solve(i,j+1); A[i][j]=-1; } } } }
void ch_att::run(double riskRate) { long xx,yy,mx=0,my=0; double max=-(1e100),value; for (int i=0;i<=sp;i++) for (int j=0;j<=i;j++) for (int d1=-1;d1<=1;d1+=2) for (int d2=-1;d2<=1;d2+=2) { xx=x+j*d1; yy=y+(i-j)*d2; if (!legal(xx,yy) || (g[xx][yy]!=EMPTY && g[xx][yy]!=myID)) continue; value=-riskRate*risk(xx,yy)+(1-riskRate)*benefit(xx,yy); if (value>max) { mx=xx; my=yy; } } move(mx,my); }
int main (int argc, char *argv[]) { #ifdef HW_RVL /* initialize DVD device */ DI_Init(); #endif u16 usBetweenFrames; long long now, prev; /* Initialize OGC subsystems */ ogc_video__init(); ogc_input__init(); ogc_audio__init(); #ifdef HW_DOL /* Initialize GC DVD interface */ DVD_Init (); dvd_drive_detect(); #endif #ifdef HW_RVL /* Power Button callback */ SYS_SetPowerCallback(Power_Off); #endif /* Initialize FAT Interface */ if (fatInitDefault() == true) { fat_enabled = 1; } /* Default Config */ legal(); set_option_defaults (); config_load(); #ifdef HW_RVL /* Load SMB Settings */ loadSettings(); #endif /* Restore Recent Files list */ set_history_defaults(); history_load(); /* Initialize Virtual Machine */ init_machine (); /* Show Menu */ MainMenu(); ConfigRequested = 0; /* Initialize Frame timings */ frameticker = 0; usBetweenFrames = sms.display ? 20000 : 16666; prev = gettime(); /* Emulation Loop */ while (1) { /* update inputs */ ogc_input__update(); /* Frame synchronization */ if (gc_pal != sms.display) { /* use timers */ now = gettime(); if (diff_usec(prev, now) > usBetweenFrames) { /* Frame skipping */ prev = now; system_frame(1); } else { /* Delay */ while (diff_usec(prev, now) < usBetweenFrames) now = gettime(); /* Render Frame */ prev = now; system_frame(0); } } else { /* use VSync */ if (frameticker > 1) { /* Frame skipping */ frameticker--; system_frame (1); } else { /* Delay */ while (!frameticker) usleep(10); system_frame (0); } frameticker--; } /* update video & audio */ ogc_video__update(); ogc_audio__update(); /* Check for Menu request */ if (ConfigRequested) { /* reset AUDIO */ ogc_audio__reset(); /* go to menu */ MainMenu (); ConfigRequested = 0; ogc_video__reset(); /* reset frame timings */ frameticker = 0; usBetweenFrames = sms.display ? 20000 : 16666; prev = gettime(); } } return 0; }
/* Routine creates dead.letter */ void mkdead() { static char pn[] = "mkdead"; int aret; char *dotdead = &dead[1]; gid_t egid = getegid(); struct stat st; malf = (FILE *)NULL; /* Make certain that there's something to copy. */ if (!tmpf) return; /* Try to create dead letter in current directory or in home directory */ umask(umsave); setgid(getgid()); if ((aret = legal(dotdead)) && stat(dotdead, &st) == 0) malf = fopen(dotdead, "a"); if ((malf == NULL) || (aret == 0)) { /* try to create in $HOME */ if((hmdead = malloc(strlen(home) + strlen(dead) + 1)) == NULL) { fprintf(stderr, "%s: Can't malloc\n",program); Dout(pn, 0, "Cannot malloc.\n"); goto out; } cat(hmdead, home, dead); if ((aret=legal(hmdead)) && !(stat(hmdead, &st) < 0 && errno == EOVERFLOW)) malf = fopen(hmdead, "a"); if ((malf == NULL) || (aret == 0)) { fprintf(stderr, "%s: Cannot create %s\n", program,dotdead); Dout(pn, 0, "Cannot create %s\n", dotdead); out: fclose(tmpf); error = E_FILE; Dout(pn, 0, "error set to %d\n", error); umask(7); setegid(egid); return; } else { chmod(hmdead, DEADPERM); fprintf(stderr,"%s: Mail saved in %s\n",program,hmdead); } } else { chmod(dotdead, DEADPERM); fprintf(stderr,"%s: Mail saved in %s\n",program,dotdead); } /* Copy letter into dead letter box */ umask(7); aret = fseek(tmpf,0L,0); if (aret) errmsg(E_DEAD,""); if (!copystream(tmpf, malf)) errmsg(E_DEAD,""); fclose(malf); setegid(egid); }
/*************************************************************************** * M A I N * ***************************************************************************/ int main (int argc, char *argv[]) { #ifdef HW_RVL /* enable 64-byte fetch mode for L2 cache */ L2Enhance(); /* initialize DI interface */ DI_UseCache(0); DI_Init(); sprintf(osd_version, "%s (IOS %d)", VERSION, IOS_GetVersion()); #else sprintf(osd_version, "%s (GCN)", VERSION); #endif /* initialize video engine */ gx_video_Init(); #ifndef HW_RVL /* initialize DVD interface */ DVD_Init(); #endif /* initialize input engine */ gx_input_Init(); /* initialize FAT devices */ int retry = 0; int fatMounted = 0; /* try to mount FAT devices during 3 seconds */ while (!fatMounted && (retry < 12)) { fatMounted = fatInitDefault(); usleep(250000); retry++; } if (fatMounted) { /* base directory */ char pathname[MAXPATHLEN]; sprintf (pathname, DEFAULT_PATH); DIR *dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); /* default SRAM & Savestate files directories */ sprintf (pathname, "%s/saves",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/saves/md",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/saves/ms",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/saves/gg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/saves/sg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/saves/cd",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); /* default Snapshot files directories */ sprintf (pathname, "%s/snaps",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/snaps/md",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/snaps/ms",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/snaps/gg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/snaps/sg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/snaps/cd",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); /* default Cheat files directories */ sprintf (pathname, "%s/cheats",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/cheats/md",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/cheats/ms",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/cheats/gg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/cheats/sg",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); sprintf (pathname, "%s/cheats/cd",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); /* default BIOS ROM files directories */ sprintf (pathname, "%s/bios",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); /* default LOCK-ON ROM files directories */ sprintf (pathname, "%s/lock-on",DEFAULT_PATH); dir = opendir(pathname); if (dir) closedir(dir); else mkdir(pathname,S_IRWXU); } /* initialize sound engine */ gx_audio_Init(); /* initialize genesis plus core */ history_default(); config_default(); init_machine(); /* auto-load last ROM file */ if (config.autoload) { SILENT = 1; if (OpenDirectory(TYPE_RECENT, -1)) { if (LoadFile(0)) { reloadrom(); gx_video_Start(); gx_audio_Start(); ConfigRequested = 0; } } SILENT = 0; } /* show disclaimer */ if (ConfigRequested) { legal(); } #ifdef HW_RVL /* power button callback */ SYS_SetPowerCallback(PowerOff_cb); #endif /* reset button callback */ SYS_SetResetCallback(Reset_cb); /* main emulation loop */ run_emulation(); /* we should never return anyway */ return 0; }
bool Shop::ShopItem::is_available() const { return valid_item() && (!only_sell_legal_ || legal()) && can_afford(); }
/* * Print mail entries */ void printmail() { static char pn[] = "printmail"; int flg, curlet, showlet, k, print, aret, stret, rc; int nsmbox = 0; /* 1 ==> mailbox is in non-standard place */ int sav_j = -1; char *p, *getarg(); struct stat stbuf; struct stat *stbufp; int ttyf = isatty(1) ? TTY : ORDINARY; char readbuf[LSIZE]; /* holds user's response in interactive mode */ char *resp; gid_t savedegid; stbufp = &stbuf; /* * create working directory mbox name */ if ((hmbox = malloc(strlen(home) + strlen(mbox) + 1)) == NULL) { errmsg(E_MBOX, ""); return; } cat(hmbox, home, mbox); /* * If we are not using an alternate mailfile, then get * the $MAIL value and build the filename for the mailfile. * If $MAIL is set, but is NOT the 'standard' place, then * use it but set flgf to circumvent :saved processing. */ if (!flgf) { if ((p = malloc(strlen(maildir) + strlen(my_name) + 1)) == NULL) { errmsg(E_MEM, ""); return; } cat(p, maildir, my_name); if (((mailfile = getenv("MAIL")) == NULL) || (strlen(mailfile) == 0)) { /* $MAIL not set, use standard path to mailfile */ mailfile = p; } else { if (strcmp(mailfile, p) != 0) { flgf = 1; nsmbox = 1; Dout(pn, 0, "$MAIL ('%s') != standard path\n", mailfile); Dout("", 0, "\tSetting flgf to 1.\n"); } free(p); } } /* * Get ACCESS and MODIFICATION times of mailfile BEFORE we * use it. This allows us to put them back when we are * done. If we didn't, the shell would think NEW mail had * arrived since the file times would have changed. */ stret = CERROR; if (access(mailfile, A_EXIST) == A_OK) { if ((stret = stat(mailfile, stbufp)) != A_OK) { errmsg(E_FILE, "Cannot stat mailfile"); return; } mf_gid = stbufp->st_gid; mf_uid = stbufp->st_uid; utimep->actime = stbufp->st_atime; utimep->modtime = stbufp->st_mtime; file_size = stbufp->st_size; } /* Open the file as the real gid */ savedegid = getegid(); (void) setegid(getgid()); malf = fopen(mailfile, "r"); (void) setegid(savedegid); /* * stat succeeded, but we cannot access the mailfile */ if (stret == CSUCCESS && malf == NULL) { char buf[MAXFILENAME+50]; (void) snprintf(buf, sizeof (buf), "Invalid permissions on %s", mailfile); errmsg(E_PERM, buf); return; } else /* * using an alternate mailfile, but we failed on access */ if (!nsmbox && flgf && (malf == NULL)) { errmsg(E_FILE, "Cannot open mailfile"); return; } /* * we failed to access OR the file is empty */ else if ((malf == NULL) || (stbuf.st_size == 0)) { if (!flge && !flgE) { printf("No mail.\n"); } error = E_FLGE; Dout(pn, 0, "error set to %d\n", error); return; } if (flge) return; if (flgE) { if (utimep->modtime < utimep->actime) { error = E_FLGE_OM; Dout(pn, 0, "error set to %d\n", error); } return; } /* * Secure the mailfile to guarantee integrity */ lock(my_name); /* * copy mail to temp file and mark each letter in the * let array --- mailfile is still locked !!! */ mktmp(); copymt(malf, tmpf); onlet = nlet; fclose(malf); fclose(tmpf); unlock(); /* All done, OK to unlock now */ tmpf = doopen(lettmp, "r+", E_TMP); changed = 0; print = 1; curlet = 0; while (curlet < nlet) { /* * reverse order ? */ showlet = flgr ? curlet : nlet - curlet - 1; if (setjmp(sjbuf) == 0 && print != 0) { /* -h says to print the headers first */ if (flgh) { gethead(showlet, 0); flgh = 0; /* Only once */ /* set letter # to invalid # */ curlet--; showlet = flgr ? curlet : nlet - curlet - 1; } else { if (showlet != sav_j) { /* Looking at new message. */ /* Reset flag to override */ /* non-display of binary */ /* contents */ sav_j = showlet; pflg = 0; Pflg = flgP; } copylet(showlet, stdout, ttyf); } } /* * print only */ if (flgp) { curlet++; continue; } /* * Interactive */ interactive = 1; setjmp(sjbuf); stat(mailfile, stbufp); if (stbufp->st_size != file_size) { /* * New mail has arrived, load it */ k = nlet; lock(my_name); malf = doopen(mailfile, "r", E_FILE); fclose(tmpf); tmpf = doopen(lettmp, "a", E_TMP); fseek(malf, let[nlet].adr, 0); copymt(malf, tmpf); file_size = stbufp->st_size; fclose(malf); fclose(tmpf); unlock(); tmpf = doopen(lettmp, "r+", E_TMP); if (++k < nlet) printf("New mail loaded into letters %d - %d\n", k, nlet); else printf("New mail loaded into letter %d\n", nlet); } /* read the command */ printf("? "); fflush(stdout); fflush(stderr); if (fgets(readbuf, sizeof (readbuf), stdin) == NULL) break; resp = readbuf; while (*resp == ' ' || *resp == '\t') resp++; print = 1; Dout(pn, 0, "resp = '%s'\n", resp); if ((rc = atoi(resp)) != 0) { if (!validmsg(rc)) print = 0; else curlet = flgr ? rc - 1 : nlet - rc; } else switch (resp[0]) { default: printf("Usage:\n"); /* * help */ case '?': print = 0; for (rc = 0; help[rc]; rc++) printf("%s", help[rc]); break; /* * print message number of current message */ case '#': print = 0; if ((showlet == nlet) || (showlet < 0)) { printf("No message selected yet.\n"); } else { printf("Current message number is %d\n", showlet+1); } break; /* * headers */ case 'h': print = 0; if (resp[2] != 'd' && resp[2] != 'a' && (rc = getnumbr(resp+1)) > 0) { showlet = rc - 1; curlet = flgr ? rc - 1 : nlet - rc- 1; } if (rc == -1 && resp[2] != 'a' && resp[2] != 'd') break; if (resp[2] == 'a') rc = 1; else if (resp[2] == 'd') rc = 2; else rc = 0; /* * if (!validmsg(showlet)) break; */ gethead(showlet, rc); break; /* * skip entry */ case '+': case 'n': case '\n': curlet++; break; case 'P': Pflg++; break; case 'p': pflg++; break; case 'x': changed = 0; case 'q': goto donep; /* * Previous entry */ case '^': case '-': if (--curlet < 0) curlet = 0; break; /* * Save in file without header */ case 'y': case 'w': /* * Save mail with header */ case 's': print = 0; if (!validmsg(curlet)) break; if (resp[1] == '\n' || resp[1] == '\0') { cat(resp+1, hmbox, ""); } else if (resp[1] != ' ') { printf("Invalid command\n"); break; } umask(umsave); flg = 0; if (getarg(lfil, resp + 1) == NULL) { cat(resp + 1, hmbox, ""); } malf = (FILE *)NULL; p = resp + 1; while ((p = getarg(lfil, p)) != NULL) { if (flg) { fprintf(stderr, "%s: File '%s' skipped\n", program, lfil); continue; } malf = NULL; if ((aret = legal(lfil))) { malf = fopen(lfil, "a"); } if ((malf == NULL) || (aret == 0)) { fprintf(stderr, "%s: Cannot append to %s\n", program, lfil); flg++; } else if (aret == 2) { chown(lfil, my_euid, my_gid); } if (!flg && copylet(showlet, malf, resp[0] == 's'? ORDINARY: ZAP) == FALSE) { fprintf(stderr, "%s: Cannot save mail to '%s'\n", program, lfil); flg++; } else Dout(pn, 0, "!saved\n"); if (malf != (FILE *)NULL) { fclose(malf); } } umask(7); if (!flg) { setletr(showlet, resp[0]); print = 1; curlet++; } break; /* * Reply to a letter */ case 'r': print = 0; if (!validmsg(curlet)) break; replying = 1; for (k = 1; resp[k] == ' ' || resp[k] == '\t'; ++k); resp[strlen(resp)-1] = '\0'; (void) strlcpy(m_sendto, resp+k, sizeof (m_sendto)); goback(showlet); replying = 0; setletr(showlet, resp[0]); break; /* * Undelete */ case 'u': print = 0; if ((k = getnumbr(resp+1)) <= 0) k = showlet; else k--; if (!validmsg(k)) break; setletr(k, ' '); break; /* * Mail letter to someone else */ case 'm': { reciplist list; print = 0; if (!validmsg(curlet)) break; new_reciplist(&list); flg = 0; k = 0; if (substr(resp, " -") != -1 || substr(resp, "\t-") != -1) { printf("Only users may be specified\n"); break; } p = resp + 1; while ((p = getarg(lfil, p)) != NULL) { char *env; if (lfil[0] == '$') { if (!(env = getenv(&lfil[1]))) { fprintf(stderr, "%s: %s has no value or is not exported.\n", program, lfil); flg++; } else add_recip(&list, env, FALSE); k++; } else if (lfil[0] != '\0') { add_recip(&list, lfil, FALSE); k++; } } (void) strlcpy(Rpath, my_name, sizeof (Rpath)); sending = TRUE; flg += sendlist(&list, showlet, 0); sending = FALSE; if (k) { if (!flg) { setletr(showlet, 'm'); print = 1; curlet++; } } else printf("Invalid command\n"); del_reciplist(&list); break; } /* * Read new letters */ case 'a': if (onlet == nlet) { printf("No new mail\n"); print = 0; break; } curlet = 0; print = 1; break; /* * Escape to shell */ case '!': systm(resp + 1); printf("!\n"); print = 0; break; /* * Delete an entry */ case 'd': print = 0; k = 0; if (strncmp("dq", resp, 2) != SAME && strncmp("dp", resp, 2) != SAME) if ((k = getnumbr(resp+1)) == -1) break; if (k == 0) { k = showlet; if (!validmsg(curlet)) break; print = 1; curlet++; } else k--; setletr(k, 'd'); if (resp[1] == 'p') print = 1; else if (resp[1] == 'q') goto donep; break; } } /* * Copy updated mailfile back */ donep: if (changed) { copyback(); stamp(); } }
int main (int argc, char *argv[]) { #ifdef HW_RVL /* initialize DVDX */ DI_Init(); #endif /* initialize hardware */ gx_video_Init(); gx_input_Init(); #ifdef HW_DOL DVD_Init (); dvd_drive_detect(); #endif /* initialize FAT devices */ if (fatInitDefault()) { /* check for default directories */ DIR_ITER *dir = NULL; /* base directory */ char pathname[MAXPATHLEN]; sprintf (pathname, DEFAULT_PATH); dir = diropen(pathname); if (dir == NULL) mkdir(pathname,S_IRWXU); else dirclose(dir); /* SRAM & Savestate files directory */ sprintf (pathname, "%s/saves",DEFAULT_PATH); dir = diropen(pathname); if (dir == NULL) mkdir(pathname,S_IRWXU); else dirclose(dir); /* Snapshot files directory */ sprintf (pathname, "%s/snaps",DEFAULT_PATH); dir = diropen(pathname); if (dir == NULL) mkdir(pathname,S_IRWXU); else dirclose(dir); /* Cheat files directory */ sprintf (pathname, "%s/cheats",DEFAULT_PATH); dir = diropen(pathname); if (dir == NULL) mkdir(pathname,S_IRWXU); else dirclose(dir); } /* initialize sound engine */ gx_audio_Init(); /* initialize core engine */ legal(); config_default(); history_default(); init_machine(); /* run any injected rom */ if (cart.romsize) { ARAMFetch((char *)cart.rom, (void *)0x8000, cart.romsize); reloadrom (cart.romsize,"INJECT.bin"); gx_video_Start(); gx_audio_Start(); frameticker = 1; } else { /* Main Menu */ ConfigRequested = 1; } /* initialize GUI engine */ GUI_Initialize(); #ifdef HW_RVL /* Power button callback */ SYS_SetPowerCallback(Power_Off); #endif /* main emulation loop */ while (1) { /* Main Menu request */ if (ConfigRequested) { /* stop video & audio */ gx_audio_Stop(); gx_video_Stop(); /* show menu */ MainMenu (); ConfigRequested = 0; /* start video & audio */ gx_audio_Start(); gx_video_Start(); frameticker = 1; } if (frameticker > 1) { /* skip frame */ system_frame(1); --frameticker; } else { while (frameticker < 1) usleep(10); /* render frame */ system_frame(0); --frameticker; /* update video */ gx_video_Update(); } /* update audio */ gx_audio_Update(); } return 0; }
int minimax(point* root) { int rec[8][8]; if(root->depth == depth) { root->value = valueOfEvaluation(); addToLog4Minimax(*root); return FALSE; } if(terminalStateCheck()) { root->value = valueOfEvaluation(); addToLog4Minimax(*root); return FALSE; } if(root->parent != NULL && root->pass && root->parent->pass) { root->value = valueOfEvaluation(); addToLog4Minimax(*root); return FALSE; } ply = (ply + 1) % 2; addToLog4Minimax(*root); int visited = FALSE; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (legal(i, j, rec, TRUE)) { visited = TRUE; point* tmp = new point((root->depth % 2 == 0) ? INF : -INF, root->depth + 1); tmp->x = i; tmp->y = j; tmp->parent = root; minimax(tmp); if(root->depth % 2 == 0 && root->value < tmp->value) { root->value = tmp->value; if(root->child != NULL) delete root->child; root->child = tmp; } else if (root->depth % 2 == 1 && root->value > tmp->value) { root->value = tmp->value; if(root->child != NULL) delete root->child; root->child = tmp; } addToLog4Minimax(*root); backup(rec); } } } if(!visited) { point* tmp = new point((root->depth % 2 == 0) ? INF : -INF, root->depth + 1); tmp->parent = root; tmp->pass = TRUE; minimax(tmp); if(root->depth % 2 == 0 && root->value < tmp->value) { root->value = tmp->value; } else if (root->depth % 2 == 1 && root->value > tmp->value) { root->value = tmp->value; } addToLog4Minimax(*root); } ply = (ply + 1) % 2; if(root->child == NULL) { return FALSE; } return TRUE; }
int main(int argc, char **argv) { config.verbose = false; config.quiet = false; config.repl = false; config.javascript = false; config.static_fns = false; config.elide_asserts = false; config.cache_path = NULL; config.theme = "light"; config.dumb_terminal = false; config.out_path = NULL; config.num_src_paths = 0; config.src_paths = NULL; config.num_scripts = 0; config.scripts = NULL; config.main_ns_name = NULL; struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"legal", no_argument, NULL, 'l'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {"repl", no_argument, NULL, 'r'}, {"static-fns", no_argument, NULL, 's'}, {"elide-asserts", no_argument, NULL, 'a'}, {"cache", required_argument, NULL, 'k'}, {"eval", required_argument, NULL, 'e'}, {"theme", required_argument, NULL, 't'}, {"dumb-terminal", no_argument, NULL, 'd'}, {"classpath", required_argument, NULL, 'c'}, {"auto-cache", no_argument, NULL, 'K'}, {"init", required_argument, NULL, 'i'}, {"main", required_argument, NULL, 'm'}, // development options {"javascript", no_argument, NULL, 'j'}, {"out", required_argument, NULL, 'o'}, {0, 0, 0, 0} }; int opt, option_index; while ((opt = getopt_long(argc, argv, "h?lvrsak:je:t:dc:o:Ki:qm:", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0]); exit(0); case 'l': legal(); return 0; case 'v': config.verbose = true; break; case 'q': config.quiet = true; break; case 'r': config.repl = true; break; case 's': config.static_fns = true; break; case 'a': config.elide_asserts = true; break; case 'k': config.cache_path = argv[optind - 1]; break; case 'K': config.cache_path = ".planck_cache"; { char *path_copy = strdup(config.cache_path); char *dir = dirname(path_copy); if (mkdir_p(dir) < 0) { fprintf(stderr, "Could not create %s: %s\n", config.cache_path, strerror(errno)); } free(path_copy); } break; case 'j': config.javascript = true; break; case 'e': config.num_scripts += 1; config.scripts = realloc(config.scripts, config.num_scripts * sizeof(struct script)); config.scripts[config.num_scripts - 1].type = "text"; config.scripts[config.num_scripts - 1].expression = true; config.scripts[config.num_scripts - 1].source = argv[optind - 1]; break; case 'i': config.num_scripts += 1; config.scripts = realloc(config.scripts, config.num_scripts * sizeof(struct script)); config.scripts[config.num_scripts - 1].type = "path"; config.scripts[config.num_scripts - 1].expression = false; config.scripts[config.num_scripts - 1].source = argv[optind - 1]; break; case 'm': config.main_ns_name = argv[optind - 1]; break; case 't': config.theme = argv[optind - 1]; break; case 'd': config.dumb_terminal = true; break; case 'c': { char *classpath = argv[optind - 1]; char *source = strtok(classpath, ":"); while (source != NULL) { char *type = "src"; if (str_has_suffix(source, ".jar") == 0) { type = "jar"; } config.num_src_paths += 1; config.src_paths = realloc(config.src_paths, config.num_src_paths * sizeof(struct src_path)); config.src_paths[config.num_src_paths - 1].type = type; config.src_paths[config.num_src_paths - 1].path = strcmp(type, "jar") == 0 ? strdup(source) : ensure_trailing_slash(source); source = strtok(NULL, ":"); } break; } case 'o': config.out_path = ensure_trailing_slash(argv[optind - 1]); break; case '?': usage(argv[0]); exit(1); default: printf("unhandled argument: %c\n", opt); } } if (config.dumb_terminal) { config.theme = "dumb"; } config.num_rest_args = 0; config.rest_args = NULL; if (optind < argc) { config.num_rest_args = argc - optind; config.rest_args = malloc((argc - optind) * sizeof(char*)); int i = 0; while (optind < argc) { config.rest_args[i++] = argv[optind++]; } } if (config.num_scripts == 0 && config.main_ns_name == NULL && config.num_rest_args == 0) { config.repl = true; } if (config.main_ns_name != NULL && config.repl) { printf("Only one main-opt can be specified.\n"); exit(1); } config.is_tty = isatty(STDIN_FILENO) == 1; JSGlobalContextRef ctx = JSGlobalContextCreate(NULL); global_ctx = ctx; cljs_engine_init(ctx); // Process init arguments for (int i = 0; i < config.num_scripts; i++) { // TODO: exit if not successfull struct script script = config.scripts[i]; evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, config.theme, true); } // Process main arguments if (config.main_ns_name != NULL) { run_main_in_ns(ctx, config.main_ns_name, config.num_rest_args, config.rest_args); } else if (!config.repl && config.num_rest_args > 0) { char *path = config.rest_args[0]; struct script script; if (strcmp(path, "-") == 0) { char *source = read_all(stdin); script.type = "text"; script.source = source; script.expression = false; } else { script.type = "path"; script.source = path; script.expression = false; } evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, config.theme, true); } else if (config.repl) { if (!config.quiet) { banner(); } run_repl(ctx); } return exit_value; }
bool Shop::ShopItem::display() const { return (valid_item() && (!only_sell_legal_ || legal())); }
int main(int argc, char *argv[]) { // define usage const char *usage = "Usage: sudoku n00b|l33t [#]\n"; // ensure that number of arguments is as expected if (argc != 2 && argc != 3) { fprintf(stderr, usage); return 1; } // ensure that level is valid if (strcmp(argv[1], "debug") == 0) g.level = "debug"; else if (strcmp(argv[1], "n00b") == 0) g.level = "n00b"; else if (strcmp(argv[1], "l33t") == 0) g.level = "l33t"; else { fprintf(stderr, usage); return 2; } // n00b and l33t levels have 1024 boards; debug level has 9 int max = (strcmp(g.level, "debug") == 0) ? 9 : 1024; // ensure that #, if provided, is in [1, max] if (argc == 3) { // ensure n is integral char c; if (sscanf(argv[2], " %d %c", &g.number, &c) != 1) { fprintf(stderr, usage); return 3; } // ensure n is in [1, max] if (g.number < 1 || g.number > max) { fprintf(stderr, "That board # does not exist!\n"); return 4; } // seed PRNG with # so that we get same sequence of boards srand(g.number); } else { // seed PRNG with current time so that we get any sequence of boards srand(time(NULL)); // choose a random n in [1, max] g.number = rand() % max + 1; } // start up ncurses if (!startup()) { fprintf(stderr, "Error starting up ncurses!\n"); return 5; } // register handler for SIGWINCH (SIGnal WINdow CHanged) signal(SIGWINCH, (void (*)(int)) handle_signal); // start the first game if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } redraw_all(); // initialize legal variable g.legal = true; // initialize won variable g.won = false; // let the user play! int ch; do { // refresh the screen refresh(); // get user's input ch = getch(); // capitalize input to simplify cases ch = toupper(ch); // process user's input switch (ch) { // start a new game case 'N': g.number = rand() % max + 1; if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } break; // restart current game case 'R': if (!restart_game()) { shutdown(); fprintf(stderr, "Could not load board from disk!\n"); return 6; } break; // let user manually redraw screen with ctrl-L case CTRL('l'): redraw_all(); break; // move cursor on the board case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT: move_cursor(ch); break; case KEY_BACKSPACE: case KEY_DC: case 46: // . case 48: // 0 case 49: // 1 case 50: // 2 case 51: // 3 case 52: // 4 case 53: // 5 case 54: // 6 case 55: // 7 case 56: // 8 case 57: // 9 change_number(ch); if(!legal(ch)) { show_banner("Illegal Move..."); show_cursor(); g.legal = false; } else { hide_banner(); show_cursor(); g.legal = true; } if(won(ch)) { show_banner("You Won The Game!!!"); g.won = true; } break; } // log input (and board's state) if any was received this iteration if (ch != ERR) log_move(ch); } while (ch != 'Q'); // shut down ncurses shutdown(); // tidy up the screen (using ANSI escape sequences) printf("\033[2J"); printf("\033[%d;%dH", 0, 0); // that's all folks printf("\nkthxbai!\n\n"); return 0; }
static int fplayer(int race) { int l; while(-1) { gotoxy(48,15); prints("目前位置(%2d,%2d)",m,n); put_box(mm,nn,0); put_box(m,n,race); mm=m; nn=n; l=igetkey(); switch(l) { // case '1': case '2': if(role[l-'1']==1) { role[l-'1']=2; attack[l-'1']=0; } else if(attack[l-'1']==0) attack[l-'1']=1; else role[l-'1']=1; who(l-'1'); break; case 'q': return 2; case 'r': return 1; case 'b': if(sum>2) { int i; for(i=0;i<2;i++) { int x=repent[sum-1][1],y=repent[sum-1][2]; put_chess(x,y,0); move(y+1,x*2); outs("┼"); sum-=2; } for(i=sum;i>=sum-5;i--) { gotoxy(48,i-sum+13); if(i>0) prints("第%d步: %s (%2d,%2d) ",i,(repent[i-1][0]==1)?"●":"○", repent[i-1][1],repent[i-1][2]); else outs(" "); } } break; case ' ': if(!chess[m-1][n-1]) { int hk=put_chess(m,n,race); if(hk) { int dk = show_win(hk); if(dk=='n'||dk=='q') return 2; return 1; } return 0; } break; case KEY_DOWN: //down if(legal(m,n+1)) n++; break; case KEY_UP: //up if(legal(m,n-1)) n--; break; case KEY_RIGHT: //right if(legal(m+1,n)) m++; break; case KEY_LEFT: //left if(legal(m-1,n)) m--; break; } } }
int main(int argc, char **argv) { struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"legal", no_argument, NULL, 'l'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {"repl", no_argument, NULL, 'r'}, {"static-fns", no_argument, NULL, 's'}, {"elide-asserts", no_argument, NULL, 'a'}, {"cache", required_argument, NULL, 'k'}, {"eval", required_argument, NULL, 'e'}, {"theme", required_argument, NULL, 't'}, {"classpath", required_argument, NULL, 'c'}, {"auto-cache", no_argument, NULL, 'K'}, {"init", required_argument, NULL, 'i'}, {"main", required_argument, NULL, 'm'}, // development options {"javascript", no_argument, NULL, 'j'}, {"out", required_argument, NULL, 'o'}, {0, 0, 0, 0} }; int opt, option_index; while ((opt = getopt_long(argc, argv, "h?lvrsak:je:t:c:o:Ki:qm:", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0]); exit(0); case 'l': legal(); return 0; case 'v': verbose = true; break; case 'q': quiet = true; break; case 'r': repl = true; break; case 's': static_fns = true; break; case 'a': elide_asserts = true; break; case 'k': cache_path = argv[optind - 1]; break; case 'K': cache_path = ".planck_cache"; { char *path_copy = strdup(cache_path); char *dir = dirname(path_copy); if (mkdir_p(dir) < 0) { fprintf(stderr, "Could not create %s: %s\n", cache_path, strerror(errno)); } free(path_copy); } break; case 'j': javascript = true; break; case 'e': num_scripts += 1; scripts = realloc(scripts, num_scripts * sizeof(struct script)); scripts[num_scripts - 1].type = "text"; scripts[num_scripts - 1].expression = true; scripts[num_scripts - 1].source = argv[optind - 1]; break; case 'i': num_scripts += 1; scripts = realloc(scripts, num_scripts * sizeof(struct script)); scripts[num_scripts - 1].type = "path"; scripts[num_scripts - 1].expression = false; scripts[num_scripts - 1].source = argv[optind - 1]; break; case 'm': main_ns_name = argv[optind - 1]; case 't': theme = argv[optind - 1]; break; case 'c': { char *classpath = argv[optind - 1]; char *source = strtok(classpath, ":"); while (source != NULL) { char *type = "src"; if (str_has_suffix(source, ".jar") == 0) { type = "jar"; } num_src_paths += 1; src_paths = realloc(src_paths, num_src_paths * sizeof(struct src_path)); src_paths[num_src_paths - 1].type = type; src_paths[num_src_paths - 1].path = strdup(source); source = strtok(NULL, ":"); } break; } case 'o': out_path = argv[optind - 1]; break; case '?': usage(argv[0]); exit(1); default: printf("unhandled argument: %c\n", opt); } } int num_rest_args = 0; char **rest_args = NULL; if (optind < argc) { num_rest_args = argc - optind; rest_args = malloc((argc - optind) * sizeof(char*)); int i = 0; while (optind < argc) { rest_args[i++] = argv[optind++]; } } if (num_scripts == 0 && main_ns_name == NULL && num_rest_args == 0) { repl = true; } if (main_ns_name != NULL && repl) { printf("Only one main-opt can be specified."); } JSGlobalContextRef ctx = JSGlobalContextCreate(NULL); JSStringRef nameRef = JSStringCreateWithUTF8CString("planck"); JSGlobalContextSetName(ctx, nameRef); evaluate_script(ctx, "var global = this;", "<init>"); register_global_function(ctx, "AMBLY_IMPORT_SCRIPT", function_import_script); bootstrap(ctx, out_path); register_global_function(ctx, "PLANCK_CONSOLE_LOG", function_console_log); register_global_function(ctx, "PLANCK_CONSOLE_ERROR", function_console_error); evaluate_script(ctx, "var console = {};"\ "console.log = PLANCK_CONSOLE_LOG;"\ "console.error = PLANCK_CONSOLE_ERROR;", "<init>"); evaluate_script(ctx, "var PLANCK_VERSION = \"" PLANCK_VERSION "\";", "<init>"); // require app namespaces evaluate_script(ctx, "goog.require('planck.repl');", "<init>"); // without this things won't work evaluate_script(ctx, "var window = global;", "<init>"); register_global_function(ctx, "PLANCK_READ_FILE", function_read_file); register_global_function(ctx, "PLANCK_LOAD", function_load); register_global_function(ctx, "PLANCK_LOAD_DEPS_CLJS_FILES", function_load_deps_cljs_files); register_global_function(ctx, "PLANCK_CACHE", function_cache); register_global_function(ctx, "PLANCK_EVAL", function_eval); register_global_function(ctx, "PLANCK_GET_TERM_SIZE", function_get_term_size); register_global_function(ctx, "PLANCK_PRINT_FN", function_print_fn); register_global_function(ctx, "PLANCK_PRINT_ERR_FN", function_print_err_fn); register_global_function(ctx, "PLANCK_SET_EXIT_VALUE", function_set_exit_value); is_tty = isatty(STDIN_FILENO) == 1; register_global_function(ctx, "PLANCK_RAW_READ_STDIN", function_raw_read_stdin); register_global_function(ctx, "PLANCK_RAW_WRITE_STDOUT", function_raw_write_stdout); register_global_function(ctx, "PLANCK_RAW_FLUSH_STDOUT", function_raw_flush_stdout); register_global_function(ctx, "PLANCK_RAW_WRITE_STDERR", function_raw_write_stderr); register_global_function(ctx, "PLANCK_RAW_FLUSH_STDERR", function_raw_flush_stderr); { JSValueRef arguments[num_rest_args]; for (int i = 0; i < num_rest_args; i++) { arguments[i] = c_string_to_value(ctx, rest_args[i]); } JSValueRef args_ref = JSObjectMakeArray(ctx, num_rest_args, arguments, NULL); JSValueRef global_obj = JSContextGetGlobalObject(ctx); JSStringRef prop = JSStringCreateWithUTF8CString("PLANCK_INITIAL_COMMAND_LINE_ARGS"); JSObjectSetProperty(ctx, JSValueToObject(ctx, global_obj, NULL), prop, args_ref, kJSPropertyAttributeNone, NULL); JSStringRelease(prop); } evaluate_script(ctx, "cljs.core.set_print_fn_BANG_.call(null,PLANCK_PRINT_FN);", "<init>"); evaluate_script(ctx, "cljs.core.set_print_err_fn_BANG_.call(null,PLANCK_PRINT_ERR_FN);", "<init>"); char *elide_script = str_concat("cljs.core._STAR_assert_STAR_ = ", elide_asserts ? "false" : "true"); evaluate_script(ctx, elide_script, "<init>"); free(elide_script); { JSValueRef arguments[4]; arguments[0] = JSValueMakeBoolean(ctx, repl); arguments[1] = JSValueMakeBoolean(ctx, verbose); JSValueRef cache_path_ref = NULL; if (cache_path != NULL) { JSStringRef cache_path_str = JSStringCreateWithUTF8CString(cache_path); cache_path_ref = JSValueMakeString(ctx, cache_path_str); } arguments[2] = cache_path_ref; arguments[3] = JSValueMakeBoolean(ctx, static_fns); JSValueRef ex = NULL; JSObjectCallAsFunction(ctx, get_function(ctx, "planck.repl", "init"), JSContextGetGlobalObject(ctx), 4, arguments, &ex); debug_print_value("planck.repl/init", ctx, ex); } if (repl) { evaluate_source(ctx, "text", "(require '[planck.repl :refer-macros [apropos dir find-doc doc source pst]])", true, false, "cljs.user", "dumb"); } evaluate_script(ctx, "goog.provide('cljs.user');", "<init>"); evaluate_script(ctx, "goog.require('cljs.core');", "<init>"); evaluate_script(ctx, "cljs.core._STAR_assert_STAR_ = true;", "<init>"); // Process init arguments for (int i = 0; i < num_scripts; i++) { // TODO: exit if not successfull evaluate_source(ctx, scripts[i].type, scripts[i].source, scripts[i].expression, false, NULL, theme); } // Process main arguments if (main_ns_name != NULL) { run_main_in_ns(ctx, main_ns_name, num_rest_args, rest_args); } else if (!repl && num_rest_args > 0) { char *path = rest_args[0]; struct script script; if (strcmp(path, "-") == 0) { char *source = read_all(stdin); script.type = "text"; script.source = source; script.expression = false; } else { script.type = "path"; script.source = path; script.expression = false; } evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, theme); } else if (repl) { if (!quiet) { banner(); } char *home = getenv("HOME"); char *history_path = NULL; if (home != NULL) { char history_name[] = ".planck_history"; int len = strlen(home) + strlen(history_name) + 2; history_path = malloc(len * sizeof(char)); snprintf(history_path, len, "%s/%s", home, history_name); linenoiseHistoryLoad(history_path); } char *prompt = javascript ? " > " : " => "; char *line; while ((line = linenoise(prompt)) != NULL) { if (javascript) { JSValueRef res = evaluate_script(ctx, line, "<stdin>"); print_value("", ctx, res); } else { evaluate_source(ctx, "text", line, true, true, "cljs.user", theme); } linenoiseHistoryAdd(line); if (history_path != NULL) { linenoiseHistorySave(history_path); } free(line); } } return exit_value; }
// 利きのある場所への取れない近接王手からの3手詰め Move Position::weak_mate_n_ply(int ply) const { // 1手詰めであるならこれを返す Move m = mate1ply(); if (m) return m; // 詰まない if (ply <= 1) return MOVE_NONE; Color us = side_to_move(); Color them = ~us; Bitboard around8 = kingEffect(king_square(them)); // const剥がし Position* This = ((Position*)this); StateInfo si; StateInfo si2; // 近接王手で味方の利きがあり、敵の利きのない場所を探す。 for (auto m : MoveList<CHECKS>(*this)) { // 近接王手で、この指し手による駒の移動先に敵の駒がない。 Square to = to_sq(m); if ((around8 & to) #ifndef LONG_EFFECT_LIBRARY // toに利きがあるかどうか。mが移動の指し手の場合、mの元の利きを取り除く必要がある。 && (is_drop(m) ? effected_to(us, to) : (attackers_to(us, to, pieces() ^ from_sq(m)) ^ from_sq(m))) // 敵玉の利きは必ずtoにあるのでそれを除いた利きがあるかどうか。 && (attackers_to(them,to,pieces()) ^ king_square(them)) #else && (is_drop(m) ? effected_to(us, to) : board_effect[us].effect(to) >= 2 || (long_effect.directions_of(us, from_sq(m)) & Effect8::directions_of(from_sq(m), to)) != 0) // 敵玉の利きがあるので2つ以上なければそれで良い。 && (board_effect[them].effect(to) <= 1) #endif ) { if (!legal(m)) continue; ASSERT_LV3(gives_check(m)); This->do_move(m,si,true); ASSERT_LV3(in_check()); // この局面ですべてのevasionを試す for (auto m2 : MoveList<EVASIONS>(*this)) { if (!legal(m2)) continue; // この指し手で逆王手になるなら、不詰めとして扱う if (gives_check(m2)) goto NEXT_CHECK; This->do_move(m2, si2, false); ASSERT_LV3(!in_check()); if (!weak_mate_n_ply(ply-2)) { // 詰んでないので、m2で詰みを逃れている。 This->undo_move(m2); goto NEXT_CHECK; } This->undo_move(m2); } // すべて詰んだ This->undo_move(m); // mによって3手で詰む。 return m; NEXT_CHECK:; This->undo_move(m); } } return MOVE_NONE; }