资源简介
Qt写的网络五子棋代码,使用了QTcpSocket类,进行网络通信。

代码片段和文件信息
#include “chessboard.h“
#include
#include
#include
#include
ChessBoard::ChessBoard(QWidget *parent) :
QWidget(parent)
{
isLocal = false;
isServer = false;
localImg = QImage(“:/images/local.png“).scaled(ZOOM ZOOM);
remoteImg = QImage(“:/images/remote.png“).scaled(ZOOM ZOOM);
localPoints.clear();
remotePoints.clear();
}
int ChessBoard::directionNumber(const QPoint &p
int xoffset
int yoffset
bool local)
{
QMap &points = local ? localPoints : remotePoints;
int num = 1;
int i;
for(i = 0; i < 5; i++)
{
if(-1 == points.key(p + QPoint(xoffset * i yoffset * i) -1))
break;
}
num += i -1;
for(i = 0; i < 5 - num + 1; i++)
{
if(-1 == points.key(p + QPoint(-xoffset * i -yoffset * i) -1))
break;
}
num += i -1;
return num;
}
void ChessBoard::start(bool s)
{
isLocal = s;
isServer = s;
}
bool ChessBoard::isVectory(const QPoint &p bool local)
{
if(directionNumber(p 0 1 local) >= 5) return true;
if(directionNumber(p 1 1 local) >= 5) return true;
if(directionNumber(p 1 0 local) >= 5) return true;
if(directionNumber(p -1 1 local) >= 5) return true;
return false;
}
QSize ChessBoard::sizeHint()const
{
return QSize(ZOOM * WIDTH + ZOOM
ZOOM * WIDTH + ZOOM);
}
void ChessBoard::paintEvent(QPaintEvent *)
{
QPainter painter(this);
//绘制棋盘网格
for(int i = 0; i <= WIDTH; i++)
{
painter.drawLine(i * ZOOM + ZOOM / 2
ZOOM / 2
i * ZOOM + ZOOM / 2
WIDTH * ZOOM + ZOOM / 2);
}
for(int i = 0; i <= HEIGHT; i++)
{
painter.drawLine(ZOOM / 2
i * ZOOM + ZOOM / 2
HEIGHT * ZOOM + ZOOM / 2
i * ZOOM + ZOOM / 2);
}
//绘制棋子
for(int i = 0; i < localPoints.count(); i++)
{
painter.drawImage(ZOOM * localPoints[i].x()
ZOOM * localPoints[i].y()
localImg);
}
for(int i = 0; i < remotePoints.count(); i++)
{
painter.drawImage(ZOOM * remotePoints[i].x()
ZOOM * remotePoints[i].y()
remoteImg);
}
}
void ChessBoard::mousePressEvent(QMouseEvent *e)
{
int x y;
QPoint p;
//是否为本方下棋
if(!isLocal) goto Ret;
//是否为左键点击
if(e->button() != Qt::LeftButton) goto Ret;
//是否在有效区域
x = e->pos().x() / ZOOM;
y = e->pos().y() / ZOOM;
if(x > WIDTH || y > HEIGHT) goto Ret;
//是否重复坐标
p = QPoint(x y);
if(-1 != localPoints.key(p -1)) goto Ret;
if(-1 != remotePoints.key(p -1)) goto Ret;
isLocal = false;
emit localDone(p);
localPoints[localPoints.count()] = p;
this->update();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
I.A.... 3640 2013-04-21 11:40 netgobang\chessboard.cpp
I.A.... 928 2013-04-21 11:23 netgobang\chessboard.h
I.A.... 1630 2005-09-10 16:27 netgobang\images\local.png
I.A.... 1594 2005-09-10 16:27 netgobang\images\remote.png
I.A.... 134 2013-04-18 18:42 netgobang\images.qrc
I.A.... 1440 2013-04-18 18:29 netgobang\logindialog.cpp
I.A.... 638 2013-04-18 18:29 netgobang\logindialog.h
I.A.... 7392 2013-04-21 11:32 netgobang\logindialog.ui
I.A.... 798 2013-04-21 11:49 netgobang\main.cpp
I.A.... 2524 2013-04-21 12:53 netgobang\netgobang.cpp
I.A.... 677 2013-04-21 12:44 netgobang\netgobang.h
I.A.... 272 2013-04-21 09:31 netgobang\netgobang.pro
I.A.... 7970 2013-06-23 21:45 netgobang\netgobang.pro.user
I.A.... 427 2013-04-18 12:24 netgobang\tcpserver.cpp
I.A.... 406 2013-04-17 19:46 netgobang\tcpserver.h
I..D... 0 2013-07-03 20:20 netgobang\images
I..D... 0 2013-07-03 20:20 netgobang
----------- --------- ---------- ----- ----
30470 17
- 上一篇:labview模拟风扇
- 下一篇:mathcad 经典电路设计
相关资源
- mgn-mqt82.exe
- Trojan-Qt5-Windows-0.0.4.rar
- 酒店管理系统基于Qt Creator5)
- vtk QT做的三维地质可视化系统2of2
- Qt局域网聊天软件
- Qt Creator opengl实现四元数鼠标控制轨迹
- QT局域网聊天系统(基于QT5.修改过)
- qt-电子点菜系统
- C 餐厅叫号系统(QT平)
- QT 实现文件下载
- qt图像处理
- QT,JPEG解码源代码(已完成)
- Qt 播放音频文件
- Qt 读取16进制的data文件
- MQTT+串口(usart)透传
- 易语言QQTEA算法源码
- VC工程转Qt工程文件的工具
- MQTT推送Demo
- 基于Linux、QT的视频监控系统的设计与
- Qt 百度地图 定位
- QT酷炫界面开发指南《QmlBook》
- Ubuntu下操作Excel,qt代码
- Qt图片浏览器 --基于Qt的Graphics View f
- qtnribbon2破解
- Qt软件开发 完整项目代码
- MQTT_3.1protocol_Specific中文版
- 在QT中使用RTP进行视频的采集和传输
- Mini6410 Qt4和Qtopia编程开发指南
- Qt实现Code39条形码
- qt_ffmpeg_mp4_export_and_import.zip
评论
共有 条评论