//五子棋 #include<graphics.h> #define board_size 20 #define pixel 600 int pr = pixel / board_size; char board_data[board_size][board_size]; char current_piece = 'o'; int count = 0; //检测指定玩家是否获胜 bool CheckWin(char c) { int i, j; //检查行 for (i = 0; i < board_size; i++) { for (j = 0; j < board_size - 4; j++) { if (board_data[i][j] == c && board_data[i][j + 1] == c && board_data[i][j + 2] == c && board_data[i][j + 3] == c && board_data[i][j + 4] == c) return true; } } //检查列 for (i = 0; i < board_size - 4; i++) { for (j = 0; j < board_size; j++) { if (board_data[i][j] == c && board_data[i + 1][j] == c && board_data[i + 2][j] == c && board_data[i + 3][j] == c && board_data[i + 4][j] == c) return true; } } //检查主对角线 for (i = 0; i < board_size - 4; i++) { for (j = 0; j < board_size - 4; j++) { if (board_data[i][j] == c && board_data[i + 1][j + 1] == c && board_data[i + 2][j + 2] == c && board_data[i + 3][j + 3] == c && board_data[i + 4][j + 4] == c) return true; } } //检查副对角线 for (i = 0; i < board_size - 4; i++) { for (j = 4; j < board_size; j++) { if (board_data[i][j] == c && board_data[i + 1][j - 1] == c && board_data[i + 2][j - 2] == c && board_data[i + 3][j - 3] == c && board_data[i + 4][j - 4] == c) return true; } } return false; } //检测是否平局 bool CheckDraw() { if (count <= board_size * board_size) return false; return true; } //绘制网格 void DrawBoard() { setlinestyle(PS_SOLID, 1); setlinecolor(WHITE); for (int i = 1; i <= board_size-1; i++) { line(0, pr * i, pixel, pr * i); line(pr * i, 0, pr * i, pixel); } } //绘制棋子 void DrawPiece() { setlinestyle(PS_SOLID, 3); for (int i = 0; i < board_size; i++) { for (int j = 0; j < board_size; j++) { switch (board_data[i][j]) { case 'o': setlinecolor(YELLOW); circle(j * pr + (pr / 2), i * pr + (pr / 2), pr / 2 - 4); break; case 'x': setlinecolor(LIGHTRED); line(j * pr + 4, i * pr + 4, (j + 1) * pr - 4, (i + 1) * pr - 4); line((j + 1) * pr - 4, i * pr + 4, j * pr + 4, (i + 1) * pr - 4); break; case '-':break; } } } } //绘制提示信息 void DrawTipText() { static TCHAR str[64]; _stprintf_s(str, _T("当前棋子的类型为: %c"), current_piece); settextcolor(RGB(225, 175, 45)); outtextxy(0, 0, str); } int main() { initgraph(pixel, pixel); setbkcolor(RGB(0,220,220)); ExMessage msg; bool running = true; memset(board_data, '-', sizeof(board_data)); BeginBatchDraw(); while (running) { DWORD start_time = GetTickCount(); while (peekmessage(&msg)) { if (msg.message == WM_LBUTTONDOWN) { count++; int x = msg.x; int y = msg.y; int index_x = x / pr; int index_y = y / pr; if (board_data[index_y][index_x] == '-') { board_data[index_y][index_x] = current_piece; if (current_piece == 'o') current_piece = 'x'; else current_piece = 'o'; } } } cleardevice(); DrawBoard(); DrawPiece(); DrawTipText(); FlushBatchDraw(); if (CheckWin('o')) { MessageBox(GetHWnd(), _T("O玩家获胜"), _T("游戏结束"), MB_OK); running = false; } else if (CheckWin('x')) { MessageBox(GetHWnd(), _T("X玩家获胜"), _T("游戏结束"), MB_OK); running = false; } else if (CheckDraw()) { MessageBox(GetHWnd(), _T("平局!"), _T("游戏结束"), MB_OK); running = false; } DWORD end_time = GetTickCount(); DWORD delta_time = end_time - start_time; if (delta_time < 1000 / 60) { Sleep(1000 / 60 - delta_time); } } EndBatchDraw(); }