CHDK_DE Vorschauversion  Trunk Rev. 5779
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_reversi.c-Dateireferenz
#include "camera_info.h"
#include "keyboard.h"
#include "clock.h"
#include "lang.h"
#include "gui.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_reversi.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define FIELD_EMPTY   0
 
#define FIELD_PLAYER1   1
 
#define FIELD_PLAYER2   2
 
#define COMPUTER_ONLY   3
 
#define FIELD_COLOR_WHITE   COLOR_GREEN
 
#define FIELD_COLOR_BLACK   COLOR_GREEN_DK
 
#define SELECTED_COLOR   COLOR_RED
 

Typdefinitionen

typedef unsigned char uchar
 
typedef unsigned int uint
 

Funktionen

void gui_game_menu_kbd_process ()
 
int gui_reversi_kbd_process ()
 
void gui_reversi_draw ()
 
static int gui_reversi_touch_handler (int, int)
 
static void redraw ()
 
static uchar NotPlayer (uchar Pl)
 
static void GetNum ()
 
static void DrawCell (uchar x, uchar y)
 
static uchar Place (uchar x, uchar y, uchar Player, uchar Placed)
 
static void ComputerPlace (uchar Player)
 
static uchar CanPlace (uchar Player)
 
static void Result ()
 
static void DrawMainWindow ()
 
static void InitMainWindow ()
 
static void NewGame ()
 
static void Clk (uchar x, uchar y)
 
static void Timer ()
 
int basic_module_init ()
 

Variablen

gui_handler GUI_MODE_REVERSI
 
static uchar Field [8][8]
 
static uchar CurrPlayer
 
static uchar Computer
 
static uchar InGame
 
static uchar NumPl1
 
static uchar NumPl2
 
static unsigned short field_size
 
static unsigned short field_x
 
static unsigned short field_y
 
static unsigned short cell_size
 
static uchar xPos
 
static uchar yPos
 
static uchar OldxPos
 
static uchar OldyPos
 
static volatile uchar need_redraw = 0
 
static volatile uchar need_redraw_all = 0
 
static volatile uchar MFlag = 0
 
static char buf [128]
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define COMPUTER_ONLY   3

Definiert in Zeile 27 der Datei gui_reversi.c.

#define FIELD_COLOR_BLACK   COLOR_GREEN_DK

Definiert in Zeile 30 der Datei gui_reversi.c.

#define FIELD_COLOR_WHITE   COLOR_GREEN

Definiert in Zeile 29 der Datei gui_reversi.c.

#define FIELD_EMPTY   0

Definiert in Zeile 24 der Datei gui_reversi.c.

#define FIELD_PLAYER1   1

Definiert in Zeile 25 der Datei gui_reversi.c.

#define FIELD_PLAYER2   2

Definiert in Zeile 26 der Datei gui_reversi.c.

#define SELECTED_COLOR   COLOR_RED

Definiert in Zeile 31 der Datei gui_reversi.c.

Dokumentation der benutzerdefinierten Typen

typedef unsigned char uchar

Definiert in Zeile 34 der Datei gui_reversi.c.

typedef unsigned int uint

Definiert in Zeile 35 der Datei gui_reversi.c.

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 343 der Datei gui_reversi.c.

343  {
345  InitMainWindow();
346  NewGame();
347  need_redraw_all = 1;
348  return 1;
349 }
static uchar CanPlace ( uchar  Player)
static

Definiert in Zeile 193 der Datei gui_reversi.c.

193  {
194  uchar I, J, E = 0, E1;
195 
196  for (I=0; I<8; I++) {
197  for (J=0; J<8; J++) {
198  E1 = Place(I, J, Player, 0);
199  if (E1<0xFF) E+=E1;
200  }
201  }
202  return (E>0);
203 }
static void Clk ( uchar  x,
uchar  y 
)
static

Definiert in Zeile 255 der Datei gui_reversi.c.

255  {
256  uchar Placed;
257 
258  if ((CurrPlayer==Computer) || (! InGame) || (Computer==COMPUTER_ONLY))
259  ;
260  else {
261  Placed = Place(x, y ,CurrPlayer, 0);
262  if (Placed==0) {
264  need_redraw_all = 1;
265  } else if (Placed==0xFF) {
267  need_redraw_all = 1;
268  } else {
269  Placed = Place(x, y, CurrPlayer, 1);
271  GetNum();
272  need_redraw = 1;
274  InGame = 0;
275  Result();
276  }
277  if (InGame && !CanPlace(CurrPlayer)) {
279  need_redraw = 1;
280  }
281  }
282  }
283 }
static void ComputerPlace ( uchar  Player)
static

Definiert in Zeile 129 der Datei gui_reversi.c.

129  {
130 
131  static const uchar PlaceTable[8][8] ={{ 11, 78, 62, 65, 65, 62, 78, 11},
132  { 78, 94, 74, 73, 73, 74, 94, 78},
133  { 62, 74, 63, 64, 64, 63, 74, 62},
134  { 65, 73, 64, 11, 11, 64, 73, 65},
135  { 65, 73, 64, 11, 11, 64, 73, 65},
136  { 62, 74, 63, 64, 64, 63, 74, 62},
137  { 78, 94, 74, 73, 73, 74, 94, 78},
138  { 11, 78, 62, 65, 65, 62, 78, 11}};
139 
140  uchar PlX[61];
141  uchar PlY[61];
142  static uchar DynTable[8][8];
143  uchar NPl = 0, I, J, MaxE = 0, E, MinPr = 100;
144 
146 
147  for (I=0; I<8; I++)
148  for (J=0; J<8; J++)
149  DynTable[I][J] = PlaceTable[I][J] + (rand()%3) - 1;
150 
151  for (I=0; I<8; I++) {
152  for (J=0; J<8; J++) {
153  E = Place(I, J, Player, 0);
154  if ((MinPr>DynTable[I][J]) && (E<0xFF) && (E>0)) {
155  MinPr = DynTable[I][J];
156  NPl = 0;
157  MaxE = 0;
158  }
159  if ((E<0xFF) && (MinPr==DynTable[I][J])) {
160  if (E>MaxE) {
161  MaxE = E;
162  NPl = 1;
163  PlX[1] = I;
164  PlY[1] = J;
165  } else {
166  if (E==MaxE) {
167  NPl++;
168  PlX[NPl] = I;
169  PlY[NPl] = J;
170  }
171  }
172  }
173  }
174  }
175 
176  MaxE = MinPr = 0;
177 
178  for(I=0; I<8; I+=7) {
179  for(J=0; J<8; J+=7) {
180  E = Place(I, J, Player, 0);
181  if ((E<0xFF) && (E>0)) {MaxE = E; MinPr = (I<<4) + J + 1;}
182  }
183  }
184 
185  E = (rand()%NPl) + 1;
186  if(MinPr) Place(MinPr>>4, (MinPr&0x0F)-1, Player, 1);
187  else Place(PlX[E], PlY[E], Player, 1);
188 
189  return;
190 }
static void DrawCell ( uchar  x,
uchar  y 
)
static

Definiert in Zeile 69 der Datei gui_reversi.c.

69  {
71  field_x+cell_size*(x+1), field_y+cell_size*(y+1),
74  switch (Field[x][y]) {
75  case FIELD_EMPTY:
76  break;
77  case FIELD_PLAYER1:
79  (cell_size>>1)-4, (cell_size>>1)-4, COLOR_WHITE, DRAW_FILLED);
80  break;
81  case FIELD_PLAYER2:
83  (cell_size>>1)-4, (cell_size>>1)-4, COLOR_BLACK, DRAW_FILLED);
84  break;
85  }
86 }
static void DrawMainWindow ( )
static

Definiert in Zeile 218 der Datei gui_reversi.c.

218  {
219  uchar x, y;
220 
223  for (y=0; y<8; ++y) {
224  for (x=0; x<8; ++x) {
225  DrawCell(x, y);
226  }
227  }
228 }
static void GetNum ( )
inlinestatic

Definiert in Zeile 57 der Datei gui_reversi.c.

57  {
58  uchar x, y;
59  NumPl1 = NumPl2 = 0;
60 
61  for (x=0; x<8; ++x)
62  for (y=0; y<8; ++y) {
63  if (Field[x][y]==FIELD_PLAYER1) ++NumPl1;
64  if (Field[x][y]==FIELD_PLAYER2) ++NumPl2;
65  }
66 }
void gui_game_menu_kbd_process ( )

Definiert in Zeile 7 der Datei simple_game.c.

8 {
9  running = 0;
11 }
void gui_reversi_draw ( )

Definiert in Zeile 407 der Datei gui_reversi.c.

407  {
408  if (MFlag) {
409  MFlag = 0;
411  DrawCell(xPos, yPos);
412  OldxPos = xPos, OldyPos = yPos;
413  }
414  if (need_redraw_all) {
415  need_redraw_all = 0;
416  DrawMainWindow();
417  need_redraw = 1;
418  }
419  if (need_redraw) {
420  need_redraw = 0;
421  redraw();
422  }
423  sprintf(buf, "Batt:%3d%%", get_batt_perc());
426  Timer();
427 }
int gui_reversi_kbd_process ( )

Definiert in Zeile 352 der Datei gui_reversi.c.

352  {
353  switch (kbd_get_autoclicked_key()) {
354  case KEY_UP:
355  yPos = (yPos-1)&7;
356  MFlag = 1;
357  break;
358  case KEY_DOWN:
359  yPos = (yPos+1)&7;
360  MFlag = 1;
361  break;
362  case KEY_LEFT:
363  xPos = (xPos-1)&7;
364  MFlag = 1;
365  break;
366  case KEY_RIGHT:
367  xPos = (xPos+1)&7;
368  MFlag = 1;
369  break;
370  case KEY_SET:
371  if (InGame)
372  Clk(xPos, yPos);
373  else
374  NewGame();
375  need_redraw = 1;
376  break;
377  case KEY_ERASE:
378  case KEY_DISPLAY:
379  if (InGame)
381  else
382  NewGame();
383  need_redraw = 1;
384  break;
385  }
386 return 0;
387 }
static int gui_reversi_touch_handler ( int  sx,
int  sy 
)
static

Definiert in Zeile 390 der Datei gui_reversi.c.

390  {
391  if ((sx >= field_x) && (sx < field_x+cell_size*8) && (sy >= field_y) && (sy < field_x+cell_size*8))
392  {
393  sx = (sx - field_x) / cell_size;
394  sy = (sy - field_y) / cell_size;
395  if ((sx != xPos) || (sy != yPos))
396  {
397  xPos = sx;
398  yPos = sy;
399  need_redraw = 1;
400  }
401  return KEY_SET;
402  }
403  return 0;
404 }
static void InitMainWindow ( )
static
static void NewGame ( )
static

Definiert in Zeile 242 der Datei gui_reversi.c.

242  {
243  memset(Field, FIELD_EMPTY, sizeof(Field));
244  Field[3][3] = Field[4][4] = FIELD_PLAYER1;
245  Field[3][4] = Field[4][3] = FIELD_PLAYER2;
248  NumPl1 = NumPl2 = 2;
249  xPos = yPos = OldxPos = OldyPos = 3;
250  InGame = 1;
251  need_redraw_all = 1;
252 }
static uchar NotPlayer ( uchar  Pl)
static

Definiert in Zeile 52 der Datei gui_reversi.c.

52  {
53  return (Pl ^ COMPUTER_ONLY);
54 }
static uchar Place ( uchar  x,
uchar  y,
uchar  Player,
uchar  Placed 
)
static

Definiert in Zeile 89 der Datei gui_reversi.c.

89  {
90 /* 0..64 - Ok 0xFF - not empty*/
91  int I, J, x1, y1;
92  uchar Eated, E;
93 
94  if (Field[x][y]!=FIELD_EMPTY) {
95  return 0xFF;
96  } else {
97  Eated = 0;
98  for (I=-1; I<=1; I++) {
99  for (J=-1; J<=1; J++) {
100  E = 0;
101  x1 = x + I;
102  y1 = y + J;
103  while (((x1>=0) && (x1<8) && (y1>=0) && (y1<8)) && (Field[x1][y1]!=FIELD_EMPTY) && (Field[x1][y1]!=Player)) {
104  E++;
105  x1 = x1 + I;
106  y1 = y1 + J;
107  }
108  if ((Placed) && ((x1>=0) && (x1<8) && (y1>=0) && (y1<8)) && (Field[x1][y1]!=FIELD_EMPTY) && (E>0)) {
109  Field[x][y] = Player;
110  DrawCell(x, y);
111  x1 = x + I;
112  y1 = y + J;
113  while (((x1>=0) && (x1<8) && (y1>=0) && (y1<8)) && (Field[x1][y1]!=FIELD_EMPTY) && (Field[x1][y1]!=Player)) {
114  Field[x1][y1] = Player;
115  DrawCell(x1, y1);
116  x1 = x1 + I;
117  y1 = y1 + J;
118  }
119  }
120  if (((x1>=0) && (x1<8) && (y1>=0) && (y1<8)) && (Field[x1][y1]!=FIELD_EMPTY))
121  Eated += E;
122  }
123  }
124  return Eated;
125  }
126 }
static void redraw ( )
static

Definiert in Zeile 306 der Datei gui_reversi.c.

306  {
307  uint x, y, mid;
308  char *plm1, *plm2;
309 
310  if (InGame) {
311  if (CurrPlayer==FIELD_PLAYER1) {
312  plm1 = "\x10";
313  plm2 = " ";
314  }
315  else {
316  plm1 = " ";
317  plm2 = "\x10";
318  }
319  }
320  else {
321  plm1 = plm2 = " ";
322  }
323 
326  x = camera_screen.disp_left+field_size, y = 40;
327  mid = ((camera_screen.width-field_size) >> 2);
328  x += mid;
329  draw_string(x, y-10, "REVERSI", MAKE_COLOR(COLOR_BLUE, COLOR_WHITE));
330  sprintf(buf, " %d ", NumPl1);
334  (cell_size>>1)-4, (cell_size>>1)-4, COLOR_WHITE, DRAW_FILLED);
335  sprintf(buf, " %d ", NumPl2);
339  (cell_size>>1)-4, (cell_size>>1)-4, COLOR_BLACK, DRAW_FILLED);
340 }
static void Timer ( )
static

Definiert in Zeile 286 der Datei gui_reversi.c.

286  {
288  if (CanPlace(CurrPlayer)) {
290  GetNum();
291  need_redraw = 1;
292  }
295  InGame = 0;
296  Result();
297  }
298  if (InGame && !CanPlace(CurrPlayer)) {
300  need_redraw = 1;
301  }
302  }
303 }

Variablen-Dokumentation

char buf[128]
static

Definiert in Zeile 46 der Datei gui_reversi.c.

unsigned short cell_size
static

Definiert in Zeile 41 der Datei gui_reversi.c.

uchar Computer
static

Definiert in Zeile 39 der Datei gui_reversi.c.

uchar CurrPlayer
static

Definiert in Zeile 39 der Datei gui_reversi.c.

uchar Field[8][8]
static

Definiert in Zeile 38 der Datei gui_reversi.c.

unsigned short field_size
static

Definiert in Zeile 41 der Datei gui_reversi.c.

unsigned short field_x
static

Definiert in Zeile 41 der Datei gui_reversi.c.

unsigned short field_y
static

Definiert in Zeile 41 der Datei gui_reversi.c.

uchar InGame
static

Definiert in Zeile 39 der Datei gui_reversi.c.

volatile uchar MFlag = 0
static

Definiert in Zeile 44 der Datei gui_reversi.c.

volatile uchar need_redraw = 0
static

Definiert in Zeile 44 der Datei gui_reversi.c.

volatile uchar need_redraw_all = 0
static

Definiert in Zeile 44 der Datei gui_reversi.c.

uchar NumPl1
static

Definiert in Zeile 39 der Datei gui_reversi.c.

uchar NumPl2
static

Definiert in Zeile 39 der Datei gui_reversi.c.

uchar OldxPos
static

Definiert in Zeile 43 der Datei gui_reversi.c.

uchar OldyPos
static

Definiert in Zeile 43 der Datei gui_reversi.c.

uchar xPos
static

Definiert in Zeile 43 der Datei gui_reversi.c.

uchar yPos
static

Definiert in Zeile 43 der Datei gui_reversi.c.