#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QLabel>
#include <QDebug>
#include <QPushButton>
#include <QStringList>
#include <QMessageBox>
#include "qtsocket.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->mBot = NULL;
this->setWindowTitle("五子棋");
//初始化棋盘
this->chessboardInit();
//本地对战
connect(ui->singerGame,&QPushButton::clicked,this,&MainWindow::singerGame);
//联机对战
connect(ui->multiGame,&QPushButton::clicked,this,&MainWindow::qtSocket);
//人机对战
connect(ui->botGame,&QPushButton::clicked,this,&MainWindow::botGame);
}
MainWindow::~MainWindow()
{
if (mBot != NULL)
delete mBot;
delete ui;
}
void MainWindow::chessboardInit()
{
this->role = 1;
for (int i = 1 ; i < 16 ; i++)
{
for (int j = 1 ; j < 16 ; j++)
{
chessboard[i][j] = -1;
}
}
for (int i = 1 ; i < 16 ; i++)
{
if (i == 1)
{
row[i] = 38;
col[i] = 53;
}
else
{
row[i] = row[i-1] + 52;
col[i] = col[i-1] + 49;
}
}
isTcpConnect = false;
isMulGame = false;
isBotGame = false;
isTurn = true;
}
//键盘点击事件
void MainWindow::mousePressEvent(QMouseEvent * e)
{
int putX = e->x();
int putY = e->y();
if (isTcpConnect) //如果是tcp发送请求
{
//qDebug() << isTurn;
if (isTurn)
{
if (isInChessboard(putX,putY))
{
QString content;
isTurn = false;
content = "true,";
QString msg = content + QString::number(putX) + ',' + QString::number(putY);
tcpSocket->write(msg.toUtf8());
tcpSocket->flush();
runGame(putX,putY);
}
}
}
else if (isMulGame)
{
runGame(putX,putY);
}
else if (isBotGame)
{
if (isTurn)
{
runGame(putX,putY);
}
}
}
void MainWindow::runGame(int putX, int putY)
{
int ansX = 0;
int ansY = 0;
QPixmap black(":/new/bg1/image/black.png");
QPixmap white(":/new/bg1/image/white.png");
QLabel* chess = new QLabel(this);
int tempx , tempy;
if (isGameBegin)
{
for (int i = 1 ; i < 16 ; i++)
{
for (int j = 1 ; j < 16 ; j++)
{
if ( abs(putX-row[i]) < 20 && abs(putY-col[j]) < 20 )
{
ansX = row[i] - 13;
ansY = col[j] - 13;
tempx = row[i];
tempy = col[j];
//qDebug() << "row col:" << row[i] << col[j] << endl;
break;
}
}
}
if (role == 0) //黑
{
chess->setPixmap(black);
}
else //白
{
chess->setPixmap(white);
}
int bookx = (tempx-38) / 50 + 1; //人工测距
int booky = (tempy-53) / 50 + 1;
//qDebug() << "ans : " << ansX << " " << ansY;
//qDebug() << bookx << " " << booky;
//测试棋盘
/*
for (int t = 1 ; t < 16 ; t++)
{
for (int k = 1 ; k < 16 ; k++)
{
qDebug() << chessboard[t][k] << " ";
}
qDebug() << endl;
}
*/
if (ansX != 0 && ansY != 0 && chessboard[bookx][booky] == -1)
{
chess->setGeometry(ansX,ansY,30,30);
chess->setScaledContents(true);
labels.push_back(chess);
chess->show();
chessboard[bookx][booky] = role;
for (int i = 1 ; i < 16 ; i++)
{
for (int j = 1 ; j < 16 ; j++)
{
//判断胜利 还原环境
if (ifWin(role,i,j))
{
if (role == 0)
ui->who->setText("游戏结束,黑方获胜");
else
ui->who->setText("游戏结束,白方获胜");
ui->singerGame->setDisabled(false);
ui->botGame->setDisabled(false);
ui->multiGame->setDisabled(false);
// 初始化游戏
if (isBotGame)
delete mBot;
/*
if (isTcpConnect)
{
tcpServer->close();
tcpSocket->close();
}
*/
this->isTcpConnect = false;
this->isMulGame = false;
this->isBotGame = false;
isGameBegin = false;
return;
}
}
}
role = !role;
//机器人下棋
if (isBotGame && isTurn)
{
isTurn = false;
emit sendToBot(bookx,booky);
}
if (!isTcpConnect)
{
if (isGameBegin)
{
if (role == 0)
ui->who->setText("轮到黑方");
else
ui->who->setText("轮到白方");
}
}
else
{
ui->who->setText("轮到对方");
}
}
else
{
delete chess;
}
}
}
void MainWindow::singerGame()
{
this->isGameBegin = true;
this->isMulGame = true;
ui->singerGame->setDisabled(true);
ui->multiGame->setDisabled(true);
ui->botGame->setDisabled(true);
ui->who->setText("游戏开始...");
this->resetGame();
}
bool MainWindow::ifWin(int role , int x , int y)
{
//九宫格判断
int flag[8];
for (int i = 0 ; i < 8 ; i++)
flag[i] = 1;
//往右边
for (int i = x ; i < x+5 ; i++)
{
if (chessboard[i][y] != role || i > 15)
{
flag[0] = 0;
break;
}
}
//往下边
for (int i = y ; i < y+5; i++)
{
if (chessboard[x][i] != role || i > 15)
{
flag[1] = 0;
break;
}
}
//往左边
for (int i = x ; i > x-5 ; i--)
{
if (chessboard[i][y] != role || i < 1)
{
flag[2] = 0;
break;
}
}
//往上边
for (int i = y ; i > y-5 ; i--)
{
if (chessboard[x][i] != role || i < 1)
{
flag[3] = 0;
break;
}
}
int temp_x , temp_y;
//往右上
temp_x = x;
temp_y = y;
for (int index = 1 ; index <= 5 ; index++)
{
if (chessboard[temp_x][temp_y] != role)
{
flag[4] = 0;
break;
}
temp_x++;
temp_y--;
if (temp_x > 15 || temp_y < 1)
{
flag[4] = 0;
break;
}
}
//往右下
temp_x = x;
temp_y = y;
for (int index = 1 ; index <= 5 ; index++)
{
if (chessboard[temp_x][temp_y] != role)
{
flag[5] = 0;
break;
}
temp_x++;
temp_y++;
if (temp_x > 15 || temp_y > 15)
{
flag[5] = 0;
break;
}
}
//往左下
temp_x = x;
temp_y = y;
for (int index = 1 ; index <= 5 ; index++)
{
if (chessboard[temp_x][temp_y] != role)
{
flag[6] = 0;
break;
}
temp_x--;
temp_y++;
if (temp_x < 1 || temp_y > 15)
{
flag[6] = 0;
break;
}
}
//往�
没有合适的资源?快使用搜索试试~ 我知道了~
课程设计 基于QT、C++课程设计五子棋游戏源码+资料齐全+部署文档 高分项目.zip
共28个文件
png:8个
cpp:4个
h:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 177 浏览量
2024-05-14
19:24:16
上传
评论
收藏 1.68MB ZIP 举报
温馨提示
【资源说明】 课程设计 基于QT、C++课程设计五子棋游戏源码+资料齐全+部署文档 高分项目.zip课程设计 基于QT、C++课程设计五子棋游戏源码+资料齐全+部署文档 高分项目.zip 【备注】 1、该项目是高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
课程设计 基于QT、C++课程设计五子棋游戏源码+资料齐全+部署文档 高分项目.zip (28个子文件)
C、C++系统部署文档.md 3KB
Gomoku-master
mainwindow.h 2KB
Gomoku.pro.user 23KB
image
baisheng.png 14KB
logo.png 53KB
white.bmp 2KB
white.png 7KB
chessboard.png 533KB
chessboard.bmp 1.35MB
black.bmp 2KB
levelselect.png 697KB
heisheng.png 15KB
black.png 6KB
mainwindow.cpp 13KB
Gomoku.pro 1KB
qtsocket.h 621B
Gomoku.pro.user.5a82c5a 23KB
main.cpp 172B
mainwindow.ui 4KB
qtsocket.ui 3KB
gomoku.ico 9KB
res.qrc 505B
myico.rc 39B
gomokubot.h 1008B
black.png 6KB
gomokubot.cpp 8KB
qtsocket.cpp 2KB
171265889347208773632.zip 416B
共 28 条
- 1
资源评论
不走小道
- 粉丝: 3237
- 资源: 5112
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功