Ejemplo n.º 1
0
    void mk_unbound_compressor::add_task(func_decl * pred, unsigned arg_index) {
        c_info ci = c_info(pred, arg_index);
        if (m_map.contains(ci)) {
            return; //this task was already added
        }

        unsigned parent_arity = pred->get_arity();
        sort * const * parent_domain = pred->get_domain();
        symbol const& parent_name = pred->get_name();
        unsigned arity = parent_arity-1;
        ptr_vector<sort> domain;
        for (unsigned i = 0; i < parent_arity; i++) {
            if (i != arg_index) {
                domain.push_back(parent_domain[i]);
            }
        }

        std::stringstream name_suffix;
        name_suffix << "compr_arg_" << arg_index;

        func_decl * cpred = m_context.mk_fresh_head_predicate(parent_name, symbol(name_suffix.str().c_str()), 
            arity, domain.c_ptr(), pred);
        m_pinned.push_back(cpred);

        m_todo.push_back(ci);
        m_map.insert(ci, cpred);
    }
Ejemplo n.º 2
0
/// \brief  Read the saved file
/// \param  boards The number of the saved board.NA for not to use
/// \return void
void c_readFromDisk(int boards,bool info){
    char name[20];
    int ver=c_version();
    if(boards!=NA){
        boards=abs(boards%MAX_BOARD_NUM);
        sprintf(name,"2048.%d.%X.save",boards,ver);
    }else{
        sprintf(name,"2048.%X.save",ver);
        boards=curs;
    }
    boards=abs(boards%MAX_BOARD_NUM);
    FILE *fp;
    if((fp=fopen(name,"r"))) {
        int iptN;
        int iptVer=0;
        fscanf(fp,"%X",&iptVer);
        char w[1024];
        if(iptVer!=ver){
            sprintf(w,"Librazy found that the game's version dosen't match!\nYour version:%X, saved:%X",ver,iptVer);
            c_warning(w);
            return ;
        }
        sprintf(w,"Opening %s",name);
        if(info)c_info(w);
        fscanf(fp,"%d",&iptN);
        c_loadStr(iptN%MAX_BOARD_SIZE,fp,info);
    }else{
        char str[1000];
        sprintf(str,"Librazy don't know how to open %s,\n read file failed",name);
        c_warning(str);
    }
    fclose(fp);
}
Ejemplo n.º 3
0
/// \brief  Ask player whether to quit
/// \return void
void c_tryQuit(){
    c_warning("You game progress won't be saved!\nPress q to quit, others to resume");
    int ch=0;
    ch=getch();
    if(ch=='q'||ch=='Q'){
        c_forceQuit();
    }
    c_info("");
}
Ejemplo n.º 4
0
/// \brief  Save the board in memory
/// \param  to The number of the board to save to.NA for auto find nnext
/// \param  jmp If should jump to new board
/// \return void
void c_saveBoard(int to,bool jmp){
    if(to==NA){
        to=abs((curs+1)%MAX_BOARD_NUM);
    }
    to=abs(to%MAX_BOARD_NUM);
    boardseed[to]=boardseed[curs]=Rando(RAND_MAX);
    srand(boardseed[curs]);
    memcpy(board[to],board[curs],sizeof(char)*MAX_BOARD_SIZE*MAX_BOARD_SIZE);
    move(MENU_POSITION_Y,MENU_POSITION_X);
    clrtoeol();
    char str[1000];
    if(jmp){
        sprintf(str,"Save board#%d, current#%d",curs,to);
        c_info(str);
        curs=to;
    }else{
        sprintf(str,"Save board#%d to #%d",curs,to);
        c_info(str);
    }
}
Ejemplo n.º 5
0
/// \brief  Read the saved board
/// \param  from The number of board to read from
/// \return void
void c_readBoard(int from){
    if(from==NA){
        from=abs((16+curs-1)%MAX_BOARD_NUM);
    }
    from%=MAX_BOARD_NUM;
    boardseed[curs]=Rando(RAND_MAX);
    char str[1000];
    if(boardseed[from]==NA){
        sprintf(str,"Librazy found board#%d empty",from);
        c_warning(str);
        return;
    }
    curs=from;
    srand(boardseed[from]);
    sprintf(str,"Load board#%d",curs);
    c_info(str);
}
Ejemplo n.º 6
0
/// \brief  Write the board to disk
/// \param  boards The number of board to save
/// \return Whether the file is saved successfully
bool c_writeBoardToDisk(char boards,bool info){
    char name[20];
    int ver=c_version();
    if(boards!=NA){
        boards=abs(boards%MAX_BOARD_NUM);
        sprintf(name,"2048.%d.%X.save",boards,ver);
    }else{
        sprintf(name,"2048.%X.save",ver);
        boards=curs;
    }
    boards=abs(boards%MAX_BOARD_NUM);
    FILE *fp;
    if((fp=fopen(name,"w+"))) {
        fprintf(fp,"%X\n",ver);
        char tmp=curs;
        curs=boards;
        c_currentStr(false);
        fprintf(fp,"%d\n",N);
        for(int i=0;i!=N;++i){
            for(int j=0;j!=N;++j){
                fprintf(fp,"%s",boardstr[i][j]);
            }
        }
        boardseed[curs]=Rando(RAND_MAX);
        srand(boardseed[curs]);
        fprintf(fp,"\n%d\n",boardseed[curs]);
        int checksum=c_checksum();
        fprintf(fp,"%d\n",checksum+boardseed[curs]);
        curs=tmp;
        char str[1000];
        sprintf(str,"Saved board#%d at %s",boards,name);
        if(info)c_info(str);
        fclose(fp);
        return true;
    }else{
        char str[1000];
        sprintf(str,"Librazy don't know how to open %s,\nsave failed",name);
        c_warning(str);
        fclose(fp);
        return false;
    }
}
Ejemplo n.º 7
0
int McpServlet::get_grab_info_by_c_id(const idl::mcp_get_grab_info_by_c_id_params& in, idl::mcp_get_grab_info_by_c_id_response& out)
{
    UB_LOG_TRACE( "get_grab_info_by_c_id start" );
    string c_id(in.c_id());
    vector<grab_t> grab_list;
    int count=0;
    ContentInfo c_info(c_id);
    int res=c_info.getGrabList(count, grab_list);
    if(res!=0){
        UB_LOG_FATAL( "getGrabList failed, [%s:%d]", __FILE__, __LINE__ );
        count = res;
        goto end;
    }
end:
    out.m_result_params()->set_result(count);
    vector<grab_t>::const_iterator iter=grab_list.begin();
    for(int i=0; iter!=grab_list.end(); ++i, ++iter){
        setGrabInfoResult(out.m_result_params()->mutable_grab_list(i), *iter);
    }
    UB_LOG_TRACE( "get_grab_info_by_c_id start" );
    return 0;
}
Ejemplo n.º 8
0
/// \brief  Load the string representing saved board
/// \param  iptN The N in the saved game
/// \param  fp The file stream to read from
/// \return void
void c_loadStr(int iptN,FILE* fp,bool info){
    N=iptN;
    Clrboard(curs);
    char str[256*3+2];
    fscanf(fp,"%s",str);
    int checksum;int seed;
    fscanf(fp,"%d",&seed);
    fscanf(fp,"%d",&checksum);
    boardseed[curs]=seed;
    srand(seed);
    checksum-=seed;
    int offset=0;
    for(int i=0;i!=N;++i){
        for(int j=0;j!=N;++j){
            if(('@'<=str[i*N+j+offset])&&('Z'>=str[i*N+j+offset])){
                board[curs][i][j]=str[i*N+j+offset]-'A';
                continue;
            }
            if(('a'<=str[i*N+j+offset])&&('z'>=str[i*N+j+offset])){
                board[curs][i][j]=str[i*N+j+offset]-'a';
                continue;
            }
            if(('0'<=str[i*N+j+offset])&&('9'>=str[i*N+j+offset])){
                board[curs][i][j]=(str[i*N+j+offset]-'0')*100+
                                        (str[i*N+j+offset+1]-'0')*10+
                                        (str[i*N+j+offset+2]-'0');
                offset+=2;
                continue;
            }
        }
    }
    if(c_checksum()==checksum){
        if(info)c_info("Game progress loaded successful!");
    }else{
        Clrboard(curs);
        c_warning("Librazy found you are cheating!");
    }
}
Ejemplo n.º 9
0
/// \brief  Main executable
/// \return 0
int main()
{
    srand((int)time(0));
    initscr();
    clear();
    curs_set(0);
    noecho();
    raw();
    if(has_colors())start_color();
    settings();
    dyad_init();
    pthread_attr_init (&AThread);
    pthread_attr_setdetachstate (&AThread, PTHREAD_CREATE_JOINABLE);
    bool cho=true;
    while(cho){
        pthread_mutex_init (&MBoard, NULL);
        pthread_cond_init (&CBoard, NULL);
        pthread_mutex_init (&MInfo, NULL);
        pthread_cond_init (&CInfo, NULL);
        pthread_mutex_init (&MScr, NULL);
        pthread_mutex_init (&MNet, NULL);
        pthread_mutex_init (&MNetC, NULL);
        pthread_cond_init (&CNet, NULL);
        Clrboard(curs);
        welcome();
        if(isnetworking){
            dyad_setUpdateTimeout(0.05);
            pthread_create (&TNetworkPlay, &AThread, t_NetworkPlay, NULL);
            while (dyad_getStreamCount() > 0 && isconnected) {
                pthread_mutex_lock(&MNet);
                dyad_update();
                pthread_mutex_unlock(&MNet);
                usleep(10000);
            }
            if(result){
                if(score[0]>score[1])
					c_info("You Win!");
				else if(score[0]<score[1])
					c_info("You Lost!");
				else
					c_info("Neck and neck!");
                pthread_cancel(TNetworkPlay);
                pthread_join(TNetworkPlay, NULL);
                pthread_cancel(TNetworkSend);
                pthread_cancel(TNetworkShow);
                pthread_cond_signal (&CBoard);
                pthread_cond_signal (&CNet);
                pthread_join(TNetworkSend, NULL);
                pthread_join(TNetworkShow, NULL);
                pthread_cancel(TInfo);
                pthread_cond_signal (&CInfo);
                pthread_join(TInfo, NULL);
                getch();
            }
            cho=0;
        }else{
            cho=play();
        }
        if(result){
            pthread_mutex_destroy (&MScr);
            pthread_mutex_destroy (&MInfo);
            pthread_cond_destroy (&CInfo);
            pthread_mutex_destroy (&MBoard);
            pthread_cond_destroy (&CBoard);
            pthread_mutex_destroy (&MNet);
            pthread_mutex_destroy (&MNetC);
            pthread_cond_destroy (&CNet);
        }
    }
    if(result){c_forceQuit();}
    return 0;
}
Ejemplo n.º 10
0
/// \brief  The thread to print Board to screen
/// \param  arg Void
/// \return void* NULL
void* t_NetworkPlay(void* arg){
    pthread_mutex_lock(&MNetC);
    pthread_mutex_lock(&MBoard);
    curs=0;
    if(boardseed[curs]==NA){
        boardseed[curs]=Rando(RAND_MAX);
    }
    srand(boardseed[curs]);
    Clrboard(curs);
    clear();
    pthread_create (&TNetworkShow, &AThread, t_NetworkShow , NULL);
    pthread_create (&TNetworkSend, &AThread, t_NetworkSend , NULL);
    keypad(stdscr, TRUE);
    int ch=0;
    int res=NA,lastres=NA;GetRandNums();
    pthread_mutex_unlock(&MBoard);
    usleep(100000);
    pthread_cond_signal(&CNet);
    pthread_cond_signal(&CBoard);
    usleep(100000);
    pthread_cond_signal(&CNet);
    pthread_cond_signal (&CBoard);
    while((ch = getch())&&dyad_getStreamCount()>0){
        pthread_testcancel();
        move(0,0);
        clrtoeol();
        pthread_mutex_lock(&MInfo);
        pthread_mutex_lock(&MScr);
        wclear(MenuWin);
        pthread_mutex_unlock(&MScr);
        memset(sinfo,0,sizeof(sinfo));
        pthread_mutex_unlock(&MInfo);
        pthread_cond_signal(&CInfo);
        lastres=res;
        pthread_mutex_lock(&MBoard);
        curs=0;
        switch(ch)
        {
            case KEY_LEFT:case 'H':case 'h':
                res=Eat(ELEFT);GetRandNums();
                break;
            case KEY_RIGHT:case 'L':case 'l':
                res=Eat(ERIGHT);GetRandNums();
                break;
            case KEY_UP:case 'K':case 'k':
                res=Eat(EUP);GetRandNums();
                break;
            case KEY_DOWN:case 'J':case 'j':
                res=Eat(EDOWN);GetRandNums();
                break;
            case 3:case KEY_F(1):
                pthread_mutex_unlock(&MBoard);
                c_tryQuit();
                pthread_mutex_lock(&MBoard);
                break;
			case ':':
				pthread_mutex_unlock(&MBoard);
				x_netCommand();
			    pthread_mutex_lock(&MBoard);
                break;
        }
        pthread_mutex_unlock(&MBoard);
        pthread_cond_signal (&CBoard);
        pthread_cond_signal (&CNet);
        if(res==lastres&&res==0){
			if(score[0]>score[1])
				c_info("You Win!");
			else if(score[0]<score[1])
				c_info("You Lost!");
			else
				c_info("Neck and neck!");
            result=0;
            break;
        }
    }
    getch();
    dyad_end(SGaming);
    dyad_end(SServ);
    dyad_end(SClient);
    usleep(100000);
    pthread_cancel(TNetworkSend);
    pthread_cancel(TNetworkShow);
    pthread_cond_signal (&CBoard);
    pthread_cond_signal (&CNet);
    pthread_join(TNetworkSend, NULL);
    pthread_join(TNetworkShow, NULL);
    pthread_cancel(TInfo);
    pthread_cond_signal (&CInfo);
    pthread_join(TInfo, NULL);
    usleep(100000);
    wclear(BoardWin);
    wclear(MenuWin);
    delwin(BoardWin);
    delwin(MenuWin);
    BoardWin=NULL;
    MenuWin=NULL;
    pthread_mutex_unlock(&MNetC);
    pthread_mutex_destroy (&MScr);
    pthread_mutex_destroy (&MInfo);
    pthread_cond_destroy (&CInfo);
    pthread_mutex_destroy (&MBoard);
    pthread_cond_destroy (&CBoard);
    pthread_mutex_destroy (&MNet);
    pthread_mutex_destroy (&MNetC);
    pthread_cond_destroy (&CNet);
    c_forceQuit();
    return NULL;
}