Advertisement
Siapran

Minesweeper

Jun 16th, 2012
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.88 KB | None | 0 0
  1. /**********************************************/
  2. /* Minesweeper (15.06.12-  .  .  )            */
  3. /* [GeekBros]Siapran                          */
  4. /* tristan.robin@hotmail.fr                   */
  5. /**********************************************/
  6. #include "fxlib.h"
  7. #include "ECode.h"
  8. #include "MonochromeLib.h"
  9. #include "syscall.h"
  10.  
  11. //sprites of the game (I lost it)
  12.  
  13. //open 8*8
  14. const unsigned char open[] = {1,1,1,1,1,1,1,255};
  15.  
  16. //closed 8*8
  17. const unsigned char closed[] = {1,127,87,107,87,107,127,255};
  18.  
  19. //flag 8*8
  20. const unsigned char flag[] = {1,127,71,71,119,67,127,255};
  21.  
  22. //mine 8*8
  23. const unsigned char mine[] = {1,91,45,95,127,61,91,255};
  24.  
  25. //wrong 8*8
  26. const unsigned char wrong[] = {1,127,93,107,119,107,93,255};
  27.  
  28. //boom 8*8
  29. const unsigned char boom[] = {1,37,67,1,1,67,37,255};
  30.  
  31. //gui 32*64
  32. const unsigned char gui[] = {0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,127,255,1,0,0,0,1,0,0,0,1,0,234,46,1,0,75,104,1,0,74,172,1,0,74,40,1,0,74,46,1,0,0,0,1,0,0,0,1,1,220,119,65,1,85,85,65,1,84,85,65,1,85,85,65,1,220,119,65,0,0,0,65,1,255,255,193,0,0,0,1,0,0,0,1,2,42,92,193,3,107,81,1,2,170,216,129,2,42,80,65,2,42,93,129,0,0,0,1,0,0,0,1,0,14,224,65,0,10,160,65,0,10,160,65,0,10,160,65,0,14,224,65,0,0,0,65,1,255,255,193,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,127,255,255,255};
  33.  
  34. //face1 16*16
  35. const unsigned char face1[] = {0,0,0,1,7,225,12,49,16,9,50,77,34,69,32,5,32,5,36,37,51,205,16,9,12,49,7,225,0,1,127,255};
  36.  
  37. //face2 16*16
  38. const unsigned char face2[] = {0,0,0,1,7,225,12,49,16,9,50,77,34,69,32,5,32,5,33,133,51,205,17,137,12,49,7,225,0,1,127,255};
  39.  
  40. //face3 16*16
  41. const unsigned char face3[] = {0,0,0,1,7,225,12,49,16,9,62,125,63,253,62,125,32,5,36,5,51,205,16,9,12,49,7,225,0,1,127,255};
  42.  
  43. //face4 16*16
  44. const unsigned char face4[] = {0,0,0,1,7,225,12,49,16,9,50,77,34,69,36,37,40,21,35,197,52,45,16,9,12,49,7,225,0,1,127,255};
  45.  
  46. //char1 3*5
  47. const unsigned char char1[] = {64,192,64,64,224};
  48.  
  49. //char2 3*5
  50. const unsigned char char2[] = {224,32,224,128,224};
  51.  
  52. //char3 3*5
  53. const unsigned char char3[] = {224,32,224,32,224};
  54.  
  55. //char4 3*5
  56. const unsigned char char4[] = {160,160,224,32,32};
  57.  
  58. //char5 3*5
  59. const unsigned char char5[] = {224,128,224,32,224};
  60.  
  61. //char6 3*5
  62. const unsigned char char6[] = {224,128,224,160,224};
  63.  
  64. //char7 3*5
  65. const unsigned char char7[] = {224,32,32,32,32};
  66.  
  67. //char8 3*5
  68. const unsigned char char8[] = {224,160,224,160,224};
  69.  
  70. //char9 3*5
  71. const unsigned char char9[] = {224,160,224,32,224};
  72.  
  73. //char0 3*5
  74. const unsigned char char0[] = {224,160,160,160,224};
  75.  
  76. //cursor 12*12 monochrome alpha
  77. const unsigned char cursor[] = {255,240,128,16,128,16,128,16,128,16,128,16,128,16,128,16,128,16,128,16,128,16,255,240};
  78. const unsigned char cursor_alpha[] = {0,0,0,0,63,192,63,192,63,192,63,192,63,192,63,192,63,192,63,192,0,0,0,0};
  79.  
  80.  
  81. void replaceAwithBinGrid(unsigned char grid[], char a, char b) //replaces a with b in the grid
  82. {
  83.     int i;
  84.     for(i=0;i<(14*10+1);i++) //explore the whole grid
  85.     {
  86.         if(grid[i]==a) {grid[i] = b;} //replace a with b
  87.     }
  88. }
  89.  
  90. void creategrid(unsigned char grid[]) //creates a new grid
  91. {
  92.     unsigned char x=0;
  93.     unsigned char y=0;
  94.     unsigned char i;
  95.     unsigned char ok=0;
  96.     unsigned char mcount=0; //mine count
  97.     unsigned char a=0;
  98.     unsigned char b=0;
  99.     replaceAwithBinGrid(grid, 0, 42); //fill w/ 42
  100.     for(i=0;i<16;i++) //16=nb of mines
  101.     {
  102.         ok = 0;
  103.         while(!ok) //tries untill finds empty spot
  104.         {
  105.             x = 1+rand()%12;
  106.             y = 1+rand()%8;
  107.             if(grid[x+14*y]==42) {ok = 1;}
  108.         }
  109.         grid[x+14*y] = 9; //puts mine
  110.     }
  111.     for(x=0;x<13;x++) //create indicators
  112.     {
  113.         for(y=0;y<9;y++)
  114.         {
  115.             if(grid[(x+1)+14*(y+1)]=42)
  116.             {
  117.                 mcount=0;
  118.                 for(a=0;a<3;a++)
  119.                 {
  120.                     for(b=0;b<3;b++)
  121.                     {
  122.                         if(grid[(x+a)+14*(y+b)]=9) {mcount++;} //if mine near
  123.                     }
  124.                 }
  125.             grid[(x+1)+14*(y+1)]=mcount;
  126.             }
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. void drawtilefromgrid(unsigned char grid[], char x, char y) //draws tile grid indicates at pos x,y
  134. {
  135.     char value = grid[x+14*y];
  136.     if(value<10) //if spot closed
  137.     {
  138.         ML_bmp_8_or(closed, (x-1)*8, (y-1)*8);
  139.     }
  140.     else if(value>19&&value<30) //flag
  141.     {
  142.         ML_bmp_8_or(flag, (x-1)*8, (y-1)*8);
  143.     }
  144.     else
  145.     {
  146.         if(value>9&&value<19)
  147.         {
  148.             ML_bmp_8_or(open, (x-1)*8, (y-1)*8);
  149.         }
  150.         switch(value) //other situations
  151.         {
  152.             case 10: //empty open
  153.             break;
  154.            
  155.             case 11:
  156.             ML_bmp_or(char1, 1+(x-1)*8, (y-1)*8, 3, 5);
  157.             break;
  158.            
  159.             case 12:
  160.             ML_bmp_or(char2, 1+(x-1)*8, (y-1)*8, 3, 5);
  161.             break;
  162.            
  163.             case 13:
  164.             ML_bmp_or(char3, 1+(x-1)*8, (y-1)*8, 3, 5);
  165.             break;
  166.            
  167.             case 14:
  168.             ML_bmp_or(char4, 1+(x-1)*8, (y-1)*8, 3, 5);
  169.             break;
  170.            
  171.             case 15:
  172.             ML_bmp_or(char5, 1+(x-1)*8, (y-1)*8, 3, 5);
  173.             break;
  174.            
  175.             case 16:
  176.             ML_bmp_or(char6, 1+(x-1)*8, (y-1)*8, 3, 5);
  177.             break;
  178.            
  179.             case 17:
  180.             ML_bmp_or(char7, 1+(x-1)*8, (y-1)*8, 3, 5);
  181.             break;
  182.            
  183.             case 18:
  184.             ML_bmp_or(char8, 1+(x-1)*8, (y-1)*8, 3, 5);
  185.             break;
  186.            
  187.             case 19: //opened mine (boom)
  188.             ML_bmp_8_or(boom, (x-1)*8, (y-1)*8);
  189.             break;
  190.            
  191.             case 30: //mine defused
  192.             ML_bmp_8_or(mine, (x-1)*8, (y-1)*8);
  193.             break;
  194.            
  195.             case 31: //wrong flag location
  196.             ML_bmp_8_or(wrong, (x-1)*8, (y-1)*8);
  197.             break;
  198.         }
  199.     }
  200. }
  201.  
  202. void refreshscreen(unsigned char grid[])
  203. {
  204.    
  205. }
  206.  
  207. void openspot(unsigned char grid[], char x, char y) //opens a spot
  208. {
  209.     grid[x+14*y] += 10;
  210.     drawtilefromgrid(grid, x, y);
  211. }
  212.  
  213. void openblanckspace(unsigned char grid[], char x, char y) //explores a blank space
  214. {
  215.     char list_x[96]; //cordinate lists
  216.     char list_y[96];
  217.     char a = 0; //list position a
  218.     char b = 1; //list position b
  219.     char c;
  220.     char d;
  221.     char e;
  222.     char f;
  223.     char value;
  224.     list_x[1] = x-1;
  225.     list_y[1] = y-1;
  226.     while(a!=b)
  227.     {
  228.         a++;
  229.         e = list_x[a];
  230.         f = list_y[a];
  231.         for(c=0;c<3;c++)
  232.         {
  233.             for(d=0;d<3;d++)
  234.             {
  235.                 value = grid[(e+c)+14*(f+d)];
  236.                 if(!value)
  237.                 {
  238.                     b++;
  239.                     list_x[b] = e+c;
  240.                     list_y[b] = f+d;
  241.                 }
  242.                 if(value!=42)
  243.                 {
  244.                     drawtilefromgrid(grid, e+c, f+d);
  245.                 }
  246.             }
  247.         }
  248.     }
  249. }
  250.  
  251. void clickspot(unsigned char grid[], char x, char y)
  252. {
  253.  
  254. }
  255.  
  256. void toggleflag(unsigned char grid[], char x, char y)
  257. {
  258.  
  259. }
  260.  
  261.  
  262. //===================================//
  263.  
  264.  
  265. int AddIn_main(int isAppli, unsigned short OptionNum)
  266. {
  267.  
  268. //actual int main() lol
  269.  
  270.     char i;
  271.     char j;
  272.     unsigned char grid[14*10];
  273.     srand(RTC_GetTicks());
  274.     creategrid(grid);
  275.  
  276.    
  277.     ML_clear_vram();
  278.     for(i=1;i<13;i++)
  279.     {
  280.         for(j=1;j<9;j++)
  281.         {
  282.             openspot(grid, i, j);
  283.         }
  284.     }
  285.     ML_display_vram();
  286.    
  287.     while(1) {}
  288.     return 1;
  289. }
  290.  
  291.  
  292.  
  293.  
  294. //========================================================================//
  295.  
  296. #pragma section R_Size
  297. unsigned long BR_Size;
  298. #pragma section
  299.  
  300.  
  301. #pragma section _TOP
  302.  
  303. int InitializeSystem(int isAppli, unsigned short OptionNum)
  304. {
  305.     return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  306. }
  307.  
  308. #pragma section
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement