五子棋源码

五子棋源码

如下:

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
文件名称:main.cpp
文件描述:实现图形界面五子棋游戏
编译环境:VS2015
最后修改:
<2018年1月15日> <创建工程> <aloner>
邮箱:aloner@baimu.red
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
//====================头文件包含部分=======================
#include<graphics.h> //要放在多媒体设备接口前边
#include<mmsystem.h> //多媒体设备接口
#include<conio.h>

//=====================库文件包含部分======================
#pragma comment (lib,”winmm.lib”)

//=====================全局变量的声明======================
#define WINDOW_WIDTH 1000 //窗口的长
#define WINDOW_HEIGHT 800 //窗口的高
#define Num 18 //格子数
#define PanLength 720 //棋盘边长
#define GeLength (PanLength/Num) //格子边长/*****宏定义没加括号出问题******/
#define Leftjie 80 //左界
#define Rightjie 200 //右界
#define UpDownjie 40 //上下界
#define hong RGB(255,0,0) //自定义颜色
#define cheng RGB(255,125,0) //用于画线
#define huang RGB(255,255,0)
#define lv RGB(0,255,0)
#define lan RGB(0,0,255)
#define dian RGB(0,255,255)
#define zi RGB(255,0,255)
#define heise BLACK //黑棋颜色(颜色可修改)
#define baise RGB(255,255,255) //白棋颜色(颜色可修改)

IMAGE Background; //背景图片
HWND hWnd; //窗口句柄

int Map[Num + 1][Num + 1] = { 0 };
int drawX = 0, drawY = 0; //棋的坐标
int X = 0, Y = 0; //数组坐标
int cnt; //黑棋白棋一共走的步数
int i, j;
TCHAR heibuS[30];
TCHAR baibuS[30];
TCHAR wanjia[30];
MOUSEMSG msg = { 0 }; //鼠标消息
int flag; //胜利标志
int chongwan; //重玩信息标志
//所有棋子坐标记录
struct {
int rx[(Num + 1)*(Num + 1)];
int ry[(Num + 1)*(Num + 1)];
}Record;

TCHAR hei[] = _T(“黑棋:玩家1”);
TCHAR bai[] = _T(“白棋:玩家2”);
TCHAR heibu[] = _T(“黑棋已走 步”);
TCHAR baibu[] = _T(“白棋已走 步”);
TCHAR luozi[] = _T(“请玩家 落子”);
TCHAR again[] = _T(“重玩游戏”);
TCHAR huiqi[] = _T(“悔棋”);

//======================全局函数声明=======================
VOID GameInit();
VOID PlayGame();
VOID MouseClick();
VOID Judge();
VOID Back();

//========================主函数===========================
int main()
{
initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
PlaySound(L”纯音乐 – 一首好听的古筝曲.wav”, NULL, SND_LOOP | SND_FILENAME | SND_ASYNC| SND_NODEFAULT);
do {
cnt = 0;
chongwan = 0;
flag = 0;
GameInit();
while (flag == 0)
{
MouseClick();
Judge();
if (chongwan == 1)
break;
}
for (i = 0; i < Num + 1; i++) {
for (j = 0; j < Num + 1; j++)
{
Map[i][j] = 0;
}
}
if (flag)
{
settextcolor(hong);
LOGFONT q;
gettextstyle(&q); // 获取当前字体设置
q.lfHeight = 64; // 设置字体高度为 24
_tcscpy_s(q.lfFaceName, _T(“黑体”)); // 设置字体为“草书”(高版本 VC 推荐使用 _tcscpy_s 函数)
q.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿
settextstyle(&q); // 设置字体样式
setbkmode(0);
TCHAR baoliu[] = _T(“请按任意键继续…”);
outtextxy(170, 400, baoliu);
_getch();
}
} while (1);
return 0;
}

//游戏初始化函数
VOID GameInit()
{
loadimage(&Background, TEXT(“Background.jpg”), WINDOW_WIDTH, WINDOW_HEIGHT);
putimage(0, 0, &Background);
BeginBatchDraw();
for (i = 0; i < 19; i++)
{
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 2);
if (i < 7)
j = i + 1;
else if (i >= 7 && i<14)
j = i6;
else j = i13;
switch (j)
{
case 1:
setlinecolor(hong);
break;
case 2:
setlinecolor(cheng);
break;
case 3:
setlinecolor(huang);
break;
case 4:
setlinecolor(lv);
break;
case 5:
setlinecolor(lan);
break;
case 6:
setlinecolor(dian);
break;
case 7:
setlinecolor(zi);
break;
}
line(Leftjie + GeLength*i, UpDownjie, Leftjie + GeLength*i, WINDOW_HEIGHTUpDownjie);
line(Leftjie, UpDownjie + GeLength*i, WINDOW_WIDTHRightjie, UpDownjie + GeLength*i);
}
EndBatchDraw();

//输出右侧说明文字
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置
f.lfHeight = 24; // 设置字体高度为 24
_tcscpy_s(f.lfFaceName, _T(“草书”)); // 设置字体为“草书”(高版本 VC 推荐使用 _tcscpy_s 函数)
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿
settextstyle(&f); // 设置字体样式
setbkmode(0);

settextcolor(huang);
outtextxy(850, 60, hei);
outtextxy(850, 90, bai);
settextcolor(hong);
outtextxy(850, 150, luozi);

//重玩游戏
settextcolor(hong);
outtextxy(850, 390, again);

//悔棋
settextcolor(zi);
outtextxy(850, 470, huiqi);

//步数显示
settextcolor(lv);
outtextxy(850, 210, heibu);
outtextxy(850, 240, baibu);
}

VOID MouseClick()
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
case WM_LBUTTONUP:
if (msg.x > 70 && msg.x < 810 && msg.y > 30 && msg.y < 770)
{
if (msg.x>100 && msg.y>60)
{
if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength + 1][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 0)
{
X = (msg.x – Leftjie – GeLength / 2) / GeLength + 1;
Y = (msg.y – UpDownjie – GeLength / 2) / GeLength + 1;
Record.rx[cnt] = X;
Record.ry[cnt] = Y;
cnt++;
drawX = ((msg.x – Leftjie – GeLength / 2) / GeLength + 1)*GeLength + Leftjie;
drawY = ((msg.y – UpDownjie – GeLength / 2) / GeLength + 1)*GeLength + UpDownjie;
if (cnt % 2) {
Map[X][Y] = 1;
}
else Map[X][Y] = 2;
if (cnt > 0)
PlayGame();
}
else if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength + 1][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 1 || Map[(msg.x – Leftjie – GeLength / 2) / GeLength + 1][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 2)
{
hWnd = GetHWnd();
MessageBox(hWnd, L”这里有棋子哦,请选择别处”, L”Are you逗比?”, MB_OK);
}
}
else if (msg.x<100 && msg.y > 60)
{
if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 0)
{
X = 0;
Y = (msg.y – UpDownjie – GeLength / 2) / GeLength + 1;
Record.rx[cnt] = X;
Record.ry[cnt] = Y;
cnt++;
drawX = Leftjie;
drawY = ((msg.y – UpDownjie – GeLength / 2) / GeLength + 1)*GeLength + UpDownjie;
if (cnt % 2) {
Map[X][Y] = 1;
}
else Map[X][Y] = 2;
if (cnt > 0)
PlayGame();
}
else if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 1 || Map[(msg.x – Leftjie – GeLength / 2) / GeLength][(msg.y – UpDownjie – GeLength / 2) / GeLength + 1] == 2)
{
hWnd = GetHWnd();
MessageBox(hWnd, L”这里有棋子哦,请选择别处”, L”Are you逗比?”, MB_OK);
}
}
else if (msg.x>100 && msg.y < 60)
{
if (Map[(msg.xLeftjieGeLength / 2) / GeLength + 1][(msg.yUpDownjieGeLength / 2) / GeLength] == 0)
{
X = (msg.xLeftjieGeLength / 2) / GeLength + 1;
Y = 0;
Record.rx[cnt] = X;
Record.ry[cnt] = Y;
cnt++;
drawX = ((msg.xLeftjieGeLength / 2) / GeLength + 1)*GeLength + Leftjie;
drawY = UpDownjie;
if (cnt % 2) {
Map[X][Y] = 1;
}
else Map[X][Y] = 2;
if (cnt > 0)
PlayGame();
}
else if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength + 1][(msg.y – UpDownjie – GeLength / 2) / GeLength] == 1 || Map[(msg.x – Leftjie – GeLength / 2) / GeLength + 1][(msg.y – UpDownjie – GeLength / 2) / GeLength] == 2)
{
hWnd = GetHWnd();
MessageBox(hWnd, L”这里有棋子哦,请选择别处”, L”Are you逗比?”, MB_OK);
}
}
else if (msg.x<100 && msg.y < 60)
{
if (Map[(msg.xLeftjieGeLength / 2) / GeLength][(msg.yUpDownjieGeLength / 2) / GeLength] == 0)
{
X = 0;
Y = 0;
Record.rx[cnt] = X;
Record.ry[cnt] = Y;
cnt++;
drawX = Leftjie;
drawY = UpDownjie;
if (cnt % 2) {
Map[X][Y] = 1;
}
else Map[X][Y] = 2;
if (cnt > 0)
PlayGame();
}
else if (Map[(msg.x – Leftjie – GeLength / 2) / GeLength][(msg.y – UpDownjie – GeLength / 2) / GeLength] == 1 || Map[(msg.x – Leftjie – GeLength / 2) / GeLength][(msg.y – UpDownjie – GeLength / 2) / GeLength] == 2)
{
hWnd = GetHWnd();
MessageBox(hWnd, L”这里有棋子哦,请选择别处”, L”Are you逗比?”, MB_OK);
}
}
}
else if (msg.x >= 845 && msg.y >= 370 && msg.y <= 410)
chongwan = 1;
else if (msg.x >= 845 && msg.y >= 450 && msg.y <= 490)
{
if (cnt>0)
{
Back();
}
else {
hWnd = GetHWnd();
MessageBox(hWnd, L”啊?什么?你已经悔到头了呢!”, L”你是猴子吗?”, MB_OK);
}
}
break;
}
}

VOID PlayGame()
{
BeginBatchDraw();
if (cnt % 2)
setfillcolor(heise);
else setfillcolor(baise);
//if (Map[X][Y] == 0)
solidcircle(drawX, drawY, 19);

//计步
setcolor(RGB(255, 255, 255));
setbkmode(OPAQUE);
heibuS[1] = (int)(cnt – 0.001) / 2 + 1;
_stprintf_s(heibuS, _T(“%d”), heibuS[1]);
outtextxy(951, 210, heibuS);

baibuS[1] = cnt / 2;
_stprintf_s(baibuS, _T(“%d”), baibuS[1]);
outtextxy(951, 240, baibuS);

//轮到玩家显示
if (cnt % 2) {
wanjia[1] = 2;
}
else wanjia[1] = 1;
_stprintf_s(wanjia, _T(“%d”), wanjia[1]);
outtextxy(927, 150, wanjia);
EndBatchDraw();
}

VOID Judge()
{
//黑棋赢
if (
(Map[X][Y] == 1 && Map[X + 1][Y] == 1 && Map[X + 2][Y] == 1 && Map[X + 3][Y] == 1 && Map[X + 4][Y] == 1) ||
(Map[X – 1][Y] == 1 && Map[X][Y] == 1 && Map[X + 1][Y] == 1 && Map[X + 2][Y] == 1 && Map[X + 3][Y] == 1) ||
(Map[X – 2][Y] == 1 && Map[X – 1][Y] == 1 && Map[X][Y] == 1 && Map[X + 1][Y] == 1 && Map[X + 2][Y] == 1) ||
(Map[X – 3][Y] == 1 && Map[X – 2][Y] == 1 && Map[X – 1][Y] == 1 && Map[X][Y] == 1 && Map[X + 1][Y] == 1) ||
(Map[X – 4][Y] == 1 && Map[X – 3][Y] == 1 && Map[X – 2][Y] == 1 && Map[X – 1][Y] == 1 && Map[X][Y] == 1) ||
(Map[X][Y] == 1 && Map[X][Y + 1] == 1 && Map[X][Y + 2] == 1 && Map[X][Y + 3] == 1 && Map[X][Y + 4] == 1) ||
(Map[X][Y – 1] == 1 && Map[X][Y] == 1 && Map[X][Y + 1] == 1 && Map[X][Y + 2] == 1 && Map[X][Y + 3] == 1) ||
(Map[X][Y – 2] == 1 && Map[X][Y – 1] == 1 && Map[X][Y] == 1 && Map[X][Y + 1] == 1 && Map[X][Y + 2] == 1) ||
(Map[X][Y – 3] == 1 && Map[X][Y – 2] == 1 && Map[X][Y – 1] == 1 && Map[X][Y] == 1 && Map[X][Y + 1] == 1) ||
(Map[X][Y – 4] == 1 && Map[X][Y – 3] == 1 && Map[X][Y – 2] == 1 && Map[X][Y – 1] == 1 && Map[X][Y] == 1) ||
(Map[X][Y] == 1 && Map[X + 1][Y – 1] == 1 && Map[X + 2][Y – 2] == 1 && Map[X + 3][Y – 3] == 1 && Map[X + 4][Y – 4] == 1) ||
(Map[X – 1][Y + 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y – 1] == 1 && Map[X + 2][Y – 2] == 1 && Map[X + 3][Y – 3] == 1) ||
(Map[X – 2][Y + 2] == 1 && Map[X – 1][Y + 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y – 1] == 1 && Map[X + 2][Y – 2] == 1) ||
(Map[X – 3][Y + 3] == 1 && Map[X – 2][Y + 2] == 1 && Map[X – 1][Y + 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y – 1] == 1) ||
(Map[X – 4][Y + 4] == 1 && Map[X – 3][Y + 3] == 1 && Map[X – 2][Y + 2] == 1 && Map[X – 1][Y + 1] == 1 && Map[X][Y] == 1) ||
(Map[X][Y] == 1 && Map[X + 1][Y + 1] == 1 && Map[X + 2][Y + 2] == 1 && Map[X + 3][Y + 3] == 1 && Map[X + 4][Y + 4] == 1) ||
(Map[X – 1][Y – 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y + 1] == 1 && Map[X + 2][Y + 2] == 1 && Map[X + 3][Y + 3] == 1) ||
(Map[X – 2][Y – 2] == 1 && Map[X – 1][Y – 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y + 1] == 1 && Map[X + 2][Y + 2] == 1) ||
(Map[X – 3][Y – 3] == 1 && Map[X – 2][Y – 2] == 1 && Map[X – 1][Y – 1] == 1 && Map[X][Y] == 1 && Map[X + 1][Y + 1] == 1) ||
(Map[X – 4][Y – 4] == 1 && Map[X – 3][Y – 3] == 1 && Map[X – 2][Y – 2] == 1 && Map[X – 1][Y – 1] == 1 && Map[X][Y] == 1)
)
flag = 1;

//白棋赢
if (
(Map[X][Y] == 2 && Map[X + 1][Y] == 2 && Map[X + 2][Y] == 2 && Map[X + 3][Y] == 2 && Map[X + 4][Y] == 2) ||
(Map[X – 1][Y] == 2 && Map[X][Y] == 2 && Map[X + 1][Y] == 2 && Map[X + 2][Y] == 2 && Map[X + 3][Y] == 2) ||
(Map[X – 2][Y] == 2 && Map[X – 1][Y] == 2 && Map[X][Y] == 2 && Map[X + 1][Y] == 2 && Map[X + 2][Y] == 2) ||
(Map[X – 3][Y] == 2 && Map[X – 2][Y] == 2 && Map[X – 1][Y] == 2 && Map[X][Y] == 2 && Map[X + 1][Y] == 2) ||
(Map[X – 4][Y] == 2 && Map[X – 3][Y] == 2 && Map[X – 2][Y] == 2 && Map[X – 1][Y] == 2 && Map[X][Y] == 2) ||
(Map[X][Y] == 2 && Map[X][Y + 1] == 2 && Map[X][Y + 2] == 2 && Map[X][Y + 3] == 2 && Map[X][Y + 4] == 2) ||
(Map[X][Y – 1] == 2 && Map[X][Y] == 2 && Map[X][Y + 1] == 2 && Map[X][Y + 2] == 2 && Map[X][Y + 3] == 2) ||
(Map[X][Y – 2] == 2 && Map[X][Y – 1] == 2 && Map[X][Y] == 2 && Map[X][Y + 1] == 2 && Map[X][Y + 2] == 2) ||
(Map[X][Y – 3] == 2 && Map[X][Y – 2] == 2 && Map[X][Y – 1] == 2 && Map[X][Y] == 2 && Map[X][Y + 1] == 2) ||
(Map[X][Y – 4] == 2 && Map[X][Y – 3] == 2 && Map[X][Y – 2] == 2 && Map[X][Y – 1] == 2 && Map[X][Y] == 2) ||
(Map[X][Y] == 2 && Map[X + 1][Y – 1] == 2 && Map[X + 2][Y – 2] == 2 && Map[X + 3][Y – 3] == 2 && Map[X + 4][Y – 4] == 2) ||
(Map[X – 1][Y + 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y – 1] == 2 && Map[X + 2][Y – 2] == 2 && Map[X + 3][Y – 3] == 2) ||
(Map[X – 2][Y + 2] == 2 && Map[X – 1][Y + 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y – 1] == 2 && Map[X + 2][Y – 2] == 2) ||
(Map[X – 3][Y + 3] == 2 && Map[X – 2][Y + 2] == 2 && Map[X – 1][Y + 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y – 1] == 2) ||
(Map[X – 4][Y + 4] == 2 && Map[X – 3][Y + 3] == 2 && Map[X – 2][Y + 2] == 2 && Map[X – 1][Y + 1] == 2 && Map[X][Y] == 2) ||
(Map[X][Y] == 2 && Map[X + 1][Y + 1] == 2 && Map[X + 2][Y + 2] == 2 && Map[X + 3][Y + 3] == 2 && Map[X + 4][Y + 4] == 2) ||
(Map[X – 1][Y – 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y + 1] == 2 && Map[X + 2][Y + 2] == 2 && Map[X + 3][Y + 3] == 2) ||
(Map[X – 2][Y – 2] == 2 && Map[X – 1][Y – 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y + 1] == 2 && Map[X + 2][Y + 2] == 2) ||
(Map[X – 3][Y – 3] == 2 && Map[X – 2][Y – 2] == 2 && Map[X – 1][Y – 1] == 2 && Map[X][Y] == 2 && Map[X + 1][Y + 1] == 2) ||
(Map[X – 4][Y – 4] == 2 && Map[X – 3][Y – 3] == 2 && Map[X – 2][Y – 2] == 2 && Map[X – 1][Y – 1] == 2 && Map[X][Y] == 2)
)
flag = 2;
if (flag == 1) {
hWnd = GetHWnd();
MessageBox(hWnd, L”玩家1胜”, L”恭喜!”, MB_OK);
}
else if (flag == 2)
{
hWnd = GetHWnd();
MessageBox(hWnd, L”玩家2胜”, L”恭喜!”, MB_OK);
}
}

//悔棋
VOID Back()
{
GameInit();
if(cnt>0)
cnt–;
X=Record.rx[cnt];
Y=Record.ry[cnt];
Map[X][Y] = 0;

for (i = 0; i < Num + 1; i++)
{
for (j = 0; j < Num + 1; j++)
{
if (Map[i][j]==1)
setfillcolor(heise);
else if(Map[i][j]==2)
setfillcolor(baise);
else continue;
drawX = Leftjie + i*GeLength;
drawY = UpDownjie + j*GeLength;
solidcircle(drawX, drawY, 19);
}
}
//计步
setcolor(RGB(255, 255, 255));
setbkmode(OPAQUE);
if (cnt == 0)
{
heibuS[1] = 0;
}else
heibuS[1] = (int)(cnt0.001) / 2 + 1;
_stprintf_s(heibuS, _T(“%d”), heibuS[1]);
outtextxy(951, 210, heibuS);

baibuS[1] = cnt / 2;
_stprintf_s(baibuS, _T(“%d”), baibuS[1]);
outtextxy(951, 240, baibuS);

//轮到玩家显示
if (cnt % 2) {
wanjia[1] = 2;
}
else wanjia[1] = 1;
_stprintf_s(wanjia, _T(“%d”), wanjia[1]);
outtextxy(927, 150, wanjia);
}

背景音乐和背景图片请根据文件名自行添加

C
C
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论