用QT来进行开发,采用TCP传输协议进行联机对局。程序运行后,用户用鼠标进行反馈下棋,一方胜利后棋盘便无法使用且显示胜利方,必须进行按键重载操作;如联机,创建方必须输入有效房间号(大于2)才能创建房间,链接方要输入创建方的IP地址和相对应的房间号才能链接,链接完成后双方都会收到反馈信息,然后就可以开始对决;联机时,只有一方下棋后另一方才能下棋,胜利后显示胜利方,并且双方都重置棋盘收到反馈才能继续对决。
注:在自己编写时请将dialog替换成widget就能正常运行,main.cpp不需要编写。
注:.por文件需添加网络支持库QT += core gui network才能运行!
注:标志位的使用,特别是网络方面的还有待提高,如第一次使用可暂时不用,待后续调试再添加。
注:屏幕自锁程序不建议使用,锁死后无法进行任何操作只能关闭程序再次启动!
界面设计dialog.ui文件:
头文件dialog.h文件:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPaintEvent>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
protected:
void paintEvent(QPaintEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private slots:
void on_pushButton_clicked();//重新开始
void on_pushButton_2_clicked();//暂停/继续
void on_pushButton_3_clicked();//发送消息(网络连接后才会使用)
void on_pushButton_4_clicked();//创建房间
void on_pushButton_5_clicked();//链接房间
void new_connect();//创建服务器
void recvdata();
void net_connect();//链接服务器
void readdata();
private:
Ui::Dialog *ui;
int a[15][15];//棋盘大小15*15
void chess(int x,int y);//下棋程序
void iswin(int x,int y);//胜利条件
int player;
int f1(int x,int y);
int f2(int x,int y);
int f3(int x,int y);
int f4(int x,int y);
int long_s;//单机锁屏
int inte;//联网锁屏
QTcpServer server;
QTcpSocket *socket;
int TCP;//服务器标志位
int TCP_F;
};
#endif // DIALOG_H
C程序dialog.cpp文件整体预览:
#include "dialog.h"
#include "ui_dialog.h"
#include <QPainter>
#inc