Qt平台写的五子棋(人人对战)

tomtc123 2011-08-26 07:16:03
加精
没实现人机对战,求人机对战算法,没学过数据结构,纠结。、

源件:
http://download.csdn.net/source/3551972


#include <qpainter.h>
#include <qbrush.h>
#include <qevent.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qstring.h>
#include <qlabel.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <QMenu>
#include <QAction>
#include <stdio.h>
#include <QMessageBox>
#include "chess.h"

int mychess::timecount = 0;
int gametime_count = 0;
int mousecheck_count = 0;
int mousecheck_x = 0;
int mousecheck_y = 0;
int flag =1;
int gameoverFlag = 0;
int gamewinFlag =0;//
QString game_str;

struct _playFlag
{
int usedFlag;// 1 means pressed.
int pressFlag;//0 nothing,1 white,2 black
//int test;

}a[600][600];


void initplay();


mychess::mychess(QWidget *parent)//构造函数
:QWidget(parent)
{
initplay();
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(myredraw()));
connect(this,SIGNAL(mystop()),this,SLOT(killtime()));
QAction *newgame_action = new QAction(tr("新游戏"),this);
connect(newgame_action,SIGNAL(triggered()),this,SLOT(new_game()));

QAction *exit_action = new QAction(tr("退出"),this);
connect(exit_action,SIGNAL(triggered()),this,SLOT(close()));

QAction *help_action = new QAction(tr("帮助"),this);
connect(help_action,SIGNAL(triggered()),this,SLOT(help_function()));

gamemenu_btn = new QPushButton(tr("游戏"),this);
QMenu *menu_game = new QMenu();
gamemenu_btn->setMenu(menu_game);
menu_game->addAction(newgame_action);
menu_game->addAction(exit_action);
gamemenu_btn->setGeometry(525,150,60,25);

helpmenu_btn = new QPushButton(tr("帮助"),this);
QMenu *menu_help = new QMenu();
helpmenu_btn->setMenu(menu_help);
menu_help->addAction(help_action);
background_image = new QPixmap();
background_image->load("./image/background.jpg");///
resize(620,520);
setMaximumSize(620,520);
setMinimumSize(620,520);

time_label = new QLabel(this);//时间条

mytimer->start(100);

}

void mychess::new_game()//新游戏
{
mytimer->start(100);
initplay();
mousecheck_count=0;
gametime_count = 0;//
mousecheck_x = 0;
mousecheck_y = 0;
gameoverFlag = 0;
gamewinFlag = 0;
game_str = tr("");
this->update();//key
//printf("new game.\n");

}

void mychess::help_function()
{
QMessageBox::about(this,tr("关于"),tr("Chess Desktop V2.0 \nRelease Build(1108261718)\n\t---- 广ヨ〡ロ"));

}


void initplay()//结构体初始化
{
int i;
for(i=0;i<=500;(i++))
{
for(int j=0;j<=500;(j++))
{
//a[i][j] = 0;
a[i][j].pressFlag = 0;
a[i][j].usedFlag = 0;
//a.test[i][j] = 0;
}


}

}



mychess::~mychess()
{

}

void mychess::killtime()
{
mytimer->stop();
}

void mychess::myredraw()//重绘函数
{
if(mytimer->isActive())
{
if(timecount =100)//
{
this->update();
}
else
{
emit mystop();
}
gametime_count++;

}

}


void mychess::paintEvent(QPaintEvent *)//绘图事件
{
time_label->setGeometry(530,230,150,50);
QString Time = tr("时间=")+QString::number(gametime_count/10);
time_label->setText(Time);
time_label->show();


QPainter *pain_line = new QPainter(this);
pain_line->setPen(Qt::black);
pain_line->drawPixmap(0,0,*background_image);

for(int line_count=0;line_count<25;line_count++)//绘制网格
{
pain_line->drawLine(20,20+(line_count*20),20*25,20+(line_count*20));
pain_line->drawLine(20+(line_count*20),20,20+(line_count*20),20*25);

}
pain_line->setBrush(Qt::black);
{
pain_line->drawEllipse(80-5,80-5,10,10);
pain_line->drawEllipse(440-5,80-5,10,10);
pain_line->drawEllipse(80-5,440-5,10,10);
pain_line->drawEllipse(440-5,440-5,10,10);
pain_line->drawEllipse(260-5,240-5,10,10);//中间的定位黑点
}



draw_chessman();
judge_line();



}



void mychess::mousePressEvent(QMouseEvent *e)//鼠标按下判断事件
{

QString str_x;
QString str_y;
str_x = QString::number(e->x());//鼠标左键按下时x坐标
str_y = QString::number(e->y());//鼠标左键按下时y坐标
mousecheck_x = str_x.toInt();
mousecheck_y = str_y.toInt();
if(e->button() == Qt::LeftButton && mousecheck_x>=20&&mousecheck_x<=510
&& mousecheck_y>=20 && mousecheck_y<=510
&& a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag != 1)
{
mousecheck_count++;

if(a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag==0)
{
if(mousecheck_count%2==0)
{
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].pressFlag = 2;

a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag = 1;
printf("b_a[%d][%d]=%d\n",(mousecheck_x/20)*20,(mousecheck_y/20)*20,
a[(mousecheck_x/20)*20][(mousecheck_y/20)*20].pressFlag);


}

if(mousecheck_count%2==1)
{
a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].pressFlag = 1;

a[(mousecheck_x/20)-1][(mousecheck_y/20)-1].usedFlag = 1;
//printf("w_a[%d][%d]=%d\n",(mousecheck_x/20)*20,(mousecheck_y/20)*20,
//a[(mousecheck_x/20)*20][(mousecheck_y/20)*20].pressFlag);

}
}


}
draw_chessman();
judge_line();

}




...全文
5893 75 打赏 收藏 转发到动态 举报
写回复
用AI写文章
75 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaobingcaicai 2013-08-29
  • 打赏
  • 举报
回复
楼主我曾经写过一个人机对战的,很简单的,只计算一步,需要的话可以留个方式我给你
qcgao 2013-03-14
  • 打赏
  • 举报
回复
坐标定位不是很准,不过很有用,谢谢分享!
lzjlys 2012-06-10
  • 打赏
  • 举报
回复
楼主,能把完整原码(应该是三个文件吧)发我邮箱吗,谢谢!我的邮箱lzjlys614521@126.com
chengrIII 2012-06-08
  • 打赏
  • 举报
回复
不错,好东西
cgstsinghua 2011-09-13
  • 打赏
  • 举报
回复
很好,这个对学习C++感觉帮助很大,我有个想法:那些想学c++的人,就可以自己实现个小游戏,这样又有兴趣又学到了东西, 真的很不错哦
wqcvcyn 2011-09-12
  • 打赏
  • 举报
回复
我是新手,学习这个起来有困难啊
lyp2281231 2011-08-30
  • 打赏
  • 举报
回复
向楼主学习!!!!
szramon 2011-08-30
  • 打赏
  • 举报
回复
很不错哦 支持支持
谢谢分享 ~~~
lilysjtu 2011-08-30
  • 打赏
  • 举报
回复
哇 各路牛人啊 膜拜
kxq2011 2011-08-29
  • 打赏
  • 举报
回复
好,不错
a757533556 2011-08-29
  • 打赏
  • 举报
回复
我晕,,这么多。。看着头大。。
zmkkobe 2011-08-29
  • 打赏
  • 举报
回复
支持原创,支持开源,。呵呵
du__xing 2011-08-29
  • 打赏
  • 举报
回复
很不错啊
woaifaye1988 2011-08-29
  • 打赏
  • 举报
回复
haohaohaohao
zhh_kv 2011-08-29
  • 打赏
  • 举报
回复
支持共享,支持开源
Up~~~~~
liyf_liyunfeng 2011-08-29
  • 打赏
  • 举报
回复
来学习,顶一下。
supduo 2011-08-29
  • 打赏
  • 举报
回复
感觉很不错,学习下
asssasharp 2011-08-29
  • 打赏
  • 举报
回复
这里还可以插图片么?
litaopro 2011-08-28
  • 打赏
  • 举报
回复
学习啦路过
czl0325 2011-08-28
  • 打赏
  • 举报
回复
搞什么啊,坐标都计算错误了,把源代码发上来我搞看看
加载更多回复(49)

16,175

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧