#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;
}
}
//往�
没有合适的资源?快使用搜索试试~ 我知道了~
C++课程设计五子棋游戏,基于QT制作.zip
共26个文件
png:8个
cpp:4个
h:3个
1 下载量 123 浏览量
2024-01-06
11:32:15
上传
评论
收藏 1.67MB ZIP 举报
温馨提示
C++课程设计五子棋游戏,基于QT制作.zip
资源推荐
资源详情
资源评论
收起资源包目录
C++课程设计五子棋游戏,基于QT制作.zip (26个子文件)
afdgafds
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
共 26 条
- 1
资源评论
Lei宝啊
- 粉丝: 2067
- 资源: 1330
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功