#include<graphics.h>
#include<iostream>
#define DELTA 50
#define LINE 5
char board_data[3][3] =
{
{'-','-','-'},
{'-','-','-'},
{'-','-','-'},
};
char current_piece = 'O';
//检测是否获胜
bool CheakWin(char c)
{
if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c) return true;
if (board_data[1][0] == c && board_data[1][1] == c && board_data[1][2] == c) return true;
if (board_data[2][0] == c && board_data[2][1] == c && board_data[2][2] == c) return true;
if (board_data[0][0] == c && board_data[1][0] == c && board_data[2][0] == c) return true;
if (board_data[0][1] == c && board_data[1][1] == c && board_data[2][1] == c) return true;
if (board_data[0][2] == c && board_data[1][2] == c && board_data[2][2] == c) return true;
if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c) return true;
if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c) return true;
return false;
}
//检测是否平局
bool CheakDraw()
{
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
if (board_data[i][j] == '-')
{
return false;
}
}
}
return true;
}
//绘制棋盘网格
void DrawBoard()
{
setlinecolor(BLACK);
solidrectangle(0, 200 + LINE, 600, 200 - LINE);
solidrectangle(0, 400 + LINE, 600, 400 - LINE);
solidrectangle(200 + LINE, 0, 200 - LINE, 600);
solidrectangle(400 + LINE, 0, 400 - LINE, 600);
}
//绘制棋子
void DrawPiece()
{
//设置画线样式为宽度 10 像素的实线,连接处为斜面
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 10);
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 3; j++)
{
switch (board_data[i][j])
{
case 'O':
circle(200 * j + 100, 200 * i + 100, 100 - DELTA);
break;
case 'X':
line(200 * j + DELTA, 200 * i + DELTA, 200 * (j + 1) - DELTA, 200 * (i + 1) - DELTA);
line(200 * (j + 1) - DELTA, 200 * i + DELTA, 200 * j + DELTA, 200 * (i + 1) - DELTA);
break;
case '-':
break;
default:
break;
}
}
}
}
//绘制提示信息
void DrawTipText()
{
static TCHAR str[64];
_stprintf_s(str, _T("当前棋子类型:%c"), current_piece);
settextstyle(24, 0, _T("华文琥珀"));//设置字号、字体
settextcolor(RGB(124, 37, 249));
outtextxy(0, 0, str);
}
int main()
{
initgraph(600, 600);
setbkcolor(RGB(113, 240, 247));
bool running = true;
ExMessage msg;
BeginBatchDraw();
while (running)
{
DWORD start_time = GetTickCount();
while (peekmessage(&msg))
{
//鼠标左键
if (msg.message == WM_LBUTTONDOWN)
{
//计算点击位置
int x = msg.x;
int y = msg.y;
int index_x = x / 200;
int index_y = y / 200;
//尝试落子
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 (CheakWin('X'))
{
MessageBox(GetHWnd(), _T("X 玩家获胜"), _T("游戏结束"), MB_OK);
running = false;
}
else if (CheakWin('O'))
{
MessageBox(GetHWnd(), _T("O 玩家获胜"), _T("游戏结束"), MB_OK);
running = false;
}
else if(CheakDraw())
{
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();
return 0;
}
井字棋(easyX实现)
于 2024-01-10 14:59:17 首次发布