📄 renjuview.cpp
字号:
#include "renjuview.h"
RenjuView::RenjuView()
{
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);
indexserver = 1;
mousepress = 1;
indexforregret = 1;
indexclient = 1;
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);
//QSocket *socket(this);
connect(pbinput, SIGNAL(clicked()),SLOT(sendMsg()));
// connect( socket, SIGNAL(readyRead()),SLOT( recvMsg(QString &msg)));
}
void RenjuView::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 RenjuView::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 RenjuView::mousePressEvent ( QMouseEvent * e )
{
if(mousepress == 1)
{
x = (e->x()-10)/23;
y = (e->y()-50)/23;
if (x>14|y>14|x<0|y<0|(doc->stone[x][y] !=0))
return;
//qDebug("%d,%d",x,y);
doc->stone[x][y]=1;
update();
mousepress =0;
int i = 0;
int j = 0;
for(i=0;i<11;i++)
{
for(j=i;j<(j+5);j++)
{
if(doc->stone[j][y] !=1)
break;
}
if((j-i) == 5)
{
if (client != NULL)
{
client->sendDone(x,y);
}
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] !=1)
break;
}
if((j-i) == 5)
{
if (client != NULL)
{
client->sendDone(x,y);
}
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] ==1)
{
for( a=0;a<4;a++)
{
if(doc->stone[++i][--j]!= 1)
break;
}
if (a==4)
{
if (client != NULL)
{
client->sendDone(x,y);
}
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] ==1)
{
for( a=0;a<4;a++)
{
if(doc->stone[++i][++j]!= 1)
break;
}
if (a==4)
{
if (client != NULL)
{
client->sendDone(x,y);
}
message->about(this,"game information","you win!");
indexforregret = 0;
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] ==1)
{
for( a=0;a<4;a++)
{
if(doc->stone[--i][--j]!= 1)
break;
}
if (a==4)
{
if (client != NULL)
{
client->sendDone(x,y);
}
message->about(this,"game information","you win!");
break;
mousepress = 0;
indexforregret = 0;
}
}
}
}
if (client != NULL)
client->serverzhouqi(x,y);
}
}
void RenjuView::startServer()
{
if(1 == indexserver)
{
server = new RenjuServer(this);
connect ( server,SIGNAL(newConnect(RenjuSocket*)),this,SLOT(newConnect(RenjuSocket*)));
//connect ( server,SIGNAL(serverok()),this,SLOT(serverok()));
}
if(0 == indexserver)
{
message->about(this,"game information","server already run!");
}
indexserver = 0;
}
/*void RenjuView::serverok()
{
message->about(0,"game information","server is running successfull!");
}*/
void RenjuView::startClient()
{
if (1 == indexclient)
{
this->hide();
clientdialog = new clientwindow;
clientdialog->show();
//QSocket *socket;
//QTextStream os(socket);
//os << "0,x,y,"<< "\n";
}
if (0 == indexclient)
{
message->about(this,"game information","client already run!");
}
}
//发送请求开新局
void RenjuView::newRenjuClick()
{
if (client != NULL)
client->reqNewRenju();
}
//处理对方发送开新局的请求
//void RenjuView::reqNewRenju(int mycolor){}
//处理对方回复的开新局请求
//void RenjuView::reNewRenju(bool b, int mycolor){}
//发送悔棋请求
void RenjuView::huiqiClick()
{
if(1 == indexforregret)
{
if (client != NULL)
client->sendhuiqi(x,y);
}
if(0 == indexforregret)
{
message->about(this,"game information","you can not regret!");
}
}
//处理对方发送的悔棋请求
void RenjuView::reqHuiqi(){}
//处理对方发送的悔棋请求的回复
//void RenjuView::reHuiqi(bool b){}
//处理对方的走棋,直接调用doc对象添加数据,然后update
void RenjuView::zhouqi(int x, int y)//,int color){}
{
if(doc->stone[x][y] ==0)
{
doc->stone[x][y] = 2;
update();
mousepress = 1;
indexforregret = 0;
}
}
void RenjuView::clientagreetohuiqi(int x,int y)
{
qDebug("%d,%d",x,y);
message->about(this,"game information","the other side agree to regret!");
doc->stone[x][y] = 0;
update();
mousepress = 1;
}
void RenjuView::clienthuiqi(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";
client->reqHuiqiRenju(x,y);
break;
case QMessageBox::No:
client->reqHuiqiRenjuno(x,y);
//os <<"donotagreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
break;
}
}
void RenjuView::clientdonotagreetohuiqi()
{
message->about(this,"game information","the other side do not agree to regret!");
}
void RenjuView::clientrestart()
{
message->about(this,"game information","the other side agree to restart!");
for(int i=0; i<15; i++)
for(int j=0; j<15; j++)
{
doc->stone[i][j]=0;
}
update();
mousepress = 1;
indexforregret = 1;
}
void RenjuView::clientnewrequest()
{
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;
update();
//os <<"agreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
client->serveragreetorestart();
break;
case QMessageBox::No:
client->serverdonotagreetorestart();
//os <<"donotagreetohuiqi"<<"/"<<x<<"/"<<y<<"\n";
break;
}
}
void RenjuView::clientdonotagreetonew()
{
message->about(this,"game information","the other side do not agree to restart!");
}
//发送信息给对方
void RenjuView::sendMsg()
{
QString msg;
msg = le->text();
tv->append("server:");
tv->append(msg);
if (client != NULL)
client->sendMsg(msg);
le->setText("");
}
//处理对方发送过来的消息
void RenjuView::recvMsg(QString &msg)
{
tv->append("client:");
tv->append(msg);
}
//处理对方发送的赢的信号
void RenjuView::clinetdone()
{
message->about(this,"game information","you lose!");
mousepress = 0;
indexforregret = 0;
}
void RenjuView::clientconnectionClosed()
{
message->about(this,"game information","connection closed!");
mousepress = 0;
indexforregret = 0;
exit(1);
}
//连接建立的信号处理
void RenjuView::conneted(){}
void RenjuView::newConnect(RenjuSocket *c)
{
client = c;
tv->append("New connection,you can play! \n" );
connect(c,SIGNAL(recvMsg(QString &)),this,SLOT(recvMsg(QString &)));
connect(c,SIGNAL(parserzhouqi(int , int )),this,SLOT(zhouqi(int , int )));
connect(c,SIGNAL(clinetdone()),this,SLOT(clinetdone()));
connect(c,SIGNAL(clientagreetohuiqi(int ,int)),this,SLOT(clientagreetohuiqi(int,int)));
connect(c,SIGNAL(clientdonotagreetohuiqi()),this,SLOT(clientdonotagreetohuiqi()));
connect(c,SIGNAL(clienthuiqi(int ,int)),this,SLOT(clienthuiqi(int,int)));
connect(c,SIGNAL(clientrestart()),this,SLOT(clientrestart()));
connect(c,SIGNAL(clientnewrequest()),this,SLOT(clientnewrequest()));
connect(c,SIGNAL(clientdonotagreetonew()),this,SLOT(clientdonotagreetonew()));
connect(c,SIGNAL(clientconnectionClosed()),this,SLOT(clientconnectionClosed()));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -