CHDK_DE Vorschauversion  Trunk Rev. 5163
 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 BORDER   20
 
#define XBORDER   (camera_screen.disp_left+BORDER)
 
#define RECT_SIZE   30
 
#define BORDER_TOP   RECT_SIZE
 
#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 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
 
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 32 der Datei gui_4wins.c.

#define BORDER   20

Definiert in Zeile 24 der Datei gui_4wins.c.

#define BORDER_TOP   RECT_SIZE

Definiert in Zeile 27 der Datei gui_4wins.c.

#define FIELD_COLOR   MAKE_COLOR(COLOR_GREY_LT,COLOR_GREY_LT)

Definiert in Zeile 33 der Datei gui_4wins.c.

#define FIELD_HEIGHT   7

Definiert in Zeile 28 der Datei gui_4wins.c.

#define FIELD_WIDTH   7

Definiert in Zeile 29 der Datei gui_4wins.c.

#define INFO_COLOR   COLOR_GREY_LT

Definiert in Zeile 35 der Datei gui_4wins.c.

#define INFO_TEXT_COLOR   MAKE_COLOR(INFO_COLOR, COLOR_WHITE)

Definiert in Zeile 36 der Datei gui_4wins.c.

#define P1_COLOR   COLOR_BLUE

Definiert in Zeile 30 der Datei gui_4wins.c.

#define P2_COLOR   COLOR_GREEN

Definiert in Zeile 31 der Datei gui_4wins.c.

#define RECT_SIZE   30

Definiert in Zeile 26 der Datei gui_4wins.c.

#define TEXT_COLOR   MAKE_COLOR(COLOR_GREY, COLOR_WHITE)

Definiert in Zeile 34 der Datei gui_4wins.c.

#define XBORDER   (camera_screen.disp_left+BORDER)

Definiert in Zeile 25 der Datei gui_4wins.c.

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 342 der Datei gui_4wins.c.

342  {
343  return gui_4wins_init();
344 }
void change_mode ( )

Definiert in Zeile 251 der Datei gui_4wins.c.

252 {
253  cur_player=1;
254  count_win[0]=count_win[1]=0;
255  if(mode_rival==1)
256  mode_rival=0; //CAM
257  else
258  mode_rival=1; //HUMAN
259  draw_mode();
260 }
static void change_player ( )
static

Definiert in Zeile 187 der Datei gui_4wins.c.

188 {
189  if (cur_player==1) cur_player=2; else cur_player=1;
191 }
void draw_mode ( )

Definiert in Zeile 245 der Datei gui_4wins.c.

void draw_txt_message ( char *  text)

Definiert in Zeile 176 der Datei gui_4wins.c.

176  {
177  coord w, x, y;
179  w = (strlen(text) + 2) * FONT_WIDTH;
180 
181  x = (camera_screen.width-w)>>1;
182  y = ((camera_screen.height)>>1);
184  draw_string_justified(x, y+4, text, cl, 0, w, TEXT_CENTER);
185 }
void gui_4wins_draw ( )

Definiert in Zeile 366 der Datei gui_4wins.c.

int gui_4wins_init ( )

Definiert in Zeile 298 der Datei gui_4wins.c.

299 {
300  static char str[16];
301  int i=0,j=0;
302 
303  cursor_position=3;
304  finished=in_game=0;
305  srand(time(NULL));
306 
312 
313  for(i=0;i<7;i++)
314  {
315  for(j=0;j<6;j++)
316  {
317  field[i][j+1]=0;
318  draw_ellipse(XBORDER+(i*RECT_SIZE)+(RECT_SIZE/2), BORDER+(j*RECT_SIZE)+(RECT_SIZE/2)+BORDER_TOP, 10, 10, BK_COLOR, DRAW_FILLED);
319  }
320  }
321  for(i=0;i<7;i++)
322  {
323  field[i][0]=9;
324  }
325 
326  move_cursor(0);
328  sprintf(str, "%d",count_win[0]);
330  sprintf(str, ":");
332  sprintf(str, "%d",count_win[1]);
334  draw_mode();
335  if(cur_player==2&&!mode_rival) set();
336 
338 
339  return 1;
340 }
int gui_4wins_kbd_process ( )

Definiert in Zeile 347 der Datei gui_4wins.c.

348 {
349  switch (kbd_get_autoclicked_key())
350  {
351  case KEY_SET:
352  if(!finished) set(); else gui_4wins_init();
353  break;
354  case KEY_LEFT:
355  if(finished==0)
356  move_cursor(-1);
357  break;
358  case KEY_RIGHT:
359  if(finished==0)
360  move_cursor(1);
361  break;
362  }
363  return 0;
364 }
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 47 der Datei gui_4wins.c.

48 {
49  if(field[column-1][6])
50  return 1;
51  else
52  return 0;
53 }
char ki_2 ( char  player)

Definiert in Zeile 116 der Datei gui_4wins.c.

116  {
117  int i,j;
118  //waagerecht und Diagonal ber Anstieg
119  for(i=0;i<=3;i++){ //column
120  for(j=1;j<=6;j++){ //row
121  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
122  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)
123  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; //--**
124  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; //-*-*
125  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; //*-*-
126  }
127  for(j=1;j<=3;j++){
128  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
129  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
130  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)
131  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; //--**
132  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; //-*-*
133  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; //*-*-
134  }
135  for(j=4;j<=6;j++){
136  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
137  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
138  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)
139  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; //--**
140  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; //-*-*
141  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; //*-*-
142  }
143  }
144  //senkrecht
145  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;
146  return 0;
147 }
char ki_3 ( char  player)

Definiert in Zeile 82 der Datei gui_4wins.c.

82  {
83  int i,j;
84  //waagerecht und Diagonal (ber Anstieg)
85  for(i=0;i<=3;i++){ // column
86  for(j=1;j<=6;j++){ //row
87  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; //***-
88  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; //**-*
89  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; //*-**
90  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; //-***
91  }
92  for(j=1;j<=3;j++){
93  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; //***-
94  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; //**-*
95  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; //*-**
96  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; //-***
97  }
98  for(j=4;j<=6;j++){
99  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; //***-
100  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; //**-*
101  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; //*-**
102  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; //-***
103  }
104  }
105  //Unterreihentrick in allen ebenen
106  for(i=0;i<=2;i++){
107  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; //_*_*_
108  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; //_*_*_
109  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; //_*_*_
110  }
111  //senkrecht
112  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;
113  return 0;
114 }
char ki_findColumn ( char  mode,
char  player 
)

Definiert in Zeile 149 der Datei gui_4wins.c.

149  { //player = 1|2
150  char erg=0, cam=player, otherplayer, i, counter=0;
151  otherplayer=(player==1)?2:1;
152  switch(mode) {
153  case 'b':
154  case 1: erg=ki_3(cam); //ich3
155  if(!erg&&mode!='b') erg=ki_3(otherplayer); else break; //du3
156  if(!erg) { //ungnstige Spalten ermitteln
157  for(i=1;i<=7;i++) {
158  if(!isFull(i)) {
159  set_stone(i,9,0); //Stein setzen 9=irgendwas
160  badColumns[i-1]=ki_findColumn('b',otherplayer); //ungnstige Spalten ermitteln b=bad
161  if(!badColumns[i-1]) badColumns[i-1]=ki_findColumn('b',cam);
162  unset_stone(i);
163  } else badColumns[i-1]=9; //9=full
164  }
165  } else break;
166  erg=ki_2(cam); //ich2 (fr passivere Methode vertauschen)
167  if(!erg) erg=ki_2(otherplayer); else break; //du2
168  if(!erg) erg=(!isFull(4)&&!ki_isBadColumn(4))?4:0; else break;//mitte
169  if(!erg) do erg=(rand()%7)+1; while((isFull(erg)||erg==0||ki_isBadColumn(erg))&&counter++<100); //zufall
170  if(counter>100) for(i=1;i<=7;i++) if(!isFull(i)) erg=i;
171  break;
172  }
173  return erg;
174 }
char ki_isBadColumn ( int  column)

Definiert in Zeile 78 der Datei gui_4wins.c.

78  {
79  if(badColumns[column-1]) return column; else return 0;
80 }
static void move_cursor ( int  in_x_pos)
static

Definiert in Zeile 278 der Datei gui_4wins.c.

279 {
281  if(in_game)
282  {
283  if(cursor_position==0 && in_x_pos<0)
284  cursor_position=7+in_x_pos;
285  else
286  cursor_position=(cursor_position+in_x_pos)%7;
287  }
288  else
289  {
290  if(cursor_position==0 && in_x_pos<0)
291  cursor_position=8+in_x_pos;
292  else
293  cursor_position=(cursor_position+in_x_pos)%8;
294  }
296 }
void set ( )

Definiert in Zeile 262 der Datei gui_4wins.c.

263 {
264  if(finished==0)
265  {
266  if(cursor_position==7 && in_game==0)
267  {
268  change_mode();
269  }
270  else
271  {
272  if(cur_player==1 || (cur_player==2 && mode_rival==1)) if(set_stone(cursor_position+1,cur_player,1)) win();
274  }
275  }
276 }
static char set_stone ( int  column,
char  player,
char  visible 
)
static

Definiert in Zeile 55 der Datei gui_4wins.c.

56 {
57  int i;
58  in_game=1;
59  if(!isFull(column))
60  {
61  for(i=1;field[column-1][i];i++); //1 ist wichtig (0=Fundament)
62  if (visible) {
63  draw_ellipse((XBORDER+((column-1)*RECT_SIZE))+15, (BORDER+((6-i)*RECT_SIZE))+15+BORDER_TOP, 10,10, (cur_player==1)?P1_COLOR:P2_COLOR, DRAW_FILLED);
64  }
65  field[column-1][i]=player;
66  return 1;
67  }
68  else return 0;
69 }
static void unset_stone ( int  column)
static

Definiert in Zeile 71 der Datei gui_4wins.c.

72 {
73  int i;
74  for(i=1;(i<=6)&&field[column-1][i];i++); //1 ist wichtig (0=Fundament)
75  field[column-1][i-1]=0;
76 }
void win ( )

Definiert in Zeile 233 der Datei gui_4wins.c.

233  {
234  if(win_query()==1){
235  finished=1;
236  count_win[cur_player-1]++;
238  } else if (win_query()==9) {
239  finished=1;
241  }
242  change_player();
243 }
static char win_query ( )
static

Definiert in Zeile 193 der Datei gui_4wins.c.

194 {
195  int i=0,j=0;
196 
197  for(i=0;i<4;i++)
198  {
199  for(j=1;j<4;j++)
200  {
201  // '1'
202  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 >
203  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 > /
204  // '2'
205  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 >
206  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 > /
207  }
208  }
209 
210  for(i=0;i<7;i++)
211  {
212  for(j=1;j<4;j++)
213  {
214  if(field[i][j]==2 && field[i][j+1]==2 && field[i][j+2]==2 && field[i][j+3]==2) return 1; // vertical > |
215  if(field[i][j]==1 && field[i][j+1]==1 && field[i][j+2]==1 && field[i][j+3]==1) return 1; // vertical > |
216  }
217  }
218 
219  for(i=0;i<4;i++)
220  {
221  for(j=1;j<7;j++)
222  {
223  if(field[i][j]==2 && field[i+1][j]==2 && field[i+2][j]==2 && field[i+3][j]==2) return 1; // horizontal > -
224  if(field[i][j]==1 && field[i+1][j]==1 && field[i+2][j]==1 && field[i+3][j]==1) return 1; // horizontal > -
225  }
226  }
227 
228  if (isFull(1) && isFull(2) && isFull(3) && isFull(4) && isFull(5) && isFull(6) && isFull(7)) return 9;
229 
230  return 0;
231 }

Variablen-Dokumentation

char badColumns[7]

Definiert in Zeile 41 der Datei gui_4wins.c.

char count_win[2] ={0,0}

Definiert in Zeile 44 der Datei gui_4wins.c.

char cur_player =1

Definiert in Zeile 38 der Datei gui_4wins.c.

char cursor_position

Definiert in Zeile 38 der Datei gui_4wins.c.

char field[FIELD_HEIGHT][FIELD_WIDTH]

Definiert in Zeile 39 der Datei gui_4wins.c.

char finished =0

Definiert in Zeile 40 der Datei gui_4wins.c.

char in_game =0

Definiert in Zeile 42 der Datei gui_4wins.c.

char mode_rival =0

Definiert in Zeile 43 der Datei gui_4wins.c.