Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "all.h"
  3.  
  4. void init_board()
  5. {
  6. int i, x, y, z;
  7. int x1, y1;
  8.  
  9. for (x = 0; x < 8; x++)
  10.         for (y = 0; y < 8; y++)
  11.                 for (z = 0; z < 4; z++)
  12.                         board[x][y][z] = 0;
  13.  
  14. for (i = 0; i < 4; i++)
  15.         board[rand()%8][rand()%8][LAY_HIDDEN] = 1;
  16.  
  17. foxN = 0;
  18. for (x = 0; x < 8; x++)
  19.         for (y = 0; y < 8; y++)
  20.                 if (board[x][y][LAY_HIDDEN] == 1)
  21.                         foxN++;
  22.  
  23. if (foxN < 4) // if some foxes are in one hole
  24.         {
  25.         init_board();
  26.         return;
  27.         }
  28.  
  29. // count of visible foxes      
  30. for (x = 0; x < 8; x++)
  31.         for (y = 0; y < 8; y++)
  32.                 {
  33.  
  34.                 for (x1 = 0; x1 < 8; x1++) // horizontal
  35.                         if (board[x1][y][LAY_HIDDEN] == 1)
  36.                                 board[x][y][LAY_NUM]++;
  37.  
  38.                                
  39.                 for (y1 = 0; y1 < 8; y1++) // vertical
  40.                         if (board[x][y1][LAY_HIDDEN] == 1)
  41.                                 board[x][y][LAY_NUM]++;
  42.  
  43.  
  44.                 if (x-y>0) // north-west to south-east
  45.                         x1 = x-y;
  46.                 else
  47.                         x1 = 0;
  48.  
  49.                 if (y-x>0)
  50.                         y1 = y-x;
  51.                 else
  52.                         y1 = 0;
  53.                        
  54.                 do
  55.                         {
  56.                         if (board[x1][y1][LAY_HIDDEN] == 1)
  57.                                 board[x][y][LAY_NUM]++;
  58.                         x1++;
  59.                         y1++;
  60.                         } while ((x1<8)&&(y1<8));
  61.  
  62.                        
  63.                 if (x+y-7>0) // south-west to north-east
  64.                         x1 = x+y-7;
  65.                 else
  66.                         x1 = 0;
  67.  
  68.                 if (x+y<8)
  69.                         y1 = x+y;
  70.                 else
  71.                         y1 = 7;
  72.                        
  73.                 do
  74.                         {
  75.                         if (board[x1][y1][LAY_HIDDEN] == 1)
  76.                                 board[x][y][LAY_NUM]++;
  77.                         x1++;
  78.                         y1--;
  79.                         } while ((x1<8)&&(y1>-1));
  80.                        
  81.                        
  82.                 if (board[x][y][LAY_HIDDEN] == 1)
  83.                         board[x][y][LAY_NUM] -= 3;
  84.                        
  85.                 }
  86.  
  87. foxLeft = foxN;
  88. moves = 0;
  89. result = 0;
  90.                
  91. }
  92.  
  93. void init_grid_sizes()
  94. {
  95. size = 20;
  96. x_start = 5;
  97. y_start = 30;
  98. window_width = 2*x_start + 8*size + 9;
  99. window_height = y_start + x_start + 8*size + kol_skin_height() + 9 + 14;
  100. }
  101.  
  102. void wnd_draw()
  103. {
  104.  
  105. int i;
  106. int x, y;
  107. int x1, y1;
  108. char tmp[64];
  109. char tmp2[64];
  110.  
  111. kol_paint_start();
  112.  
  113. kol_wnd_define(100, 100, window_width, window_height, 0x34ddddff, 0x34ddddff, "FoxHunt v0.2 by Albom");
  114.  
  115. kol_btn_define(x_start, x_start, 40, 16, 2, 0xccccee);
  116. kol_paint_string(x_start+20-12, x_start+5, "New", 0x902222ff);
  117.  
  118. strcpy(tmp, "F:");
  119. itoa(foxLeft, tmp2);
  120. strcat(tmp, tmp2);
  121. strcat(tmp, "/4");
  122. kol_paint_string(x_start+55, x_start+5, tmp, 0x902222ff);
  123.  
  124. strcpy(tmp, "M:");
  125. itoa(moves, tmp2);
  126. strcat(tmp, tmp2);
  127. kol_paint_string(x_start+110, x_start+5, tmp, 0x902222ff);
  128.  
  129. for (i = 0; i <= 8; i++)
  130.         {
  131.         kol_paint_line(x_start+i*size, y_start, x_start+i*size, y_start+8*size, 0x33); // vertical
  132.         kol_paint_line(x_start, y_start+i*size, x_start+8*size, y_start+i*size, 0x33); // horizontal
  133.         }
  134.  
  135. // foxes found by user 
  136. for (x1 = 0; x1 < 8; x1++)
  137.         for (y1 = 0; y1 < 8; y1++)
  138.                 {
  139.                 if (board[x1][y1][LAY_FOUND]==1)
  140.                         {
  141.                         grid_to_pos(x1, y1, &x, &y);
  142.                         kol_paint_string(x+size/2-4, y+size/2-4, "F", 0x90227722);
  143.                         }
  144.                 }      
  145.  
  146. // opened cells
  147. for (x1 = 0; x1 < 8; x1++)
  148.         for (y1 = 0; y1 < 8; y1++)
  149.         {
  150.         if (board[x1][y1][LAY_OPENED] == 1)
  151.                 {
  152.                 itoa(board[x1][y1][LAY_NUM], tmp);
  153.                 grid_to_pos(x1, y1, &x, &y);
  154.                 kol_paint_string(x+size/2-4, y+size/2-4, tmp, 0x902222ff);
  155.                 }
  156.         }              
  157.                
  158. if (result > 0)
  159.         {
  160.         char victory[]={"V I C T O R Y !"};
  161.         kol_paint_string(window_width/2-7*8+4, window_height-14-kol_skin_height()-4, victory, 0x90227722);
  162.         kol_paint_string(window_width/2-7*8+4-1, window_height-14-kol_skin_height()-4, victory, 0x90227722);
  163.         kol_paint_string(window_width/2-7*8+4+1, window_height-14-kol_skin_height()-4, victory, 0x90227722);
  164.         }
  165.  
  166. if (result < 0)
  167.         {
  168.  
  169.         // draw real position of foxes
  170.         for (x1 = 0; x1 < 8; x1++)
  171.                 for (y1 = 0; y1 < 8; y1++)
  172.                         {
  173.                         if (board[x1][y1][LAY_HIDDEN]==1)
  174.                                 {
  175.                                 grid_to_pos(x1, y1, &x, &y);
  176.                                 kol_paint_string(x+size/2-2, y+size/2-2, "F", 0x90ff2222);
  177.                                 }
  178.                         }              
  179.                        
  180.         char game_over[]={"G A M E   O V E R !"};
  181.         kol_paint_string(window_width/2-9*8+4, window_height-14-kol_skin_height()-4, game_over, 0x90ff2222);
  182.         kol_paint_string(window_width/2-9*8+4-1, window_height-14-kol_skin_height()-4, game_over, 0x90ff2222);
  183.         kol_paint_string(window_width/2-9*8+4+1, window_height-14-kol_skin_height()-4, game_over, 0x90ff2222);
  184.         }      
  185.                
  186. kol_paint_end();
  187.  
  188.  
  189.  
  190. }
  191.  
  192.  
  193. int check()
  194. {
  195. int x, y;
  196. for (x = 0; x < 8; x++)
  197.         for (y = 0; y < 8; y++)
  198.                 if (board[x][y][LAY_HIDDEN] != board[x][y][LAY_FOUND])
  199.                         return -1;
  200. return 1;
  201. }
  202.  
  203.  
  204. void grid_to_pos(unsigned gx, unsigned gy, unsigned* x, unsigned* y)
  205. {
  206. *x = gx*size + x_start;
  207. *y = gy*size + y_start;
  208. }
  209.  
  210.  
  211. int pos_to_grid(unsigned x, unsigned y, int* gx, int* gy)
  212. {
  213.  
  214. *gx = (x - x_start)/size;
  215. *gy = (y - y_start)/size;
  216.  
  217. if ((*gx < 0) || (*gx>7) || (*gy < 0) || (*gy>7) )
  218.         return -1;
  219. else
  220.         return 1;
  221. }
  222.  
  223.  
  224. /// ===========================================================
  225.  
  226. void kol_main()
  227. {
  228.  
  229. unsigned event;
  230. unsigned key;
  231. unsigned btn;
  232. unsigned pos, x, y;
  233. int gx, gy;
  234.  
  235.  
  236. srand(kol_system_time_get());
  237.  
  238. kol_event_mask( 0xC0000027 ); // enable using of mouse
  239. init_grid_sizes();
  240. init_board();
  241. wnd_draw();
  242.  
  243.  
  244. for(;;)
  245.         {
  246.         event = kol_event_wait();
  247.  
  248.         switch (event)
  249.                 {
  250.                 case 1:
  251.                         wnd_draw();
  252.                         break;
  253.  
  254.                 case 2:
  255.                         key = (kol_key_get() & 0xff00)>>8;
  256.                         break;
  257.                        
  258.                 case 3:
  259.                         switch ((kol_btn_get() & 0xff00)>>8)
  260.                                 {
  261.                                 case 1: // close button
  262.                                         kol_exit();
  263.                                 case 2: // 'new' button
  264.                                         init_board();
  265.                                         wnd_draw();
  266.                                         break;
  267.                                 }
  268.                         break;
  269.                        
  270.                 case 6:
  271.                         btn = kol_mouse_btn(); // read mouse button
  272.                         pos = kol_mouse_posw(); // read mouse position
  273.                         x = pos / 65536;
  274.                         y = pos % 65536;
  275.                         if (x > window_width)
  276.                                 x=0;
  277.                         if (y > window_height)
  278.                                 y=0;
  279.  
  280.                         if ( pos_to_grid(x, y, &gx, &gy) > 0 )
  281.                                 {
  282.                                 switch (btn & 3)
  283.                                         {
  284.                                         case 1: // left button
  285.                                                 if (result == 0) // are we in game?
  286.                                                         {
  287.                                                         if (board[gx][gy][LAY_FOUND] == 0)
  288.                                                                 {
  289.                                                                 if ( board[gx][gy][LAY_OPENED] == 0 )
  290.                                                                         board[gx][gy][LAY_OPENED] = 1;
  291.                                                                 else
  292.                                                                         board[gx][gy][LAY_OPENED] = 0;
  293.                                                                 moves++;
  294.                                                                 wnd_draw();
  295.                                                                 }
  296.                                                         }
  297.                                                 break;
  298.                                         case 2: // right button
  299.                                                 if (result == 0) // are we in game?
  300.                                                         {
  301.                                                         if (board[gx][gy][LAY_FOUND] == 0)
  302.                                                                 {
  303.                                                                 if ( board[gx][gy][LAY_OPENED] == 0 )
  304.                                                                         {
  305.                                                                         board[gx][gy][LAY_FOUND] = 1;
  306.                                                                         foxLeft--;
  307.                                                                         moves++;
  308.                                                                         }
  309.                                                                 }
  310.                                                         else
  311.                                                                 {
  312.                                                                 board[gx][gy][LAY_FOUND] = 0;
  313.                                                                 foxLeft++;
  314.                                                                 moves++;
  315.                                                                 }
  316.                                                                
  317.                                                         if (foxLeft == 0) // all 4 foxes are marked
  318.                                                                 result = check();
  319.                                                                
  320.                                                         wnd_draw();
  321.                                                         }
  322.                                                 break;
  323.                                         }
  324.                                 }
  325.                                 break;
  326.                 }
  327.  
  328.        
  329.         }
  330.  
  331. kol_exit();
  332. }
  333.  
  334. /// ===========================================================
  335.