当前位置:   article > 正文

Cursor——ChatGPT的替代品【笔记】_cursor chat

cursor chat

前言

2023-3-31 22:00:44

以下内容源自《笔记》
仅供学习交流使用

推荐

什么? 你还没用过 Cursor? 智能 AI 代码生成工具 Cursor 安装和使用介绍

Cursor AI IDE 开发者工具

【黑科技】任何人都可以白嫖最强AI!!!

Cursor——ChatGPT的替代品

下载

官网下载Cursor
在这里插入图片描述

登录

点击设置
点击sign in
在这里插入图片描述

可以使用github账号登录

使用

先别打开任何文件,就可以回答问题了

如果回答的是英文,就它让用中文回答
在这里插入图片描述
在这里插入图片描述
它能回答,编程问题

在这里插入图片描述
如何回答,变成以外的问题,请看后面
在这里插入图片描述

可以发现,它默认的是python代码
在这里插入图片描述

新建一个文件 main,java
在这里插入图片描述
输入文件名,敲回车
在这里插入图片描述
编写区,Ctrl+L
在这里插入图片描述
就写出的是java代码了
在这里插入图片描述
怎么让它问答一个编程以外的问题

新建一个hello.txt文件
然后写一个编程之外的问题

测试有可能不成功

提问:写一封情书
在这里插入图片描述
提问:西安邮电大学简介
在这里插入图片描述

高级

参考 Cursor AI IDE 开发者工具

以下是扫雷程序

  1. import java.util.Scanner;
  2. public class Minesweeper {
  3. private int[][] grid;
  4. private boolean[][] revealed;
  5. private boolean[][] mines;
  6. private int rows;
  7. private int cols;
  8. private int numMines;
  9. public Minesweeper(int rows, int cols, int numMines) {
  10. this.rows = rows;
  11. this.cols = cols;
  12. this.numMines = numMines;
  13. grid = new int[rows][cols];
  14. revealed = new boolean[rows][cols];
  15. mines = new boolean[rows][cols];
  16. placeMines();
  17. calculateNumbers();
  18. }
  19. private void placeMines() {
  20. int count = 0;
  21. while (count < numMines) {
  22. int row = (int) (Math.random() * rows);
  23. int col = (int) (Math.random() * cols);
  24. if (!mines[row][col]) {
  25. mines[row][col] = true;
  26. count++;
  27. }
  28. }
  29. }
  30. private void calculateNumbers() {
  31. for (int r = 0; r < rows; r++) {
  32. for (int c = 0; c < cols; c++) {
  33. grid[r][c] = countAdjacentMines(r, c);
  34. }
  35. }
  36. }
  37. public void reveal(int row, int col) {
  38. if (mines[row][col]) {
  39. gameOver();
  40. return;
  41. }
  42. revealed[row][col] = true;
  43. if (grid[row][col] == 0) {
  44. for (int r = Math.max(0, row - 1); r <= Math.min(rows - 1, row + 1); r++) {
  45. for (int c = Math.max(0, col - 1); c <= Math.min(cols - 1, col + 1); c++) {
  46. if (!revealed[r][c]) {
  47. reveal(r, c);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. private void gameOver() {
  54. System.out.println("Game over!");
  55. System.exit(0);
  56. }
  57. public boolean isGameOver() {
  58. for (int r = 0; r < rows; r++) {
  59. for (int c = 0; c < cols; c++) {
  60. if (mines[r][c] && !revealed[r][c]) {
  61. return false;
  62. }
  63. if (!mines[r][c] && !revealed[r][c]) {
  64. return false;
  65. }
  66. }
  67. }
  68. return true;
  69. }
  70. // This method returns the number of adjacent mines for a given cell
  71. private int countAdjacentMines(int row, int col) {
  72. int count = 0;
  73. for (int r = Math.max(0, row - 1); r <= Math.min(rows - 1, row + 1); r++) {
  74. for (int c = Math.max(0, col - 1); c <= Math.min(cols - 1, col + 1); c++) {
  75. if (mines[r][c]) {
  76. count++;
  77. }
  78. }
  79. }
  80. return count;
  81. }
  82. // This method returns the current state of the game as a string
  83. // Each cell is represented by a single character
  84. // If the cell is not revealed, it is represented by '-'
  85. // If the cell is revealed and contains a mine, it is represented by '*'
  86. // If the cell is revealed and does not contain a mine, it is represented by the number of adjacent mines
  87. public String toString() {
  88. StringBuilder sb = new StringBuilder();
  89. for (int r = 0; r < rows; r++) {
  90. for (int c = 0; c < cols; c++) {
  91. if (!revealed[r][c]) {
  92. sb.append("-");
  93. } else if (mines[r][c]) {
  94. sb.append("*");
  95. } else {
  96. sb.append(grid[r][c]);
  97. }
  98. }
  99. sb.append("\n");
  100. }
  101. return sb.toString();
  102. }
  103. // We need to add a main method to the Minesweeper class to continue the Minesweeper program
  104. // In the main method, we can create a new instance of the Minesweeper class and start the game
  105. // We can use a Scanner object to get input from the user and call the reveal method with the user's input
  106. // We can also use the isGameOver method to check if the game is over and end the game if necessary
  107. public static void main(String[] args) {
  108. Minesweeper game = new Minesweeper(10, 10, 10);
  109. Scanner scanner = new Scanner(System.in);
  110. while (!game.isGameOver()) {
  111. System.out.println(game.toString());
  112. System.out.print("Enter row and column to reveal: ");
  113. int row = scanner.nextInt();
  114. int col = scanner.nextInt();
  115. game.reveal(row, col);
  116. }
  117. System.out.println(game.toString());
  118. System.out.println("Game over!");
  119. }
  120. }

运行结果
在这里插入图片描述

以下是GUI编写的扫雷程序

  1. // Import necessary packages// The code provided does not contain any syntax errors that would prevent it from compiling.
  2. // If the user is experiencing compilation errors, they should check their Java environment and ensure that all necessary packages and dependencies are properly installed.
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. // Create a Minesweeper class that extends JFrame and implements ActionListener
  7. public class Minesweeper extends JFrame implements ActionListener {
  8. // Declare necessary variables
  9. private Container contentPane;
  10. private JPanel gamePanel, buttonPanel;
  11. private JButton[][] buttons;
  12. private int[][] counts;
  13. private int size = 10;
  14. private int numMines = 10;
  15. // Create a constructor for the Minesweeper class
  16. public Minesweeper() {
  17. // Set the title of the JFrame
  18. setTitle("Minesweeper");
  19. // Get the content pane of the JFrame
  20. contentPane = getContentPane();
  21. // Set the layout of the content pane to BorderLayout
  22. contentPane.setLayout(new BorderLayout());
  23. // Call the createGamePanel method to create the game panel
  24. createGamePanel();
  25. // Call the createButtonPanel method to create the button panel
  26. createButtonPanel();
  27. // Pack the JFrame
  28. pack();
  29. // Set the JFrame to be visible
  30. setVisible(true);
  31. }
  32. // Create a method to create the game panel
  33. private void createGamePanel() {
  34. // Create a new JPanel for the game panel
  35. gamePanel = new JPanel();
  36. // Set the layout of the game panel to GridLayout
  37. gamePanel.setLayout(new GridLayout(size, size));
  38. // Create a new 2D array of JButtons for the buttons
  39. buttons = new JButton[size][size];
  40. // Create a new 2D array of integers for the counts
  41. counts = new int[size][size];
  42. // Initialize the buttons and counts arrays
  43. for (int i = 0; i < size; i++) {
  44. for (int j = 0; j < size; j++) {
  45. buttons[i][j] = new JButton();
  46. buttons[i][j].addActionListener(this);
  47. gamePanel.add(buttons[i][j]);
  48. counts[i][j] = 0;
  49. }
  50. }
  51. // Add the game panel to the content pane
  52. contentPane.add(gamePanel, BorderLayout.CENTER);
  53. }
  54. // Create a method to create the button panel
  55. private void createButtonPanel() {
  56. // Create a new JPanel for the button panel
  57. buttonPanel = new JPanel();
  58. // Set the layout// Set the layout of the button panel to FlowLayout
  59. buttonPanel.setLayout(new FlowLayout());
  60. // Create a new JButton for the "New Game" button
  61. JButton newGameButton = new JButton("New Game");
  62. // Add an ActionListener to the "New Game" button
  63. newGameButton.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e) {
  65. newGame();
  66. }
  67. });
  68. // Add the "New Game" button to the button panel
  69. buttonPanel.add(newGameButton);
  70. // Add the button panel to the content pane
  71. contentPane.add(buttonPanel, BorderLayout.SOUTH);// Create a new JButton for the "Reset" button
  72. JButton resetButton = new JButton("Reset");
  73. // Add an ActionListener to the "Reset" button
  74. resetButton.addActionListener(new ActionListener() {
  75. public void actionPerformed(ActionEvent e) {
  76. reset();
  77. }
  78. });
  79. // Add the "Reset" button to the button panel
  80. buttonPanel.add(resetButton);
  81. // Add the button panel to the content pane
  82. contentPane.add(buttonPanel, BorderLayout.SOUTH);
  83. // Create a new JButton for the "Settings" button
  84. JButton settingsButton = new JButton("Settings");
  85. // Add an ActionListener to the "Settings" button
  86. settingsButton.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent e) {
  88. // Call the showSettingsDialog method to show the settings dialog
  89. showSettingsDialog();
  90. }
  91. });
  92. // Add the "Settings" button to the button panel
  93. buttonPanel.add(settingsButton);
  94. // Add the button panel to the content pane
  95. contentPane.add(buttonPanel, BorderLayout.SOUTH);
  96. }
  97. // Create a method to generate the mines and update the counts
  98. private void generateMines(int row, int col) {
  99. // Declare necessary variables
  100. int mines = 0;
  101. int i, j;
  102. // Generate the mines
  103. while (mines < numMines) {
  104. i = (int)(Math.random() * size);
  105. j = (int)(Math.random() * size);
  106. if (counts[i][j] != -1 && !(i == row && j == col)) {
  107. counts[i][j] = -1;
  108. mines++;
  109. }
  110. }
  111. // Update the counts
  112. for (i = 0; i < size; i++) {
  113. for (j = 0; j < size; j++) {
  114. if (counts[i][j] == -1) {
  115. updateCounts(i, j);
  116. }
  117. }
  118. }
  119. }
  120. // Create a method to update the counts
  121. private void updateCounts(int row, int col) {
  122. // Declare necessary variables
  123. int i, j;
  124. // Update the counts
  125. for (i = row - 1; i <= row + 1; i++) {
  126. for (j = col - 1; j <= col + 1; j++) {
  127. if (i >= 0 && i < size && j >= 0 && j < size && counts[i][j] != -1) {
  128. counts[i][j]++;
  129. }
  130. }
  131. }
  132. }
  133. // Create a method to reveal the button at the specified row and column
  134. private void reveal(int row, int col) {
  135. // Check if the button is already revealed or flagged
  136. if (buttons[row][col].getText().length() > 0) {
  137. return;
  138. }
  139. // Check if the button contains a mine
  140. if (counts[row][col] == -1) {
  141. // Reveal all the mines and end the game
  142. for (int i = 0; i < size; i++) {
  143. for (int j = 0; j < size; j++) {
  144. if (counts[i][j] == -1) {
  145. buttons[i][j].setText("*");
  146. }
  147. }
  148. }
  149. JOptionPane.showMessageDialog(this, "You lose!");
  150. newGame();
  151. return;
  152. }
  153. // Reveal the button and check if the game is won
  154. // Reveal the button and check if the game is won
  155. buttons[row][col].setText(Integer.toString(counts[row][col]));
  156. if (checkWin()) {
  157. JOptionPane.showMessageDialog(this, "You win!");
  158. newGame();
  159. }
  160. }
  161. // Create a method to check if the game is won
  162. private boolean checkWin() {
  163. // Declare necessary variables
  164. int revealed = 0;
  165. // Check if all non-mine buttons are revealed
  166. for (int i = 0; i < size; i++) {
  167. for (int j = 0; j < size; j++) {
  168. if (buttons[i][j].getText().length() > 0 || counts[i][j] == -1) {
  169. revealed++;
  170. }
  171. }
  172. }
  173. if (revealed == size * size) {
  174. return true;
  175. }
  176. return false;
  177. }
  178. // Create a method to start a new game
  179. private void newGame() {
  180. // Reset the counts and buttons arrays
  181. counts = new int[size][size];
  182. for (int i = 0; i < size; i++) {
  183. for (int j = 0; j < size; j++) {
  184. buttons[i][j].setText("");
  185. counts[i][j] = 0;
  186. }
  187. }
  188. // Generate the mines and update the counts
  189. generateMines(-1, -1);
  190. }
  191. // Create a method to reset the game
  192. private void reset() {
  193. // Reset the counts and buttons arrays
  194. for (int i = 0; i < size; i++) {
  195. for (int j = 0; j < size; j++) {
  196. buttons[i][j].setText("");
  197. counts[i][j] = 0;
  198. }
  199. }
  200. }
  201. private void showSettingsDialog() {
  202. // Declare necessary variables
  203. JTextField sizeField = new JTextField(Integer.toString(size));
  204. JTextField numMinesField = new JTextField(Integer.toString(numMines));
  205. Object[] message = {
  206. "Size:", sizeField,
  207. "Number of Mines:", numMinesField
  208. };
  209. // Show the settings dialog and update the settings
  210. int option = JOptionPane.showConfirmDialog(this, message, "Settings", JOptionPane.OK_CANCEL_OPTION);
  211. if (option == JOptionPane.OK_OPTION) {
  212. try {
  213. int newSize = Integer.parseInt(sizeField.getText());
  214. int newNumMines = Integer.parseInt(numMinesField.getText());
  215. if (newSize > 0 && newSize <= 30 && newNumMines >= 0 && newNumMines < newSize * newSize) {
  216. size = newSize;
  217. numMines = newNumMines;
  218. newGame();
  219. } else {
  220. JOptionPane.showMessageDialog(this, "Invalid input!");
  221. }
  222. } catch (NumberFormatException e) {
  223. JOptionPane.showMessageDialog(this, "Invalid input!");
  224. }
  225. }
  226. }
  227. // Create an actionPerformed method to handle button clicks
  228. public void actionPerformed(ActionEvent e) {
  229. // Get the button that was clicked
  230. JButton button = (JButton)e.getSource();
  231. // Find the row and column of the button
  232. int row = -1, col = -1;
  233. for (int i = 0; i < size; i++) {
  234. for (int j = 0; j < size; j++) {
  235. if (buttons[i][j] == button) {
  236. row = i;
  237. col = j;
  238. break;
  239. }
  240. }
  241. }
  242. // Call the reveal method to reveal the button
  243. reveal(row, col);
  244. }
  245. // Create a main method to create a new Minesweeper object
  246. public static void main(String[] args) {
  247. new Minesweeper();
  248. }
  249. // Create a method to flag the button at the specified row and column
  250. private void flag(int row, int col) {
  251. // Check if the button is already revealed
  252. if (buttons[row][col].getText().length() > 0) {
  253. return;
  254. }
  255. // Check if the button is already flagged
  256. if (buttons[row][col].getText().equals("F")) {
  257. buttons[row][col].setText("");
  258. return;
  259. }
  260. // Flag the button
  261. buttons[row][col].setText("F");
  262. }
  263. // Create a method to handle right-clicks on buttons
  264. private void handleRightClick(MouseEvent e) {
  265. // Get the button that was clicked
  266. JButton button = (JButton)e.getSource();
  267. // Find the row and column of the button
  268. int row = -1, col = -1;
  269. for (int i = 0; i < size; i++) {
  270. for (int j = 0; j < size; j++) {
  271. if (buttons[i][j] == button) {
  272. row = i;
  273. col = j;
  274. break;
  275. }
  276. }
  277. }
  278. // Call the flag method to flag the button
  279. flag(row, col);
  280. }
  281. // Override the mousePressed method to handle right-clicks on buttons
  282. public void mousePressed(MouseEvent e) {
  283. if (SwingUtilities.isRightMouseButton(e)) {
  284. handleRightClick(e);
  285. }
  286. }
  287. // Create a method to handle keyboard events
  288. public void keyPressed(KeyEvent e) {
  289. // Check if the "r" key is pressed
  290. if (e.getKeyCode() == KeyEvent.VK_R) {
  291. reset();
  292. }
  293. // Check if the "n" key is pressed
  294. if (e.getKeyCode() == KeyEvent.VK_N) {
  295. newGame();
  296. }
  297. // Check if the "s" key is pressed
  298. if (e.getKeyCode() == KeyEvent.VK_S) {
  299. showSettingsDialog();
  300. }
  301. }
  302. // Create a method to initialize the game
  303. private void initGame() {
  304. // Set the size and number of mines
  305. size = 10;
  306. numMines = 10;
  307. // Create the counts and buttons arrays
  308. counts = new int[size][size];
  309. buttons = new JButton[size][size];
  310. // Create the game panel
  311. gamePanel = new JPanel();
  312. gamePanel.setLayout(new GridLayout(size, size));
  313. // Create the buttons and add them to the game panel
  314. for (int i = 0; i < size; i++) {
  315. for (int j = 0; j < size; j++) {
  316. buttons[i][j] = new JButton();
  317. buttons[i][j].addActionListener(this);
  318. buttons[i][j].addMouseListener(new MouseAdapter() {
  319. public void mousePressed(MouseEvent e) {
  320. if (SwingUtilities.isRightMouseButton(e)) {
  321. handleRightClick(e);
  322. }
  323. }
  324. });
  325. gamePanel.add(buttons[i][j]);
  326. }
  327. }
  328. // Add the game panel to the content pane
  329. contentPane.add(gamePanel, BorderLayout.CENTER);
  330. // Create the button panel
  331. createButtonPanel();
  332. // Generate the mines and update the counts
  333. generateMines(-1, -1);
  334. }
  335. }

运行结果
在这里插入图片描述

另外

VSCode软件插件Chatmoss,也可以体验,但是好像有额度。
在这里插入图片描述

以下是GUI的扫雷程序


最后

2023-3-31 22:43:31

祝大家逢考必过
点赞收藏关注哦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/67279
推荐阅读
相关标签
  

闽ICP备14008679号