3.系统(界面)类
之所以有两个名字,是因为这个类本身是界面的,但是我们又用它来作为判断棋子,操作棋盘的=,所以可以叫为“系统”吧,因为那些操作不是界面来做的,而是由“幕后黑手”来做的,但是由不想再写一个了,已经够用了,所以就将就将就了。
3.1.头文件
看着很长很多啊。因为自从学会了布局之后,就再也不想用设计器布局了,代码布局也挺方便的,所以按钮都作为属性变量了,有些没用的就不需要加进来,还有一些头文件要加进去。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QGroupBox>
#include <QRadioButton>
#include <QPaintEvent>
#include <QPainter>
#include <QMouseEvent>
#include "chessboard.h"
#define NO_CHESS 0
#define WHITE 1
#define BLACK 2
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
void initData();
void mySetlayout();
void initChessboard();
void paintEvent(QPaintEvent*);
void drawChessboard();
void drawChess();
void mousePressEvent(QMouseEvent* mevent);
void handleChess(int current_x,int current_y);
bool isWin(int x,int y,int chesscolor);
bool getLocation(int& x,int& y,int chesscolor);
bool centreIntercept(int& x,int& y,int max,int color);
bool interceptJudgment(int max);
bool centreIntercept_SN(int x,int y,int max,int color);
bool centreIntercept_EW(int x,int y,int max,int color);
bool centreIntercept_WS_EN(int x,int y,int max,int color);
bool centreIntercept_ES_WN(int x,int y,int max,int color);
bool breakTriangle(int &x,int &y,int color);
void breakTriangle_SN(int x, int y, int& count, int color);
void breakTriangle_EW(int x, int y, int& count, int color);
void breakTriangle_WN_WS(int x, int y, int& count, int color);
void breakTriangle_EN_ES(int x, int y, int& count, int color);
bool jiuGongge(int& x,int& y,int no_color);
~Widget();
public slots:
void on_button_edit();//编辑
void on_button_record();//记录
void on_button_regretchess();//悔棋
void on_button_begin();//开始
void on_button_help();//帮助
private:
ChessBoard *chessBoard;
QPainter* painter;
bool isblack;
bool iswin;
bool isplayer;
int start_x;
int start_y;
int end_x;
int end_y;
private:
QLabel* label_headportrait;//头像
QLabel* label_petname;//昵称
QLabel* label_set;//性别
QLabel* label_titlenumber;//称号
QLabel* label_fieldnumber;//场数
QPushButton* button_edit;//编辑
QPushButton* button_record;//记录
QPushButton* button_regretchess;//悔棋
QPushButton* button_begin;//开始
QPushButton* button_help;//帮助
QLabel* label_chessboard;
QLineEdit* edit_contestants;//选手
QGroupBox* group_model;//模式
QRadioButton* radio_manmachine;//人机
QRadioButton* radio_double;//双人
QGroupBox* group_successivehand;//先后手
QRadioButton* radio_offensiveposition;//先手
QRadioButton* radio_defensiveposition;//后手
QGroupBox* group_chesssquare;//棋方
QRadioButton* radio_black;//黑棋
QRadioButton* radio_white;//白棋
};
#endif // WIDGET_H
3.2.源文件
因为还没有很好的处理代码,所以也许有些是多余的,但是比之前的由好了那么一点。
#include "widget.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setWindowIcon(QIcon(":new/picture/chessicon.jpg"));
this->setWindowTitle("五子棋");
this->setMinimumSize(900,650);
this->initData();
this->initChessboard();
this->mySetlayout();
}
void Widget::initData()
{
this->label_headportrait = new QLabel("0");//头像
this->label_headportrait->setMaximumSize(150,150);
QImage imge(":new/picture/fivechess.webp");
this->label_headportrait->setScaledContents(true);
this->label_headportrait->setPixmap(QPixmap::fromImage(imge));
this->label_petname = new QLabel("0");//昵称
this->label_set = new QLabel("0");//性别
this->label_titlenumber = new QLabel("0");//称号
this->label_fieldnumber = new QLabel("0");//场数
this->button_edit = new QPushButton("编辑");//编辑
QObject::connect(this->button_edit,SIGNAL(clicked()),this,SLOT(on_button_edit()));
this->button_record = new QPushButton("记录");//记录
QObject::connect(this->button_record,SIGNAL(clicked()),this,SLOT(on_button_record()));
this->button_regretchess = new QPushButton("悔棋");//悔棋
QObject::connect(this->button_regretchess,SIGNAL(clicked()),this,SLOT(on_button_regretchess()));
this->button_begin = new QPushButton("开始");//开始
QObject::connect(this->button_begin,SIGNAL(clicked()),this,SLOT(on_button_begin()));
this->button_help = new QPushButton("帮助");//帮助
QObject::connect(this->button_help,SIGNAL(clicked()),this,SLOT(on_button_help()));
this->label_chessboard = new QLabel("",this);
this->edit_contestants = new QLineEdit("选手");//选手
this->edit_contestants->setEnabled(false);
this->group_model = new QGroupBox("模式");//模式
this->radio_manmachine = new QRadioButton("人机模式");//人机
this->radio_double = new QRadioButton("双人模式");//双人
this->group_successivehand = new QGroupBox("先后手");//先后手
this->radio_offensiveposition = new QRadioButton("先手");//先手
this->radio_defensiveposition = new QRadioButton("后手");//后手
this->group_chesssquare = new QGroupBox("棋方");//棋方
this->radio_black = new QRadioButton("黑棋");//黑棋
this->radio_white = new QRadioButton("白棋");//白棋
}
void Widget::mySetlayout()
{
QGridLayout* glayout1 = new QGridLayout;
QGridLayout* glayout2 = new QGridLayout;
QVBoxLayout* vlayout = new QVBoxLayout;
QHBoxLayout* hlayout1 = new QHBoxLayout;
QHBoxLayout* hlayout2 = new QHBoxLayout;
glayout1->addWidget(this->label_headportrait,0,0,2,2);
glayout1->addWidget(new QLabel("昵称"),1,0,1,1);
glayout1->addWidget(this->label_petname,1,1,1,1);
glayout1->addWidget(new QLabel("性别"),2,0,1,1);
glayout1->addWidget(this->label_set,2,1,1,1);
glayout1->addWidget(new QLabel("称号"),3,0,1,1);
glayout1->addWidget(this->label_titlenumber,3,1,1,1);
glayout1->addWidget(new QLabel("场数"),4,0,1,1);
glayout1->addWidget(this->label_fieldnumber,4,1,1,1);
hlayout1->addLayout(glayout1,2);
hlayout1->addWidget(this->label_chessboard,8);
glayout2->addWidget(new QLabel("当前选手"),0,0,1,1);
glayout2->addWidget(this->edit_contestants,1,0);
glayout2->addWidget(new QLabel(""),2,0);
QVBoxLayout* vlayout1 = new QVBoxLayout;
vlayout1->addWidget(this->radio_manmachine);
vlayout1->addWidget(this->radio_double);
this->group_model->setLayout(vlayout1);
glayout2->addWidget(this->group_model,3,0);
QVBoxLayout* vlayout2 = new QVBoxLayout;
vlayout2->addWidget(this->radio_offensiveposition);
vlayout2->addWidget(this->radio_defensiveposition);
this->group_successivehand->setLayout(vlayout2);
glayout2->addWidget(this->group_successivehand,4,0);
QVBoxLayout* vlayout3 = new QVBoxLayout;
vlayout3->addWidget(this->radio_white);
vlayout3->addWidget(this->radio_black);
this->group_chesssquare->setLayout(vlayout3);
glayout2->addWidget(this->group_chesssquare,5,0);
hlayout1->addLayout(glayout2,1);
vlayout->addLayout(hlayout1);
hlayout2->addWidget(this->button_edit);
hlayout2->addWidget(this->button_record);
hlayout2->addWidget(this->button_regretchess);
hlayout2->addWidget(this->button_begin);
hlayout2->addWidget(this->button_help);
vlayout->addLayout(hlayout2);
this->setLayout(vlayout);
}
void Widget::initChessboard()
{
this->chessBoard = new ChessBoard;
this->chessBoard->setRow(16);
this->chessBoard->setCol(16);
this->chessBoard->setStart_x(170);
this->chessBoard->setStart_y(30);
this->chessBoard->initChessStore(this->chessBoard->getRow() * this->chessBoard->getCol());
this->chessBoard->initPositionRecord(this->chessBoard->getRow(),this->chessBoard->getCol());
this->chessBoard->setChessCount(0);
this->chessBoard->setLineSpace(36);
this->isblack = true;
this->iswin = false;
this->isplayer = true;
}
void Widget::paintEvent(QPaintEvent *)
{
painter = new QPainter;
this->painter->begin(this);
painter->drawPixmap(0,0,900,650,QPixmap(":new/picture/chessbackground.jpg"));
this->drawChessboard();
this->drawChess();
this->painter->end();
}
void Widget::drawChessboard()
{
int start_x = this->chessBoard->getStart_x();
int start_y = this->chessBoard->getStart_y();
int linespace = this->chessBoard->getLineSpace();
int row = this->chessBoard->getRow();
int col = this->chessBoard->getCol();
//竖线
for(int x = start_x;x < start_x + linespace * row;x += linespace){
painter->drawLine(x,start_y,x,570);
}
//横线
for(int y = start_y;y < start_y + linespace * col;y += linespace){
painter->drawLine(start_x,y,540+start_x,y);
};
}
void Widget::drawChess()
{
int location_x = 0;
int location_y = 0;
for(int i = 0;i < this->chessBoard->getRow();i++){
location_x = this->chessBoard->getStart_x() + i * this->chessBoard->getLineSpace() - this->chessBoard->getRow();
for(int j = 0;j < this->chessBoard->getCol();j++){
location_y = this->chessBoard->getStart_y() + j * this->chessBoard->getLineSpace() - this->chessBoard->getRow();
if(this->chessBoard->getLocationColor(i,j) == WHITE){
painter->drawPixmap(location_x,location_y,Chess::width,Chess::height,QPixmap(":new/picture/wchess.png"));
}
else if(this->chessBoard->getLocationColor(i,j) == BLACK){
painter->drawPixmap(location_x,location_y,Chess::width,Chess::height,QPixmap(":new/picture/blackchess.png"));
}
}
}
}
void Widget::mousePressEvent(QMouseEvent *mevent)
{
if((mevent->x() > 715 || mevent->x() < 155) || (mevent->y() < 15 || mevent->y() > 575)){
return;
}
if(iswin){
if(isblack){
QMessageBox::information(this,"提示","白棋赢了");
}
else{
QMessageBox::information(this,"提示","黑棋赢了");
}
return;
}
int current_x = mevent->x();
int current_y = mevent->y();
//计算位置
for(int x = 0;x < this->chessBoard->getRow();x++){
if(current_x - this->chessBoard->getStart_x() - x * this->chessBoard->getLineSpace() <
this->chessBoard->getLineSpace() / 2){
current_x = x;
break;
}
}
for(int y = 0;y < this->chessBoard->getCol();y++){
if(current_y - this->chessBoard->getStart_y() - y * this->chessBoard->getLineSpace() <
this->chessBoard->getLineSpace() / 2){
current_y = y;
break;
}
};
if(this->chessBoard->getLocationColor(current_x,current_y) == 0){
this->handleChess(current_x, current_y);
this->isplayer = false;
}
if(!this->iswin && !this->isplayer && this->radio_manmachine->isChecked() &&
this->getLocation(current_x,current_y,1)){
this->handleChess(current_x, current_y);
this->isplayer = true;
}
}
void Widget::handleChess(int current_x, int current_y)
{
int color = this->isblack ? BLACK : WHITE;
if(this->isblack){
this->edit_contestants->setText("白方");
}
else{
this->edit_contestants->setText("黑方");
}
this->chessBoard->markLocationColor(current_x,current_y,color);
this->isblack = !this->isblack;
Chess chess;
chess.setChessInfo(current_x,current_y,color);
this->chessBoard->addChess(chess);
this->update();
if(this->isWin(current_x,current_y,color)){
this->iswin = true;
QMessageBox::information(this,"提示","胜利!");
return;
}
}
bool Widget::isWin(int x,int y,int chesscolor)
{
if(this->centreIntercept_EW(x,y,4,chesscolor) || this->centreIntercept_SN(x,y,4,chesscolor) ||
this->centreIntercept_WS_EN(x,y,4,chesscolor) || this->centreIntercept_ES_WN(x,y,4,chesscolor)){
return true;
}
return false;
}
bool Widget::getLocation(int& x,int& y,int chesscolor)
{
int on_chesscolor = (chesscolor == 1) ? 2 : 1;
//优先级1--------五缺一
if(this->centreIntercept(x,y,4,chesscolor)){
return true;
}
//优先级2--------对手五缺一
if(this->centreIntercept(x,y,4,on_chesscolor)){
return true;
}
//优先级3--------四缺一
if(this->centreIntercept(x,y,3,chesscolor)){
return true;
}
//优先级4--------对手四缺一
if(this->centreIntercept(x,y,3,on_chesscolor)){
return true;
}
// 优先级5--------三角阵
if(this->breakTriangle(x,y,chesscolor)){
return true;
}
// 优先级6--------对手三角阵
if(this->breakTriangle(x,y,on_chesscolor)){
return true;
}
//优先级7--------三缺一
if(this->centreIntercept(x,y,2,chesscolor)){
return true;
}
//优先级8--------九宫格
if(this->jiuGongge(x,y,on_chesscolor)){
return true;
}
//优先级9-------随机
return false;
}
bool Widget::centreIntercept(int& cx,int& cy,int max, int color)
{
for(int x = 0;x < 16;x++){
for(int y = 0;y < 16;y++){
if(this->chessBoard->getLocationColor(x,y) == 0){
if( (centreIntercept_SN(x,y,max,color) || centreIntercept_EW(x,y,max,color) ||
centreIntercept_ES_WN(x,y,max,color)||centreIntercept_WS_EN(x,y,max,color) ) &&
interceptJudgment(max)){
cx = x;
cy = y;
return true;
}
}
}
}
return false;
}
bool Widget::interceptJudgment(int max)
{
switch(max){
case 4:
return true;
break;
case 3:
if(this->chessBoard->getLocationColor(this->start_x,this->start_y) == NO_CHESS ||
this->chessBoard->getLocationColor(this->start_x,this->start_y) == NO_CHESS){
return true;
}
break;
case 2:
if(this->chessBoard->getLocationColor(this->start_x,this->start_y) == NO_CHESS &&
this->chessBoard->getLocationColor(this->start_x,this->start_y) == NO_CHESS){
return true;
}
break;
}
return false;
}
bool Widget::centreIntercept_SN(int x,int y,int max, int color)
{
int yt = 0;
int count = 0;
//南-北
yt = y;
count = 0;
while(yt - 1 >= 0 && this->chessBoard->getLocationColor(x,yt - 1) == color){
count++;
yt--;
}
this->start_x = x;
this->start_y = yt - 1 >= 0 ? yt - 1 : yt;
yt = y;
while(yt + 1 <= 15 && this->chessBoard->getLocationColor(x,yt + 1) == color){
count++;
yt++;
}
this->end_x = x;
this->end_y = yt + 1 <= 15 ? yt + 1 : yt;
if(count >= max){
return true;
}
return false;
}
bool Widget::centreIntercept_EW(int x,int y,int max, int color)
{
int xt = x;
int count = 0;
while(xt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,y) == color){
count++;
xt--;
}
this->start_x = xt - 1 >= 0 ? xt - 1 : xt;
this->start_y = y;
xt = x;
while(xt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,y) == color){
count++;
xt++;
}
this->end_x = xt + 1 <= 15 ? xt + 1 : xt;
this->end_y = y;
if(count >= max){
return true;
}
return false;
}
bool Widget::centreIntercept_ES_WN(int x,int y,int max, int color)
{
int xt = x,yt = y;
int count = 0;
while(xt - 1 >= 0 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,yt - 1) == color){
count++;
xt--;
yt--;
}
if(xt - 1 >= 0 && yt - 1 >= 0){
this->start_x = xt - 1;
this->start_y = yt - 1;
}
else{
this->start_x = xt;
this->start_y = yt;
}
xt = x,yt = y;
while(xt + 1 <= 15 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,yt + 1) == color){
count++;
xt++;
yt++;
}
if(xt + 1 <= 15 && yt + 1 <= 15){
this->end_x = xt + 1;
this->end_y = yt + 1;
}
else{
this->end_x = xt;
this->end_y = yt;
}
if(count >= max){
return true;
}
return false;
}
bool Widget::centreIntercept_WS_EN(int x,int y,int max, int color)
{
int xt = x,yt = y;
int count = 0;
while(xt + 1 <= 15 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt + 1,yt - 1) == color){
count++;
xt++;
yt--;
}
if(xt + 1 <= 15 && yt - 1 >= 0){
this->start_x = xt + 1;
this->start_y = yt - 1;
}
else{
this->start_x = xt;
this->start_y = yt;
}
xt = x,yt = y;
while(xt - 1 >= 0 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt - 1,yt + 1) == color){
count++;
xt--;
yt++;
}
if(xt - 1 >= 0 && yt + 1 <= 15){
this->end_x = xt - 1;
this->end_y = yt + 1;
}
else{
this->end_x = xt;
this->end_y = yt;
}
if(count >= max){
return true;
}
return false;
}
bool Widget::breakTriangle(int &x, int &y, int color)
{
int count = 0;
for (int i = 0;i < this->chessBoard->getRow();i++) {
for (int j = 0;j < this->chessBoard->getCol();j++) {
if (this->chessBoard->getLocationColor(i,j) == 0) {
count = 0;
//东-西
this->breakTriangle_EW(i,j,count,color);
//南-北
this->breakTriangle_SN(i,j,count,color);
//西北-西南
this->breakTriangle_WN_WS(i,j,count,color);
//东南-东北
this->breakTriangle_EN_ES(i,j,count,color);
if (count >= 2) {
x = i;
y = j;
return true;
}
}
}
}
return false;
}
void Widget::breakTriangle_SN(int x, int y, int& count, int color)
{
//南方
int xi = x, yi = y;
int k = 1;
if ((yi - 1) >= 0 && this->chessBoard->getLocationColor(xi,yi - 1) == 0) {
k++;
}
yi++;
while (yi >= 0 && yi < this->chessBoard->getCol() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
yi++;
}
if (yi < this->chessBoard->getCol() && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
//北方
xi = x, yi = y;
k = 1;
if ((yi + 1) < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi + 1) == 0) {
k++;
}
yi--;
while (yi >= 0 && yi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
yi--;
}
if (yi >= 0 && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
}
void Widget::breakTriangle_EW(int x, int y, int& count, int color)
{
//东方
int xi = x, yi = y;
int k = 1;
if ((xi - 1) >= 0 && this->chessBoard->getLocationColor(xi - 1,yi) == 0) {
k++;
}
xi++;
while (xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi++;
}
if (xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
//西方
xi = x, yi = y;
k = 1;
if ((xi + 1) < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi + 1,yi) == 0) {
k++;
}
xi--;
while (xi >= 0 && xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi--;
}
if (xi > 0 && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
}
void Widget::breakTriangle_WN_WS(int x, int y, int& count, int color)
{
//西北
int xi = x, yi = y;
int k = 1;
if ((xi + 1) < this->chessBoard->getRow() && (yi + 1) < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi + 1,yi + 1) == 0) {
k++;
}
xi--;
yi--;
while (yi >= 0 && yi < this->chessBoard->getRow() && xi >= 0 && xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi--;
yi--;
}
if (xi >= 0 && yi >= 0 && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
//西南
xi = x, yi = y;
k = 1;
if ((xi + 1) < this->chessBoard->getRow() && (yi - 1) >= 0 && this->chessBoard->getLocationColor(xi + 1,yi - 1) == 0) {
k++;
}
xi--;
yi++;
while (yi >= 0 && yi < this->chessBoard->getRow() && xi >= 0 && xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi--;
yi++;
}
if (xi >= 0 && yi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
}
void Widget::breakTriangle_EN_ES(int x, int y, int& count, int color)
{
//东南
int xi = x, yi = y;
int k = 1;
if ((xi - 1) >= 0 && (yi - 1) >= 0 && this->chessBoard->getLocationColor(xi - 1,yi - 1) == 0) {
k++;
}
xi++;
yi++;
while (yi >= 0 && yi < this->chessBoard->getRow() && xi >= 0 && xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi++;
yi++;
}
if (xi < this->chessBoard->getRow() && yi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
//东北
xi = x, yi = y;
k = 1;
if ((xi - 1) >= 0 && (yi + 1) < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi - 1,yi + 1) == 0) {
k++;
}
xi++;
yi--;
while (yi >= 0 && yi < this->chessBoard->getRow() && xi >= 0 && xi < this->chessBoard->getRow() && this->chessBoard->getLocationColor(xi,yi) == color) {
k++;
xi++;
yi--;
}
if (xi < this->chessBoard->getRow() && yi >= 0 && this->chessBoard->getLocationColor(xi,yi) == 0) {
k++;
}
if (k >= 5) {
count++;
}
}
bool Widget::jiuGongge(int& x, int& y,int on_color)
{
int xt = this->chessBoard->getLocation_x();
int yt = this->chessBoard->getLocation_y();
//南-北
if(yt - 1 >= 0 && this->chessBoard->getLocationColor(xt,yt - 1) == NO_CHESS &&
yt + 1 <= 15 && this->chessBoard->getLocationColor(xt,yt + 1) != on_color){
x = xt;
y = yt - 1;
return true;
}
if(yt + 1 <= 15 && this->chessBoard->getLocationColor(xt,yt + 1) == NO_CHESS &&
yt - 1 >= 0 && this->chessBoard->getLocationColor(xt,yt - 1) != on_color){
x = xt;
y = yt + 1;
return true;
}
//东-西
if(xt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,yt) == NO_CHESS &&
xt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,yt) != on_color){
x = xt - 1;
y = yt;
return true;
}
if(xt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,yt) == NO_CHESS &&
xt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,yt) != on_color){
x = xt + 1;
y = yt;
return true;
}
//西南-东北
if(xt - 1 >= 0 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt - 1,yt + 1) == NO_CHESS &&
xt + 1 <= 15 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt + 1,yt - 1) != on_color){
x = xt - 1;
y = yt + 1;
return true;
}
if(xt + 1 <= 15 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt + 1,yt - 1) == NO_CHESS &&
xt - 1 >= 0 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt - 1,yt + 1) != on_color){
x = xt + 1;
y = yt - 1;
return true;
}
//东南-西北
if(xt + 1 <= 15 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,yt + 1) == NO_CHESS &&
xt - 1 >= 0 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,yt - 1) != on_color){
x = xt + 1;
y = yt + 1;
return true;
}
if(xt - 1 >= 0 && yt - 1 >= 0 && this->chessBoard->getLocationColor(xt - 1,yt - 1) == NO_CHESS &&
xt + 1 <= 15 && yt + 1 <= 15 && this->chessBoard->getLocationColor(xt + 1,yt + 1) != on_color){
x = xt - 1;
y = yt - 1;
return true;
}
return false;
}
Widget::~Widget()
{
}
void Widget::on_button_edit()
{
QMessageBox::information(this,"提示","对您的信息进行编辑!");
}
void Widget::on_button_record()
{
QMessageBox::information(this,"提示","你暂时没有记录!");
}
void Widget::on_button_regretchess()
{
if(this->chessBoard->getChessCount() == 0){
QMessageBox::information(this,"提示","无棋可悔!");
return ;
}
this->chessBoard->subChess();
this->iswin = false;
this->isblack = !this->isblack;
this->update();
}
void Widget::on_button_begin()
{
this->initChessboard();
if(this->radio_manmachine->isChecked()){
//先手
if(this->radio_offensiveposition->isChecked()){
if(this->radio_black->isChecked()){//选择黑棋
this->edit_contestants->setText("黑方");
}
else{
this->isblack = false;
this->edit_contestants->setText("白方");
}
}
else{
if(this->radio_black->isChecked()){
this->isblack = false;
}
else{
this->isblack = true;
}
this->handleChess(this->chessBoard->getLocation_x(),this->chessBoard->getLocation_y());
}
}
this->update();
}
void Widget::on_button_help()
{
QMessageBox::information(this,"提示","若已经有输赢或者想重新开始,"
"点开始按钮即可刷新界面重新开始游戏,且可选择先后手,棋方。");
}
很大的缺陷是获取电脑落子的算法不好,还没有想到好的一些方法,这个是自己想到的一些方面,在网上看到一篇评分法的,因为一个函数代码量太多的问题,没有好好看看,等等有时间了,再去看看,再来优化。