怎样用C语言编一个简单的五子棋游戏?

2024-11-23 03:07:34
推荐回答(3个)
回答1:

#include
#include
#include
IMAGE* IMG;
IMAGE* IMG2;
IMAGE* IMG3;
IMAGE* whole;
bool mark = false;
int x = 0, y = 0;
int flag[15][15];

void show()
{
outtextxy(550, 100, "白方:");
outtextxy(550, 150, " 箭头移动");
outtextxy(550, 200, " 回车键落子");
outtextxy(550, 250, "黑方:");
outtextxy(550, 300, " ADWS移动");
outtextxy(550, 350, " 空格键落子");
}

int success1(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) >= 0 && (temp_x / 35 + dir1) < 15) && ((temp_y / 35 + dir2) >= 0 && (temp_y / 35 + dir2) < 15) && (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 1))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}

int success2(int dir1, int dir2)
{
int number = 0;
int temp_x = x, temp_y = y;
while (((temp_x / 35 + dir1) >= 0 && (temp_x / 35 + dir1) < 15) && ((temp_y / 35 + dir2) >= 0 && (temp_y / 35 + dir2) < 15) && (flag[(temp_x / 35 + dir1)][(temp_y / 35 + dir2)] == 2))
{
temp_x = temp_x + dir1 * 35;
temp_y = temp_y + dir2 * 35;
++number;
}
return number;
}

int success1()
{
int number = 0;
number = success1(0, -1) + success1(0, 1);//上下
if (number < 4)
{
number = success1(-1, 0) + success1(1, 0);//左右
if (number < 4)
{
number = success1(-1, -1) + success1(1, 1);//左上右下
if (number < 4)
{
number = success1(-1, 1) + success1(1, -1);//左下右上
}
}
}
return number;
}
int success2()
{
int number = 0;
number = success2(0, -1) + success2(0, 1);//上下
if (number < 4)
{
number = success2(-1, 0) + success2(1, 0);//左右
if (number < 4)
{
number = success2(-1, -1) + success2(1, 1);//左上右下
if (number < 4)
{
number = success2(-1, 1) + success2(1, -1);//左下右上
}
}
}
return number;
}

void control()
{
char key = 0;

while (key != 27)
{
Sleep(10);
if (kbhit())
{
key = getch();
switch (key)
{
case VK_LEFT:
if (mark)
break;
if (x > 0)
x = x - 35;
break;
case 'a':
case 'A':
if (!mark)
break;
if (x > 0)
x = x - 35;
break;
case VK_RIGHT:
if (mark)
break;
if (x < 490)
x = x + 35;
break;
case 'd':
case 'D':
if (!mark)
break;
if (x < 490)
x = x + 35;
break;
case VK_UP:
if (mark)
break;
if (y > 0)
y = y - 35;
break;
case 'w':
case 'W':
if (!mark)
break;
if (y > 0)
y = y - 35;
break;
case VK_DOWN:
if (mark)
break;
if (y < 490)
y = y + 35;
break;
case 's':
case 'S':
if (!mark)
break;
if (y < 490)
y = y + 35;
break;
case VK_RETURN:
if (mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 32, IMG2, 0, 0);
flag[x / 35][y / 35] = 1;
if (success1() >= 4)
{
outtextxy(600, 50, "黑方 胜!");
key = 27;
}
mark = true;
}
break;
case VK_SPACE:
if (!mark)
break;
if (flag[x / 35][y / 35] == 0)
{
putimage(whole, x + 6, y + 6, 31, 31, IMG3, 0, 0);
flag[x / 35][y / 35] = 2;
if (success2() >= 4)
{
outtextxy(600, 50, "白方 胜!");
key = 27;
}
mark = false;
}
break;
default:
break;
}
putimage(0, 0, whole);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);
}
}
}

void main()
{
setinitmode(0);
initgraph(800, 538);
SetWindowText(GetHWnd(), "五子棋20110327");
setcolor(0xffffff);
setfont(36, 0, "楷体_GB2312");

IMAGE* IMG1 = new IMAGE;
getimage(IMG1, "JPG", MAKEINTRESOURCE(102));//棋盘
putimage(0, 0, IMG1);
IMG2 = new IMAGE;
getimage(IMG2, "JPG", MAKEINTRESOURCE(103));//黑棋
IMG3 = new IMAGE;
getimage(IMG3, "JPG", MAKEINTRESOURCE(104));//白棋
IMG = new IMAGE;
getimage(IMG, "GIF", MAKEINTRESOURCE(101));//手形
whole = new IMAGE;
getimage(whole, 0, 0, 537, 537);
putimage_transparent(NULL, IMG, x + 20, y + 20, 0x0, 0, 0, 20, 20);

show();
control();

delete IMG1;
delete IMG2;
delete IMG3;
delete whole;
getch();
getch();
closegraph();
}

回答2:

这个问题有点泛,你要先弄懂五子棋的游戏规则,和程序实现的算法,然后再做图形用户界面,如果要进行人机对战,还要写一个智能算法,当然,简单的就不用了···

回答3:

我有源代码,留下邮箱,我发你