当前位置:文档之家› 连连看游戏课程设计+源代码

连连看游戏课程设计+源代码

课程设计课程名称:程序设计课程设计课题名称:网络连连看游戏班级:xxx学号:xxx姓名:xxx指导教师:计算机学院一、课程设计目的本课程设计的目的最主要是掌握linux系统下C++编程思想,以及关于QT软件编程,设计出连连看图形界面,实现连连看各项功能,提高编程和解决问题的能力。

二、课程设计内容设计一个连连看游戏项目,实现如下功能:1、设计连连看游戏界面,能让用户在该界面上进行相关操作。

2、为游戏添加功能:聊天对话,逻辑层相消,游戏级别,时间限制等。

三、课程设计要求1、课程设计的程序必须用C++语言完成。

2、课程设计必须在linux系统下进行。

3、要求写出需求分析报告。

分析部分包括功能需求和界面需求。

4、本项目要求分团队完成,连连看游戏五人一组共同合作,培养学生团队合作的能力。

四、系统的需求分析和模块设计1、需求分析本项目需完成两大模块设计:一为游戏界面设计,二为游戏功能设计。

2、模块分解系统功能层次模块图:五、系统的程序设计与实现程序的运行环境:Linux终端开发环境:QT、C++程序的详细设计:连连看消去算法实现在检验两个方块能否消掉的时候,我们要让两个方块同时满足两个条件才行,就是两者配对并且连线成功。

分3种情况:(从下面的这三种情况,我们可以知道,需要三个检测,这三个检测分别检测一条直路经。

这样就会有三条路经。

若这三条路经上都是空按钮,那么就刚好是三种直线(两个转弯点)把两个按钮连接起来了)* 1.相邻* 2. 若不相邻的先在第一个按钮的同行找一个空按钮。

1).找到后看第二个按钮横向到这个空按钮所在的列是否有按钮。

2).没有的话再看第一个按钮到与它同行的那个空按钮之间是否有按钮。

3).没有的话,再从与第一个按钮同行的那个空按钮竖向到与第二个按钮的同行看是否有按钮。

没有的话路经就通了,可以消了.* 3.若2失败后,再在第一个按钮的同列找一个空按钮。

1).找到后看第二个按钮竖向到这个空按钮所在的行是否有按钮2).没有的话,再看第一个按钮到与它同列的那个空按钮之间是否有按钮。

3).没有的话,再从与第一个按钮同列的那个空按钮横向到与第二个按钮同列看是否有按钮。

没有的话路经就通了,可以消了。

* 若以上三步都失败,说明这两个按钮不可以消去。

六、系统的运行结果与分析程序源代码:Mainwindow.cpp#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <QScrollBar>#include "mainwindow.h"#include "ui_mainwindow.h"#include"udp.h"#include<QImage>#include <QTime>#include "llk.h"#include<iostream>using namespace std;MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);connect(this, SIGNAL(recvMessage(QString,QString)), this, SLOT(onRecvMessage(QString,QString)));//this->setCentralWidget(link);//link->setMaximumSize(300,300);ui->editChatRec->setReadOnly(true);// link = new llk();size=6;isLinkE = false;sendMessage("192.168.3.255",8838,"o192.168.3.117",true); }MainWindow::~MainWindow(){delete ui;}void MainWindow::changeEvent(QEvent *e){QMainWindow::changeEvent(e);switch (e->type()) {case QEvent::LanguageChange:ui->retranslateUi(this);break;default:break;}}void MainWindow::on_btnSend_clicked(){QString string = ui->lineEdit->text();if(ui->radiosingle->isChecked()==true){sendMessage(string,8838,'c' + ui->editChat->text(),true);}else{string = "192.168.3.255";sendMessage("192.168.3.255",8838,'c' + ui->editChat->text(),true);}QString str =trUtf8("自己对") + string +trUtf8("发送:") + "\n" + ui->editChat->text();ui->editChatRec->setPlainText(ui->editChatRec->toPlainText() +" \n" +str); ui->editChat->clear();QScrollBar *bar = ui->editChatRec->verticalScrollBar();bar->setSliderPosition(bar->maximum());}Ui::MainWindow* MainWindow::getUi(){return ui;}void MainWindow::onRecvMessage(QString msg, QString ipAdd){//QString time = QTime::currentTime().toString();QString string =time + "\n" +QString(trUtf8("收到来自")) + ipAdd + trUtf8(":的消息:") + "\n" + msg.right(msg.length()-1);switch(msg.at(0).toAscii()){case 'o': //如果接受的是上线消息ui->listWidget->addItem(msg.right(msg.length()-1));break;default: //如果接受的是普通消息ui->editChatRec->setPlainText(ui->editChatRec->toPlainText() +"\n" + string);break;}QScrollBar *bar = ui->editChatRec->verticalScrollBar();bar->setSliderPosition(bar->maximum());}void MainWindow::on_action_begin_triggered(){/* link=new llk();link->setsize(6);link->arrayInit(6);link->Create(6);link->show();isLinkE = true;*/// size=4;if(isLinkE){//link=new llk();//link->setsize(8);// link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(size); link->arrayInit(size); link->Create(size);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(size); link->arrayInit(size); link->Create(size);link->show();isLinkE = true;}}void MainWindow::on_action_close_triggered() {this->close();link->close();}void MainWindow::on_action_pause_triggered() {if(isLinkE)link->setEnabled(false);}void MainWindow::on_action_return_triggered() {link->setEnabled(true);}void MainWindow::on_action_easy_triggered() {size=6;if(isLinkE){//link=new llk();//link->setsize(8); // link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(6);link->arrayInit(6); link->Create(6);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(6);link->arrayInit(6); link->Create(6);link->show();isLinkE = true;}}void MainWindow::on_action_soso_triggered() {size=8;/*llk *lin;lin = new llk();// this->setCentralWidget(lin);//lin->setGeometry(30,30,500,500);lin->setsize(8);lin->arrayInit(8);lin->Create(8);lin->setVisible(true);lin->close();*/if(isLinkE){//link=new llk();//link->setsize(8);// link->arrayInit(8);//link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(8);link->arrayInit(8);link->Create(8);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(8);link->arrayInit(8);link->Create(8);link->show();isLinkE = true;}}void MainWindow::on_action_diffcult_triggered() {size=10;/*llk *lin;lin = new llk();// this->setCentralWidget(lin);//lin->setGeometry(30,30,500,500);lin->setsize(8);lin->arrayInit(8);lin->Create(8);lin->setVisible(true);lin->close();*/if(isLinkE){//link=new llk();//link->setsize(8); // link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(10);link->arrayInit(10); link->Create(10);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(10);link->arrayInit(10);link->Cremybutton.cpp#include "mybutton.h"MyButton::MyButton(int value, QWidget *parent){this->value = value;this->setParent(parent);QObject::connect(this,SIGNAL(clicked()),this,SLOT(mySlot())); }void MyButton::mySlot(){emit myClicked(this->value);}Main.cpp#include <QtGui/QApplication>#include "mainwindow.h"#include <QtGui/QApplication>#include <errno.h>#include <sys/socket.h>#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <iostream>#include <string>using namespace std;const unsigned int MAXBUF = 1024;void* thd_handler(void* window){MainWindow* w=(MainWindow*)window;int sockfd;struct sockaddr_in my_addr;socklen_t addrlen;char buffer[MAXBUF];unsigned int myport = 8838;printf("server started!\n");//create a socket for udpif((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) //attention {perror("socket creation failure");exit(errno);}//initialize sockaddr_inbzero(&my_addr,sizeof(my_addr));my_addr.sin_family = AF_INET;my_addr.sin_port = htons(myport);my_addr.sin_addr.s_addr = INADDR_ANY;//bindif(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){perror("bind failure");exit(errno);}printf("bind success\n");addrlen = sizeof(struct sockaddr);while(true){bzero(buffer,MAXBUF);int len = recvfrom(sockfd, buffer, MAXBUF, 0,(sockaddr*)&my_addr, &addrlen); if (len < 0) {perror("recvfrom");exit(errno);}emitw->recvMessage(QObject::trUtf8(buffer),inet_ntoa(my_addr.sin_addr)); }//close connectionclose(sockfd);}int main(int argc, char *argv[]){pthread_t ntid;QApplication a(argc, argv);MainWindow w;pthread_create(&ntid,NULL,thd_handler,&w);w.show();return a.exec();}udp.cpp#include"udp.h"#include<QString>#include <errno.h>#include <sys/socket.h>#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>void sendMessage(QString ipAddress,unsigned int port,QString msg,bool isBroad) {// const unsigned int MAXBUF = 1024;int sockfd;int addr_len;int len;struct sockaddr_in dest;//char buffer[MAXBUF];char addr[16] ;strcpy(addr,ipAddress.toAscii());//create a socket for udpif((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) //attention{perror("socket creation failure");exit(errno);}if(isBroad){int broad=1;setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad));strcpy(addr,"192.168.3.255");}//initialize sockaddr_inbzero(&dest,sizeof(dest));dest.sin_family = AF_INET;dest.sin_port = htons( port);if(inet_aton(addr,(struct in_addr*)&dest.sin_addr)==0){perror("ip address format is error");exit(errno);}//send msg to serveraddr_len = sizeof(dest);// bzero(buffer,MAXBUF);len = sendto(sockfd, msg.toLocal8Bit(), msg.toLocal8Bit().length(), 0,(sockaddr*)&dest, addr_len);if (len < 0){printf("\n\rsend error.\n\r");exit(errno);}//close connection::close(sockfd);}llk.cpp#include "llk.h"#include<iostream>#include"judge.h"llk::llk(){isFirstClick = true;llk::createBtnRerank();size=6;llk::createCurrentProgressBar(size);timer = new QTimer();currentTime=0;QObject::connect(timer,SIGNAL(timeout()),this,SLOT(timeProcess())); }void llk::xxx(int x){clear(x);// std::cout<<x<<std::endl;// mybutton[x]->setVisible(false);// Widget::arrayInit(6);// progressBar[1]->setValue(20);}void llk::addPic(QString path,int i){labelPic[i] = new QLabel(this);labelPic[i]->setGeometry(i*100+5,30,100,120);QPixmap pixmap(path);labelPic[i]->setPixmap(path);labelPic[i]->setVisible(false);progressBar[i] = new QProgressBar();progressBar[i]->setGeometry(i*100+10,120,100,20);progressBar[i]->setParent(this);progressBar[i]->setValue(10);}void llk::Create(int size){createTimeProcessBar(size*size);timer->start(1000);charss[26][20]={"begin","./icon/icon1.png","./icon/icon2.png","./icon/icon3.png","./icon/icon4.png ","./icon/icon5.png","./icon/icon6.png","./icon/icon7.png","./icon/icon8.png","./icon/icon9.png","./icon/icon10.png","./icon/icon11.gif","./icon/icon12.gif","./icon/icon13.gif","./icon/icon14.gif","./icon/icon15.gif","./icon/icon16.gif","./icon/icon17.gif","./icon/icon18.gif","./icon/icon19.gif","./icon/icon20.gif","./icon/icon21.gif","./icon/icon22.gif","./icon/ic on23.gif","./icon/icon24.gif","./icon/icon25.gif"};createCurrentProgressBar(size);currentIndex=size*size;progressBarCurrent->setValue(0);for(unsigned int i=0;i<size*size;i++){mybutton[i] = new MyButton(i,this);mybutton[i]->setGeometry(20+i%size*60,150+i/size*60,60,60);int x,y;x=i/size+1;if((i+1)%size){y=(i+1)%size;}elsey=size;int m=array[x][y];// std::cout<<x<<","<<y<<std::endl;mybutton[i]->setIcon(QIcon(ss[m]));mybutton[i]->setIconSize(QSize(50,50));QObject::connect(mybutton[i],SIGNAL(myClicked(int)),this,SLOT(xxx(int )));//mybutton[i]->setIcon(QIcon(s));}}void llk::arrayInit(int size){array[size+1][0]=0;array[size+1][size+1]=0;array[0][size+1]=0;array[0][0]=0;for(int i=1;i<=size;i++){array[0][i]=0;array[size+1][i]=0;array[i][0]=0;array[i][size+1]=0;for(int j=1;j<=size;j++){array[i][j]=((i-1)*size+j-1)/4+1;}}for(int i=1;i<=size;i++){for(int j=1;j<=size;j++){int x1,x2,y1,y2,tmp1;// srand(unsigned(time(NULL)));x1=rand()%size+1;x2=rand()%size+1;y1=rand()%size+1;y2=rand()%size+1;tmp1=array[x1][y1];array[x1][y1]=array[x2][y2];array[x2][y2]=tmp1;}}for(int i=0;i<=size+1;i++){for(int j=0;j<=size+1;j++){std::cout<<array[i][j]<<" ";}std::cout<<std::endl;}}void llk::clear(int index){if(isFirstClick){firstIndex=index;isFirstClick=false;std::cout<<firstIndex<<",";}else{std::cout<<index<<std::endl;int x1,y1,x2,y2;x1=firstIndex/size+1;x2=index/size+1;if((firstIndex+1)%size){y1=(firstIndex+1)%size;}elsey1=size;if((index+1)%size){y2=(index+1)%size;}elsey2=size;std::cout<<x1<<","<<y1<<std::endl;std::cout<<x2<<","<<y2<<std::endl;std::cout<<"**************"<<std::endl;if(judge(x1,y1,x2,y2,array,size)){mybutton[firstIndex]->setVisible(false);// free(mybutton[firstIndex]);//mybutton[firstIndex]=NULL;mybutton[index]->setVisible(false);array[x1][y1]=0;array[x2][y2]=0;currentIndex-=2;progressBarCurrent->setValue(size*size-currentIndex);}isFirstClick=true;}}void llk::setsize(int size1){if(size1==8||size1==6||size1==10||size1==4){size=size1;}}void llk::mybuttonDelt(){for(int i=0;i<size*size;i++){free(mybutton[i]);}}void llk::createBtnRerank(){btnReRank = new QPushButton();btnReRank->setParent(this);btnReRank->setText("reRank");QObject::connect(btnReRank,SIGNAL(clicked()),this,SLOT(Rerank( )));}void llk::Rerank(){charss[26][20]={"begin","./icon/icon1.png","./icon/icon2.png","./icon/icon3.png","./icon/icon4.png ","./icon/icon5.png","./icon/icon6.png","./icon/icon7.png","./icon/icon8.png","./icon/icon9.png","./icon/icon10.png","./icon/icon11.gif","./icon/icon12.gif","./icon/icon13.gif","./icon/icon14.gif","./icon/icon15.gif","./icon/icon16.gif","./icon/icon17.gif","./icon/icon18.gif","./icon/icon19.gif","./icon/icon20.gif","./icon/icon21.gif","./icon/icon22.gif","./icon/ic on23.gif","./icon/icon24.gif","./icon/icon25.gif"};int tmpx,tmpy,tmp;std::cout<<"rerank"<<std::endl;for(int i=1;i<=size;i++){for(int j=1;j<=size;j++){if(array[i][j]!=0){tmpx=rand()%size+1;tmpy=rand()%size+1;tmp =array[i][j];array[i][j]=array[tmpx][tmpy];array[tmpx][tmpy]=tmp;mybutton[(i-1)*size+j-1]->setIcon(QIcon(ss[array[i][j]]));mybutton[(tmpx-1)*size+tmpy-1]->setIcon(QIcon(ss[array[tmpx][tmpy]]));if(array[i][j]){mybutton[(i-1)*size+j-1]->setVisible(true);}else{mybutton[(i-1)*size+j-1]->setVisible(false);}mybutton[(tmpx-1)*size+tmpy-1]->setVisible(true);}}}}void llk::createCurrentProgressBar(int size){progressBarCurrent = new QProgressBar();progressBarCurrent->setGeometry(20,40,60*size,30);progressBarCurrent->setMaximum(size*size);progressBarCurrent->setValue(255);progressBarCurrent->setParent(this);}void llk::createTimeProcessBar(int sumtime){progressBarTime = new QProgressBar();progressBarTime->setGeometry(20,80,60*size,30);progressBarTime->setParent(this);progressBarTime->setMaximum(sumtime);}void llk::timeProcess(){if(currentTime>=size*size)this->close();currentTime++;progressBarTime->setValue(currentTime);}Judge.cpp#include"judge.h"#include<iostream>int judge(int x1,int y1,int x2,int y2,int array[16][16],int size) {if(x1==x2&&y1==y2){return 0;}if((array[x1][y1]==array[x2][y2])){if(judge1(x1,y1,x2,y2,array,size)||judge2(x1,y1,x2,y2,array,size)||judge3(x1,y1,x2,y2,array,size )){array[x1][y1]=0;array[x2][y2]=0;return 1;}}return 0;}int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size){//if(array[x1][y1]==array[x2][y2])//{int tmp;if(x1!=x2&&y1!=y2)return 0;if(y1>y2){tmp=y1;y1=y2;y2=tmp;}if(x1==x2){if(y1>y2){tmp=y1;y1=y2;y2=tmp;}for(int i=y1+1;i<y2;i++){if(array[x1][i]){return 0;}}}if(x1>x2){tmp=x1;x1=x2;x2=tmp;}if(y1==y2){if(x1>x2){tmp=x1;x1=x2;x2=tmp;}for(int j=x1+1;j<x2;j++){if(array[j][y1]){return 0;}}}//return 1;//}return 1;}int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size){int flag=0;if(judge1(x1,y2,x1,y1,array,size)&&judge1(x1,y2,x2,y2,array,size)&&array[x1][y2]==0) {//flag++;//std::cout<<x1<<","<<i<<std::endl;return 1;}if(judge1(x2,y1,x1,y1,array,size)&&judge1(x2,y1,x2,y2,array,size)&&array[x2][y1]==0) {//flag++;//std::cout<<j<<","<<y1<<std::endl;return 1;}return 0;}int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size){for(int i=0;i<=size+1;i++){if(judge2(x1,i,x2,y2,array,size)&&judge1(x1,i,x1,y1,array,size)&&array[x1][i]==0) { //std::cout<<x1<<","<<i<<std::endl;return 1;}}for(int j=0;j<=size+1;j++){if(judge2(j,y1,x2,y2,array,size)&&judge1(j,y1,x1,y1,array,size)&&array[j][y1]==0) {// std::cout<<j<<","<<y1<<std::endl;return 1;}}return 0;}头文件源代码:Judge.h#ifndef JUDGE_H#define JUDGE_Hint judge(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size);#endif // JUDGE_HLlk.h#ifndef LLK_H#define LLK_H#include<mybutton.h>#include <QWidget>#include<QPicture>#include <QWidget>#include<QObject>#include<QProgressBar>#include<QLabel>#include<QTimer>class llk : public QWidget{Q_OBJECTpublic:llk();void setsize(int size1);void addPic(QString path,int i);void Create(int size);void clear(int index);void mybuttonDelt();void createBtnRerank();void newGame();void createCurrentProgressBar(int size);void createTimeProcessBar(int sumtime);int currentTime;private:MyButton * mybutton[150];QLabel * labelPic[4];QProgressBar * progressBar[4];int array[16][16];bool isFirstClick;int firstIndex;QPushButton * btnReRank;QProgressBar *progressBarCurrent;QProgressBar *progressBarTime;int currentIndex;QTimer *timer;int size;public slots:void xxx(int x);void arrayInit(int size);void Rerank();void timeProcess();};#endif // LLK_HMainwindow.h#ifndef JUDGE_H#define JUDGE_Hint judge(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size);#endif // JUDGE_HMybutton.h#ifndef MYBUTTON_H#define MYBUTTON_H#include <QPushButton>class MyButton : public QPushButton{Q_OBJECTpublic:MyButton(int value,QWidget * parent = 0);private:int value;signals:void myClicked(int value);public slots:void mySlot();};#endif // MYBUTTON_HUdp.h#ifndef UDP_H#define UDP_H#include<QString>void sendMessage(QString ipAddress,unsigned int port,QString msg,bool isBroad); #endif // UDP_H游戏主界面:1、聊天界面:2、游戏界面:3、玩家游戏过程截图:。

相关主题