CHDK_DE Vorschauversion  Trunk Rev. 5234
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_4wins.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.h"
#include "keyboard.h"
#include "lang.h"
#include "conf.h"
#include "gui.h"
#include "gui_osd.h"
#include "gui_draw.h"
#include "gui_lang.h"
#include "gui_batt.h"
#include "gui_mbox.h"
#include "modes.h"
#include "module_def.h"
#include "simple_game.c"
+ Include-Abhängigkeitsdiagramm für gui_4wins.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define XBORDER   (camera_screen.disp_left+FONT_WIDTH)
 
#define RECT_SIZE   rect_size
 
#define STONE_SIZE   (rect_size/2-5)
 
#define BORDER_TOP   (FONT_HEIGHT + 10)
 
#define FIELD_HEIGHT   7
 
#define FIELD_WIDTH   7
 
#define P1_COLOR   COLOR_BLUE
 
#define P2_COLOR   COLOR_GREEN
 
#define BK_COLOR   COLOR_GREY
 
#define FIELD_COLOR   MAKE_COLOR(COLOR_GREY_LT,COLOR_GREY_LT)
 
#define TEXT_COLOR   MAKE_COLOR(COLOR_GREY, COLOR_WHITE)
 
#define INFO_COLOR   COLOR_GREY_LT
 
#define INFO_TEXT_COLOR   MAKE_COLOR(INFO_COLOR, COLOR_WHITE)
 

Funktionen

void gui_game_menu_kbd_process ()
 
int gui_4wins_kbd_process ()
 
void gui_4wins_draw ()
 
static void stone (int column, int row, color cl)
 
static void cursor (int column, color cl)
 
static char isFull (int column)
 
static char set_stone (int column, char player, char visible)
 
static void unset_stone (int column)
 
char ki_isBadColumn (int column)
 
char ki_3 (char player)
 
char ki_2 (char player)
 
char ki_findColumn (char mode, char player)
 
void draw_txt_message (char *text)
 
static void change_player ()
 
static char win_query ()
 
void win ()
 
void draw_mode ()
 
void change_mode ()
 
void set ()
 
static void move_cursor (int in_x_pos)
 
int gui_4wins_init ()
 
int basic_module_init ()
 

Variablen

gui_handler GUI_MODE_4WINS
 
int rect_size = 30
 
char cursor_position
 
char cur_player =1
 
char field [FIELD_HEIGHT][FIELD_WIDTH]
 
char finished =0
 
char badColumns [7]
 
char in_game =0
 
char mode_rival =0
 
char count_win [2] ={0,0}
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define BK_COLOR   COLOR_GREY

Definiert in Zeile 34 der Datei gui_4wins.c.

#define BORDER_TOP   (FONT_HEIGHT + 10)

Definiert in Zeile 29 der Datei gui_4wins.c.

#define FIELD_COLOR   MAKE_COLOR(COLOR_GREY_LT,COLOR_GREY_LT)

Definiert in Zeile 35 der Datei gui_4wins.c.

#define FIELD_HEIGHT   7

Definiert in Zeile 30 der Datei gui_4wins.c.

#define FIELD_WIDTH   7

Definiert in Zeile 31 der Datei gui_4wins.c.

#define INFO_COLOR   COLOR_GREY_LT

Definiert in Zeile 37 der Datei gui_4wins.c.

#define INFO_TEXT_COLOR   MAKE_COLOR(INFO_COLOR, COLOR_WHITE)

Definiert in Zeile 38 der Datei gui_4wins.c.

#define P1_COLOR   COLOR_BLUE

Definiert in Zeile 32 der Datei gui_4wins.c.

#define P2_COLOR   COLOR_GREEN

Definiert in Zeile 33 der Datei gui_4wins.c.

#define RECT_SIZE   rect_size

Definiert in Zeile 27 der Datei gui_4wins.c.

#define STONE_SIZE   (rect_size/2-5)

Definiert in Zeile 28 der Datei gui_4wins.c.

#define TEXT_COLOR   MAKE_COLOR(COLOR_GREY, COLOR_WHITE)

Definiert in Zeile 36 der Datei gui_4wins.c.

#define XBORDER   (camera_screen.disp_left+FONT_WIDTH)

Definiert in Zeile 26 der Datei gui_4wins.c.

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 384 der Datei gui_4wins.c.

385 {
386  return gui_4wins_init();
387 }
void change_mode ( )

Definiert in Zeile 278 der Datei gui_4wins.c.

279 {
280  cur_player = 1;
281  count_win[0] = count_win[1] = 0;
282  if (mode_rival == 1)
283  mode_rival = 0; //CAM
284  else
285  mode_rival = 1; //HUMAN
286  draw_mode();
287 }
static void change_player ( )
static

Definiert in Zeile 207 der Datei gui_4wins.c.

208 {
209  if (cur_player == 1)
210  cur_player = 2;
211  else
212  cur_player = 1;
214 }
static void cursor ( int  column,
color  cl 
)
static

Definiert in Zeile 53 der Datei gui_4wins.c.

54 {
55  stone(column+1, 7, cl);
56 }
void draw_mode ( )

Definiert in Zeile 272 der Datei gui_4wins.c.

273 {
274  int x = camera_screen.disp_right - 15 * FONT_WIDTH;
276 }
void draw_txt_message ( char *  text)

Definiert in Zeile 195 der Datei gui_4wins.c.

196 {
197  coord w, x, y;
199  w = (strlen(text) + 2) * FONT_WIDTH;
200 
201  x = (camera_screen.width - w) >> 1;
202  y = ((camera_screen.height) >> 1);
204  draw_string_justified(x, y + 4, text, cl, 0, w, TEXT_CENTER);
205 }
void gui_4wins_draw ( )

Definiert in Zeile 412 der Datei gui_4wins.c.

413 {
414  static char str[16];
415  sprintf(str, "Batt: %3d%%", get_batt_perc());
418 }
int gui_4wins_init ( )

Definiert in Zeile 329 der Datei gui_4wins.c.

330 {
331  static char str[16];
332  int i = 0, j = 0;
333 
334  if (camera_screen.height * 3 > camera_screen.width * 2)
335  {
336  // worst case scenario (640x480)
337  rect_size = 8 * (camera_screen.height - FONT_HEIGHT - 20) / (9 * FIELD_HEIGHT);
338  }
339  else
340  {
342  }
343 
344  cursor_position = 3;
345  finished = in_game = 0;
346  srand(time(NULL));
347 
353 
354  for (i = 0; i < 7; i++)
355  {
356  for (j = 0; j < 6; j++)
357  {
358  field[i][j + 1] = 0;
359  stone(i + 1, j + 1, BK_COLOR);
360  }
361  }
362  for (i = 0; i < 7; i++)
363  {
364  field[i][0] = 9;
365  }
366 
367  move_cursor(0);
369  sprintf(str, "%d", count_win[0]);
371  sprintf(str, ":");
373  sprintf(str, "%d", count_win[1]);
375  draw_mode();
376  if (cur_player == 2 && !mode_rival)
377  set();
378 
380 
381  return 1;
382 }
int gui_4wins_kbd_process ( )

Definiert in Zeile 390 der Datei gui_4wins.c.

391 {
392  switch (kbd_get_autoclicked_key())
393  {
394  case KEY_SET:
395  if (!finished)
396  set();
397  else
398  gui_4wins_init();
399  break;
400  case KEY_LEFT:
401  if (finished == 0)
402  move_cursor(-1);
403  break;
404  case KEY_RIGHT:
405  if (finished == 0)
406  move_cursor(1);
407  break;
408  }
409  return 0;
410 }
void gui_game_menu_kbd_process ( )

Definiert in Zeile 7 der Datei simple_game.c.

8 {
9  running = 0;
11 }
static char isFull ( int  column)
static

Definiert in Zeile 58 der Datei gui_4wins.c.

59 {
60  if (field[column - 1][6])
61  return 1;
62  else
63  return 0;
64 }
char ki_2 ( char  player)

Definiert in Zeile 135 der Datei gui_4wins.c.

135  {
136  int i,j;
137  //waagerecht und Diagonal ber Anstieg
138  for(i=0;i<=3;i++){ //column
139  for(j=1;j<=6;j++){ //row
140  if(field[i][j]==player&&field[i+1][j]==player&&field[i+2][j]==0&&field[i+3][j]==0&&(field[i+2][j-1]||field[i+3][j-1])&&!ki_isBadColumn(i+3)) return i+3; //**-- return und if ndern
141  if(field[i][j]==player&&field[i+1][j]==0&&field[i+2][j]==0&&field[i+3][j]==player&&(field[i+1][j-1]||field[i+2][j-1])&&!ki_isBadColumn(i+2)) return i+2; //*--* (i+3 geht auch)
142  if(field[i][j]==0&&field[i+1][j]==0&&field[i+2][j]==player&&field[i+3][j]==player&&(field[i][j-1]||field[i+1][j-1])&&!ki_isBadColumn(i+2)) return i+2; //--**
143  if(field[i][j]==0&&field[i+1][j]==player&&field[i+2][j]==0&&field[i+3][j]==player&&(field[i][j-1]||field[i+2][j-1])&&!ki_isBadColumn(i+3)) return i+3; //-*-*
144  if(field[i][j]==player&&field[i+1][j]==0&&field[i+2][j]==player&&field[i+3][j]==0&&(field[i+1][j-1]||field[i+3][j-1])&&!ki_isBadColumn(i+2)) return i+2; //*-*-
145  }
146  for(j=1;j<=3;j++){
147  if(field[i][j]==player&&field[i+1][j+1]==player&&field[i+2][j+2]==0&&field[i+3][j+3]==0&&field[i+2][j+2-1]&&!ki_isBadColumn(i+3)) return i+3; //**-- return und if ndern
148  if(field[i][j]==player&&field[i+1][j+1]==player&&field[i+2][j+2]==0&&field[i+3][j+3]==0&&field[i+3][j+3-1]&&!ki_isBadColumn(i+3)) return i+4; //**-- return und if ndern
149  if(field[i][j]==player&&field[i+1][j+1]==0&&field[i+2][j+2]==0&&field[i+3][j+3]==player&&(field[i+1][j+1-1]||field[i+2][j+2-1])&&!ki_isBadColumn(i+2)) return i+2; //*--* (i+3 geht auch)
150  if(field[i][j]==0&&field[i+1][j+1]==0&&field[i+2][j+2]==player&&field[i+3][j+3]==player&&(field[i][j-1]||field[i+1][j+1-1])&&!ki_isBadColumn(i+2)) return i+2; //--**
151  if(field[i][j]==0&&field[i+1][j+1]==player&&field[i+2][j+2]==0&&field[i+3][j+3]==player&&(field[i][j-1]||field[i+2][j+2-1])&&!ki_isBadColumn(i+3)) return i+3; //-*-*
152  if(field[i][j]==player&&field[i+1][j+1]==0&&field[i+2][j+2]==player&&field[i+3][j+3]==0&&(field[i+1][j+1-1]||field[i+3][j+3-1])&&!ki_isBadColumn(i+2)) return i+2; //*-*-
153  }
154  for(j=4;j<=6;j++){
155  if(field[i][j]==player&&field[i+1][j-1]==player&&field[i+2][j-2]==0&&field[i+3][j-3]==0&&field[i+2][j-2-1]&&!ki_isBadColumn(i+3)) return i+3; //**-- return und if ndern
156  if(field[i][j]==player&&field[i+1][j-1]==player&&field[i+2][j-2]==0&&field[i+3][j-3]==0&&field[i+3][j-3-1]&&!ki_isBadColumn(i+3)) return i+4; //**-- return und if ndern
157  if(field[i][j]==player&&field[i+1][j-1]==0&&field[i+2][j-2]==0&&field[i+3][j-3]==player&&(field[i+1][j-1-1]||field[i+2][j-2-1])&&!ki_isBadColumn(i+2)) return i+2; //*--* (i+3 geht auch)
158  if(field[i][j]==0&&field[i+1][j-1]==0&&field[i+2][j-2]==player&&field[i+3][j-3]==player&&(field[i][j-1]||field[i+1][j-1-1])&&!ki_isBadColumn(i+2)) return i+2; //--**
159  if(field[i][j]==0&&field[i+1][j-1]==player&&field[i+2][j-2]==0&&field[i+3][j-3]==player&&(field[i][j-1]||field[i+2][j-2-1])&&!ki_isBadColumn(i+3)) return i+3; //-*-*
160  if(field[i][j]==player&&field[i+1][j-1]==0&&field[i+2][j-2]==player&&field[i+3][j-3]==0&&(field[i+1][j-1-1]||field[i+3][j-3-1])&&!ki_isBadColumn(i+2)) return i+2; //*-*-
161  }
162  }
163  //senkrecht
164  for(i=0;i<=6;i++) for(j=1;j<=3;j++) if(field[i][j]==player&&field[i][j+1]==player&&field[i][j+2]==0&&!ki_isBadColumn(i+1)) return i+1;
165  return 0;
166 }
char ki_3 ( char  player)

Definiert in Zeile 100 der Datei gui_4wins.c.

101 {
102  int i,j;
103  //waagerecht und Diagonal (ber Anstieg)
104  for(i=0;i<=3;i++){ // column
105  for(j=1;j<=6;j++){ //row
106  if(field[i][j]==player&&field[i+1][j]==player&&field[i+2][j]==player&&field[i+3][j]==0&&field[i+3][j-1]) return i+4; //***-
107  if(field[i][j]==player&&field[i+1][j]==player&&field[i+2][j]==0&&field[i+3][j]==player&&field[i+2][j-1]) return i+3; //**-*
108  if(field[i][j]==player&&field[i+1][j]==0&&field[i+2][j]==player&&field[i+3][j]==player&&field[i+1][j-1]) return i+2; //*-**
109  if(field[i][j]==0&&field[i+1][j]==player&&field[i+2][j]==player&&field[i+3][j]==player&&field[i][j-1]) return i+1; //-***
110  }
111  for(j=1;j<=3;j++){
112  if(field[i][j]==player&&field[i+1][j+1]==player&&field[i+2][j+2]==player&&field[i+3][j+3]==0&&field[i+3][j+3-1]) return i+4; //***-
113  if(field[i][j]==player&&field[i+1][j+1]==player&&field[i+2][j+2]==0&&field[i+3][j+3]==player&&field[i+2][j+2-1]) return i+3; //**-*
114  if(field[i][j]==player&&field[i+1][j+1]==0&&field[i+2][j+2]==player&&field[i+3][j+3]==player&&field[i+1][j+1-1]) return i+2; //*-**
115  if(field[i][j]==0&&field[i+1][j+1]==player&&field[i+2][j+2]==player&&field[i+3][j+3]==player&&field[i][j-1]) return i+1; //-***
116  }
117  for(j=4;j<=6;j++){
118  if(field[i][j]==player&&field[i+1][j-1]==player&&field[i+2][j-2]==player&&field[i+3][j-3]==0&&field[i+3][j-3-1]) return i+4; //***-
119  if(field[i][j]==player&&field[i+1][j-1]==player&&field[i+2][j-2]==0&&field[i+3][j-3]==player&&field[i+2][j-2-1]) return i+3; //**-*
120  if(field[i][j]==player&&field[i+1][j-1]==0&&field[i+2][j-2]==player&&field[i+3][j-3]==player&&field[i+1][j-1-1]) return i+2; //*-**
121  if(field[i][j]==0&&field[i+1][j-1]==player&&field[i+2][j-2]==player&&field[i+3][j-3]==player&&field[i][j-1]) return i+1; //-***
122  }
123  }
124  //Unterreihentrick in allen ebenen
125  for(i=0;i<=2;i++){
126  for(j=1;j<=6;j++) if(field[i][j]==0&&field[i+1][j]==player&&field[i+2][j]==0&&field[i+3][j]==player&&field[i+4][j]==0&&field[i][j-1]&&field[i+2][j-1]&&field[i+4][j-1]) return i+3; //_*_*_
127  for(j=1;j<=3;j++) if(field[i][j]==0&&field[i+1][j+1]==player&&field[i+2][j+2]==0&&field[i+3][j+3]==player&&field[i+4][j+4]==0&&field[i][j-1]&&field[i+2][j+2-1]&&field[i+4][j+4-1]) return i+3; //_*_*_
128  for(j=4;j<=6;j++) if(field[i][j]==0&&field[i+1][j-1]==player&&field[i+2][j-2]==0&&field[i+3][j-3]==player&&field[i+4][j-4]==0&&field[i][j-1]&&field[i+2][j-2-1]&&field[i+4][j-4-1]) return i+3; //_*_*_
129  }
130  //senkrecht
131  for(i=0;i<=6;i++) for(j=1;j<=3;j++) if(field[i][j]==player&&field[i][j+1]==player&&field[i][j+2]==player&&field[i][j+3]==0) return i+1;
132  return 0;
133 }
char ki_findColumn ( char  mode,
char  player 
)

Definiert in Zeile 168 der Datei gui_4wins.c.

168  { //player = 1|2
169  char erg=0, cam=player, otherplayer, i, counter=0;
170  otherplayer=(player==1)?2:1;
171  switch(mode) {
172  case 'b':
173  case 1: erg=ki_3(cam); //ich3
174  if(!erg&&mode!='b') erg=ki_3(otherplayer); else break; //du3
175  if(!erg) { //ungnstige Spalten ermitteln
176  for(i=1;i<=7;i++) {
177  if(!isFull(i)) {
178  set_stone(i,9,0); //Stein setzen 9=irgendwas
179  badColumns[i-1]=ki_findColumn('b',otherplayer); //ungnstige Spalten ermitteln b=bad
180  if(!badColumns[i-1]) badColumns[i-1]=ki_findColumn('b',cam);
181  unset_stone(i);
182  } else badColumns[i-1]=9; //9=full
183  }
184  } else break;
185  erg=ki_2(cam); //ich2 (fr passivere Methode vertauschen)
186  if(!erg) erg=ki_2(otherplayer); else break; //du2
187  if(!erg) erg=(!isFull(4)&&!ki_isBadColumn(4))?4:0; else break;//mitte
188  if(!erg) do erg=(rand()%7)+1; while((isFull(erg)||erg==0||ki_isBadColumn(erg))&&counter++<100); //zufall
189  if(counter>100) for(i=1;i<=7;i++) if(!isFull(i)) erg=i;
190  break;
191  }
192  return erg;
193 }
char ki_isBadColumn ( int  column)

Definiert in Zeile 92 der Datei gui_4wins.c.

93 {
94  if (badColumns[column - 1])
95  return column;
96  else
97  return 0;
98 }
static void move_cursor ( int  in_x_pos)
static

Definiert in Zeile 309 der Datei gui_4wins.c.

310 {
312  if (in_game)
313  {
314  if (cursor_position == 0 && in_x_pos < 0)
315  cursor_position = 7 + in_x_pos;
316  else
317  cursor_position = (cursor_position + in_x_pos) % 7;
318  }
319  else
320  {
321  if (cursor_position == 0 && in_x_pos < 0)
322  cursor_position = 8 + in_x_pos;
323  else
324  cursor_position = (cursor_position + in_x_pos) % 8;
325  }
327 }
void set ( )

Definiert in Zeile 289 der Datei gui_4wins.c.

290 {
291  if (finished == 0)
292  {
293  if (cursor_position == 7 && in_game == 0)
294  {
295  change_mode();
296  }
297  else
298  {
299  if (cur_player == 1 || (cur_player == 2 && mode_rival == 1))
300  if (set_stone(cursor_position + 1, cur_player, 1))
301  win();
302  if ((mode_rival == 0 && cur_player == 2) && !finished)
304  win();
305  }
306  }
307 }
static char set_stone ( int  column,
char  player,
char  visible 
)
static

Definiert in Zeile 66 der Datei gui_4wins.c.

67 {
68  int i;
69  in_game = 1;
70  if (!isFull(column))
71  {
72  for (i = 1; field[column - 1][i]; i++)
73  ; //1 ist wichtig (0=Fundament)
74  if (visible)
75  {
76  stone(column, i, (cur_player == 1) ? P1_COLOR : P2_COLOR);
77  }
78  field[column - 1][i] = player;
79  return 1;
80  }
81  else
82  return 0;
83 }
static void stone ( int  column,
int  row,
color  cl 
)
static

Definiert in Zeile 48 der Datei gui_4wins.c.

49 {
50  draw_ellipse((XBORDER+((column-1)*RECT_SIZE))+(RECT_SIZE/2), (BORDER_TOP+((7-row)*RECT_SIZE))+(RECT_SIZE/2), STONE_SIZE, STONE_SIZE, cl, DRAW_FILLED);
51 }
static void unset_stone ( int  column)
static

Definiert in Zeile 85 der Datei gui_4wins.c.

86 {
87  int i;
88  for(i=1;(i<=6)&&field[column-1][i];i++); //1 ist wichtig (0=Fundament)
89  field[column-1][i-1]=0;
90 }
void win ( )

Definiert in Zeile 256 der Datei gui_4wins.c.

257 {
258  if (win_query() == 1)
259  {
260  finished = 1;
261  count_win[cur_player - 1]++;
263  }
264  else if (win_query() == 9)
265  {
266  finished = 1;
268  }
269  change_player();
270 }
static char win_query ( )
static

Definiert in Zeile 216 der Datei gui_4wins.c.

217 {
218  int i=0,j=0;
219 
220  for(i=0;i<4;i++)
221  {
222  for(j=1;j<4;j++)
223  {
224  // '1'
225  if(field[i][j]==1 && field[i+1][j+1]==1 && field[i+2][j+2]==1 && field[i+3][j+3]==1) return 1; // diagonal >
226  if(field[i][j+3]==1 && field[i+1][j+2]==1 && field[i+2][j+1]==1 && field[i+3][j]==1) return 1; // diagonal > /
227  // '2'
228  if(field[i][j]==2 && field[i+1][j+1]==2 && field[i+2][j+2]==2 && field[i+3][j+3]==2) return 1;// diagonal >
229  if(field[i][j+3]==2 && field[i+1][j+2]==2 && field[i+2][j+1]==2 && field[i+3][j]==2) return 1;// diagonal > /
230  }
231  }
232 
233  for(i=0;i<7;i++)
234  {
235  for(j=1;j<4;j++)
236  {
237  if(field[i][j]==2 && field[i][j+1]==2 && field[i][j+2]==2 && field[i][j+3]==2) return 1; // vertical > |
238  if(field[i][j]==1 && field[i][j+1]==1 && field[i][j+2]==1 && field[i][j+3]==1) return 1; // vertical > |
239  }
240  }
241 
242  for(i=0;i<4;i++)
243  {
244  for(j=1;j<7;j++)
245  {
246  if(field[i][j]==2 && field[i+1][j]==2 && field[i+2][j]==2 && field[i+3][j]==2) return 1; // horizontal > -
247  if(field[i][j]==1 && field[i+1][j]==1 && field[i+2][j]==1 && field[i+3][j]==1) return 1; // horizontal > -
248  }
249  }
250 
251  if (isFull(1) && isFull(2) && isFull(3) && isFull(4) && isFull(5) && isFull(6) && isFull(7)) return 9;
252 
253  return 0;
254 }

Variablen-Dokumentation

char badColumns[7]

Definiert in Zeile 43 der Datei gui_4wins.c.

char count_win[2] ={0,0}

Definiert in Zeile 46 der Datei gui_4wins.c.

char cur_player =1

Definiert in Zeile 40 der Datei gui_4wins.c.

char cursor_position

Definiert in Zeile 40 der Datei gui_4wins.c.

char field[FIELD_HEIGHT][FIELD_WIDTH]

Definiert in Zeile 41 der Datei gui_4wins.c.

char finished =0

Definiert in Zeile 42 der Datei gui_4wins.c.

char in_game =0

Definiert in Zeile 44 der Datei gui_4wins.c.

char mode_rival =0

Definiert in Zeile 45 der Datei gui_4wins.c.

int rect_size = 30

Definiert in Zeile 24 der Datei gui_4wins.c.