📄 reversi.c
字号:
/*********************************************************************
*
* _ShowAboutBox
*/
static void _ShowAboutBox(void) {
WM_HWIN hFrame, hItem;
/* Create framewin */
hFrame = FRAMEWIN_CreateEx(70, 40, 180, 160, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, 0, "About Reversi", &_cbAboutBox);
FRAMEWIN_SetClientColor (hFrame, GUI_WHITE);
FRAMEWIN_SetFont (hFrame, &GUI_Font10_1);
FRAMEWIN_SetTextAlign (hFrame, GUI_TA_HCENTER);
/* Create dialog items */
hItem = BUTTON_CreateEx(111, 7, 55, 18, WM_GetClientWindow(hFrame), WM_CF_SHOW, 0, GUI_ID_OK);
BUTTON_SetText (hItem, "Ok");
/* Exec modal dialog */
WM_SetFocus(hFrame);
WM_MakeModal(hFrame);
GUI_ExecCreatedDialog(hFrame);
WM_SetFocus(_hFrame);
}
/*********************************************************************
*
* Static code, message box
*
**********************************************************************
*/
/*******************************************************************
*
* _cbMessageBox
*/
static void _cbMessageBox(WM_MESSAGE* pMsg) {
WM_HWIN hWin = pMsg->hWin;
switch (pMsg->MsgId) {
case WM_NOTIFY_PARENT:
if (pMsg->Data.v == WM_NOTIFICATION_RELEASED) {
int Id = WM_GetId(pMsg->hWinSrc);
GUI_EndDialog(hWin, (Id == GUI_ID_OK) ? 1 : 0);
}
break;
default:
WM_DefaultProc(pMsg);
}
}
/*********************************************************************
*
* _ShowMessageBox
*/
static int _ShowMessageBox(const char* pTitle, const char* pText, int YesNo) {
WM_HWIN hFrame, hClient, hBut;
int r;
/* Create framewin */
hFrame = FRAMEWIN_CreateEx(65, 75, 190, 90, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, 0, pTitle, &_cbMessageBox);
FRAMEWIN_SetClientColor (hFrame, GUI_WHITE);
FRAMEWIN_SetFont (hFrame, &GUI_Font10_1);
FRAMEWIN_SetTextAlign (hFrame, GUI_TA_HCENTER);
/* Create dialog items */
hClient = WM_GetClientWindow(hFrame);
TEXT_CreateEx(10, 7, 170, 30, hClient, WM_CF_SHOW, GUI_TA_HCENTER, 0, pText);
if (YesNo) {
hBut = BUTTON_CreateEx(97, 45, 55, 18, hClient, WM_CF_SHOW, 0, GUI_ID_CANCEL);
BUTTON_SetText (hBut, "No");
hBut = BUTTON_CreateEx(32, 45, 55, 18, hClient, WM_CF_SHOW, 0, GUI_ID_OK);
BUTTON_SetText (hBut, "Yes");
} else {
hBut = BUTTON_CreateEx(64, 45, 55, 18, hClient, WM_CF_SHOW, 0, GUI_ID_OK);
BUTTON_SetText (hBut, "Ok");
}
/* Exec modal dialog */
WM_SetFocus(hFrame);
WM_MakeModal(hFrame);
r = GUI_ExecCreatedDialog(hFrame);
WM_SetFocus(_hFrame);
return r;
}
/*********************************************************************
*
* Static code, dialog settings
*
**********************************************************************
*/
/*******************************************************************
*
* _cbDialogSettings
*/
static void _cbDialogSettings(WM_MESSAGE* pMsg) {
WM_HWIN hWin = pMsg->hWin;
switch (pMsg->MsgId) {
case WM_NOTIFY_PARENT:
if (pMsg->Data.v == WM_NOTIFICATION_RELEASED) {
WM_HWIN hItem = pMsg->hWinSrc;
switch (WM_GetId(hItem)) {
case GUI_ID_OK:
GUI_EndDialog(hWin, 1);
break;
case GUI_ID_CHECK0:
_ShowPossibleMoves = CHECKBOX_GetState(hItem);
_InvalidateBoard();
break;
}
}
break;
default:
WM_DefaultProc(pMsg);
}
}
/*********************************************************************
*
* _ShowDialogSettings
*/
static void _ShowDialogSettings(void) {
WM_HWIN hFrame, hClient, hItem;
/* Create framewin */
hFrame = FRAMEWIN_CreateEx(70, 75, 180, 90, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, 0, "Game settings", &_cbDialogSettings);
FRAMEWIN_SetClientColor (hFrame, GUI_WHITE);
FRAMEWIN_SetFont (hFrame, &GUI_Font10_1);
FRAMEWIN_SetTextAlign (hFrame, GUI_TA_HCENTER);
/* Create dialog items */
hClient = WM_GetClientWindow(hFrame);
/* Create button */
hItem = BUTTON_CreateEx(59, 46, 55, 18, hClient, WM_CF_SHOW, 0, GUI_ID_OK);
BUTTON_SetText (hItem, "Ok");
/* Create checkbox */
hItem = CHECKBOX_CreateEx(10, 10, 140, 0, hClient, WM_CF_SHOW, 0, GUI_ID_CHECK0);
CHECKBOX_SetText (hItem, "Show possible moves");
CHECKBOX_SetBkColor (hItem, GUI_INVALID_COLOR);
CHECKBOX_SetState (hItem, _ShowPossibleMoves);
/* Exec modal dialog */
WM_SetFocus(hFrame);
WM_MakeModal(hFrame);
GUI_ExecCreatedDialog(hFrame);
WM_SetFocus(_hFrame);
}
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*******************************************************************
*
* _SetPlayer
*/
static void _SetPlayer(int Player) {
int PossibleMoves;
_Board.ActPlayer = Player;
if (Player == 1) {
FRAMEWIN_SetText(_hFrame, "Reversi - Player 1");
} else {
FRAMEWIN_SetText(_hFrame, "Reversi - Player 2");
}
FRAMEWIN_SetBarColor(_hFrame, 1, (Player == 1) ? GUI_RED : GUI_BLUE);
PossibleMoves = _CalcValidMoves(&_Board);
GUI_Exec();
if (!PossibleMoves) {
int ValidMoves;
GUI_Exec();
_Board.ActPlayer = 3 - Player;
ValidMoves = _CalcValidMoves(&_Board);
_Board.ActPlayer = Player;
_CalcValidMoves(&_Board);
if (ValidMoves) { /* No valid moves, player must pass */
if (_pPlayerAI[_Board.ActPlayer - 1] == NULL) {
_ShowMessageBox("Reversi", "No possible moves.\nYou have to pass!", 0);
} else {
/* Pass ai player */
}
_SetPlayer(3 - _Board.ActPlayer);
} else { /* No valid moves for all players, game is over */
char ac[256];
int Score;
_GameOver = 1;
_Board.ActPlayer = 1;
Score = _CalcScore(&_Board);
if (Score > 0) {
sprintf(ac, "Red wins by %d stones!\nDo you want to start a new game?", Score);
} else if (Score) {
sprintf(ac, "Blue wins by %d stones!\nDo you want to start a new game?", -Score);
} else {
strcpy(ac, "The game ends in a draw!\nDo you want to start a new game?");
}
if (_ShowMessageBox("Reversi", ac, 1)) {
_StartNewGame();
}
}
}
}
/*******************************************************************
*
* _NextPlayer
*/
static void _NextPlayer(void) {
do {
_SetPlayer(3 - _Board.ActPlayer);
if (_pPlayerAI[_Board.ActPlayer - 1]&& !_GameOver) {
int x, y;
char DoMove;
DoMove = (*_pPlayerAI[_Board.ActPlayer - 1])(&_Board, &x, &y);
_Delay(100);
if (DoMove) {
_MakeMove(&_Board, x, y);
}
}
} while (_pPlayerAI[_Board.ActPlayer - 1] && !_GameOver);
}
/*******************************************************************
*
* _StartNewGame
*/
static void _StartNewGame(void) {
memset(&_Board, 0, sizeof(BOARD));
_Board.aCells[3][3] = 1;
_Board.aCells[4][4] = 1;
_Board.aCells[3][4] = 2;
_Board.aCells[4][3] = 2;
_GameOver = 0;
_SetPlayer(1);
_InvalidateBoard();
}
/*******************************************************************
*
* _HandlePID
*/
static void _HandlePID(int x, int y, int Pressed) {
static int _IsInHandlePID = 0;
if (_IsInHandlePID++ == 0) {
_CalcBoardDimensions();
x -= _BoardX0;
y -= _BoardY0;
if ((x >= 0) && (y >= 0)) {
x /= _CellSize;
y /= _CellSize;
if ((x < 8) && (y < 8)) {
if (_IsValidMove(&_Board, x, y)) {
if (Pressed == 0) {
_ReleaseCapture();
_MakeMove(&_Board, x, y);
_NextPlayer();
} else {
_SetCapture();
}
goto EndHandlePID;
}
}
}
_ReleaseCapture();
}
EndHandlePID:
_IsInHandlePID--;
}
/*********************************************************************
*
* _OnTouch
*/
static void _OnTouch(WM_MESSAGE* pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE*)pMsg->Data.p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -