用httplib.h完成一个两人玩的三子棋

本文介绍了如何使用C++的httplib库实现一个简单的在线双人三子棋游戏。游戏包括客户端和服务端,服务端通过定义不同方法处理初始化、更新棋盘、设置棋子位置和清空棋盘的请求。客户端通过发送HTTP请求与服务端交互,进行游戏。玩家可以在控制台输入坐标来落子,服务端判断输赢并返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现功能:客户端和服务端数据交互

服务端:

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 &reg, 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 &reg, 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 &reg, 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 &reg, 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;
                }
            }

}
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值