C3-Qt realize Gobang games 2021.11.08

Qt Gobang games (II) Steps: ...
Steps:
Qt Gobang games (II)

Steps:

The second big step is to create the main interface, separate the drawing interface from the initialization of interface elements, and make the logic clearer. The so-called interface refers to frame, background, title, interface size and other contents; Interface elements refer to, for example, adding item s to combox. In the main interface, the core problem is to handle mouse events, ensure that the current chess piece falls in the accurate position, and prompt the user to switch roles; There is to judge the victory of the game.

1.init initialization interface

Use QPainter to draw the interface background, add frame and interface element parts to the ui designer, and beautify the buttons through the style sheet; Add a checkerboard widget using gridLayout; Set the window title; Set the Gobang title; set and display black and white chess; the ui Designer's interface is as follows. Find the corresponding relationship when looking at the code.

2. Interface element initialization

This logic is not written well, and the actual program should not have this part. It is more appropriate to decompose initialization into two actions.

3. Click the start button

First, configure the button sound effect, empty (initialize) the chessboard, set the role, and connect the mouse signal (chessboard) As with the refresh interface function, the refresh interface function will save the coordinate information into a two-dimensional array, and use the two-dimensional array as a parameter to call the method of refreshing the chessboard provided by the chessboard class, so as to refresh the interface. At the same time, you should switch roles and add dubbing for the fall. Judge whether the player wins. Once a player wins, a dialog box pops up to congratulate the player.

4. Judge whether the player wins


Idea: the red and green lines represent the double-layer loop, the red line represents the memory loop, and the green line represents the outer loop. Therefore, when traversing, it will be traversed in the order from top to bottom and from left to right. Therefore, if each chess piece is regarded as the first chess piece of "five son Lianzhu", it only needs to detect the "five son Lianzhu" in four directions, as shown by the blue line in the figure. Once the "five son Lianzhu" is detected "Stop traversing immediately and return to the winning player.

5 window closed

When you click "exit the game" or "X", you will be prompted whether to exit. Click Yes to exit immediately, otherwise you will not exit.

effect

reflection

1. You can write your own algorithm on this basis to make a "man-machine combat mode";
2. On this basis, combined with network port programming, public network war can be realized.

code

main

#include "chessform.h" #include "chessplate.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); chessForm w; w.show(); // chessPlate e; // e.show(); return a.exec(); }

chessForm.h

#ifndef CHESSFORM_H #define CHESSFORM_H #include <QWidget> #include "chessplate.h" #include <QMessageBox> #include <QSound> namespace Ui { class chessForm; } class chessForm : public QWidget { Q_OBJECT public: explicit chessForm(QWidget *parent = nullptr); ~chessForm(); protected: void paintEvent(QPaintEvent *); void closeEvent(QCloseEvent *); private: Ui::chessForm *ui; chessPlate * myPlate; chessPlate::ChessType currentRole; int formChessData[15][15]; int black_cnt = 0,white_cnt = 0; void Init(); void showRole(const QString whiteFileName,const QString blackFileName); void setRole(chessPlate::ChessType currentRole);//Set who goes first void setChessInit(); bool isGot(int i,int j); public slots: void doProcessStartGame(); void doProcessExitGame(); void doProcessFrushPlate(int p,int q ); }; #endif // CHESSFORM_H

chessForm.cpp

#include "chessform.h" #include "ui_chessform.h" chessForm::chessForm(QWidget *parent) : QWidget(parent), ui(new Ui::chessForm) { ui->setupUi(this); Init();//Initialize main interface // This - > setwindowicon (qicon (": / RES / Chinese. png")); connect(ui->openGameBtn,&QPushButton::clicked,this,&chessForm::doProcessStartGame); connect(ui->exitBtn,&QPushButton::clicked,this,&chessForm::doProcessExitGame); } chessForm::~chessForm() { delete ui; } void chessForm::paintEvent(QPaintEvent *) { //Draw main interface background QPainter painter(this); painter.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/res/back.jpg")); } void chessForm::closeEvent(QCloseEvent *event) { if(QMessageBox::Yes==QMessageBox::information(this,"Tips","Exit",QMessageBox::Yes,QMessageBox::No)) { event->accept(); } else { event->ignore(); } } void chessForm::Init() { //Apply for chessboard space myPlate = new chessPlate(); //Put the chessboard in the container of the main interface ui->gridLayout->addWidget(myPlate); //Set Title, Icon this->setWindowTitle("Gobang"); //Set title word ui->titleLab->setPixmap(QPixmap(":/res/title.png")); //Add competing parts QStringList list; list.clear(); list.append("Bai Zixian"); list.append("Sunspot first"); ui->comboBox->addItems(list); //Display white and black chess walking signs showRole(":/res/white.png",":/res/black.png"); } void chessForm::showRole(const QString whiteFileName, const QString blackFileName) { ui->whiteLab->setPixmap(QPixmap(whiteFileName)); ui->whiteLab->setScaledContents(true); ui->blackLab->setPixmap(QPixmap(blackFileName)); ui->blackLab->setScaledContents(true); } void chessForm::setRole(chessPlate::ChessType Role) { currentRole = Role; if(Role == chessPlate::Black) { ui->blackLab->setVisible(true); ui->whiteLab->setVisible(false); } else { ui->blackLab->setVisible(false); ui->whiteLab->setVisible(true); } } void chessForm::setChessInit() { for (int i=0;i<15 ;i++ ) { for (int j = 0;j<15 ;j++ ) { formChessData[i][j]=chessPlate::Empty; } } myPlate->setChessStatus(formChessData); white_cnt=0; black_cnt=0; ui->lcdNumBlack->display(black_cnt); ui->lcdNumWhite->display(white_cnt); } bool chessForm::isGot(int i, int j) { //The scanning mode determines this judgment mode chessPlate::ChessType a; a= chessPlate::ChessType(formChessData[i][j]); if(i>10)//Right judgment { //No right judgment } else { qDebug()<<"111111"; if(formChessData[i+1][j]==a&&formChessData[i+2][j]==a&&formChessData[i+3][j]==a&&formChessData[i+4][j]==a) } if(j>10)//Downward judgment { //No downward judgment } else { if(formChessData[i][j+1]==a&&formChessData[i][j+2]==a&&formChessData[i][j+3]==a&&formChessData[i][j+4]==a) } if(j>10||i>10)//Synclinal downward judgment { //Non synclinal downward judgment } else { if(formChessData[i+1][j+1]==a&&formChessData[i+2][j+2]==a&&formChessData[i+3][j+3]==a&&formChessData[i+4][j+4]==a) } if(j<4||i>10) { //Non synclinal upward judgment } else { if(formChessData[i+1][j-1]==a&&formChessData[i+2][j-2]==a&&formChessData[i+3][j-3]==a&&formChessData[i+4][j-4]==a) } return false; } void chessForm::doProcessStartGame() { QSound *sound = new QSound(":/res/startSound.wav",this); //Play start sound sound->play(); //Set the number of cycles - 1 for wireless sound->setLoops(3); //Interface initialization if(ui->comboBox->currentText().contains("white")) { setRole(chessPlate::White); } else { setRole(chessPlate::Black); } //Chessboard initialization setChessInit(); //You can start playing chess and connect the signal slot connect(myPlate,&chessPlate::mousePosSend,this,&chessForm::doProcessFrushPlate); } void chessForm::doProcessExitGame() { this->close(); } void chessForm::doProcessFrushPlate(int p, int q) { QSound *gameSound = new QSound(":/res/gamesound.wav",this); //Judge whether it can be done at once if(formChessData[p][q]==chessPlate::Empty) { //Which one can you take down at once if(currentRole==chessPlate::Black) { formChessData[p][q]=chessPlate::Black; black_cnt++; currentRole = chessPlate::White; } else { formChessData[p][q] = chessPlate::White; white_cnt++; currentRole = chessPlate::Black; } myPlate->setChessStatus(formChessData); gameSound->play(); ui->lcdNumBlack->display(black_cnt); ui->lcdNumWhite->display(white_cnt); //Judge whether to win int i=0,j=0; for (i=0;i<15 ;i++ ) { for (j=0;j<15 ;j++ ) { if(formChessData[i][j]==0) {} else { if(isGot(i,j))//Whether this piece constitutes a successful chess shape judgment function } } if(j!=15) } if((i!=15)&&formChessData[p][q] == chessPlate::White) { QMessageBox::information(this,"Tips","Congratulations on the white chess victory!",QMessageBox::Ok); setChessInit(); showRole(":/res/white.png",":/res/black.png"); disconnect(myPlate,&chessPlate::mousePosSend,this,&chessForm::doProcessFrushPlate); } else if((i!=15)&&formChessData[p][q] == chessPlate::Black) { QMessageBox::information(this,"Tips","Congratulations on the victory of black chess!",QMessageBox::Ok); setChessInit(); showRole(":/res/white.png",":/res/black.png"); disconnect(myPlate,&chessPlate::mousePosSend,this,&chessForm::doProcessFrushPlate); } else {} } else {} }

7 November 2021, 21:14 | Views: 8506

Add new comment

For adding a comment, please log in
or create account

0 comments