⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientview.cpp

📁 linux下的网络五指棋。用QT做的界面。
💻 CPP
字号:
#include "clientview.h"

ClientView::ClientView(const QString &host, Q_UINT16 port )
{
	setGeometry(150,100,550,420);
	
	setMaximumSize(550,420);
	setMinimumSize(550,420);
	
	game = new QPopupMenu(this);
	game->insertItem("&new",this,SLOT(newRenjuClick()));
	game->insertItem("&huiqi",this,SLOT(huiqiClick()));
	game->setEnabled(true);
	//setting = new QPopupMenu(this);
	//setting->insertItem("&server",this,SLOT(startServer()));
	//setting->insertItem("&client",this,SLOT(startClient()));

	bar = new QMenuBar(this);
	bar->insertItem("game",game);
	//bar->insertItem("setting",setting);
	
	doc = new RenjuDoc();
	sv = new QScrollView(this);
	sv->setGeometry(370,50,170,300);
	tv = new QTextView(sv);
	tv->setGeometry(0,0,170,300);
	
	le = new QLineEdit(this);
	le->setGeometry(370,360,130,30);
	pbinput = new QPushButton("&send",this,SLOT(sendMsg()));
	pbinput->setGeometry(500,360,40,30);
	
	paint = new QPainter();
	board = new QPixmap("board.bmp");
	mask = new QBitmap("mask.bmp");
	pixblack = new QPixmap("black.bmp");
	pixwhite = new QPixmap("white.bmp");
	pixblack->setMask(*mask);
	pixwhite->setMask(*mask);

	   mousepress = 1;
	   indexforregret = 1;
	   
	   
	socket = new QSocket( this );
    socket->connectToHost( host, port );
	connect(socket,SIGNAL(error(int)),this,SLOT(serverdone()));
	connect(socket,SIGNAL(connected()),this,SLOT(connected()));
	connect(socket,SIGNAL(connectionClosed()),this,SLOT(connectionClosed())); 
	connect(socket,SIGNAL(readyRead()),this,SLOT(recvMsg()));
	connect(pbinput, SIGNAL(clicked()),SLOT(sendMsg()));
}

void ClientView::serverdone()
{
  message->about(this,"game information","there is a wrong with server!");
  mousepress = 0;
	indexforregret = 0;
	exit(1);
}

void ClientView::connected()
{
   tv->append("connect to server successfull");
}

void ClientView::connectionClosed()
{
   message->about(this,"game information","connection closed!");
  mousepress = 0;
	indexforregret = 0;
	exit(1);
}

void ClientView::drawPoint(int x, int y, int color)
{
	if (color%2 == 1)
		paint->drawPixmap(QPoint(23*x+10,23*y+50),*pixblack);
	else
		paint->drawPixmap(QPoint(23*x+10,23*y+50),*pixwhite);
}

void ClientView::paintEvent(QPaintEvent*)
{
	//int stone[][];
	//stone = doc->stone;
	paint->begin(this);
	paint->drawPixmap(QPoint(10,50),*board);
	
	for(int i=0; i<15; i++)
	for(int j=0; j<15; j++)
	{
		if (doc->stone[i][j]!=0)
			drawPoint(i,j,doc->stone[i][j]);	
	}
	paint->end();	
	
}

void ClientView::mousePressEvent ( QMouseEvent * e )
{
       if(  1 == mousepress)
       	{
		x = (e->x()-10)/23;
	y = (e->y()-50)/23;
	qDebug("%d,%d",x,y);
	if (x>14|y>14|x<0|y<0|(doc->stone[x][y] !=0))
		return;
	doc->stone[x][y]=2;
	mousepress = 0;
	QTextStream os(socket);
        os <<"position"<<"/"<<x<<"/"<<y<<"\n";
	update();
	int i  = 0;
	int j  = 0;
	
	for(i=0;i<11;i++)
		{
		    for(j=i;j<(j+5);j++)
			  {
		         if(doc->stone[j][y] !=2)
		                break;
			  }
		    if((j-i) == 5)
                 {
               
               os <<"done"<<"/"<<"\n";
              message->about(this,"game information","you win!");
				    break;
	                
	                    mousepress = 0;
                        indexforregret = 0;
	                	

                 }
		}

	for(i=0;i<11;i++)
		{
		    for(j=i;j<(j+5);j++)
			  {
		         if(doc->stone[x][j] !=2)
		                break;
			  }
		    if((j-i) == 5)
              {
                os <<"done"<<"/"<<"\n";
              message->about(this,"game information","you win!");
				    break;
	                
	                    mousepress = 0;
                        indexforregret = 0;
	                	

              }
		}

	int k = x+y;
	int a = 0;
	for(i=0,j=k;(i<=k)&&(j>=0);i++,j--)
		{
		  
		    if(doc->stone[i][j] ==2)
		    	{
		    	   for( a=0;a<4;a++)
		    	   	{
		    	   	  if(doc->stone[++i][--j]!= 2)
					  	break;
		    	   	}
				   if (a==4)
				   	{
				   	  os <<"done"<<"/"<<x<<"/"<<y<<"\n";
                                     message->about(this,"game information","you win!");
				         break;
	                
	                    mousepress = 0;
                        indexforregret = 0;
				   	}
		    	}
		    
		}

	 k =x-y;
	 a = 0;
     if(k<=0)
     	{
	     for(i=0,j=abs(k);(i<=(14-k))&&(j<=14);i++,j++)
		  {
		  
		    if(doc->stone[i][j] ==2)
		    	{
		    	   for( a=0;a<4;a++)
		    	   	{
		    	   	  if(doc->stone[++i][++j]!= 2)
					  	break;
		    	   	}
				   if (a==4)
				   	{
				   	  os <<"done"<<"/"<<x<<"/"<<y<<"\n";
                      message->about(this,"game information","you win!");
				        break;
	                
	                    mousepress = 0;
                        indexforregret = 0;
				   	}
		    	}
		    
		  }
     	}
	 if(k>0)
	 	{
	 	  for(i=14,j=14-k;((i>k)&&j>=0);i--,j--)
	 	  	{
		        if(doc->stone[i][j] ==2)
		    	{
		    	   for( a=0;a<4;a++)
		    	   	{
		    	   	  if(doc->stone[--i][--j]!= 2)
					  	break;
		    	   	}
				   if (a==4)
				   	{

	                    os <<"done"<<"/"<<x<<"/"<<y<<"\n";
              	       
                        message->about(this,"game information","you win!");
				        break;
	                
	                   mousepress = 0;
                        indexforregret = 0;
				   	  	
				   	}
		    	}
	 	  	}
	 	}
		  
			
			
	//if (client != NULL)
        //client->serverzhouqi(x,y);
       	}
       	
        
}

/*void ClientView::zhouqi(int x, int y)//,int color)
{
     doc->stone[x][y] = 1;
	 update();
}*/

/*void ClientView::newconnect()
{
      tv->append("New connection,you can play! \n" );
}*/

//发送请求开新局
void ClientView::newRenjuClick()
{
    QTextStream os(socket);
   os<<"clientrestart"<<"/"<<"\n";
}
//处理对方发送开新局的请求
//void ClientView::reqNewRenju(int mycolor){}
//处理对方回复的开新局请求
//void ClientView::reNewRenju(bool b, int mycolor){}
//发送悔棋请求
void ClientView::huiqiClick()
{
   if (1 == indexforregret)
   	{
   		QTextStream os(socket);
   		os<<"clienthuiqi"<<"/"<<x<<"/"<<y<<"\n";
   	}
   if (0 == indexforregret)
   	{
   	    message->about(this,"game information","you cannot regret!");
   	}
  
}
//处理对方发送的悔棋请求
void ClientView::reqHuiqi(int x,int y)
{
   QTextStream os(socket);
   QMessageBox mb( "huiqi request",
        "Do you agree?\n",
        QMessageBox::NoIcon,
        QMessageBox::Ok,
        QMessageBox::No,
        QMessageBox::NoButton,
        this
        );
   switch(mb.exec())
   	{
   	  case QMessageBox::Ok:
   
        doc->stone[x][y] = 0;
		//qDebug("%d,%d",x,y);
		//doc->stone[x+1][y+1] = 0;
		update();
		
        os <<"agreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
		break;
   	
   case QMessageBox::No:
   	
   	    
        os <<"donotagreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
        break;
   	}

}
//处理对方发送的悔棋请求的回复
//void ClientView::reHuiqi(bool b){}
//处理对方的走棋,直接调用doc对象添加数据,然后update
void ClientView::zhouqi(int x, int y)
{
     if(doc->stone[x][y] ==0)
    	{
   doc->stone[x][y] = 1;
   //qDebug("%d,%d",x,y);
   update();
   mousepress = 1;
   indexforregret = 0;
    	}
}
//发送信息给对方
void ClientView::sendMsg()
{
	QString msg;
	msg = le->text();
	tv->append("client:");
	tv->append(msg);
	QTextStream os(socket);
        os <<"msg"<<"/"<<msg<<"\n";
	le->setText("");
}
//处理对方发送过来的信息 
void ClientView::recvMsg()
{
    while ( socket->canReadLine() )
		{
            QString str = socket->readLine();
			QString s = str.section('/',0,0);
			//qDebug(s);
			if (s == "msg")
			{
			s = str.section('/',1,1);
			tv->append("server:");
			tv->append(s);
			}
			if (s == "done")
			{
            	QString i=str.section('/',1,1);
  				QString j=str.section('/',2,2);
				bool ok;
				int a = i.toInt(&ok,10);//how?huan suan?
				int b = j.toInt(&ok,10);//how?huan suan?
				qDebug("%d,%d",a,b);
				zhouqi(a,b);
				message->about(this,"game information","you lose!");
			    mousepress = 0;
                indexforregret = 0;
				    
			}
		    if (s =="position")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			//qDebug("%d,%d",a,b);
			zhouqi(a,b);
			}
			if (s =="huiqi")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			//qDebug("%d,%d",a,b);
			reqHuiqi(a,b);
			}
			if (s =="serveragreetohuiqi")
			{
			QString i=str.section('/',1,1);
  			QString j=str.section('/',2,2);
			bool ok;
			int a = i.toInt(&ok,10);//how?huan suan?
			int b = j.toInt(&ok,10);//how?huan suan?
			//qDebug("%d,%d",a,b);
			doc->stone[a][b] = 0;
			mousepress = 1;
			update();
			message->about(this,"game information","the other side agree to regret!");
			}
			if (s == "serverdonotagreetohuiqi")
			{
			message->about(this,"game information","the other side do not agree to regret!");
			}
			if (s == "new")
			{
				QTextStream os(socket);
            	QMessageBox mb( "new play request",
            	"Do you agree?\n",
	            QMessageBox::NoIcon,
   	    	    QMessageBox::Ok,
    	        QMessageBox::No,
	        	QMessageBox::NoButton,
   		     	this
        		);
   				switch(mb.exec())
	   			  {
   		  			case QMessageBox::Ok:
						for(int i=0; i<15; i++)
						  for(int j=0; j<15; j++)
							{
								doc->stone[i][j]=0;
							}
   		
        			//doc->stone[x][y] = 0;
					//qDebug("%d,%d",x,y);
					//doc->stone[x+1][y+1] = 0;
					update();
		
    	    		os <<"agreetorestart"<<"/"<<x<<"/"<<y<<"\n";
					break;
   	
   					case QMessageBox::No:
   	
   	    
	        		os <<"donotagreetorestart"<<"/"<<x<<"/"<<y<<"\n";
    	    		break;
   				  }
			
				
			
				}
		  if (s == "newdone")
		  	{
		  	   for(int i=0; i<15; i++)
				for(int j=0; j<15; j++)
					{
						doc->stone[i][j]=0;
					}
				update();
				mousepress = 1;
	            indexforregret = 1;
		  	}
		  if (s == "newdenied")
		  	{
		  	  message->about(this,"game information","the other side do not agree to restart!");
		  	}
    	}

}

void ClientView::closeEvent(QCloseEvent *e ) 
{
    socket->close();
	//quit();
	delete this;
	//App ->quit();
	exit(1);
}



//连接建立的信号处理
//void RenjuView::conneted(){}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -