📄 task_1.c
字号:
static void _CreateMenu(WM_HWIN hWin) {
MENU_Handle hMenu, hMenuGame, hMenuOptions, hMenuHelp;
MENU_SetDefaultFont(&GUI_Font10_1);
/* Create menu 'Game' */
hMenuGame = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
_AddMenuItem(hMenuGame, 0, "New game", ID_MENU_NEW, 0);
_AddMenuItem(hMenuGame, 0, "Pass", ID_MENU_PASS, 0);
_AddMenuItem(hMenuGame, 0, 0, 0, MENU_IF_SEPARATOR);
_AddMenuItem(hMenuGame, 0, "Exit", ID_MENU_EXIT, 0);
/* Create menu 'Options' */
hMenuOptions = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
_AddMenuItem(hMenuOptions, 0, "Game settings...", ID_MENU_SETTINGS, 0);
/* Create menu 'Help' */
hMenuHelp = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
_AddMenuItem(hMenuHelp, 0, "About Reversi...", ID_MENU_ABOUT, 0);
/* Create main menu */
hMenu = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_HORIZONTAL, 0);
_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_SelectEx(&GUI_CursorCrossS, LAYER);
}
#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_SelectEx(&GUI_CursorArrowM, LAYER);
}
#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";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -