防止游戏修改的一个小算法
一个算24点的算法

更新的防止游戏修改的算法

太阳神上 posted @ 2008年4月25日 11:32 in Algorithm with tags Algorithm , 1821 阅读

  上次我把程序发给了另外一个高中同学,他似乎并没有花多少时间就找出来了。但是看他给我的描述,似乎找到不是真正的存放关键数据的内存。还是给廖传政同学修改的差不多,由于要通过画矩形来显示结果,所以必定要提供一个变量来表示真实的数据。如果放在一个函数的局部变量里,由于局部变量是存放在栈中的,只要调用堆栈相同就可以找到。所以放在堆空间更好,但是我却忽略了一点。如下:

  1. void *p=malloc(100);
  2. free(p);
  3. p=malloc(100)

  这个式子中,p在free前后的值很多情况下是相同的,这样,即使在堆空间,也可能完全相同。为了防止这种情况,我又使用了随机得到一个堆内存的方法,为了防止栈空间也相同,我同样使用了随机层次的递归,这样一来,我觉得应该会提高破解的难度。更新的代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #include <SDL/SDL.h>
  7.  
  8. SDL_Surface *screen = NULL;
  9. int blood;
  10.  
  11. int *encode;
  12. int *decode;
  13. SDL_Rect *rects;
  14.  
  15. int index_of(int array[],size_t size,int value){
  16.     int index;
  17.     for(index=0;index<size;index++){
  18.         if(array[index]==value)
  19.             return index;
  20.     }
  21.     return -1;
  22. }
  23.  
  24.  
  25. void begin_game(){
  26.     srand(time(NULL));
  27.     encode=(int *)malloc(1024*sizeof(int));
  28.     decode=(int *)malloc(1024*sizeof(int));
  29.     rects=(SDL_Rect *)malloc(1024*sizeof(SDL_Rect));
  30.    
  31.     int i,r;
  32.     for(i=0;i<101;i++){
  33.         r=rand()%101;
  34.         if(index_of(encode,i,r)==-1)
  35.             encode[i]=r;
  36.         else{
  37.             i--;
  38.             continue;
  39.         }           
  40.     }
  41.    
  42.     for(i=0;i<101;i++){
  43.         decode[i]=index_of(encode,101,i);
  44.     }   
  45.    
  46.     blood=encode[100];
  47. }
  48.  
  49. void end_game(){
  50.     free(encode);
  51.     free(decode);
  52.     free(rects);
  53. }
  54.  
  55. void draw (int layer){
  56.     if(layer>0)
  57.         return draw(--layer);
  58.    
  59.     Uint32 color;   
  60.     color=SDL_MapRGB(screen->format,0,0,0);
  61.     SDL_FillRect (screen, NULL, color);   
  62.     color = SDL_MapRGB (screen->format, 0xFF, 0, 0);
  63.    
  64.     SDL_Rect *rect=&rects[rand()%1024];
  65.     rect->w = decode[blood]*5;
  66.     rect->h = 20;
  67.     rect->x = 100;
  68.     rect->y = 100;
  69.     SDL_FillRect (screen, rect, color);
  70.  
  71.     SDL_Flip (screen);
  72.     SDL_Delay (10);
  73. }
  74.  
  75.  
  76.  
  77.  
  78. int main (int argc, char *argv[]){
  79.     char *msg;
  80.     int done;
  81.    
  82.     SDL_Init (SDL_INIT_VIDEO);
  83.     atexit (SDL_Quit);
  84.  
  85.     screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
  86.     SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
  87.     begin_game();
  88.    
  89.  
  90.     done = 0;
  91.     SDL_Event event;
  92.    
  93.     while (!done){
  94.         SDL_WaitEvent (&event);
  95.         switch (event.type){
  96.             case SDL_KEYDOWN:{
  97.                 switch(event.key.keysym.sym){
  98.                     case SDLK_UP:blood = decode[blood]+10>100 ? blood : encode[decode[blood]+10];break;
  99.                     case SDLK_DOWN:blood = decode[blood]-10<0 ? blood : encode[decode[blood]-10];break;
  100.                 }
  101.             }
  102.                 break;
  103.             case SDL_QUIT:
  104.                 done = 1;
  105.                 break;
  106.             default:
  107.                 break;
  108.         }
  109.         draw (rand()%10);
  110.     }
  111.    
  112.     end_game();
  113.  
  114.     return 0;
  115. }
  116.  

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter