root/modules/games/gui_mastermind.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. guess_box
  2. WinQuary
  3. CanNext
  4. CreateColorCombo
  5. gui_mastermind_init
  6. draw_box
  7. end_game
  8. chg_box
  9. gui_mastermind_kbd_process
  10. gui_mastermind_draw
  11. basic_module_init

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "keyboard.h"
   4 #include "lang.h"
   5 #include "conf.h"
   6 #include "gui.h"
   7 #include "gui_draw.h"
   8 #include "gui_lang.h"
   9 #include "gui_batt.h"
  10 #include "gui_mbox.h"
  11 #include "modes.h"
  12 
  13 #include "module_def.h"
  14 
  15 void gui_game_menu_kbd_process();
  16 int gui_mastermind_kbd_process();
  17 void gui_mastermind_draw();
  18 
  19 gui_handler GUI_MODE_MASTERMIND = 
  20     /*GUI_MODE_MASTERMIND*/ { GUI_MODE_MODULE, gui_mastermind_draw, gui_mastermind_kbd_process, gui_game_menu_kbd_process, 0, GUI_MODE_FLAG_NODRAWRESTORE };
  21 
  22 #define BORDER                          20
  23 #define RECT_SIZE                       10
  24 #define COLOR_LIGHT_GRAY        MAKE_COLOR(COLOR_GREY_LT,COLOR_GREY_LT)
  25 #define BK_COLOR                        MAKE_COLOR(COLOR_GREY,COLOR_GREY)
  26 #define TEXT_COLOR          MAKE_COLOR(COLOR_GREY,COLOR_BLACK)
  27 
  28 int curr_x;
  29 int curr_y;
  30 int answer[4];
  31 char colors[6];
  32 int curr_color[4];
  33 int GameGo;
  34 static char buf[128];
  35 
  36 static void guess_box(int pos, color col)
  37 {
  38         draw_rectangle(camera_screen.disp_left+100+(pos*10), BORDER+(2*curr_y*10)+2+2*curr_y,
  39                        camera_screen.disp_left+100+(pos*10)+6, BORDER+(2*curr_y*10)+8+2*curr_y, MAKE_COLOR(col,col), RECT_BORDER0|DRAW_FILLED);
  40 }
  41 
  42 char WinQuary(){
  43         char pos=0;
  44         int i, j;
  45         
  46         for (i=0; i<4; i++)
  47     {
  48                 if (answer[i] == curr_color[i])
  49         {
  50             guess_box(pos,COLOR_BLACK);
  51                         pos++;
  52                 }
  53         }
  54         
  55         if (pos == 4)
  56     {
  57                 GameGo = 0;
  58                 return 1;
  59         }
  60         
  61         for (i=0; i<4; i++)
  62     {
  63         for (j=0; j<4; j++)
  64         {
  65                     if ((answer[i] == curr_color[j]) && (i != j))
  66             {
  67                 guess_box(pos,COLOR_WHITE);
  68                             pos++;
  69             }
  70                 }
  71         }
  72 
  73         if(curr_y==0)
  74     {
  75                 GameGo = 0;
  76                 return 2;
  77         }
  78 
  79         return 0;
  80 }
  81 
  82 char CanNext(){
  83         if (curr_color[0]==99 || curr_color[1]==99 || curr_color[2]==99 || curr_color[3]==99)
  84                 return 0;
  85         else if (curr_color[0]==curr_color[1] || curr_color[0]==curr_color[2] || curr_color[0]==curr_color[3] ||
  86                      curr_color[1]==curr_color[2] || curr_color[1]==curr_color[3] || curr_color[2]==curr_color[3])
  87     {
  88                 draw_string(camera_screen.disp_left+167, 130, lang_str(LANG_MENU_GAMES_DCOLOR), TEXT_COLOR);
  89                 return 0;
  90         }
  91         return 1;
  92 }
  93 
  94 void CreateColorCombo(){
  95         char tmp=0;
  96         int i=0;
  97         
  98         for (i=0; i<4; i++) answer[i] = 99;
  99         i = 0;
 100         while (i<4)
 101     {
 102                 tmp = rand() % 6;
 103                 if (answer[0]!=tmp && answer[1]!=tmp && answer[2]!=tmp && answer[3]!=tmp)
 104         {
 105                         answer[i] = tmp;
 106                         i++;
 107                 }
 108         }
 109 }
 110 
 111 int gui_mastermind_init() {
 112         int i,j;
 113 
 114         curr_x=0;
 115         curr_y=7;
 116         
 117         draw_rectangle(camera_screen.disp_left, 0, camera_screen.disp_right, camera_screen.height-1, BK_COLOR, RECT_BORDER0|DRAW_FILLED);
 118 
 119         for (i=0;i<4;i++)
 120                 for (j=0;j<8;j++)
 121                         draw_rectangle(camera_screen.disp_left+BORDER+(2*i*10), BORDER+(2*j*10)+2*j,
 122                                        camera_screen.disp_left+BORDER+(2*i*10)+10, BORDER+(2*j*10)+2*j+10 , COLOR_LIGHT_GRAY, RECT_BORDER0|DRAW_FILLED);
 123 
 124         draw_rectangle(camera_screen.disp_left+10, BORDER+(2*j*10)+2*j, camera_screen.disp_left+150,BORDER+(2*j*10)+2*j+1, MAKE_COLOR(COLOR_GREY,COLOR_WHITE), RECT_BORDER1|DRAW_FILLED);
 125         draw_rectangle(camera_screen.disp_left+148, 10, camera_screen.disp_left+149,230, MAKE_COLOR(COLOR_GREY,COLOR_WHITE), RECT_BORDER1|DRAW_FILLED);
 126         draw_rectangle(camera_screen.disp_left+151, 10, camera_screen.disp_left+152,230, MAKE_COLOR(COLOR_GREY,COLOR_WHITE), RECT_BORDER1|DRAW_FILLED);
 127         
 128         for (i=0; i<6;i++)
 129                 draw_rectangle(camera_screen.disp_left+BORDER+(2*(i+7)*10)+20, 40, camera_screen.disp_left+BORDER+(2*(i+7)*10)+35,55, MAKE_COLOR(colors[i],colors[i]), RECT_BORDER0|DRAW_FILLED);
 130                 
 131         draw_rectangle(camera_screen.disp_left+BORDER+(2*7*10)+20, 65, camera_screen.disp_left+BORDER+(2*7*10)+35,80, MAKE_COLOR(COLOR_BLACK,COLOR_BLACK), RECT_BORDER0|DRAW_FILLED);
 132         draw_string(camera_screen.disp_left+BORDER+(2*7*10)+50, 65 , lang_str(LANG_MENU_GAMES_RIGHT_PLACE), TEXT_COLOR);
 133         
 134         draw_rectangle(camera_screen.disp_left+BORDER+(2*7*10)+20, 90, camera_screen.disp_left+BORDER+(2*7*10)+35,105, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
 135         draw_string(camera_screen.disp_left+BORDER+(2*7*10)+50, 90 , lang_str(LANG_MENU_GAMES_C_IN_ANSWER), TEXT_COLOR);
 136                                         
 137         draw_string(camera_screen.disp_left+173, 20 , lang_str(LANG_MENU_GAMES_AC_COLORS), TEXT_COLOR);
 138                 
 139         draw_string(camera_screen.disp_left+167, 200 , lang_str(LANG_MENU_GAMES_CURSOR1), TEXT_COLOR);
 140         draw_string(camera_screen.disp_left+167, 185 , lang_str(LANG_MENU_GAMES_CURSOR2), TEXT_COLOR);
 141         draw_string(camera_screen.disp_left+167, 170 , lang_str(LANG_MENU_GAMES_CURSOR3), TEXT_COLOR);
 142 
 143         for(i=0;i<4;i++) curr_color[i]=99;
 144         
 145     gui_set_mode(&GUI_MODE_MASTERMIND);
 146         return 1;
 147 }
 148 
 149 //-------------------------------------------------------------------
 150 static void draw_box(color border)
 151 {
 152     if (curr_color[curr_x] == 99) curr_color[curr_x] = 0;
 153     draw_rectangle(camera_screen.disp_left+BORDER+(2*curr_x*10), BORDER+(2*curr_y*10)+2*curr_y,
 154                    camera_screen.disp_left+BORDER+(2*curr_x*10)+10, BORDER+(2*curr_y*10)+2*curr_y+10, MAKE_COLOR(colors[curr_color[curr_x]],border), RECT_BORDER1|DRAW_FILLED);
 155 }
 156 
 157 static void end_game(int msg)
 158 {
 159     int i;
 160     draw_string(camera_screen.disp_left+198, 130 , lang_str(msg), TEXT_COLOR);
 161         for (i=0; i<4;i++)
 162             draw_rectangle(camera_screen.disp_left+BORDER+(2*i*10), 213,
 163                            camera_screen.disp_left+BORDER+(2*i*10)+10,223,  MAKE_COLOR(colors[answer[i]],colors[answer[i]]), RECT_BORDER0|DRAW_FILLED);
 164 }
 165 
 166 static void chg_box(int inc_box, int inc_val)
 167 {
 168     draw_box(colors[curr_color[curr_x]]);
 169     curr_x = (curr_x + inc_box) & 3;
 170     if (curr_color[curr_x] == 99) curr_color[curr_x] = 0;
 171     curr_color[curr_x] = (curr_color[curr_x] + inc_val);
 172     if (curr_color[curr_x] < 0) curr_color[curr_x] = 5;
 173     else if (curr_color[curr_x] > 5) curr_color[curr_x] = 0;
 174     draw_box(COLOR_BLACK);
 175 }
 176 
 177 int gui_mastermind_kbd_process() {
 178         int i=0;
 179         rand();
 180     long key = kbd_get_autoclicked_key();
 181     if (key)
 182     {
 183         if (GameGo == 1)
 184         {
 185             draw_string(camera_screen.disp_left+167, 130 , "                       ", TEXT_COLOR);
 186                 switch (key) 
 187                 {
 188                         case KEY_SET:
 189                                 if(CanNext())
 190                     {
 191                                         if(WinQuary() == 1)
 192                         {
 193                             end_game(LANG_MENU_GAMES_RIGHT);
 194                                         }
 195                                         else if(WinQuary() == 2)
 196                         {
 197                             end_game(LANG_MENU_GAMES_GAME_OVER);
 198                                         }
 199                                         else
 200                         {
 201                             draw_box(colors[curr_color[curr_x]]);
 202                                                 for(i=0;i<4;i++) curr_color[i]=99;
 203                                                 curr_y--;
 204                                                 curr_x=0;
 205                             draw_box(COLOR_BLACK);
 206                                         }
 207                                 }
 208                                 break;
 209                         case KEY_LEFT:
 210                     chg_box(-1,0);
 211                                 break;
 212                         case KEY_RIGHT:
 213                     chg_box(1,0);
 214                                 break;
 215                         case KEY_UP:
 216                     chg_box(0,1);
 217                                 break;
 218                         case KEY_DOWN:
 219                     chg_box(0,-1);
 220                                 break;
 221                 }
 222         }
 223         else if (key == KEY_SET)
 224         {
 225                         gui_mastermind_init();
 226                         CreateColorCombo();
 227             draw_box(COLOR_BLACK);
 228                         GameGo=1;
 229         }
 230     }
 231     return 0;
 232 }
 233 //-------------------------------------------------------------------
 234 void gui_mastermind_draw() {
 235     static struct tm *ttm;
 236 
 237     draw_string(camera_screen.disp_left+15*FONT_WIDTH, 0, lang_str(LANG_MENU_GAMES_MASTERMIND), MAKE_COLOR(COLOR_GREY, COLOR_WHITE));
 238 
 239     ttm = get_localtime();
 240     sprintf(buf, "Time: %2u:%02u  Batt:%3d%%", ttm->tm_hour, ttm->tm_min, get_batt_perc());
 241     draw_string_justified(camera_screen.disp_left, camera_screen.height-FONT_HEIGHT,
 242                           buf, TEXT_COLOR, 0, camera_screen.disp_width-FONT_WIDTH, TEXT_RIGHT);
 243 }
 244 
 245 
 246 int basic_module_init() {
 247         colors[0] = COLOR_RED;
 248         colors[1] = COLOR_GREEN;
 249         colors[2] = COLOR_BLUE;
 250         colors[3] = COLOR_YELLOW;
 251         colors[4] = COLOR_WHITE;
 252         colors[5] = COLOR_BLACK;
 253 
 254         return gui_mastermind_init(); 
 255 }
 256 
 257 #include "simple_game.c"
 258 
 259 /******************** Module Information structure ******************/
 260 
 261 ModuleInfo _module_info =
 262 {
 263     MODULEINFO_V1_MAGICNUM,
 264     sizeof(ModuleInfo),
 265     SIMPLE_MODULE_VERSION,              // Module version
 266 
 267     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,                       // Requirements of CHDK version
 268     ANY_PLATFORM_ALLOWED,               // Specify platform dependency
 269 
 270     -LANG_MENU_GAMES_MASTERMIND,// Module name
 271     MTYPE_GAME,
 272 
 273     &_librun.base,
 274 
 275     ANY_VERSION,                // CONF version
 276     CAM_SCREEN_VERSION,         // CAM SCREEN version
 277     ANY_VERSION,                // CAM SENSOR version
 278     ANY_VERSION,                // CAM INFO version
 279 };

/* [<][>][^][v][top][bottom][index][help] */