How to use GPT-4 to help you write games (take the 24-point game as an example)

Table of contents

give me a blackjack game

game rules

 Code given by GPT

​Improve

 improve again


Recently, there has been a wave of GPT-4 upsurge. Many people want to use GPT-4. Here I will advertise (hehe). If you don’t know how to use GPT in China, you can check out this blog: GPT- 4 Free usage method sharing (continued)_I love OJ's blog-CSDN blog

At present, if you want to use GPT-4, the main channel is ChatGPT Plus. As a user who has paid a subscription fee, you can choose a model to use during the conversation.

Another channel is to apply for the queuing of the official API. When I applied for Tongyi Qianwen, my patience was tortured for 4 or 5 days. Every day when I went home, I would happily check my phone for text messages. So I put it aside after applying this time. When to pass the application, leave it to fate. (So ​​this time I will use the domestic GPT-3.5 to test for everyone)

give me a blackjack game

game rules

"24 o'clock" is a kind of poker game, just like chess and go, it is a kind of entertainment that people like to hear and see.

With its own unique mathematical charm and rich connotation, it is gradually being accepted by more and more people. This kind of game is easy to learn, can strengthen the brain and intelligence, and is an extremely beneficial activity.

Specific content and operation method

In a deck of cards, 52 cards are removed, and 4 cards are randomly drawn, and the number on the card surface is calculated as 24 by using four arithmetic operations. Each card can only be used once.

game skills

1. Use 3×8=24, 4×6=24, 2×12=24 to solve.

Find a way to make up the four numbers on the card to form 3 and 8, 4 and 6, 2 and 12, and then multiply them to solve the problem. Practice has proved that this method is the method with the highest utilization rate and the highest hit rate.

2. Use the operation characteristics of 0 and 1 to solve the problem.

For example, 3, 4, 4, and 8 can form 3×8×(4÷4)=24. Another example is 4, 7, 1, 1 can form 4×1(7-1)×1=24.

3. See if there are 2, 3, 4, 6, 8, Q,

If so, consider using multiplication to make up the remaining 3 numbers into corresponding numbers . If there are two identical 6, 8, Q, for example, there are two 6s, as long as the rest can be made into 3, 4, 5, 24 can be calculated, if there are two 8s, the rest can be made into 2 , 3, 4, there are already two Qs, as long as the rest can make up 1, 2, 3, you can calculate 24, such as (9, J, Q, Q). If there is no 2, 3, 4, 6, 8, Q, see if you can make one of the two numbers first. In short, multiplication is very important, and 24 is the integer with the most common factors below 30 .

4. Add and subtract 4 cards to get 24.

5. Multiply first, then add, subtract a certain number.

6. Scores appear during the calculation. There is a rule, let 4 numbers be a, b, c, d. There must be ab+c=24 or ab-c=24 d=a or b. If d=a, a(b+c/a)=24 or a(bc/a)=24. There are only 17 kinds that can only use this method.

7. There are large numbers (above 30) appearing in the calculation process. Relatively difficult, but sometimes only this solution. For example (6, 9, 9, 10) is 9×10÷6+9.

 Code given by GPT

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double eval(string s) // 计算字符串表达式的值
{
    int len = s.size();
    int p = 0; // 指向当前遍历到的位置
    double ans = 0.0; // 累计所有项的和
    int op = 1; // 当前符号的正负号,1表示加法或正号,-1表示减法或负号
    int last_op = 1; // 上一个符号的正负号
    double num = 0.0; // 当前遍历到的数值

    while(p < len)
    {
        char c = s[p];
        if(c == '+' || c == '-') // 遇到加减号
        {
            ans += op * last_op * num;
            num = 0.0;
            last_op = op = (c == '+') ? 1 : -1;
        }
        else if(c == '*' || c == '/') // 遇到乘除号
        {
            double next_num = 0.0;
            ++p;
            while(p < len && '0' <= s[p] && s[p] <= '9')
                next_num = next_num*10 + s[p++] - '0';
            num = (c == '*') ? num * next_num : num / next_num;
        }
        else if(c == '(') // 遇到左括号
        {
            int cnt = 1;
            int q = p + 1;
            while(q < len && cnt > 0)
            {
                if(s[q] == '(')
                    ++cnt;
                else if(s[q] == ')')
                    --cnt;
                ++q;
            }
            num = eval(s.substr(p+1, q-p-2));
            p = q;
        }
        else if(c == ' ') // 空格分隔符
        {
            ++p;
            continue;
        }
        else // 遇到数字
        {
            num = num*10 + c - '0';
            ++p;
        }
    }

    ans += op * last_op * num;

    return ans;
}
int main()
{
    srand(time(NULL)); // 初始化随机数生成器
    int a = rand()%9+1, b = rand()%9+1, c = rand()%9+1, d = rand()%9+1; // 生成四个1~9之间的整数
    cout << "请使用加、减、乘、除以及括号使得以下四个数构成算式的结果为24:" << endl;
    cout << a << " " << b << " " << c << " " << d << endl;

    bool found = false; // 标记是否找到合法的算式
    char ops[5] = {'+', '-', '*', '/'}; // 运算符集合
    for(int i=0; i<4 && !found; ++i) // 枚举第一个数到第四个数
        for(int j=0; j<4 && !found; ++j) // 枚举第二个数到第四个数
            if(j != i)
                for(int k=0; k<4 && !found; ++k) // 枚举第三个数到第四个数
                    if(k != j && k != i)
                        for(int l=0; l<4 && !found; ++l) // 枚举第四个数
                            if(l != k && l != j && l != i)
                            {
                                // 构造算式
                                string s = "";
                                s += to_string(a) + ops[i] + to_string(b) + ops[j] + to_string(c) + ops[k] + to_string(d);

                                // 枚举中间结果的算式
                                for(int i2=0; i2<4 && !found; ++i2)
                                    for(int j2=0; j2<4 && !found; ++j2)
                                        if(j2 != i2)
                                            for(int k2=0; k2<4 && !found; ++k2)
                                                if(k2 != j2 && k2 != i2)
                                                {
                                                    // 构造中间结果的算式
                                                    string s2 = "";
                                                    s2 += "(" + to_string(a) + ops[i] + to_string(b) + ")" + ops[i2] + "(" + to_string(c) + ops[j] + to_string(d) + ")" + ops[j2] + to_string(ops[k2] - '+' + '*');

                                                    // 计算结果
                                                    if(eval(s2) == 24)
                                                    {
                                                        cout << "恭喜您,找到了一个合法的算式:" << endl;
                                                        cout << s << " = " << eval(s) << endl;
                                                        found = true;
                                                    }
                                                }
                            }

    if(!found)
        cout << "很遗憾,没有找到合法的算式。" << endl;

    return 0;
}


If there is no accident, an accident will come 

However, I searched the Internet for a long time, and finally I know why, because my C++ version is too low, and the solution is very simple, as follows

 Click the menu in turn: Tools -> Compiler Options -> Code Generation/Optimization (Settings) -> Code Generation (Code Generation), find the last language standard (Language standard), you can see the back option is empty by default.

Pull down the option bar, select ISO C++11, click OK and then compile without error

 After running it looks like this:

It can be seen that there is no output result, so I got angry and said to GPT

Improve

Below is the corrected code

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <vector>
using namespace std;

double eval(vector<string>& tokens) // 计算逆波兰表达式的值
{
    stack<double> nums;
    for(string& token: tokens)
    {
        if(token == "+") // 加法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 + num2);
        }
        else if(token == "-") // 减法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 - num2);
        }
        else if(token == "*") // 乘法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 * num2);
        }
        else if(token == "/") // 除法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 / num2);
        }
        else // 数字
        {
            nums.push(stod(token)); // stod() 可以将字符串转换为 double 类型
        }
    }

    return nums.top();
}

bool solve(vector<int>& nums, vector<string>& tokens) // 递归解决24点游戏
{
    if(nums.empty()) // 无数字剩余,得出解法
    {
        return eval(tokens) == 24;
    }

    for(int i=0; i<nums.size(); ++i)
    {
        int num = nums[i];
        nums.erase(nums.begin() + i); // 去掉当前数
        tokens.push_back(to_string(num)); // 转换为字符串并加入逆波兰表达式
        if(solve(nums, tokens)) // 尝试解决剩余的数字
            return true;
        tokens.pop_back();
        nums.insert(nums.begin() + i, num); // 还原当前数
    }

    return false;
}

int main()
{
    srand(time(NULL)); // 初始化随机数生成器
    vector<int> nums(4);
    for(int& num: nums)
        num = rand()%9+1; // 生成四个 1~9 之间的整数
    cout << "请使用加、减、乘、除以及括号使得以下四个数构成算式的结果为24:" << endl;
    cout << nums[0] << " " << nums[1] << " " << nums[2] << " " << nums[3] << endl;

    vector<string> tokens;
    if(solve(nums, tokens)) // 寻找解法
    {
        cout << "恭喜您,找到了一个合法的算式:" << endl;
        for(string& token: tokens)
            cout << token << " ";
        cout << "= 24" << endl;
    }
    else
    {
        cout << "很遗憾,没有找到合法的算式。" << endl;
    }

    return 0;
}

output result

 Obviously, this side is much stronger, but can I make it so easy, so I continue to ask questions:

 improve again

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <string>
#include <vector>

using namespace std;

double eval(vector<string>& tokens) // 计算逆波兰表达式的值
{
    stack<double> nums;
    for (string& token : tokens) {
        if (token == "+") // 加法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 + num2);
        }
        else if (token == "-") // 减法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 - num2);
        }
        else if (token == "*") // 乘法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 * num2);
        }
        else if (token == "/") // 除法
        {
            double num2 = nums.top();
            nums.pop();
            double num1 = nums.top();
            nums.pop();
            nums.push(num1 / num2);
        }
        else // 数字
        {
            nums.push(stod(token)); // stod() 可以将字符串转换为 double 类型
        }
    }

    return nums.top();
}

bool solve(vector<int>& nums, vector<string>& tokens, int step) // 递归解决24点游戏
{
    if (nums.empty()) // 无数字剩余,得出解法
    {
        if (eval(tokens) == 24)
        {
            cout << "恭喜您,找到了一个合法的算式:" << endl;
            for (string& token : tokens)
                cout << token << " ";
            cout << "= 24" << endl;
            return true;
        }
        else
        {
            return false;
        }
    }

    if (step == 0) // 已经尝试了 5 次,输出如何用这些数字组成 24
    {
        cout << "很遗憾,五次机会已用完。以下是如何用这些数字组成 24。" << endl;
        return false;
    }

    for (int i = 0; i < nums.size(); ++i) {
        int num = nums[i];
        nums.erase(nums.begin() + i); // 去掉当前数
        tokens.push_back(to_string(num)); // 转换为字符串并加入逆波兰表达式
        if (solve(nums, tokens, step - 1))
            return true;
        tokens.pop_back();
        nums.insert(nums.begin() + i, num); // 还原当前数
    }

    return false;
}

int main()
{
    srand(time(NULL)); // 初始化随机数生成器
    vector<int> nums(4);
    for (int& num : nums)
        num = rand() % 9 + 1; // 生成四个 1~9 之间的整数
    cout << "您有五次机会使用加、减、乘、除以及括号使得以下四个数构成算式的结果为 24:" << endl;
    cout << nums[0] << " " << nums[1] << " " << nums[2] << " " << nums[3] << endl;

    vector<string> tokens;
    bool found_solution = false; // 是否已经找到解法
    for (int i = 1; i <= 5; ++i) {
        cout << "第" << i << "次尝试:请输入您的算式(支持加减乘除和括号):" << endl;
        string input;
        getline(cin, input);
        tokens.push_back(input); // 将输入的字符串加入 vector 中
        if (eval(tokens) == 24)
        {
            cout << "恭喜您,找到了一个合法的算式:" << endl;
            cout << input << " = 24" << endl;
            found_solution = true;
            break;
        }
        else if (i < 5)
        {
            cout << "计算结果不为 24,请继续尝试。" << endl;
        }
        tokens.pop_back(); // 将输入的字符串从 vector 中弹出
    }

    if (!found_solution) // 五次机会用完,输出如何用这些数字组成 24
    {
        vector<string> empty_tokens;
        if (solve(nums, empty_tokens, 5))
        {
            // 已经在函数中输出了解法
        }
        else
        {
            cout << "无法组成 24,以下是如何使用这些数字组成 24:" << endl;
            // 输出如何用这些数字组成 24 的方法
            // ...
        }
    }

    return 0;
}

 GPT is really serious about modifying the code, and the running effect is like this

 I can't help feeling a sigh: what else to learn C ++

Of course, any experienced programmer can stand up and point out the various shortcomings of GPT-4 assisted programming, and then assert that it can never replace an excellent developer.

Don't forget, GPT-4 has only been released for less than a year, and many large language models are still evolving at a hurricane pace. Besides, who said that its goal is to replace "excellent developers"?

Currently, I don't have an answer. Want to hear your opinion. Welcome to leave a message, let's exchange and discuss together

Guess you like

Origin blog.csdn.net/qiuweichen1215/article/details/130905196
Recommended