CHDK_DE Vorschauversion  Trunk Rev. 5272
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_reversi.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.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 28 der Datei gui_reversi.c.

#define FIELD_COLOR_BLACK   COLOR_GREEN_DK

Definiert in Zeile 31 der Datei gui_reversi.c.

#define FIELD_COLOR_WHITE   COLOR_GREEN

Definiert in Zeile 30 der Datei gui_reversi.c.

#define FIELD_EMPTY   0

Definiert in Zeile 25 der Datei gui_reversi.c.

#define FIELD_PLAYER1   1

Definiert in Zeile 26 der Datei gui_reversi.c.

#define FIELD_PLAYER2   2

Definiert in Zeile 27 der Datei gui_reversi.c.

#define SELECTED_COLOR   COLOR_RED

Definiert in Zeile 32 der Datei gui_reversi.c.

Dokumentation der benutzerdefinierten Typen

typedef unsigned char uchar

Definiert in Zeile 35 der Datei gui_reversi.c.

typedef unsigned int uint

Definiert in Zeile 36 der Datei gui_reversi.c.

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 344 der Datei gui_reversi.c.

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

Definiert in Zeile 194 der Datei gui_reversi.c.

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

Definiert in Zeile 256 der Datei gui_reversi.c.

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

Definiert in Zeile 130 der Datei gui_reversi.c.

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

Definiert in Zeile 70 der Datei gui_reversi.c.

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

Definiert in Zeile 219 der Datei gui_reversi.c.

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

Definiert in Zeile 58 der Datei gui_reversi.c.

58  {
59  uchar x, y;
60  NumPl1 = NumPl2 = 0;
61 
62  for (x=0; x<8; ++x)
63  for (y=0; y<8; ++y) {
64  if (Field[x][y]==FIELD_PLAYER1) ++NumPl1;
65  if (Field[x][y]==FIELD_PLAYER2) ++NumPl2;
66  }
67 }
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 408 der Datei gui_reversi.c.

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

Definiert in Zeile 353 der Datei gui_reversi.c.

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

Definiert in Zeile 391 der Datei gui_reversi.c.

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

Definiert in Zeile 243 der Datei gui_reversi.c.

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

Definiert in Zeile 53 der Datei gui_reversi.c.

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

Definiert in Zeile 90 der Datei gui_reversi.c.

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

Definiert in Zeile 307 der Datei gui_reversi.c.

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

Definiert in Zeile 287 der Datei gui_reversi.c.

287  {
289  if (CanPlace(CurrPlayer)) {
291  GetNum();
292  need_redraw = 1;
293  }
296  InGame = 0;
297  Result();
298  }
299  if (InGame && !CanPlace(CurrPlayer)) {
301  need_redraw = 1;
302  }
303  }
304 }

Variablen-Dokumentation

char buf[128]
static

Definiert in Zeile 47 der Datei gui_reversi.c.

unsigned short cell_size
static

Definiert in Zeile 42 der Datei gui_reversi.c.

uchar Computer
static

Definiert in Zeile 40 der Datei gui_reversi.c.

uchar CurrPlayer
static

Definiert in Zeile 40 der Datei gui_reversi.c.

uchar Field[8][8]
static

Definiert in Zeile 39 der Datei gui_reversi.c.

unsigned short field_size
static

Definiert in Zeile 42 der Datei gui_reversi.c.

unsigned short field_x
static

Definiert in Zeile 42 der Datei gui_reversi.c.

unsigned short field_y
static

Definiert in Zeile 42 der Datei gui_reversi.c.

uchar InGame
static

Definiert in Zeile 40 der Datei gui_reversi.c.

volatile uchar MFlag = 0
static

Definiert in Zeile 45 der Datei gui_reversi.c.

volatile uchar need_redraw = 0
static

Definiert in Zeile 45 der Datei gui_reversi.c.

volatile uchar need_redraw_all = 0
static

Definiert in Zeile 45 der Datei gui_reversi.c.

uchar NumPl1
static

Definiert in Zeile 40 der Datei gui_reversi.c.

uchar NumPl2
static

Definiert in Zeile 40 der Datei gui_reversi.c.

uchar OldxPos
static

Definiert in Zeile 44 der Datei gui_reversi.c.

uchar OldyPos
static

Definiert in Zeile 44 der Datei gui_reversi.c.

uchar xPos
static

Definiert in Zeile 44 der Datei gui_reversi.c.

uchar yPos
static

Definiert in Zeile 44 der Datei gui_reversi.c.