root/modules/games/gui_mastermind.c

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

DEFINITIONS

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

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

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