⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manix.c

📁 也是遗传算法的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
              break;            case 'B':              feeling.rightGood += BFRUITSMELL / distance + 5;              if(feeling.rightGood > MAXFEELING)feeling.rightGood = MAXFEELING;              break;            default:              break;        }        distance++;        x++;        if(x == MAX_COL) x = 0;    }    x = Player.CurrPos.x;    distance = 0;    while (cpPlayField[Player.CurrPos.y][x] != 'X'){        switch (cpPlayField[Player.CurrPos.y][x]) {            case '.':              feeling.leftGood += DOTSMELL / distance + 1;              if(feeling.leftGood > MAXFEELING)feeling.leftGood = MAXFEELING;              break;            case 'g':              feeling.leftBad += GHOSTSMELL / distance + 10;              if(feeling.leftBad > MAXFEELING)feeling.leftBad = MAXFEELING;              break;            case 'C':              feeling.leftGood += CFRUITSMELL / distance + 5;              if(feeling.leftGood > MAXFEELING)feeling.leftGood = MAXFEELING;              break;            case 'B':              feeling.leftGood += BFRUITSMELL / distance + 5;              if(feeling.leftGood > MAXFEELING)feeling.leftGood = MAXFEELING;              break;            default:              break;        }        distance++;        x--;        if(x == -1) x = MAX_COL;    }    y = Player.CurrPos.y;    distance = 0;    while (cpPlayField[y][Player.CurrPos.x] != 'X'){        switch (cpPlayField[y][Player.CurrPos.x]) {            case '.':              feeling.upGood += DOTSMELL / distance + 1;              if(feeling.upGood > MAXFEELING)feeling.upGood = MAXFEELING;              break;            case 'g':              feeling.upBad += GHOSTSMELL / distance + 10;              if(feeling.upBad > MAXFEELING)feeling.upBad = MAXFEELING;              break;            case 'C':              feeling.upGood += CFRUITSMELL / distance + 5;              if(feeling.upGood > MAXFEELING)feeling.upGood = MAXFEELING;              break;            case 'B':              feeling.upGood += BFRUITSMELL / distance + 5;              if(feeling.upGood > MAXFEELING)feeling.upGood = MAXFEELING;              break;            default:              break;        }        distance++;        y++;        if(y == MAX_LINES) y = 0;    }    y = Player.CurrPos.y;    distance = 0;    while (cpPlayField[y][Player.CurrPos.x] != 'X'){        switch (cpPlayField[y][Player.CurrPos.x]) {            case '.':              feeling.downGood += DOTSMELL / distance + 1;              if(feeling.downGood > MAXFEELING)feeling.downGood = MAXFEELING;              break;            case 'g':              feeling.downBad += GHOSTSMELL / distance + 10;              if(feeling.downGood > MAXFEELING)feeling.downGood = MAXFEELING;              break;            case 'C':              feeling.downGood += CFRUITSMELL / distance + 5;              if(feeling.downGood > MAXFEELING)feeling.downGood = MAXFEELING;              break;            case 'B':              feeling.downGood += BFRUITSMELL / distance + 5;              if(feeling.downGood > MAXFEELING)feeling.downGood = MAXFEELING;              break;            default:              break;        }        distance++;        y--;        if(y == -1) y = MAX_LINES;    }    if(cpPlayField[Player.CurrPos.y + 1][Player.CurrPos.x]=='X')feeling.downTouch = 10;    if(cpPlayField[Player.CurrPos.y][Player.CurrPos.x + 1]=='X')feeling.rightTouch = 10;    if(cpPlayField[Player.CurrPos.y][Player.CurrPos.x - 1]=='X')feeling.leftTouch = 10;    if(cpPlayField[Player.CurrPos.y - 1][Player.CurrPos.x]=='X')feeling.upTouch = 10;#ifdef DEBUG    for(i = 0; i < MAX_LINES; ++i ) {        putchar('\n');        for (j=0; j< MAX_COL; ++j)            putchar( cpPlayField[i][j]);    }     printf("\n\n");#endif    ImprimirGraficos();    GuiarXMan();    VerChoque();    GerarMovimento();    VerChoque();    if( timer && !--timer ) armado = False ;}int getFinalScore(){   return Player.score + Score;}Senses* getFeeling() {    return &feeling;}void IniciaJogo(){ int i;    ReadScreen( ecra, NivelInicial );    Player.lives = N_VIDAS ;    Player.score = 0 ;    Score = 0 ;    Player.bonus = 0 ;    Player.level = NivelInicial ;    Player.CurrPos.x = X_INICIAL ;    Player.ApagaPos.x = X_INICIAL ;    Player.CurrPos.y = Y_INICIAL ;    Player.ApagaPos.y = Y_INICIAL ;    NGhosts = 3 + ( Player.level +1 ) / 3 ;    for( i = 0 ; i < NGhosts ; ++i ) PutGhost(i) ;    timer = 0 ;}void VerChoque(){    int i ;    for( i = 0 ; i < NGhosts; ++i )    if( Ghosts[i].CurrPos.x == Player.CurrPos.x &&	Ghosts[i].CurrPos.y == Player.CurrPos.y )    {//	XBell( display, 50 );	XFlush( display );	if( armado )	{			feeling.pleasure = SCORE_HUNT/10;	    ++(Player.bonus) ;	    Player.score+=SCORE_HUNT ;	    put_object( Ghosts[i].ApagaPos.x, Ghosts[i].ApagaPos.y, bm_space );	    PutGhost( i );	}	else {     morte = True ;     feeling.pain = MAXFEELING;//     ActualizaNivel();//     initLevel();//     NivelMudou = 1;	}        ImprimirGraficos();    }}void ActualizaNivel(){int i;    if( NivelMudou ) {	      if( ++(Player.level) > NIVEL_MAX ) Player.level = 0 ;        ReadScreen( ecra, Player.level );    	  Player.score += Score ;        NGhosts = 3 + ( Player.level +1 ) / 3 ;    	  Score = 0 ;    }    else --(Player.lives) ;    Player.lives += Player.bonus / VIDAS_POR_BONUS ;    Player.bonus = 0 ;    Player.CurrPos.x = X_INICIAL ;    Player.CurrPos.y = Y_INICIAL ;    Player.ApagaPos.x = X_INICIAL ;    Player.ApagaPos.y = Y_INICIAL ;    for( i = 0 ; i < NGhosts ; ++i ) PutGhost(i) ;    LastOne = Ultima = '\0' ;}void ImprimirEcra(){ int i,j;    Apagar();    for( i = 0; i < MAX_LINES; ++i )    {	for( j = 0; j< MAX_COL; ++j )	switch( ecra[i][j] )	{	    case 'X' : put_object( j, i, bm_wall ) ;		       break ;	    case '.' : put_object( j, i, bm_dot ) ;	    case ' ' : 	    case 'A' : break ;	    case 'B' : put_object( j, i, bm_apple ) ;		       break ;	    case 'C' : put_object( j, i, bm_orange ) ;	    default  : break ;	}    }    sprintf( string, "SCORE: %06d   LIVES: %3d  LEVEL: %3d",	     Player.score+Score, Player.lives, Player.level );    x_print_string( 4, Y_SCORE, string, gc_red ) ;    XSync( display, 1 ) ;}void PutGhost( i )int i;{     Ghosts[i].CurrPos.x = X_ENTRADA + rand() % X_LENGHT ;     Ghosts[i].CurrPos.y = Y_ENTRADA + rand() % Y_LENGHT ;     Ghosts[i].ApagaPos.x = Ghosts[i].CurrPos.x;     Ghosts[i].ApagaPos.y = Ghosts[i].CurrPos.y;}void ImprimirGraficos(){ int i ;    sprintf( string, "%06d",Player.score+Score);    x_print_string( X_SCORE, Y_SCORE, string, gc_red );    for( i=0 ; i<NGhosts; ++i )    {	put_object( Ghosts[i].CurrPos.x, Ghosts[i].CurrPos.y,		    (armado) ? (timer > 10) ? bm_ghost3 : bm_ghost2 : bm_ghost1 );	if( Ghosts[i].CurrPos.x != Ghosts[i].ApagaPos.x ||	    Ghosts[i].CurrPos.y != Ghosts[i].ApagaPos.y )	switch( ecra[Ghosts[i].ApagaPos.y][Ghosts[i].ApagaPos.x] )	{	    case 'A' :	    case ' ' :		put_object(Ghosts[i].ApagaPos.x,Ghosts[i].ApagaPos.y, bm_space);		break ;	    case '.' :		put_object(Ghosts[i].ApagaPos.x,Ghosts[i].ApagaPos.y, bm_dot);		break ;	    case 'B' :		put_object(Ghosts[i].ApagaPos.x,Ghosts[i].ApagaPos.y, bm_apple);		break ;	    case 'C' :		put_object(Ghosts[i].ApagaPos.x,Ghosts[i].ApagaPos.y,bm_orange);	    default  :	        break ;	}    }    switch( Player.manDir )    {	case Left : put_object(Player.CurrPos.x, Player.CurrPos.y, bm_userleft );	            break ;	case Right: put_object(Player.CurrPos.x, Player.CurrPos.y, bm_userright);	            break ;	case Up :   put_object(Player.CurrPos.x, Player.CurrPos.y, bm_userup );		    break ;	case Down : put_object(Player.CurrPos.x, Player.CurrPos.y, bm_userdown );    }    if(Player.ApagaPos.x!=Player.CurrPos.x||Player.ApagaPos.y!=Player.CurrPos.y)	put_object( Player.ApagaPos.x, Player.ApagaPos.y, bm_space ) ;}void resetFeeling(){    feeling.upGood   = 0.;    feeling.downGood   = 0.;    feeling.leftGood   = 0.;    feeling.rightGood   = 0.;    feeling.upBad  = 0.;    feeling.downBad  = 0.;    feeling.leftBad  = 0.;    feeling.rightBad  = 0.;    feeling.pain       *= 0.2;    feeling.pleasure   *= 0.2;    feeling.upTouch    = 0;    feeling.downTouch  = 0;    feeling.leftTouch  = 0;    feeling.rightTouch = 0;}void GuiarXMan(){ Position manNP ; int Pausa = False ;    Tecla = LerTeclado() ;/*    if( Tecla<=4  ) Tecla = LastOne ;    LastOne = Tecla ;*/    do    { 	Player.ApagaPos.x = Player.CurrPos.x ;	Player.ApagaPos.y = Player.CurrPos.y ;	manNP.x = Player.CurrPos.x ;	manNP.y = Player.CurrPos.y ;	if( Tecla == TecCurr.left )	{	    if( --manNP.x < 0 ) manNP.x = MAX_COL -1 ;	    Player.manDir = Left ;	}	if( Tecla == TecCurr.right )	{	    if( ++(manNP.x) >= MAX_COL ) manNP.x = 0 ;	    Player.manDir = Right ;	}	if( Tecla == TecCurr.up )	{	    if( --(manNP.y) < 0 ) manNP.y = MAX_LINES - 1 ;	    Player.manDir = Up ;	}	if( Tecla == TecCurr.down )	{	    if( ++(manNP.y) >= MAX_LINES ) manNP.y = 0 ; 	    Player.manDir = Down ;	}	if( Tecla == TecCurr.pause )	{	    LastOne = Ultima = '\0' ;	    PAUSA() ;	}	if( Tecla == TecCurr.stop )	{	    Tecla = LastOne = Ultima = '\0' ;	    Player.lives = 0 ;	    morte = 1 ;	}	if( ecra[manNP.y][manNP.x] != 'X' &&	    ecra[manNP.y][manNP.x] != 'A'    )	{	    if( !Pausa ) Ultima = Tecla ;	    Pausa = False ;	    Player.CurrPos.x = manNP.x ;	    Player.CurrPos.y = manNP.y ;	    switch( ecra[manNP.y][manNP.x] )	    {		case '.' : if( ( Score += 10 ) >= (total - LEVEL_CHEAT ) )				NivelMudou = True ;         if(feeling.pleasure<50)feeling.pleasure += 10;			   break ;		case 'B' : armado = True ;			   timer = TIMER_VALUE_1 ;    		 feeling.pleasure = SCORE_APPLE/10;			   Player.score += SCORE_APPLE ;			   break ;		case 'C' : armado = True ;			   timer = TIMER_VALUE_2 ;			   feeling.pleasure = SCORE_ORANGE/10;			   Player.score += SCORE_ORANGE ;		default :  break ;	    }	    ecra[manNP.y][manNP.x] = ' ' ;	}	else	{	    if( !Pausa ) Tecla = Ultima ;	    Pausa = !Pausa ;	}     } while( Pausa ) ;}int LerTeclado(){    XEvent ev ;    XFlush( display ) ;//    usleep( N_DELAY - 4000 * Player.level );		if(XCheckTypedEvent(display, Expose, &ev)){  	  	ImprimirEcra() ;    		ImprimirGraficos() ;		}		return Tecla;}int VerPossiveisMovimentos( i )int i ;{ int x, y, np = 0 ;    x = Ghosts[i].CurrPos.x ;    y = Ghosts[i].CurrPos.y ;    if( Ghosts[i].OldMove != Right && ( x == 0 || ecra[y][x-1] != 'X' ))	 Possibilidades[np++] = Left ;    if( Ghosts[i].OldMove != Left && ( x == MAX_COL-1 || ecra[y][x+1] != 'X' ))	 Possibilidades[np++] = Right ;    if( Ghosts[i].OldMove != Down && ( y == 0 || ecra[y-1][x] != 'X' ))	 Possibilidades[np++] = Up ;    if( Ghosts[i].OldMove != Up && ( y == MAX_LINES-1 || ecra[y+1][x] != 'X' ))	 Possibilidades[np++] = Down ;    return np ;}	Direct MaxiMove( i, n )int i, n ;{ int j ;    if( rand() % 2 )    {	if( armado ) for( j = 0 ; j < n; ++j )	{	    if( Possibilidades[j] == Left &&	        Ghosts[i].CurrPos.x<=Player.CurrPos.x) return Left ;	    if( Possibilidades[j] == Right &&	        Ghosts[i].CurrPos.x>=Player.CurrPos.x) return Right ;            if( Possibilidades[j] == Up &&	        Ghosts[i].CurrPos.y<=Player.CurrPos.y) return Up;            if( Possibilidades[j] == Down &&                Ghosts[i].CurrPos.y>=Player.CurrPos.y) return Down ;	}	else for( j = 0 ; j < n; ++j )	{	    if( Possibilidades[j] == Left &&	        Ghosts[i].CurrPos.x>Player.CurrPos.x) return Left ;	    if( Possibilidades[j] == Right &&	        Ghosts[i].CurrPos.x<Player.CurrPos.x) return Right ;            if( Possibilidades[j] == Up &&	        Ghosts[i].CurrPos.y>Player.CurrPos.y) return Up;            if( Possibilidades[j] == Down &&                Ghosts[i].CurrPos.y<Player.CurrPos.y) return Down ;	}    }    else    {	if( armado ) for( j = n-1 ; j >= 0 ; --j )	{            if( Possibilidades[j] == Up &&	        Ghosts[i].CurrPos.y<=Player.CurrPos.y) return Up;            if( Possibilidades[j] == Down &&                Ghosts[i].CurrPos.y>=Player.CurrPos.y) return Down ;	    if( Possibilidades[j] == Left &&	        Ghosts[i].CurrPos.x<=Player.CurrPos.x) return Left ;	    if( Possibilidades[j] == Right &&	        Ghosts[i].CurrPos.x>=Player.CurrPos.x) return Right ;	}	else for( j = n-1 ; j >= 0 ; --j )	{            if( Possibilidades[j] == Up &&	        Ghosts[i].CurrPos.y>Player.CurrPos.y) return Up;            if( Possibilidades[j] == Down &&                Ghosts[i].CurrPos.y<Player.CurrPos.y) return Down ;	    if( Possibilidades[j] == Left &&	        Ghosts[i].CurrPos.x>Player.CurrPos.x) return Left ;	    if( Possibilidades[j] == Right &&	        Ghosts[i].CurrPos.x<Player.CurrPos.x) return Right ;	}    }    return Possibilidades[ rand() % n] ;}void GerarMovimento(){ int i, NMovPossiveis ; Direct Move ;    for( i=0; i<NGhosts; ++i )    {        Ghosts[i].ApagaPos.x = Ghosts[i].CurrPos.x ;        Ghosts[i].ApagaPos.y = Ghosts[i].CurrPos.y ;        if( RANDOM() > 80 - 5 * Player.level )        {	    if( ecra[Ghosts[i].CurrPos.y][Ghosts[i].CurrPos.x] == 'A' )	    {	        if( Ghosts[i].CurrPos.x < (MAX_COL-1)/2 ) Move = Right ;	        else if( Ghosts[i].CurrPos.x > MAX_COL/2 ) Move = Left ;	        else Move = Up ;	    }	    else	    {	        NMovPossiveis = VerPossiveisMovimentos( i );	        Move = Possibilidades[rand() % NMovPossiveis] ;                if( (NMovPossiveis > 1) &&		    (RANDOM() > (100 - 8 * Player.level)) )		    Move = MaxiMove( i, NMovPossiveis ) ;	    }	    switch( Move )	    {	        case Left : if( --(Ghosts[i].CurrPos.x)  < 0 )			    Ghosts[i].CurrPos.x = MAX_COL - 1 ;		            break ;	        case Right : if( ++(Ghosts[i].CurrPos.x) >= MAX_COL )		             Ghosts[i].CurrPos.x = 0 ;		             break ;	        case Up : if( --(Ghosts[i].CurrPos.y) < 0 )			  Ghosts[i].CurrPos.y = MAX_LINES - 1 ;		          break ;	        case Down : if( ++(Ghosts[i].CurrPos.y) >= MAX_LINES )		             Ghosts[i].CurrPos.y = 0 ;	    }	    Ghosts[i].OldMove = Move ;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -