Cursor编辑器试用

新出的基于VsCode和大模型的编辑器Cursor,效果真的神了

avatar_cover Alex Xiang
2024-11-29 371阅读

Cursor 是基于 VSCode 修改派生,因此理论上所有在 VSCode 上的配置都能够导入到 Cursor 里使用,如果你平常使用 VSCode 进行开发,那么你能够非常便捷地迁移过来。当然实际上并非所有插件都能平滑迁移到Cursor的,至少我就碰到了在Windows环境下,WSL插件就无法在Cursor下启动,这可能会是一个问题,不过不管怎样先尝试一下Cursor,看看是否真如传说中的强大。

找了一个间的C++实现的五子棋小游戏,看看Cursor能帮我们做什么。以下都是不选任何代码的情况下,按CTL-L呼起CHAT窗口来完成任务。

问题一:增加人机对战功能

最初的代码是简单的两个玩家的对战,没有AI支持,我们来看看Cursor是否能为其增加人机对战的功能。 下面都是Cursor的回答,我们先在前面对Cursor的回答做一些评述。总的来说,Cursor可以理解整个程序代码的逻辑,并且在合适的位置插入了人机对战支持的框架代码。新增的代码并不完整,还需要自己根据提示填入需要的部分,但是总体上讲已经有差不多70%的完成度了,至少大方向没问题,需要的就是一些细节。

Cursor在生成代码的同时,还会告诉你主要的改动说明,非常贴心。

我们的问题是:

这是一个五子棋的程序,增加人机对战的逻辑,需要支持α-β剪枝

我来帮你在这个五子棋程序中添加人机对战功能。主要需要做以下几个改动:
添加评估函数来评估棋局
实现α-β剪枝的极大极小搜索算法
修改Player类以支持AI玩家
更新Game类的相关逻辑
以下是主要的代码修改:
// ... existing code ...

// 添加棋型评分常量
const int FIVE = 100000;  // 连五
const int LIVE_FOUR = 10000;  // 活四
const int DEAD_FOUR = 1000;   // 死四
const int LIVE_THREE = 1000;  // 活三
const int DEAD_THREE = 100;   // 死三
const int LIVE_TWO = 100;     // 活二
const int DEAD_TWO = 10;      // 死二

// 在Player类中添加isAI标志
class Player {
public:
    bool isAI;  // 是否为AI玩家

    Player(int _id, string _name, int _color, bool _isAI = false): 
        id(_id), name(_name), color(_color), isAI(_isAI) {}

    // ... existing code ...
};

// 添加评估函数和搜索算法
class Game {
private:
    // 评估某个位置的分数
    int evaluatePoint(int x, int y, Player* player) {
        // 实现位置评估逻辑
        int score = 0;
        // 横向、纵向、两个斜向各自评估
        // 返回综合评分
        return score;
    }

    // α-β剪枝搜索
    int alphaBetaSearch(int depth, int alpha, int beta, bool isMax) {
        if (depth == 0) {
            return evaluateBoard();
        }

        // 生成所有可能的走法
        vector<pair<int,int>> moves = generateMoves();

        if (isMax) {
            int value = -INT_MAX;
            for (auto move : moves) {
                // 尝试走法
                board[move.first][move.second] = A->id;
                value = max(value, alphaBetaSearch(depth-1, alpha, beta, false));
                // 撤销走法
                board[move.first][move.second] = 0;

                alpha = max(alpha, value);
                if (beta <= alpha)
                    break;
            }
            return value;
        } else {
            // 极小节点处理类似
            // ...
        }
    }

    // 获取AI的最佳走法
    pair<int,int> getBestMove() {
        pair<int,int> bestMove;
        int bestValue = -INT_MAX;

        vector<pair<int,int>> moves = generateMoves();
        for (auto move : moves) {
            board[move.first][move.second] = B->id;
            int value = alphaBetaSearch(3, -INT_MAX, INT_MAX, false);
            board[move.first][move.second] = 0;

            if (value > bestValue) {
                bestValue = value;
                bestMove = move;
            }
        }
        return bestMove;
    }

public:
    Game() {
        A = new Player(1, "红方", FOREGROUND_RED);
        B = new Player(2, "蓝方(AI)", FOREGROUND_BLUE, true);  // 设置B为AI玩家
    }

    // 修改run方法支持AI
    int run() {
        // ... existing code ...
        while(1) {
            if(!A->play()) {
                return 0;
            }
            if (checkWin(A)) {
                showWin(A, B);
                return 1;
            }

            // AI走棋
            if(B->isAI) {
                auto move = getBestMove();
                cursor.X = move.first * 4;
                cursor.Y = move.second * 2;
                moveTo(cursor.X, cursor.Y);
                B->go();
                board[move.first][move.second] = B->id;
            } else {
                if(!B->play()) {
                    return 0;
                }
            }

            if (checkWin(B)) {
                showWin(B, A);
                return 2;
            }
        }
    }
};
主要改动说明:
1、添加了评估函数相关的常量定义,用于不同棋型的评分
2、在Player类中增加isAI标志,用于标识AI玩家
3、添加了评估函数evaluatePoint()用于评估某个位置的分数
4、实现了α-β剪枝搜索算法alphaBetaSearch()
5、添加了getBestMove()方法用于获取AI的最佳走法
6. 修改了Game类的run()方法以支持AI走棋
这只是基础框架,你还需要实现:
1. evaluatePoint()中具体的评估逻辑
2、generateMoves()方法来生成可能的走法
3、evaluateBoard()方法来评估整个棋盘状态
4、完善极小节点的处理逻辑
5、建议先实现一个简单的评估函数,然后逐步优化AI的棋力。

问题二:将代码改成面向对象风格

原来的代码虽然是C++实现的,但有些地方还是按C语言的风格实现的,我们想将其全部改成C++风格,可以看到我们提出需求之后,Cursor为我们生成的一个叫Board的新类,并且看上去相关的功能都移到类的实现里了,并且神奇的是类名也非常贴切。

同样的,Cursor的回答会给出代码和改动的说明,比较有意思的是最后还会问是否需要更详细的说明,如果给出进一步的指令“需要”,Cursor还会把最终的代码给你贴出来,非常的智能。

我们的问题是:

将代码改成面向对象风格

我来帮你将代码重构成更面向对象的风格。主要改动包括:
将全局变量和函数移入适当的类中
增加 Board 类来管理棋盘状态
增加 Console 类来处理控制台相关操作
重构 Game 类,使其职责更清晰
以下是关键部分的修改建议:
// ... 音符和节拍的常量定义保持不变 ...

// 新增 Console 类处理控制台操作
class Console {
public:
    static void moveTo(int x, int y) {
        COORD coord{static_cast<SHORT>(x), static_cast<SHORT>(y)};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

    static void showText(int x, int y, int color, const string& text) {
        moveTo(x, y);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | color);
        cout << text;
    }

    static void showCursor(bool visible) {
        HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cInfo{};
        GetConsoleCursorInfo(hOutput, &cInfo);
        cInfo.bVisible = visible;
        cInfo.dwSize = 100;
        SetConsoleCursorInfo(hOutput, &cInfo);
    }
};

// 新增 Board 类管理棋盘
class Board {
private:
    array<array<int, BOARD_SIZE>, BOARD_SIZE> cells{};

public:
    Board() {
        clear();
    }

    void clear() {
        for(auto& row : cells) {
            row.fill(0);
        }
    }

    int getCell(int x, int y) const {
        return cells[x/4][y/2];
    }

    void setCell(int x, int y, int value) {
        cells[x/4][y/2] = value;
    }

    // ... 其他棋盘相关方法 ...
};

// Player 类修改
class Player {
private:
    int id;
    string name;
    int color;
    COORD& cursor;  // 引用游戏中的光标位置
    Board& board;   // 引用棋盘

public:
    Player(int _id, string _name, int _color, COORD& _cursor, Board& _board):
        id(_id), name(_name), color(_color), cursor(_cursor), board(_board) {}

    // ... 其他方法保持类似 ...
};

// Game 类修改
class Game {
private:
    Board board;
    COORD cursor{28, 14};
    unique_ptr<Player> playerA;
    unique_ptr<Player> playerB;

public:
    Game() {
        playerA = make_unique<Player>(1, "红方", FOREGROUND_RED, cursor, board);
        playerB = make_unique<Player>(2, "蓝方", FOREGROUND_BLUE, cursor, board);
    }

    // ... 其他方法相应调整 ...
};
主要改进:
1、将全局变量 board 和 cursor 封装到相应类中
2、新增 Console 静态类处理所有控制台操作
3、新增 Board 类专门管理棋盘状态
4、使用智能指针管理 Player 对象
5、通过依赖注入方式传递必要的引用
这样的结构更清晰,每个类都有明确的职责,代码更容易维护和扩展。建议逐步重构,确保每个改动后程序仍能正常运行。
需要我详细说明某个具体部分的修改吗?
Filter blog posts by tag cursor
rust (4)   wagtail (4)   zola (4)   kaggle (3)   vscode (3)   deepseek (2)   gemma (2)   go (2)   winui (2)   wsl (2)