⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reversi.c

📁 ucgu最新版本 4.14
💻 C
📖 第 1 页 / 共 4 页
字号:
  _AddMenuItem(hMenu, hMenuGame,    "Game",    0, 0);
  _AddMenuItem(hMenu, hMenuOptions, "Options", 0, 0);
  _AddMenuItem(hMenu, hMenuHelp,    "Help",    0, 0);
  /* Attach menu to framewin */
  FRAMEWIN_AddMenu(hWin, hMenu);
}

/*******************************************************************
*
*       _CalcBoardDimensions
*/
static void _CalcBoardDimensions(void) {
  GUI_RECT r;
  WM_GetClientRectEx(WM_GetClientWindow(_hFrame), &r);
  _CellSize  = ((r.x1 > r.y1) ? r.y1 : r.x1) >> 3;
  _BoardX0   = (r.x1 - (_CellSize << 3)) >> 1;
  _BoardY0   = (r.y1 - (_CellSize << 3)) >> 1;
}

/*******************************************************************
*
*       _InvalidateBoard
*/
static void _InvalidateBoard(void) {
  WM_InvalidateWindow(WM_GetClientWindow(_hFrame));
}

/*******************************************************************
*
*       _InvalidateCell
*/
static void _InvalidateCell(int x, int y) {
  GUI_RECT r;
  r.x0 = _BoardX0 + (x * _CellSize);
  r.y0 = _BoardY0 + (y * _CellSize);
  r.x1 = r.x0 + _CellSize - 1;
  r.y1 = r.y0 + _CellSize - 1;
  WM_InvalidateRect(WM_GetClientWindow(_hFrame), &r);
}

/*********************************************************************
*
*       _SetCapture
*/
static void _SetCapture(void) {
#if (GUI_SUPPORT_MOUSE & GUI_SUPPORT_CURSOR)
  WM_HWIN hWin;
  hWin = WM_GetClientWindow(_hFrame);
  if (WM_HasCaptured(hWin) == 0) {
    WM_SetCapture(hWin, 0);
    GUI_CURSOR_Select(&GUI_CursorCrossS);
  }
#endif
}

/*********************************************************************
*
*       _ReleaseCapture
*/
static void _ReleaseCapture(void) {
#if (GUI_SUPPORT_MOUSE & GUI_SUPPORT_CURSOR)
  WM_HWIN hWin;
  hWin = WM_GetClientWindow(_hFrame);
  if (WM_HasCaptured(hWin)) {
    WM_ReleaseCapture();
    GUI_CURSOR_Select(&GUI_CursorArrowM);
  }
#endif
}

/*********************************************************************
*
*       Static code, game API routines
*
**********************************************************************
*/
/*******************************************************************
*
*       _GetStone
*/
static char _GetStone(const BOARD* pBoard, int x, int y) {
  char r = 0;
  if ((x >= 0) && (y >= 0) && (x < NUM_CELL_X) && (y < NUM_CELL_Y)) {
    r = pBoard->aCells[x][y];
  }
  return r;
}

/*******************************************************************
*
*       _SetStone
*/
static void _SetStone(BOARD* pBoard, int x, int y) {
  if ((x >= 0) && (y >= 0) && (x < NUM_CELL_X) && (y < NUM_CELL_Y)) {
    pBoard->aCells[x][y] = pBoard->ActPlayer;
    _InvalidateCell(x, y);
  }
}

/*******************************************************************
*
*       _IsValidMove
*/
static char _IsValidMove(BOARD* pBoard, int x, int y) {
  char r = 0;
  if ((x >= 0) && (y >= 0) && (x < NUM_CELL_X) && (y < NUM_CELL_Y)) {
    r = ((pBoard->aMoves[x][y]) ? 1 : 0);
  }
  return r;
}

/*******************************************************************
*
*       _GetNextPlayer
*/
static int _GetNextPlayer(int ActPlayer) {
  return (3 - ActPlayer);
}

/*******************************************************************
*
*       _CheckDirection
*/
static char _CheckDirection(const BOARD* pBoard, int x, int y, int dx, int dy) {
  char Cell;
  x += dx;
  y += dy;
  Cell = _GetStone(pBoard, x, y);
  if ((Cell != pBoard->ActPlayer) && (Cell != 0)) {
    do {
      x += dx;
      y += dy;
      Cell = _GetStone(pBoard, x, y);
    } while ((Cell != pBoard->ActPlayer) && (Cell != 0));
    return ((Cell == pBoard->ActPlayer) ? 1 : 0);
  }
  return 0;
}

/*******************************************************************
*
*       _CalcValidMoves
*/
static int _CalcValidMoves(BOARD* pBoard) {
  int x, y, r = 0;
  U8 Valid;
  for (y = 0; y < NUM_CELL_Y; y++) {
    for (x = 0; x < NUM_CELL_X; x++) {
      Valid = 0;
      if (pBoard->aCells[x][y] == 0) {
        Valid |= _CheckDirection(pBoard, x, y, -1, -1) << 0;
        Valid |= _CheckDirection(pBoard, x, y,  0, -1) << 1;
        Valid |= _CheckDirection(pBoard, x, y,  1, -1) << 2;
        Valid |= _CheckDirection(pBoard, x, y,  1,  0) << 3;
        Valid |= _CheckDirection(pBoard, x, y,  1,  1) << 4;
        Valid |= _CheckDirection(pBoard, x, y,  0,  1) << 5;
        Valid |= _CheckDirection(pBoard, x, y, -1,  1) << 6;
        Valid |= _CheckDirection(pBoard, x, y, -1,  0) << 7;
        if (Valid) {
          r++;
        }
      }
      if (Valid != pBoard->aMoves[x][y]) {
        pBoard->aMoves[x][y] = Valid;
        _InvalidateCell(x, y);
      }
    }
  }
  return r;
}

/*******************************************************************
*
*       _DoDirection
*/
static void _DoDirection(BOARD* pBoard, int x, int y, int dx, int dy) {
  do {
    _SetStone(pBoard, x, y);
    x += dx;
    y += dy;
  } while (_GetStone(pBoard, x, y) != pBoard->ActPlayer);
}

/*******************************************************************
*
*       _MakeMove
*/
static void _MakeMove(BOARD* pBoard, int x, int y) {
  U8 Valid;
  _SetStone(pBoard, x, y);
  _Delay(100);
  Valid = pBoard->aMoves[x][y];
  if (Valid & (U8)(1 << 0)) { _DoDirection(pBoard, x, y, -1, -1); }
  if (Valid & (U8)(1 << 1)) { _DoDirection(pBoard, x, y,  0, -1); }
  if (Valid & (U8)(1 << 2)) { _DoDirection(pBoard, x, y,  1, -1); }
  if (Valid & (U8)(1 << 3)) { _DoDirection(pBoard, x, y,  1,  0); }
  if (Valid & (U8)(1 << 4)) { _DoDirection(pBoard, x, y,  1,  1); }
  if (Valid & (U8)(1 << 5)) { _DoDirection(pBoard, x, y,  0,  1); }
  if (Valid & (U8)(1 << 6)) { _DoDirection(pBoard, x, y, -1,  1); }
  if (Valid & (U8)(1 << 7)) { _DoDirection(pBoard, x, y, -1,  0); }
}

/*******************************************************************
*
*       _CalcScore
*/
static int _CalcScore(const BOARD* pBoard) {
  int x, y, r = 0;
  char Cell;
  for (y = 0; y < NUM_CELL_Y; y++) {
    for (x = 0; x < NUM_CELL_X; x++) {
      Cell = pBoard->aCells[x][y];
      if (Cell) {
        r += (Cell == pBoard->ActPlayer) ? (1) : (-1);
      }
    }
  }
  return r;
}





/*********************************************************************
*
*       Static code, about box
*
**********************************************************************
*/
/*******************************************************************
*
*       _cbAboutBox
*/
static void _cbAboutBox(WM_MESSAGE* pMsg) {
  WM_HWIN hWin = pMsg->hWin;
  switch (pMsg->MsgId) {
  case WM_PAINT:
    {
      char acText[16] = "V";
      strcat(acText, GUI_GetVersionString());
      GUI_SetColor(GUI_BLACK);
      GUI_SetFont(&GUI_Font10_1);
      GUI_SetTextMode(GUI_TM_TRANS);
      GUI_DrawBitmap(&_Logo, 4, 4);
      GUI_DispStringHCenterAt("Reversi V1.0", 49, 48);
      GUI_DispStringHCenterAt("emWin", 138, 38);
      GUI_DispStringHCenterAt(acText,  138, 48);
      GUI_DispStringHCenterAt("Compiled " __DATE__ " "__TIME__, 88, 68);
      GUI_DispStringHCenterAt("(c) 1998-2004 Segger", 88, 87);
      GUI_DispStringHCenterAt("Microcontroller Systeme GmbH", 88, 97);
      GUI_DispStringHCenterAt("www.segger.com", 88, 107);
      GUI_DispStringHCenterAt("Programmed by Tobias Quecke", 88, 126);
    }
    break;
  case WM_NOTIFY_PARENT:
    if (pMsg->Data.v == WM_NOTIFICATION_RELEASED) {
      GUI_EndDialog(hWin, 1);
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -