/* This function will freeze the agent's policy and test it after every 25 episodes. */ void offline_demo(){ int i=0; int j=0; evaluation_point_t *this_score=0; evaluation_point_t *statistics[21]; this_score=evaluate_agent(); print_score(0,this_score); statistics[0]=this_score; for(i=0;i<20;i++){ for(j=0;j<25;j++){ RL_episode(0); } this_score=evaluate_agent(); print_score((i+1)*25,this_score); statistics[i+1]=this_score; } save_result_csv(statistics,"results.csv"); for(i=0;i<21;i++){ free(statistics[i]); } }
/*! * @brief ラベルを描画する * @see initialize() */ static void print_labels(void) { mvprintw(0, MY_FIELD_X, "your field"); mvprintw(0, ENEMY_FIELD_X, "enemy_field"); mvprintw(TIME_Y - 1, TIME_X - 2, "time:"); mvprintw(MY_SCORE_Y - 1, MY_SCORE_X - 2, "my score:"); mvprintw(ENEMY_SCORE_Y - 1, ENEMY_SCORE_X - 2, "enemy's score:"); print_score(MY_SCORE_X, MY_SCORE_Y, 0); print_score(ENEMY_SCORE_X, ENEMY_SCORE_Y, 0); mvprintw(ENEMY_SCORE_Y + 2, ENEMY_SCORE_X - 2, "h : move left"); mvprintw(ENEMY_SCORE_Y + 3, ENEMY_SCORE_X - 2, "l : move right"); mvprintw(ENEMY_SCORE_Y + 4, ENEMY_SCORE_X - 2, "j : drop a block"); mvprintw(ENEMY_SCORE_Y + 5, ENEMY_SCORE_X - 2, "a : right-handed rotation"); mvprintw(ENEMY_SCORE_Y + 6, ENEMY_SCORE_X - 2, "s : left-handed rotation"); }
bool checkMap(CSnake * s) { int x, y; unsigned char item; s->GetPos(x, y); item = theMap->GetItem(x, y); switch (item) { case wall: return false; break; case food: theMap->AddItem(x, y, 0); s->Grow(); spawn_random(theMap, s, food); score += 10; print_score(); return true; break; default: return true; break; } }
int main(void) { struct score tmp, *p; LLIST *list; int i; list = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 9; i++) { tmp.id = i; tmp.ch = 100 - i; tmp.math = 100 - i * 2; tmp.en = 100 - i * 3; snprintf(tmp.name, NAMESIZE, "stu%d", i); llist_add(list, &tmp, LLIST_ADD_FORWARD); } llist_travel(list, print_score); llist_delet(list, "stu6", name_cmp); printf("\n"); llist_travel(list, print_score); #if 0 p = llist_find(list, "stuxx", name_cmp); if (p == NULL) { printf("Can not find.\n"); } else { print_score(p); } #endif llist_destroy(list); return 0; }
/*! * @brief フィールドを描画する * @param [in] field 描画するフィールド * @param [in] x どの位置に描画するか */ void show_field(uchar field[STAGE_HEIGHT][STAGE_WIDTH], uint x) { uint i; for (i = 0; i < STAGE_HEIGHT; i++) { uint j; move(i + MY_FIELD_Y, x); for (j = 0; j < STAGE_WIDTH; j++) { switch (field[i][j]) { case SPACE: addch(' '); break; case WALL: addch('x'); break; default: addch('o'); break; } } } //得点表示 print_score(MY_SCORE_X, MY_SCORE_Y, score); move(CURSOR_Y, CURSOR_X); refresh(); //ゲームオーバーの場合は GAME OVER を表示 /* if (gameover) { clear(); mvprintw(RESULT_STR_Y, RESULT_STR_X, "You lose!"); // printf("\n\n\n\n\nGAME OVER\n\n"); } */ }
void fsm_can(can_message_t *msg){ switch (msg->id) { case MENU_ID: g_state = msg->data[0]; break; case GAME_ID: break; case SCORE_ID: if(msg->data[0]){ add_score(); print_score(); } else{ g_state = STATE_GAME_OVER; oled_print_game_over(); play_melody(0); } break; case CONTROLLER_ID: break; case SLIDER_ID: if(g_state == STATE_CALIBRATE){ oled_pos(3,8); fprintf(FONT5,"Left: %d %% \nRight: %d %% ", msg->data[0], msg->data[1]); } break; default: break; } }
int main(int argc, char **argv) { struct arguments arguments; argp_parse(&argp, argc, argv, 0, 0, &arguments); char *file = arguments.args[0]; sqlite3 *db; int rc = 0; sqlite3_initialize(); rc = sqlite3_open(file, &db); if(rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); exit(0); } else { fprintf(stderr, "Opened database successfully\n"); } int wk = atoi(arguments.args[1]); print_score(db, wk); sqlite3_close(db); exit(0); }
static void new_game(uint8_t controller){ g_state = STATE_GAME; g_game_controller = controller; send_controller(g_game_controller); play_melody(g_game_melody); reset_score(); print_score(); send_state(g_state); }
void autoget(int *x, int *y) { think(); print_score(); getmax(); *x = maxpoint.row; *y = maxpoint.col; printf("Player 2 : %d %d\n", *x, *y); }
int main(int argc, char *argv[]) { int x = 0; int y = 0; SDL_Init(SDL_INIT_VIDEO); SDL_WM_SetCaption("Nibbler OpenGL",NULL); SDL_SetVideoMode(800, 480, 32, SDL_OPENGL); glMatrixMode(GL_PROJECTION); glOrtho(0, 51, 0, 51, 0, 51); SDL_Event event; SDL_EnableKeyRepeat(10,10); while (SDL_WaitEvent(&event)) { if(event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_ESCAPE: exit (0); break; case SDLK_UP: if (y < 48) y++; break; case SDLK_DOWN: if (y > 2) y--; break; case SDLK_LEFT: if (x > 1) x--; break; case SDLK_RIGHT: if (x < 35) x++; break; } glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT); } print_map(51, 51); print_snack(x, y); print_apple(15, 21); print_score(1252, 15, 5, 0); SDL_GL_SwapBuffers(); } return 0; }
/*! * @brief ゲームのメインループ * @param [in] sock 送信先ソケットのディスクリプタ */ int play_tetris(int sock) { static const uchar FLAG = GAMEOVER; /* ゲームオーバ時に送信する値 */ uint cnt = 1; /* カウンタ */ time_t base_time = time(NULL); /* ゲーム開始時刻を記憶 */ initialize(sock); /* 初期化 */ while (!gameover) { /* ゲームオーバーになるまでゲーム続行 */ static uchar enemy_field[STAGE_HEIGHT][STAGE_WIDTH]; /* 相手のフィールドデータを格納する2次元配列 */ time_t game_time; /* ゲームを開始してから、何秒経過したかを保持する */ /* キー入力があればそれに応じて操作 */ control_block(); /* 32回ループをしたら、ブロックを1マス落とす */ if ((cnt = (cnt + 1) % 32) == 0) { drop_block(); } write(sock, field, sizeof(field)); /* 自分のフィールドデータを送信 */ write(sock, &score, sizeof(score)); /* 自分のスコアを送信 */ read(sock, enemy_field, sizeof(enemy_field)); /* 相手のフィールドデータを受信 */ read(sock, &enemy_score, sizeof(enemy_score)); /* 相手のスコアを受信 */ show_field(enemy_field, ENEMY_FIELD_X); /* 相手のフィールドを描画する */ if (read_protocol(enemy_field[0][0])) { /* 特別な値を受信していないかチェックする */ sleep(1); return EXIT_SUCCESS; /* ゲーム終了 */ } print_score(ENEMY_SCORE_X, ENEMY_SCORE_Y, enemy_score); /* 相手のスコアを描画 */ if ((game_time = GAME_TIME - (time(NULL) - base_time)) == 0) { timeup(sock); sleep(1); return EXIT_SUCCESS; /* ゲーム終了 */ } print_time(game_time); /* ゲーム時間を表示する */ usleep(20000); /* 20000マイクロ秒停止する(この間、CPUに負荷をかけない) */ } write(sock, &FLAG, sizeof(FLAG)); /* 自分がゲームオーバになったことを相手に知らせる */ clear(); mvprintw(RESULT_STR_Y, RESULT_STR_X, "You lose!"); refresh(); sleep(2); endwin(); /* curses終了 */ puts("\nYou lose!"); sleep(1); return EXIT_SUCCESS; }
int app_cvss_score(const struct oscap_action *action) { assert(action->cvss_vector); bool ok = false; struct cvss_impact *impact = cvss_impact_new_from_vector(action->cvss_vector); if (impact == NULL) goto err; ok |= print_score("base", cvss_impact_base_score(impact)); ok |= print_score("temporal", cvss_impact_temporal_score(impact)); ok |= print_score("environmental", cvss_impact_environmental_score(impact)); if (!ok) goto err; cvss_impact_free(impact); return OSCAP_OK; err: cvss_impact_free(impact); fprintf(stderr, "Invalid input CVSS vector\n"); return OSCAP_ERROR; }
void menu(void) { srand(time(NULL)); Window win; clear(); start_color(); init_pair(1,COLOR_BLUE,COLOR_BLACK); init_pair(2,COLOR_RED,COLOR_BLACK); attron(COLOR_PAIR(1)); print_score(win); print_logo(win); print_menu(win); attroff(COLOR_PAIR(1)); refresh(); }
void show_bestscore() { char buf[100]; sprintf(buf, "score/%d", getpid() % TEAM); if (access(buf, F_OK) == -1) { myprintf("Something error!\n"); exit(1); } FILE *f = fopen(buf, "r"); bzero(buf, 100); fgets(buf, 100, f); fclose(f); print_score(buf); }
void ju_eat_food() { if(head->x==food.cor.x&&head->y==food.cor.y) { food.flag=1; pt=(struct Snake*)malloc(LEN); pt->x=head->x; pt->y=head->y; pt->previous=NULL; pt->next=head; head->previous=pt; head=pt; score=score+5; print_score(); } else food.flag=0; }
int main(void) { LLIST *llist; struct score tmp, *datap; int i; int id; struct sum_st sum = {0,0}; llist = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 7; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "stu%d", i); tmp.ch = 100 - i; tmp.math = 100 - 2 * i; llist_insert(llist, &tmp); /* if error */ } llist_travel(llist, print_score, NULL); printf("\n"); #if 1 id = 3; llist_delet(llist, &id, id_cmp); llist_travel(llist, print_score, NULL); #else datap = llist_find(llist, &id, id_cmp); if (datap == NULL) { printf("Can not find.\n"); } else { print_score(datap, NULL); } llist_travel(llist, get_sum, &sum); printf("sum: ch = %d, math = %d\n", sum.ch, sum.math); #endif llist_destroy(llist); return 0; }
void round_two(struct player *players, int playerc) { fprintf(stdout, "+--------------------------------------------------+\n"); fprintf(stdout, "| Round Two |\n"); fprintf(stdout, "+--------------------------------------------------+\n"); int input; int i, j; for (i = 0; i < playerc; i++) { fprintf(stdout, "%s's turn.\n", players[i].name); for (j = 0; j < AMMO; j++) { fprintf(stdout, "Shot #%d: ", j + 1); fflush(stdout); input = get_int_from_stdin(); players[i].score += (input * 2); } print_score(players[i]); } print_result(players, playerc); shuffle_winners(players, playerc); }
void round_three(struct player *players, int playerc) { fprintf(stdout, "+--------------------------------------------------+\n"); fprintf(stdout, "| Round Three |\n"); fprintf(stdout, "+--------------------------------------------------+\n"); int input; int i, j; for (i = 0; i < playerc; i++) { fprintf(stdout, "%s's turn.\n", players[i].name); for (j = 0; j < AMMO; j++) { fprintf(stdout, "Shot #%d: ", j + 1); fflush(stdout); input = get_int_from_stdin(); if (input == 5) { players[i].score += 5; } else { players[i].score += (input * 3); } } print_score(players[i]); } }
void ju_meet_mine() { if(head->x==mine.cor.x&&head->y==mine.cor.y) { mine.flag=1; int i; if(node==1) gameover(); mid=node/2; for(i=1;i<=mid;i++) { setcolor(BLACK); setfillcolor(BLACK); bar(tail->x,tail->y,tail->x+20,tail->y+20); tail=tail->previous; } tail->next=NULL; score=score-mid*5; print_score(); } else mine.flag=0; }
int sokoban_game::start_game() { int iScore = 0; int iMoves = 0; iTotalMoves = 0; int iDirY, iDirX; const int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX - FULL_SCREEN_WIDTH) / 2 : 0; const int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY - FULL_SCREEN_HEIGHT) / 2 : 0; parse_level(); WINDOW *w_sokoban = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX); draw_border(w_sokoban); center_print(w_sokoban, 0, hilite(c_white), _("Sokoban")); std::vector<std::string> shortcuts; shortcuts.push_back(_("<+> next")); // '+': next shortcuts.push_back(_("<-> prev")); // '-': prev shortcuts.push_back(_("<r>eset")); // 'r': reset shortcuts.push_back(_("<q>uit")); // 'q': quit shortcuts.push_back(_("<u>ndo move")); // 'u': undo move int indent = 10; for (size_t i = 0; i < shortcuts.size(); i++) { indent = std::max(indent, utf8_width(shortcuts[i].c_str()) + 1); } indent = std::min(indent, 30); for (size_t i = 0; i < shortcuts.size(); i++) { shortcut_print(w_sokoban, i + 1, FULL_SCREEN_WIDTH - indent, c_white, c_ltgreen, shortcuts[i].c_str()); } int input = '.'; int iPlayerY = 0; int iPlayerX = 0; bool bNewLevel = true; bool bMoved = false; do { if (bNewLevel) { bNewLevel = false; iMoves = 0; vUndo.clear(); iPlayerY = mLevelInfo[iCurrentLevel]["PlayerY"]; iPlayerX = mLevelInfo[iCurrentLevel]["PlayerX"]; mLevel = vLevel[iCurrentLevel]; } print_score(w_sokoban, iScore, iMoves); if (check_win()) { //we won yay if (!mAlreadyWon[iCurrentLevel]) { iScore += 500; mAlreadyWon[iCurrentLevel] = true; } input = '+'; } else { draw_level(w_sokoban); wrefresh(w_sokoban); //Check input input = getch(); } bMoved = false; switch (input) { case KEY_UP: /* up */ iDirY = -1; iDirX = 0; bMoved = true; break; case KEY_DOWN: /* down */ iDirY = 1; iDirX = 0; bMoved = true; break; case KEY_LEFT: /* left */ iDirY = 0; iDirX = -1; bMoved = true; break; case KEY_RIGHT: /* right */ iDirY = 0; iDirX = 1; bMoved = true; break; case 'q': return iScore; break; case 'u': { int iPlayerYNew = 0; int iPlayerXNew = 0; bool bUndoSkip = false; //undo move if (vUndo.size() > 0) { //reset last player pos mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; iPlayerYNew = vUndo[vUndo.size() - 1].iOldY; iPlayerXNew = vUndo[vUndo.size() - 1].iOldX; mLevel[iPlayerYNew][iPlayerXNew] = vUndo[vUndo.size() - 1].sTileOld; vUndo.pop_back(); bUndoSkip = true; } if (bUndoSkip && vUndo.size() > 0) { iDirY = vUndo[vUndo.size() - 1].iOldY; iDirX = vUndo[vUndo.size() - 1].iOldX; if (vUndo[vUndo.size() - 1].sTileOld == "$" || vUndo[vUndo.size() - 1].sTileOld == "*") { mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == ".") ? "*" : "$"; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (mLevel[iPlayerY + iDirY][iPlayerX + iDirX] == "*") ? "." : " "; vUndo.pop_back(); } } if (bUndoSkip) { iPlayerY = iPlayerYNew; iPlayerX = iPlayerXNew; } } break; case 'r': //reset level bNewLevel = true; break; case '+': //next level clear_level(w_sokoban); iCurrentLevel++; if (iCurrentLevel >= iNumLevel) { iCurrentLevel = 0; } bNewLevel = true; break; case '-': //prev level clear_level(w_sokoban); iCurrentLevel--; if (iCurrentLevel < 0) { iCurrentLevel = iNumLevel - 1; } bNewLevel = true; break; default: break; } if (bMoved) { //check if we can move the player std::string sMoveTo = mLevel[iPlayerY + iDirY][iPlayerX + iDirX]; bool bMovePlayer = false; if (sMoveTo != "#") { if (sMoveTo == "$" || sMoveTo == "*") { //Check if we can move the package std::string sMovePackTo = mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)]; if (sMovePackTo == "." || sMovePackTo == " ") { //move both bMovePlayer = true; mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)] = (sMovePackTo == ".") ? "*" : "$"; vUndo.push_back(cUndo(iDirY, iDirX, sMoveTo)); iMoves--; } } else { bMovePlayer = true; } if (bMovePlayer) { //move player vUndo.push_back(cUndo(iPlayerY, iPlayerX, mLevel[iPlayerY][iPlayerX])); mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (sMoveTo == "." || sMoveTo == "*") ? "+" : "@"; iPlayerY += iDirY; iPlayerX += iDirX; iMoves++; iTotalMoves++; } } } } while (true); return iScore; }
int sokoban_game::start_game() { int iScore = 0; int iMoves = 0; iTotalMoves = 0; int iDirY, iDirX; const int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX - FULL_SCREEN_WIDTH) / 2 : 0; const int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY - FULL_SCREEN_HEIGHT) / 2 : 0; parse_level(); WINDOW *w_sokoban = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX); draw_border(w_sokoban); center_print(w_sokoban, 0, hilite(c_white), _("Sokoban")); input_context ctxt("SOKOBAN"); ctxt.register_cardinal(); ctxt.register_action("NEXT"); ctxt.register_action("PREV"); ctxt.register_action("RESET"); ctxt.register_action("QUIT"); ctxt.register_action("UNDO"); ctxt.register_action("HELP_KEYBINDINGS"); std::vector<std::string> shortcuts; shortcuts.push_back(_("<+> next")); // '+': next shortcuts.push_back(_("<-> prev")); // '-': prev shortcuts.push_back(_("<r>eset")); // 'r': reset shortcuts.push_back(_("<q>uit")); // 'q': quit shortcuts.push_back(_("<u>ndo move")); // 'u': undo move int indent = 10; for (size_t i = 0; i < shortcuts.size(); i++) { indent = std::max(indent, utf8_width(shortcuts[i].c_str()) + 1); } indent = std::min(indent, 30); for (size_t i = 0; i < shortcuts.size(); i++) { shortcut_print(w_sokoban, i + 1, FULL_SCREEN_WIDTH - indent, c_white, c_ltgreen, shortcuts[i]); } int iPlayerY = 0; int iPlayerX = 0; bool bNewLevel = true; bool bMoved = false; do { if (bNewLevel) { bNewLevel = false; iMoves = 0; vUndo.clear(); iPlayerY = mLevelInfo[iCurrentLevel]["PlayerY"]; iPlayerX = mLevelInfo[iCurrentLevel]["PlayerX"]; mLevel = vLevel[iCurrentLevel]; } print_score(w_sokoban, iScore, iMoves); std::string action; if (check_win()) { //we won yay if (!mAlreadyWon[iCurrentLevel]) { iScore += 500; mAlreadyWon[iCurrentLevel] = true; } action = "NEXT"; } else { draw_level(w_sokoban); wrefresh(w_sokoban); //Check input action = ctxt.handle_input(); } bMoved = false; if (ctxt.get_direction(iDirX, iDirY, action)) { bMoved = true; } else if (action == "QUIT") { return iScore; } else if (action == "UNDO") { int iPlayerYNew = 0; int iPlayerXNew = 0; bool bUndoSkip = false; //undo move if (!vUndo.empty()) { //reset last player pos mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; iPlayerYNew = vUndo[vUndo.size() - 1].iOldY; iPlayerXNew = vUndo[vUndo.size() - 1].iOldX; mLevel[iPlayerYNew][iPlayerXNew] = vUndo[vUndo.size() - 1].sTileOld; vUndo.pop_back(); bUndoSkip = true; } if (bUndoSkip && !vUndo.empty()) { iDirY = vUndo[vUndo.size() - 1].iOldY; iDirX = vUndo[vUndo.size() - 1].iOldX; if (vUndo[vUndo.size() - 1].sTileOld == "$" || vUndo[vUndo.size() - 1].sTileOld == "*") { mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == ".") ? "*" : "$"; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (mLevel[iPlayerY + iDirY][iPlayerX + iDirX] == "*") ? "." : " "; vUndo.pop_back(); } } if (bUndoSkip) { iPlayerY = iPlayerYNew; iPlayerX = iPlayerXNew; } } else if (action == "RESET") { //reset level bNewLevel = true; } else if (action == "NEXT") { //next level clear_level(w_sokoban); iCurrentLevel++; if (iCurrentLevel >= iNumLevel) { iCurrentLevel = 0; } bNewLevel = true; } else if (action == "PREV") { //prev level clear_level(w_sokoban); iCurrentLevel--; if (iCurrentLevel < 0) { iCurrentLevel = iNumLevel - 1; } bNewLevel = true; } if (bMoved) { //check if we can move the player std::string sMoveTo = mLevel[iPlayerY + iDirY][iPlayerX + iDirX]; bool bMovePlayer = false; if (sMoveTo != "#") { if (sMoveTo == "$" || sMoveTo == "*") { //Check if we can move the package std::string sMovePackTo = mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)]; if (sMovePackTo == "." || sMovePackTo == " ") { //move both bMovePlayer = true; mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)] = (sMovePackTo == ".") ? "*" : "$"; vUndo.push_back(cUndo(iDirY, iDirX, sMoveTo)); iMoves--; } } else { bMovePlayer = true; } if (bMovePlayer) { //move player vUndo.push_back(cUndo(iPlayerY, iPlayerX, mLevel[iPlayerY][iPlayerX])); mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (sMoveTo == "." || sMoveTo == "*") ? "+" : "@"; iPlayerY += iDirY; iPlayerX += iDirX; iMoves++; iTotalMoves++; } } } } while (true); return iScore; }
static void exec_action(int op,int arg1,int arg2) { int i, j; char *tmpstr; switch(op) { case 1000:goto_room(arg1-first_room);break; case 1001:goto_room(agt_rand(arg1,arg2)-first_room);break; case 1002:agt_var[arg1]=loc+first_room;break; case 1003:agt_var[arg1]=dobj;break; case 1004:agt_var[arg1]=iobj;break; case 1005:goto_room(agt_var[arg1]-first_room);break; case 1006:it_move(arg1,agt_var[arg2]);break; case 1007:get_obj(agt_var[arg1]);break; case 1008:msgout(agt_var[arg1],1);break; case 1009:get_obj(arg1);break; case 1010:get_obj(arg1);it_move(arg1,1000);break; case 1011:drop_obj(arg1);break; case 1012: if (it_loc(arg1)==1000) { if (PURE_WEAR) drop_obj(arg1); else it_move(arg1,1); } break; case 1013:fontcmd(0,arg1-1);break; /* Load font */ case 1014:pictcmd(1,pictable[arg1-1]);break; /* Show picture */ case 1015:changepict(arg1,arg2);break; /* ChangePicture */ case 1016: if (PICT_SUPPORT && yesno("Would you like to see the picture?")) pictcmd(1,pictable[arg1-1]);break; case 1017:pictcmd(2,arg1);break; /* Show room pix */ case 1018: if (PICT_SUPPORT && yesno("Would you like to see the picture?")) pictcmd(2,arg1-1);break; case 1019:musiccmd(1,arg1-1);break; case 1020:musiccmd(1,agt_rand(arg1,arg2)-1);break; case 1021:musiccmd(2,arg1-1);break; case 1022:musiccmd(3,-1); break; /* Stop Repeat */ case 1023:musiccmd(4,-1); break; /* Stop song */ case 1024:musiccmd(5,-1); break; /* Suspend song */ case 1025:musiccmd(6,-1);break; /* Resume song */ case 1026: if (tnoun(dobj)) noun[dobj-first_noun].movable=!noun[dobj-first_noun].movable; break; case 1027: it_newdesc(arg1,&msg_ptr[arg2-1]); break; case 1028: if (tnoun(arg1)) noun[arg1-first_noun].points=arg2; else if (tcreat(arg1)) creature[arg1-first_creat].points=arg2; else if (troom(arg1)) room[arg1-first_room].points=arg2; break; case 1029:it_destroy(iobj);break; case 1030: tmpstr=agt_readline(3); i=strlen(tmpstr)-1; if (i>0 && tmpstr[i]=='\n') tmpstr[i]=0; strncpy(userstr[arg1-1],tmpstr,80); rfree(tmpstr); break; case 1031:agt_var[arg1]=read_number();break; case 1032:agt_var[arg1]=curr_time;break; case 1033:curr_time=normalize_time(agt_var[arg1]);break; case 1034:curr_time=normalize_time(arg1); break; case 1035:add_time(arg1);break; case 1036:delta_time=arg1;break; /* 1037 and 1038 are subroutine commands */ case 1039:get_obj(dobj);break; case 1040:it_move(dobj,1000);break; case 1041:drop_obj(dobj);break; case 1042: if (it_loc(dobj)==1000) { if (PURE_WEAR) it_move(dobj,1); else drop_obj(dobj); } break; case 1043: /* drop all */ safecontloop(i,j,1) drop_obj(i); break; case 1044: /* remove all */ safecontloop(i,j,1000) drop_obj(i); break; case 1045:deadflag=1;break; case 1046:it_move(arg1,loc+first_room);break; case 1047:it_move(arg1,arg2);break; case 1048:it_reposition(arg1,arg2,1);break; /* RePosition */ case 1049:it_move(dobj,loc+first_room);break; case 1050:it_move(dobj,arg1);break; case 1051: safecontloop(i,j,1) it_move(i,arg1); safecontloop(i,j,1000) it_move(i,arg1); break; case 1052: nounloop(i) if (player_has(i+first_noun) && noun[i].points>arg2) it_move(i+first_noun,arg1); break; case 1053: safecontloop(i,j,arg1) if (tnoun(i)) it_move(i,arg2); break; case 1054:it_destroy(arg1);break; case 1055:it_destroy(dobj);break; case 1056:i=it_loc(arg1); it_move(arg1,it_loc(arg2)); it_move(arg2,i); break; case 1057:it_move(arg1,it_loc(arg2));break; case 1058:it_move(dobj,it_loc(arg2));break; case 1059: case 1060: /* Add to/remove from group */ if (tcreat(arg1)) creature[arg1-first_creat].groupmemb=(op==1059); break; case 1061: /* Move group */ safecontloop(i,j,loc+first_room) if (it_group(i)) it_move(i,arg1); break; /* 1062 is RedirectTo */ case 1063:msgout(agt_rand(arg1,arg2),1);break; case 1064:print_contents(arg1,1);break; case 1065: case 1066: case 1067: case 1068: obj_act(op-1065,arg1);break; case 1069: case 1070: case 1071: case 1072: obj_act(op-1069,dobj);break; case 1073:print_score();break; case 1074: tscore+=arg1;break; case 1075: tscore-=arg1;break; case 1076:v_inventory();break; case 1077:wait_return();break; case 1078:writeln("Time passes...");break; case 1079:agt_delay(arg1);break; case 1080:agt_clrscr();break; case 1081:it_describe(arg1);break; case 1082:look_room();break; /* LOOK */ case 1083:msgout(arg1,1);break; case 1084:writeln("");break; case 1085:if (PURE_TONE && sound_on) agt_tone(arg1,arg2);break; /* Tone */ case 1086:agt_number=ask_for_number(arg1,arg2);break; case 1087:agt_answer=ask_question(arg1);break; case 1088:change_passage(loc,arg1,arg2);break; case 1089:flag[arg1]=1;break; case 1090:flag[arg1]=0;break; case 1091:flag[arg1]=!flag[arg1];break; case 1092:room[loc].flag_noun_bits|=(1 << (arg1-1) ); break; /* Roomflag on */ case 1093:room[loc].flag_noun_bits&=~(1 << (arg1-1) );break; /* Off */ case 1094:room[loc].flag_noun_bits^=(1 << (arg1-1) );break; /* Toggle */ case 1095: /* if (agt_counter[arg1]==-1)*/ agt_counter[arg1]=1;break; case 1096:agt_counter[arg1]=-1;break; case 1097:agt_var[arg1]=arg2;break; case 1098:agt_var[arg1]+=arg2;break; case 1099:agt_var[arg1]-=arg2;break; case 1100:agt_var[arg1]+=agt_var[arg2];break; case 1101:agt_var[arg1]-=agt_var[arg2];break; case 1102:agt_var[arg1]=agt_rand(0,arg2);break; case 1103:agt_var[arg1]=dobj_rec->num;break; case 1104:agt_var[arg1]=iobj_rec->num;break; /* The following are v1.8x specific */ case 1105:quote(arg1);break; case 1106:add_time(arg1);break; case 1107:add_time(-arg1);break; case 1108:curr_time=(curr_time%100)+100*arg1;break; case 1109:curr_time=(curr_time/100)*100+arg1;break; case 1110:add_time(agt_var[arg1]);break; case 1111:add_time(-agt_var[arg1]);break; case 1112:curr_time=(curr_time%100)+100*agt_var[arg1];break; case 1113:curr_time=(curr_time/100)*100+agt_var[arg1];break; /* Now for the AGX additions */ case 1114:add_time(-arg1);break; /* ME-style SubtractFromTime */ case 1115: disambig_score=arg1; break; /* SetDisambigPriority */ case 1116:agt_var[arg1]=delta_time;break; case 1117: /* ChangeStatus */ statusmode=arg1; break; case 1118: if (!mult_rangecheck(agt_var[arg1],arg2)) break; agt_var[arg1]*=arg2;break; case 1119: if (arg2==0) { if (!PURE_ERROR) writeln("GAME ERROR: Division by zero."); } else agt_var[arg1]/=arg2; break; case 1120: if (arg2==0) { if (!PURE_ERROR) writeln("GAME ERROR: Attempt to divide by zero."); } else agt_var[arg1]%=arg2; break; case 1121:agt_waitkey();break; case 1122:last_he=arg1;break; /* SetHE */ case 1123:last_she=arg1;break; case 1124:last_it=arg1;break; case 1125:last_they=arg1;break; case 1126:msgout(arg1,0);break; case 1127: if (!PURE_ERROR) sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1128: msgout(arg1,1);break; /* FailMessage */ case 1129: /* StdMessage */ sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1130: msgout(arg2,1);break; /* ErrMessage */ case 1131: /* StdErrMessage */ sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1132: /* AND */ break; /* These don't do anything under normal circumstances */ case 1133: /* SetClass */ if (troom(arg1)) room[arg1-first_room].oclass=arg2; else if (tnoun(arg1)) noun[arg1-first_noun].oclass=arg2; else if (tcreat(arg1)) noun[arg1-first_creat].oclass=arg2; break; case 1134: agt_var[arg1]=it_class(arg2); break; /* SetVariableToClass */ /* Stack commands */ case 1135: push_stack(arg1); break; case 1136: agt_var[arg1]=pop_stack(); break; case 1137: case 1138: case 1139: case 1140: case 1141: op_stack(op-1137); /* +,-,*, /,% * */ break; case 1142: { /* DupStack */ long n; n=pop_stack(); push_stack(n); push_stack(n); break; } case 1143: pop_stack();break; /* Discard TOS */ case 1144: agt_var[arg1]=agt_number;break; /* SetVariableToInput */ case 1145: setattr(arg1,arg2,1); break; /* Set */ case 1146: setattr(arg1,arg2,0); break; /* Clear */ case 1147: push_stack(getprop(arg1,arg2));break; /* PushProp */ case 1148: setprop(arg1,arg2,pop_stack());break; /* PopProp */ /* 1149, 1150 handled by run_metacommand */ /* 1151 is EndDisambig */ /* 1152 is XRedirect */ case 1153: rstrncpy(userstr[arg1-1],userstr[arg2-1],81);break; case 1154: setcase(userstr[arg1-1],1); break; case 1155: setcase(userstr[arg1-1],0);break; case 1156: op_objflag(1,arg1,arg2);break; case 1157: op_objflag(0,arg1,arg2);break; case 1158: op_objflag(3,arg1,arg2);break; case 1159: push_stack(op_objprop(2,arg1,arg2,0)); break; case 1160: op_objprop(1,arg1,arg2,pop_stack()); break; case 1161: move_in_dir(arg1,arg2); break; default: writeln("INTERNAL ERROR: Action token not supported."); rprintf("Action #%d",op); writeln(""); } }
int snake_game::start_game() { std::vector<std::pair<int, int> > vSnakeBody; std::map<int, std::map<int, bool> > mSnakeBody; int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX - FULL_SCREEN_WIDTH) / 2 : 0; int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY - FULL_SCREEN_HEIGHT) / 2 : 0; WINDOW *w_snake = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX); print_header(w_snake); //Snake start position vSnakeBody.push_back(std::make_pair(FULL_SCREEN_HEIGHT / 2, FULL_SCREEN_WIDTH / 2)); mSnakeBody[FULL_SCREEN_HEIGHT / 2][FULL_SCREEN_WIDTH / 2] = true; mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 1].first, vSnakeBody.back().second, c_white, '#'); //Snake start direction int iDirY = 0; int iDirX = 1; //Snake start length size_t iSnakeBody = 10; //GameSpeed aka inputdelay/timeout int iGameSpeed = 100; //Score int iScore = 0; int iFruitPosY = 0; int iFruitPosX = 0; //Draw Score print_score(w_snake, iScore); long ch; InputEvent input; do { //Check if we hit a border if (vSnakeBody[vSnakeBody.size() - 1].first + iDirY == 0) { vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY + FULL_SCREEN_HEIGHT - 2, vSnakeBody[vSnakeBody.size() - 1].second + iDirX)); } else if (vSnakeBody[vSnakeBody.size() - 1].first + iDirY == FULL_SCREEN_HEIGHT - 1) { vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY - FULL_SCREEN_HEIGHT + 2, vSnakeBody[vSnakeBody.size() - 1].second + iDirX)); } else if (vSnakeBody[vSnakeBody.size() - 1].second + iDirX == 0) { vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY, vSnakeBody[vSnakeBody.size() - 1].second + iDirX + FULL_SCREEN_WIDTH - 2)); } else if (vSnakeBody[vSnakeBody.size() - 1].second + iDirX == FULL_SCREEN_WIDTH - 1) { vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY, vSnakeBody[vSnakeBody.size() - 1].second + iDirX - FULL_SCREEN_WIDTH + 2)); } else { vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY, vSnakeBody[vSnakeBody.size() - 1].second + iDirX)); } //Check if we hit ourself if (mSnakeBody[vSnakeBody[vSnakeBody.size() - 1].first][vSnakeBody[vSnakeBody.size() - 1].second]) { //We are dead :( break; } else { //Add new position to map mSnakeBody[vSnakeBody[vSnakeBody.size() - 1].first][vSnakeBody[vSnakeBody.size() - 1].second] = true; } //Have we eaten the forbidden fruit? if (vSnakeBody[vSnakeBody.size() - 1].first == iFruitPosY && vSnakeBody[vSnakeBody.size() - 1].second == iFruitPosX) { iScore += 500; iSnakeBody += 10; iGameSpeed -= 3; print_score(w_snake, iScore); iFruitPosY = 0; iFruitPosX = 0; } //Check if we are longer than our max size if (vSnakeBody.size() > iSnakeBody) { mSnakeBody[vSnakeBody[0].first][vSnakeBody[0].second] = false; mvwputch(w_snake, vSnakeBody[0].first, vSnakeBody[0].second, c_black, ' '); vSnakeBody.erase(vSnakeBody.begin(), vSnakeBody.begin() + 1); } //Draw Snake mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 1].first, vSnakeBody[vSnakeBody.size() - 1].second, c_white, '#'); mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 2].first, vSnakeBody[vSnakeBody.size() - 2].second, c_ltgray, '#'); //On full length add a fruit if (iFruitPosY == 0 && iFruitPosY == 0) { do { iFruitPosY = rng(1, FULL_SCREEN_HEIGHT - 2); iFruitPosX = rng(1, FULL_SCREEN_WIDTH - 2); } while (mSnakeBody[iFruitPosY][iFruitPosX]); mvwputch(w_snake, iFruitPosY, iFruitPosX, c_ltred, '*'); } wrefresh(w_snake); //Check input timeout(iGameSpeed); ch = getch(); input = get_input(ch); timeout(-1); switch (input) { case DirectionN: /* up */ if (iDirY != 1) { iDirY = -1; iDirX = 0; } break; case DirectionS: /* down */ if (iDirY != -1) { iDirY = 1; iDirX = 0; } break; case DirectionW: /* left */ if (iDirX != 1) { iDirY = 0; iDirX = -1; } break; case DirectionE: /* right */ if (iDirX != -1) { iDirY = 0; iDirX = 1; } break; case Cancel: return iScore; break; default: break; } } while (true); snake_over(w_snake, iScore); return iScore; }
/** * Just do a single evaluate_agent and print it **/ void single_evaluation(){ evaluation_point_t *this_score=0; this_score=evaluate_agent(); print_score(0,this_score); free(this_score); }
void handle_packet(struct packet *p){ switch(p->type){ case PKT_ANNOUNCE: if(mode == MODE_CLIENT){ if(!started){ gameid = p->gameid; struct packet join; memset((void*)&join, 0, sizeof(join)); join.gameid = gameid; join.type = PKT_JOIN; memcpy(join.c.join.nick, GLOBAL(nickname), 16); join.seq = p->seq+1; nrf_snd_pkt_crc(sizeof(join), (uint8_t *)&join); player_joined = 1; } } break; case PKT_JOIN: if(mode == MODE_SERVER){ if(!started){ if(!player_joined){ started = 1; lcdClear(); lcdPrintln("Player joined"); lcdPrintln((char *)&p->c.join.nick); lcdRefresh(); struct packet ack; memset((void*)&ack, 0, sizeof(ack)); ack.gameid = gameid; ack.type = PKT_ACK; //memcpy(ack.c.ack.nick, GLOBAL(nickname), 16); ack.seq = p->seq+1; nrf_snd_pkt_crc(sizeof(ack), (uint8_t *)&ack); player_joined = 1; delayms(1000); init(); struct packet state; memset((void*)&state, 0, sizeof(state)); state.gameid = gameid; state.type = PKT_GAMESTATE; state.c.state.score1 = player1.score; state.c.state.score2 = player2.score; state.c.state.pad1 = player1.paddle_pos; state.c.state.pad2 = player2.paddle_pos; state.c.state.ball_x = ball1.x; state.c.state.ball_y = ball1.y; state.seq = p->seq+2; nrf_snd_pkt_crc(sizeof(state), (uint8_t *)&state); } } } break; case PKT_ACK: if(mode == MODE_CLIENT){ if(!started){ started = 1; lcdClear(); lcdPrintln("Joined!"); //lcdPrintln((char *)&p->c.announce.nick); init(); lcdRefresh(); } } break; case PKT_GAMESTATE: if(mode == MODE_CLIENT){ if(started){ player1.score = p->c.state.score1; player2.score = p->c.state.score2; player1.paddle_pos = p->c.state.pad1; player2.paddle_pos = p->c.state.pad2; ball1.x = p->c.state.ball_x; ball1.y = p->c.state.ball_y; if(!check_end()){ lcdClear(); print_score(); draw_paddles(); just_draw_ball(); lcdDisplay(); struct packet move; memset((void*)&move, 0, sizeof(move)); move.gameid = gameid; move.type = PKT_MOVEMENT; move.c.movement.dir = next_move; move.seq = p->seq+1; delayms(50); nrf_snd_pkt_crc(sizeof(move), (uint8_t *)&move); latest_packet = move; next_move = DIR_STAY; receive = 1; } } } break; case PKT_MOVEMENT: if(mode == MODE_SERVER){ if(started){ if(next_move != DIR_LEFT) // Only let valid keys through move_player(&player1, next_move); if(p->c.movement.dir != DIR_RIGHT) // Only let valid keys through move_player(&player2, p->c.movement.dir); check_collision_players(); draw_ball(); lcdClear(); print_score(); //check_collision_player(&player1); //check_collision_player(&player2); draw_paddles(); just_draw_ball(); lcdDisplay(); struct packet state; memset((void*)&state, 0, sizeof(state)); state.gameid = gameid; state.type = PKT_GAMESTATE; state.c.state.score1 = player1.score; state.c.state.score2 = player2.score; state.c.state.pad1 = player1.paddle_pos; state.c.state.pad2 = player2.paddle_pos; state.c.state.ball_x = ball1.x; state.c.state.ball_y = ball1.y; state.seq = p->seq+1; delayms(50); nrf_snd_pkt_crc(sizeof(state), (uint8_t *)&state); latest_packet = state; next_move = DIR_STAY; receive = 1; check_end(); } } break; case PKT_EXIT: if(started){ if(mode == MODE_CLIENT) player2.score = MAX_POINTS; else player1.score = MAX_POINTS; check_end(); } break; } }
int sokoban_game::start_game() { int iScore = 0; int iMoves = 0; iTotalMoves = 0; int iDirY, iDirX; const int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX-FULL_SCREEN_WIDTH)/2 : 0; const int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY-FULL_SCREEN_HEIGHT)/2 : 0; WINDOW* w_sokoban = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX); wborder(w_sokoban, LINE_XOXO, LINE_XOXO, LINE_OXOX, LINE_OXOX, LINE_OXXO, LINE_OOXX, LINE_XXOO, LINE_XOOX); parse_level(); mvwprintz(w_sokoban, 0, (FULL_SCREEN_WIDTH/2)-5, hilite(c_white), "Sokoban"); mvwprintz(w_sokoban, 1, FULL_SCREEN_WIDTH-10, c_ltgreen, "+"); wprintz(w_sokoban, c_white, " next"); mvwprintz(w_sokoban, 2, FULL_SCREEN_WIDTH-10, c_ltgreen, "-"); wprintz(w_sokoban, c_white, " prev"); mvwprintz(w_sokoban, 3, FULL_SCREEN_WIDTH-10, c_ltgreen, "r"); wprintz(w_sokoban, c_white, "eset"); mvwprintz(w_sokoban, 4, FULL_SCREEN_WIDTH-10, c_ltgreen, "q"); wprintz(w_sokoban, c_white, "uit"); mvwprintz(w_sokoban, 5, FULL_SCREEN_WIDTH-10, c_ltgreen, "u"); wprintz(w_sokoban, c_white, "ndo move"); int input = '.'; int iPlayerY = 0; int iPlayerX = 0; bool bNewLevel = true; bool bMoved = false; do { if (bNewLevel) { bNewLevel = false; iMoves = 0; vUndo.clear(); iPlayerY = mLevelInfo[iCurrentLevel]["PlayerY"]; iPlayerX = mLevelInfo[iCurrentLevel]["PlayerX"]; mLevel = vLevel[iCurrentLevel]; } print_score(w_sokoban, iScore, iMoves); if (check_win()) { //we won yay if (!mAlreadyWon[iCurrentLevel]) { iScore += 500; mAlreadyWon[iCurrentLevel] = true; } input = '+'; } else { draw_level(w_sokoban); wrefresh(w_sokoban); //Check input input = getch(); } bMoved = false; switch (input) { case KEY_UP: /* up */ iDirY = -1; iDirX = 0; bMoved = true; break; case KEY_DOWN: /* down */ iDirY = 1; iDirX = 0; bMoved = true; break; case KEY_LEFT: /* left */ iDirY = 0; iDirX = -1; bMoved = true; break; case KEY_RIGHT: /* right */ iDirY = 0; iDirX = 1; bMoved = true; break; case 'q': return iScore; break; case 'u': { int iPlayerYNew = 0; int iPlayerXNew = 0; bool bUndoSkip = false; //undo move if (vUndo.size() > 0) { //reset last player pos mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; iPlayerYNew = vUndo[vUndo.size()-1].iOldY; iPlayerXNew = vUndo[vUndo.size()-1].iOldX; mLevel[iPlayerYNew][iPlayerXNew] = vUndo[vUndo.size()-1].sTileOld; vUndo.pop_back(); bUndoSkip = true; } if (bUndoSkip && vUndo.size() > 0) { iDirY = vUndo[vUndo.size()-1].iOldY; iDirX = vUndo[vUndo.size()-1].iOldX; if (vUndo[vUndo.size()-1].sTileOld == "$" || vUndo[vUndo.size()-1].sTileOld == "*") { mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == ".") ? "*" : "$"; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (mLevel[iPlayerY + iDirY][iPlayerX + iDirX] == "*") ? "." : " "; vUndo.pop_back(); } } if (bUndoSkip) { iPlayerY = iPlayerYNew; iPlayerX = iPlayerXNew; } } break; case 'r': //reset level bNewLevel = true; break; case '+': //next level clear_level(w_sokoban); iCurrentLevel++; if (iCurrentLevel >= iNumLevel) { iCurrentLevel = 0; } bNewLevel = true; break; case '-': //prev level clear_level(w_sokoban); iCurrentLevel--; if (iCurrentLevel < 0) { iCurrentLevel = iNumLevel - 1; } bNewLevel = true; break; default: break; } if (bMoved) { //check if we can move the player std::string sMoveTo = mLevel[iPlayerY + iDirY][iPlayerX + iDirX]; bool bMovePlayer = false; if (sMoveTo != "#") { if (sMoveTo == "$" || sMoveTo == "*") { //Check if we can move the package std::string sMovePackTo = mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)]; if (sMovePackTo == "." || sMovePackTo == " ") { //move both bMovePlayer = true; mLevel[iPlayerY + (iDirY * 2)][iPlayerX + (iDirX * 2)] = (sMovePackTo == ".") ? "*" : "$"; vUndo.push_back(cUndo(iDirY, iDirX, sMoveTo)); iMoves--; } } else { bMovePlayer = true; } if (bMovePlayer) { //move player vUndo.push_back(cUndo(iPlayerY, iPlayerX, mLevel[iPlayerY][iPlayerX])); mLevel[iPlayerY][iPlayerX] = (mLevel[iPlayerY][iPlayerX] == "+") ? "." : " "; mLevel[iPlayerY + iDirY][iPlayerX + iDirX] = (sMoveTo == "." || sMoveTo == "*") ? "+" : "@"; iPlayerY += iDirY; iPlayerX += iDirX; iMoves++; iTotalMoves++; } } } } while (true); return iScore; }
//hlavni funkce starajici se o chod aplikace int main(int argc, char **argv ) { unsigned int cnt = 0,old_score=1; char_cnt = 0; last_ch = 0; //iniciaizace HW initialize_hardware(); keyboard_init(); int random_number,i,j; Tshape myshape; Tmatrix matrix,pane,pom; int pom_matrix[8][8] = {0}; char c=0; int latest_line; //srand(2); //vytvoreni noveho utvaru myshape = create_shape(rand()%8); //posun na zacatek matrix = move_it(myshape,pom_matrix); //ulozeni noveho utvaru pane = save_it(matrix,pom_matrix); set_led_d6(1); // rozsviceni D6 set_led_d5(1); // rozsviceni D5 //inicializace LED displeje P6DIR=0xff; P4DIR=0x0f; P2DIR=0xf0; P4OUT=0x00; P2OUT=0x00; P6OUT=0xff; while (1) { delay_ms(1); cnt++; //pri zmacknuti klavesy if (c!=last_ch){ c=last_ch; //pokud byla zmacknuta 6 if (c=='6'){ //posunu se x++; //pokusim se ulozit na to misto utvar matrix = move_it(myshape,pom_matrix); //pokud nelze ulozit if (matrix.count!=4){ //vratim se x--; //a ulozim matrix = move_it(myshape,pom_matrix); } } //pri zmacknuti 4 else if (c=='4'){ //posunu se x--; //pokusim se ulozit matrix = move_it(myshape,pom_matrix); //pokud bych vypsal mene jak 4 znaky z utvaru if (matrix.count!=4){ //posouvam se zpatky x++; matrix = move_it(myshape,pom_matrix); } } //pokud nactu 8 else if (c=='8'){ //posunu se y++; //pokusim se ulozit matrix = move_it(myshape,pom_matrix); if (matrix.count!=4){ //pokud nelze posun, posunu se nazpet y--; matrix = move_it(myshape,pom_matrix); matrix.last_line = 1; } } //rotace else if (c=='5'){ //pouzivam prvne rotate_back kvuli "hezci" rotaci myshape = rotate_back(myshape); //pokusim se ulozit dany utvar matrix = move_it(myshape,pom_matrix); //pokud pri posunu nelze vypsat vsechny 4 prvky daneho utvaru if (matrix.count!=4){ //posunu se zpatky myshape = rotate(myshape); matrix = move_it(myshape,pom_matrix); } } else { continue; } //ulozim do vysledneho hraciho pole pane = save_it(matrix,pom_matrix); //pokud jsem nacetl posledni radek if (matrix.last_line==1) { //ulozim si hraci pole do pomocne matice for (i=0;i<8;i++) { for (j=0;j<8;j++) { pom_matrix[i][j] = pane.shape[i][j]; } } matrix.last_line=0; //smazu radky pane=delete_rows(pane); x=0; y=0; //vygeneruji novy utvar myshape = create_shape(rand()%8); int i,t; //ulozim do pomocne matice hraci pole for (i=0;i<8;i++) for (t=0;t<8;t++) pom_matrix[i][t]=pane.shape[i][t]; //posunu nove vygenerovany utvar na zacatek hraciho pole matrix = move_it(myshape,pom_matrix); //ulozim tento stav pane = save_it(matrix,pom_matrix); } c=last_ch; } //tisknuti skore if (old_score!=score && (cnt%100)==0){ old_score=score; print_score("Score: "); } if (cnt > 1000) { cnt = 0; flip_led_d6(); // negace portu na ktere je LED } print_it(pane.shape); keyboard_idle(); // obsluha klavesnice terminal_idle(); // obsluha terminalu } return 0; }
int main (int argc, const char ** argv) { ProgramArgument program_args=ProcessArguments(argc,argv); bool print_ali=program_args.print_ali; ParseMatrix submatParser(program_args.matrixname); #ifdef DEBUG std::cout << "Print Matrix\n"; submatParser.print(); #endif SubstitutionMatrix subMatrix(submatParser.scores, submatParser.row_index); #ifdef DEBUG std::cout << "Print SubMatrix!\n"; subMatrix.print(); #endif SequenceLibrary sequences(program_args.seqlib); const size_t max_seq_size = sequences.get_max_seq_size(); PairsLibrary pairs(program_args.pairs,&sequences); GotohEight gotohAlgo(max_seq_size,&subMatrix, program_args.go, program_args.ge, program_args.mode); SequenceEight sequenceQuery(max_seq_size, subMatrix.aa2short,subMatrix.short2aa); SequenceEight sequenceTemplate(max_seq_size, subMatrix.aa2short,subMatrix.short2aa); char buffer[2097152]; std::cout.rdbuf()->pubsetbuf(buffer, 2097152); const char ** template_sequences=(const char **)memalign(16,(VEC_SIZE)*sizeof(const char *)); const char ** template_keys=(const char **)memalign(16,(VEC_SIZE)*sizeof(const char *)); std::map< std::string,std::vector<std::pair<std::string,size_t> > >::iterator it = pairs.pairs.begin(); for( ; it != pairs.pairs.end(); ++it ){ std::pair<std::string, size_t> query = sequences.get_sequence(it->first); std::string query_key = it->first.c_str(); sequenceQuery.MapOneSEQToSEQ8(query.first.c_str()); std::vector<std::pair<std::string, size_t> > t_seq = it->second; sort (t_seq.begin(), t_seq.end(), sort_seq_vector); int elem_count=0; size_t t_seq_size = t_seq.size(); for(std::vector<int>::size_type i = 0; i < t_seq_size ; i++) { std::string template_key=t_seq[i].first; template_keys[i%VEC_SIZE] = template_key.c_str(); template_sequences[i%VEC_SIZE] = sequences.get_sequence(template_key).first.c_str(); elem_count++; if((i+1)%VEC_SIZE==0){ elem_count=0; sequenceTemplate.MapSEQArrayToSEQ8(template_sequences, VEC_SIZE); GotohEight::GotohMatrix matrix= gotohAlgo.calc_matrix(&sequenceQuery, &sequenceTemplate); if(print_ali == true){ std::vector<char *> alignments=gotohAlgo.backtrace(matrix,&sequenceQuery,&sequenceTemplate); for(size_t i = 0; i < 8; i++) { if(program_args.check==true){ float check_score=gotohAlgo.calc_check_score(alignments[(i*2)],alignments[(i*2)+1]); printf("check score: %.3f\n",check_score); } print_ali_result(alignments[(i*2)],alignments[(i*2)+1],matrix.score[i],query_key,template_keys[i]); } }else{ print_score(matrix,query_key,template_keys,VEC_SIZE); } } } if(elem_count!=0){ sequenceTemplate.MapSEQArrayToSEQ8(template_sequences, elem_count); GotohEight::GotohMatrix matrix= gotohAlgo.calc_matrix(&sequenceQuery, &sequenceTemplate); if(print_ali == true){ std::vector<char *> alignments=gotohAlgo.backtrace(matrix,&sequenceQuery,&sequenceTemplate); for(size_t i = 0; i < elem_count; i++) { if(program_args.check==true){ float check_score=gotohAlgo.calc_check_score(alignments[(i*2)],alignments[(i*2)+1]); printf("check score: %.3f\n",check_score); } print_ali_result(alignments[(i*2)],alignments[(i*2)+1],matrix.score[i],query_key,template_keys[i]); } }else{ print_score(matrix,query_key,template_keys,elem_count); } } } std::cout.flush(); free(template_keys); free(template_sequences); return 0; }
int main( int argc, char* args[] ) { CSnake * snake; int snake_x, snake_y; unsigned char snake_hit; int k = 1, sb_toggle = 0; Uint32 delay = 10; bool bQuit = false; score = 0; srand((unsigned)time( NULL )); init(); init_items(); init_snake(); init_score(); theMap = new CSnakeMap(mapWidth, mapHeight, map_surface); theMap->AddTiles(map_tex, types_end); init_map(theMap); snake = new CSnake(mapWidth / 2, mapHeight / 2, 0, map_surface, snake_tex); snake->Grow(-1, 0); snake->Grow(-1, 0); snake->Draw(); do_move(snake, RIGHT); place_food(theMap, 1); theMap->Draw(); print_score(); draw_screen(); while (!quitcheck()) { if (bQuit) break; k = getInput(); if (k == QUIT) break; if (k >= LEFT && k <= DOWN) do_move(snake, k); if (collision(snake)) break; if (!checkMap(snake)) break; //theMap->Draw(); draw_screen(); } SDL_Rect r; r.x = 128; r.y = 256, r.w = 512, r.h = 256; SDL_BlitSurface( message, NULL, map_surface, &r ); draw_screen(); SDL_Delay(2000); SDL_FreeSurface(message); delete snake; cleanup(); return 0; }
/************************************************************************* * Entry point for ama *************************************************************************/ int main(int argc, char **argv) { AMA_OPTIONS_T options; ARRAYLST_T *motifs; clock_t c0, c1; // measuring cpu_time MOTIF_AND_PSSM_T *combo; CISML_T *cisml; PATTERN_T** patterns; PATTERN_T *pattern; FILE *fasta_file, *text_output, *cisml_output; int i, seq_loading_num, seq_counter, unique_seqs, seq_len, scan_len, x1, x2, y1, y2; char *seq_name, *path; bool need_postprocessing, created; SEQ_T *sequence; RBTREE_T *seq_ids; RBNODE_T *seq_node; double *logcumback; ALPH_T *alph; // process the command process_command_line(argc, argv, &options); // load DNA motifs motifs = load_motifs(&options); // get the alphabet if (arraylst_size(motifs) > 0) { combo = (MOTIF_AND_PSSM_T*)arraylst_get(0, motifs); alph = alph_hold(get_motif_alph(combo->motif)); } else { alph = alph_dna(); } // pick columns for GC operations x1 = -1; x2 = -1; y1 = -1; y2 = -1; if (alph_size_core(alph) == 4 && alph_size_pairs(alph) == 2) { x1 = 0; // A x2 = alph_complement(alph, x1); // T y1 = (x2 == 1 ? 2 : 1); // C y2 = alph_complement(alph, y1); // G assert(x1 != x2 && y1 != y2 && x1 != y1 && x2 != y2 && x1 != y2 && x2 != y1); } // record starting time c0 = clock(); // Create cisml data structure for recording results cisml = allocate_cisml(PROGRAM_NAME, options.command_line, options.motif_filename, options.fasta_filename); set_cisml_background_file(cisml, options.bg_filename); // make a CISML pattern to hold scores for each motif for (i = 0; i < arraylst_size(motifs); i++) { combo = (MOTIF_AND_PSSM_T*)arraylst_get(i, motifs); add_cisml_pattern(cisml, allocate_pattern(get_motif_id(combo->motif), "")); } // Open the FASTA file for reading. fasta_file = NULL; if (!open_file(options.fasta_filename, "r", false, "FASTA", "sequences", &fasta_file)) { die("Couldn't open the file %s.\n", options.fasta_filename); } if (verbosity >= NORMAL_VERBOSE) { if (options.last == 0) { fprintf(stderr, "Using entire sequence\n"); } else { fprintf(stderr, "Limiting sequence to last %d positions.\n", options.last); } } // // Read in all sequences and score with all motifs // seq_loading_num = 0; // keeps track on the number of sequences read in total seq_counter = 0; // holds the index to the seq in the pattern unique_seqs = 0; // keeps track on the number of unique sequences need_postprocessing = false; sequence = NULL; logcumback = NULL; seq_ids = rbtree_create(rbtree_strcasecmp,rbtree_strcpy,free,rbtree_intcpy,free); while (read_one_fasta(alph, fasta_file, options.max_seq_length, &sequence)) { ++seq_loading_num; seq_name = get_seq_name(sequence); seq_len = get_seq_length(sequence); scan_len = (options.last != 0 ? options.last : seq_len); // red-black trees are only required if duplicates should be combined if (options.combine_duplicates){ //lookup seq id and create new entry if required, return sequence index seq_node = rbtree_lookup(seq_ids, get_seq_name(sequence), true, &created); if (created) { // assign it a loading number rbtree_set(seq_ids, seq_node, &unique_seqs); seq_counter = unique_seqs; ++unique_seqs; } else { seq_counter = *((int*)rbnode_get(seq_node)); } } // // Set up sequence-dependent background model and compute // log cumulative probability of sequence. // This needs the sequence in raw format. // if (options.sdbg_order >= 0) logcumback = log_cumulative_background(alph, options.sdbg_order, sequence); // Index the sequence, throwing away the raw format and ambiguous characters index_sequence(sequence, alph, SEQ_NOAMBIG); // Get the GC content of the sequence if binning p-values by GC // and store it in the sequence object. if (options.num_gc_bins > 1) { ARRAY_T *freqs = get_sequence_freqs(sequence, alph); set_total_gc_sequence(sequence, get_array_item(y1, freqs) + get_array_item(y2, freqs)); // f(C) + f(G) free_array(freqs); // clean up } else { set_total_gc_sequence(sequence, -1); // flag ignore } // Scan with motifs. for (i = 0; i < arraylst_size(motifs); i++) { pattern = get_cisml_patterns(cisml)[i]; combo = (MOTIF_AND_PSSM_T*)arraylst_get(i, motifs); if (verbosity >= HIGHER_VERBOSE) { fprintf(stderr, "Scanning %s sequence with length %d " "abbreviated to %d with motif %s with length %d.\n", seq_name, seq_len, scan_len, get_motif_id(combo->motif), get_motif_length(combo->motif)); } SCANNED_SEQUENCE_T* scanned_seq = NULL; if (!options.combine_duplicates || get_pattern_num_scanned_sequences(pattern) <= seq_counter) { // Create a scanned_sequence record and save it in the pattern. scanned_seq = allocate_scanned_sequence(seq_name, seq_name, pattern); set_scanned_sequence_length(scanned_seq, scan_len); } else { // get existing sequence record scanned_seq = get_pattern_scanned_sequences(pattern)[seq_counter]; set_scanned_sequence_length(scanned_seq, max(scan_len, get_scanned_sequence_length(scanned_seq))); } // check if scanned component of sequence has sufficient length for the motif if (scan_len < get_motif_length(combo->motif)) { // set score to zero and p-value to 1 if not set yet if(!has_scanned_sequence_score(scanned_seq)){ set_scanned_sequence_score(scanned_seq, 0.0); } if(options.pvalues && !has_scanned_sequence_pvalue(scanned_seq)){ set_scanned_sequence_pvalue(scanned_seq, 1.0); } add_scanned_sequence_scanned_position(scanned_seq); if (get_scanned_sequence_num_scanned_positions(scanned_seq) > 0L) { need_postprocessing = true; } if (verbosity >= HIGH_VERBOSE) { fprintf(stderr, "%s too short for motif %s. Score set to 0.\n", seq_name, get_motif_id(combo->motif)); } } else { // scan the sequence using average/maximum motif affinity ama_sequence_scan(alph, sequence, logcumback, combo->pssm_pair, options.scoring, options.pvalues, options.last, scanned_seq, &need_postprocessing); } } // All motifs scanned free_seq(sequence); if (options.sdbg_order >= 0) myfree(logcumback); } // read sequences fclose(fasta_file); if (verbosity >= HIGH_VERBOSE) fprintf(stderr, "(%d) sequences read in.\n", seq_loading_num); if (verbosity >= NORMAL_VERBOSE) fprintf(stderr, "Finished \n"); // if any sequence identifier was multiple times in the sequence set then // postprocess of the data is required if (need_postprocessing || options.normalize_scores) { post_process(cisml, motifs, options.normalize_scores); } // output results if (options.output_format == DIRECTORY_FORMAT) { if (create_output_directory(options.out_dir, options.clobber, verbosity > QUIET_VERBOSE)) { // only warn in higher verbose modes fprintf(stderr, "failed to create output directory `%s' or already exists\n", options.out_dir); exit(1); } path = make_path_to_file(options.out_dir, text_filename); //FIXME check for errors: MEME doesn't either and we at least know we have a good directory text_output = fopen(path, "w"); free(path); path = make_path_to_file(options.out_dir, cisml_filename); //FIXME check for errors cisml_output = fopen(path, "w"); free(path); print_cisml(cisml_output, cisml, true, NULL, false); print_score(cisml, text_output); fclose(cisml_output); fclose(text_output); } else if (options.output_format == GFF_FORMAT) { print_score(cisml, stdout); } else if (options.output_format == CISML_FORMAT) { print_cisml(stdout, cisml, true, NULL, false); } else { die("Output format invalid!\n"); } // // Clean up. // rbtree_destroy(seq_ids); arraylst_destroy(motif_and_pssm_destroy, motifs); free_cisml(cisml); rbtree_destroy(options.selected_motifs); alph_release(alph); // measure time if (verbosity >= NORMAL_VERBOSE) { // starting time c1 = clock(); fprintf(stderr, "cycles (CPU); %ld cycles\n", (long) c1); fprintf(stderr, "elapsed CPU time: %f seconds\n", (float) (c1-c0) / CLOCKS_PER_SEC); } return 0; }