1、首先看效果图
此处为代码运行后进入界面
竖直判断胜负条件
斜向判断胜负条件
2、代码展示
代码中将每个棋的坐标映射到二维数组里面,进而对胜负进行判断,判断可能条件比较多,但本人在此处也想不出更好的方法,若有更好的办法,欢迎留言!
#include"iostream"
#include"graphics.h"
#include"easyx.h"
#include"math.h"
#include"Windows.h"
class Gobang
{
int x, y;
public:
static int sum;//五子棋棋数
Gobang() :x(0), y(0) {}
Gobang(int x, int y) :x(x), y(y) { sum++; }
int getx()
{
return x;
}
int gety()
{
return y;
}
};
int Gobang::sum = -1;
Gobang gobang[500];//使用类初始化500个棋子
int gobangint[20][20];//映射棋子xy到数组,用于判断
void winning()//根据int数组棋子判断胜负
{
int g;
settextcolor(RED);
for (int i = 0; i < 20; i++){
for (int j = 0; j < 20; j++){
if (gobangint[i][j] == 1 || gobangint[i][j] == 2)
{
for (int n = 1; n < 5; n++){//判断水平往右
if (i + n < 20 && gobangint[i + n][j] == gobangint[i][j]){
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g=getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++){//判断水平往左
if (i - n >=0 && gobangint[i - n][j] == gobangint[i][j]){
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g=getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断竖直往上
if (j + n < 20 && gobangint[i][j+n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g=getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断竖直往下
if (j - n >= 0 && gobangint[i][j-n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g=getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断斜线斜向右上
if (j - n >= 0&&i+n<20 && gobangint[i+n][j - n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g = getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断斜线斜向左上
if (j - n >= 0 && i - n >= 0 && gobangint[i - n][j - n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g = getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断斜线斜向右下
if (j + n < 20 && i + n < 20 && gobangint[i + n][j + n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g = getchar();
exit(0);
}
continue;
}
else
break;
}
for (int n = 1; n < 5; n++) {//判断斜线斜向左下
if (j + n < 20 && i - n >= 0 && gobangint[i - n][j + n] == gobangint[i][j]) {
if (n == 4)
{
outtextxy(640, 10, L"游戏结束!");
g = getchar();
exit(0);
}
continue;
}
else
break;
}
}
}
}
}
void gobangxy(int x,int y)//五子棋落点位置
{
for (int j = 0; j < Gobang::sum+1; j++)
{
if (x >= gobang[j].getx() - 20 && x <= gobang[j].getx() + 20 && y >= gobang[j].gety() - 20 && y <= gobang[j].gety() + 20)
return;
}
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++)
{
if (x > 100 + i * 40 && x < 100 + (i + 1) * 40 && y>100 + j * 40 && y < 100 + (j + 1) * 40)
{
if (Gobang::sum < 500)
{
gobang[Gobang::sum] = Gobang(120+i*40,120+j*40);
if (Gobang::sum % 2 == 0)
{
gobangint[i][j] = 2;
}
else
{
gobangint[i][j] = 1;
}
}
}
}
}
}
int main()
{
int g,k;
initgraph(1000, 1000, SHOWCONSOLE);
setbkcolor(RGB(251,191,15));
cleardevice();
setlinestyle(PS_SOLID, 5);
setlinecolor(RGB(3, 3, 3));
BeginBatchDraw();
for(int i = 0; i < 21; i++)
{
line(100+i * 40, 100, 100+i * 40, 900);
}
for (int i = 0; i < 21; i++)
{
line(100, 100+i * 40, 900, 100+i * 40);
}
settextcolor(RGB(3, 3, 3));
settextstyle(80, 0, TEXT("楷体"));
outtextxy(380, 10, L"五子棋");
settextstyle(50, 0, TEXT("楷体"));
settextcolor(WHITE);
outtextxy(0, 10, L"当前为白棋");
EndBatchDraw();
ExMessage msg;
while (true)
{
if (peekmessage(&msg, EM_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x >= 100 && msg.x <= 900 && msg.y >= 100 && msg.y <= 900)
{
gobangxy(msg.x, msg.y);
if (Gobang::sum % 2 == 0)
{
settextstyle(50, 0, TEXT("楷体"));
settextcolor(WHITE);
outtextxy(0, 10, L"当前为黑棋");
setfillcolor(RGB(255, 255, 255));
solidcircle(gobang[Gobang::sum].getx(), gobang[Gobang::sum].gety(), 10);
}
else
{
settextstyle(50, 0, TEXT("楷体"));
settextcolor(WHITE);
outtextxy(0, 10, L"当前为白棋");
setfillcolor(RGB(0, 0, 0));
solidcircle(gobang[Gobang::sum].getx(), gobang[Gobang::sum].gety(), 10);
}
winning();
}
break;
}
}
}
g=getchar();
closegraph();
}
感兴趣的友友们可以复制自己去运行一下,若有不妥之处,欢迎在评论区讨论,本人会及时修改!!!