实现功能:客户端和服务端数据交互
服务端:
play1.hpp文件,主要定义了三子棋的相关变量还有调用方法
#include <iostream>
#include <string>
using namespace std;
namespace player1
{
class play
{
private:
int iv[3][3];
public:
play()
{
memset(iv, 0, sizeof(iv));
}
void init()
{
memset(iv, 0, sizeof(iv));
}
bool update(int x, int y, int val)
{
if(x>=3 || x<0 || y>=3 || y<0)
{
return false;
}
if (iv[x][y] == 0)
{
iv[x][y] = val;
return true;
}
else return false;
}
void clear() //清除方法
{
memset(iv, 0, sizeof(iv));
}
bool iswin(int val) //判断输赢
{
for (int x = 0; x < 3; x++)
{
if ((iv[x][0] == iv[x][1]) && (iv[x][1] == iv[x][2]) && (iv[x][0] == val))
{
return true;
}
if ((iv[0][x] == iv[1][x]) && (iv[1][x] == iv[2][x]) && (iv[0][x] == val))
{
return true;
}
}
if (iv[0][0] == iv[1][1] && iv[1][1] == iv[2][2] && iv[1][1] == val)
{
return true;
}
if (iv[0][2] == iv[1][1] && iv[2][0] == iv[1][1] && iv[1][1] == val)
{
return true;
}
return false;
}
void getstr(string &s) //获取转换后的字符串
{
s.clear();
s += "-------\n";
for (int i = 0; i < 3; i++)
{
s += "|";
for (int j = 0; j < 3; j++)
{
s += '0' + iv[i][j];
s += "|";
}
s += "\n";
s += "-------\n";
}
}
};
static play play1; //静态全局变量
play* getplayer1()
{
return &play1;
}
};
2. getmethod.hpp文件:定义get方法
void play1(const httplib::Request ®, httplib::Response &res) //获取初始化
{
string s;
player1::getplayer1()->init();
player1::getplayer1()->getstr(s);
res.set_content(s.c_str(),s.size(),"player");
}
void updateplay1(const httplib::Request ®, httplib::Response &res) //获取更新后的棋盘
{
cout<<"enter update player1"<<endl;
string s;
player1::getplayer1()->getstr(s);
res.set_content(s.c_str(),s.size(),"player");
}
void setpos(const httplib::Request ®, httplib::Response &res) //设置对应位置
{
cout<<"enter setpos:"<<reg.path<<endl;
string s= reg.path;
int x = s[0] - '0';
int y = s[2] - '0';
if(player1::getplayer1()->update(y-1,x-1,1))
{
//cout<<"setpos ok"<<endl;
if(player1::getplayer1()->iswin(1))
{
s = "1";
res.set_content(s.c_str(),s.size(),"player");
return;
}
player1::getplayer1()->getstr(s);
cout<<s<<endl;
while(1)
{
cout<<"X:";
cin>>x;
cout<<"Y:";
cin>>y;
if(player1::getplayer1()->update(y-1,x-1,2))
{
break;
}
else
{
cout<<"input again"<<endl;
}
}
if(player1::getplayer1()->iswin(2))
{
s = "2";
res.set_content(s.c_str(),s.size(),"player");
return;
}
s = "4";
cout<<"ok:"<<s<<endl;
res.set_content(s.c_str(),s.size(),"player");
return;
}
else
{
s = "3";
res.set_content(s.c_str(),s.size(),"player");
return;
}
}
void clear(const httplib::Request ®, httplib::Response &res) //清空
{
cout<<"enter clear"<<endl;
player1::getplayer1()->clear();
}
1.main函数:挂载httplib,使能对应函数
int main()
{
Server server;
server.set_read_timeout(50, 0); // 50 seconds
server.set_write_timeout(50, 0); // 50 seconds
server.set_idle_interval(0, 10000000); // 10000 milliseconds
server.Get("play1",play1);
server.Get("clear",clear);
server.Get("up",updateplay1);
server.Get("(\\d).(\\d)",setpos);
server.listen("10.1.150.36", 8080);
return 0;
}
客户端:
#include"httplib.h"
#include<iostream>
#include<io.h>
#include<direct.h>
#include<string>
#include<Windows.h>
using namespace std;
using namespace httplib
int main()
{
Client client("10.1.150.36", 8080);
client.set_keep_alive(true);
client.set_connection_timeout(0, 30000000); // 30000 milliseconds
client.set_read_timeout(200, 0); // 200 seconds
client.set_write_timeout(50, 0); // 50 seconds
string s;
int x = 0, y = 0;
auto it = client.Get("play1");
while (1)
{
s.clear();
cout << "X:";
cin >> x;
if (x == -1)
{
break;
}
cout << "Y:";
cin >> y;
s += ('0' + x); s += (','); s += ('0' + y);
//cout << s << endl;
it = client.Get(s);
while(it == nullptr)
{
it = client.Get(s);
}
//cout << "s:" << s << "get:" << it->body << endl;
if (it->body.compare("2") == 0)
{
cout << "你个垃圾,我都赢不了" << endl;
it = client.Get("up");
cout << it->body << endl;
client.Get("clear");
}
else if (it->body.compare("1") == 0)
{
cout << "竟然让你侥幸赢了" << endl;
it = client.Get("up");
cout << it->body << endl;
client.Get("clear");
}
else if (it->body.compare("3") == 0)
{
cout << "该位置有东西了" << endl;
cout << "重新下:" << endl;
}
else
{
cout << "--------------------------------------" << endl;
//cout << "enter getupdate" << endl;
it = client.Get("up");
cout << it->body << endl;
cout << "----------输入你想移动的位置----------" << endl;
}
}
}