基本信息
源码名称:井字游戏
源码大小:3.61KB
文件格式:.cpp
开发语言:C/C++
更新时间:2024-04-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
双方玩家在3X3的网格上轮番通过鼠标以o和x落子,任意三个相同标志的棋子连城一条直线时,玩家获胜。

#include<graphics.h>


char board_data[3][3] =   //定义棋盘数据结构
{
{'-','-','-'},
{'-','-','-'},
{'-','-','-'},
};

char current_piece = 'o';  //标识当前落子类型



bool CheckWin(char c)      //检测指定棋子的玩家是否获胜,共8种情况
{
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 CheckDraw()           //检测当前是否出现平局,用双重循环遍历每个元素
{
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()             //绘制棋盘网格
{
line(0, 200, 600, 200);
line(0, 400, 600, 400);
line(200, 0, 200, 600);
line(400, 0, 400, 600);
}



void DrawPiece()             //绘制棋子
{
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);
break;
case'x':
line(200 * j, 200 * i, 200 * (j 1), 200 * (i 1));
line(200 * (j 1), 200 * i, 200 * j, 200 * (i 1));
break;
case'-':
break;
defalt:
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(600, 600);    //初始化游戏窗口

bool running = true;    //用条件变量控制程序是否继续

ExMessage msg;          //消息处理,ExMessage是EasyX中,存储消息的结构体。 

BeginBatchDraw();       //开启游戏批量绘图

while (running)
{
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';
}

}

}

//创建、显示并操作一个消息对话框,使用GetHWnd()函数获取句柄。
if (CheckWin('x'))
{
MessageBox(GetHWnd(), _T("X 玩家获胜"),_T("游戏结束"),MB_OK);
running = false;
}
else if (CheckWin('o'))
{
MessageBox(GetHWnd(), _T("O 玩家获胜"), _T("游戏结束"), MB_OK);
running = false;
}
else if (CheckDraw())
{
MessageBox(GetHWnd(), _T("平局!"), _T("游戏结束"), MB_OK);
running = false;
}

cleardevice();//清空绘图设备

DrawBoard(); //绘制棋盘网格
DrawPiece(); //绘制棋子
DrawTipText(); //绘制提示信息

FlushBatchDraw();//消除闪烁
}

EndBatchDraw();//刷新批量绘图的缓冲区

return 0;
}