用亚马逊AI代码开发助手Amazon Q Developer开发小游戏(中篇)

快用人工智能帮程序员写代码、开发游戏!今天小李哥就来介绍亚马逊推出的国际前沿人工智能AI代码开发助手Amazon Q Developer。目前该代码助手在Hugging Face代码生成权威测试集SWE-bench中排名第一,可以根据我们的需求生成整个代码项目,并可以帮助我们解释代码、提供架构设计方案、编写代码测试案例、提供漏洞扫描和修复建议、基于我们GitHub代码库中的历史代码生成新的代码段。

在本篇我们将进行代码实操,用AI开发助手开发一个猜词小游戏,该猜词小游戏的游戏截图如下图。玩家在游戏中有五次猜词机会,目标是猜测一个六个字母的单词。每次猜测后,玩家在游戏界面中会收到单词位置对应的+ ? X 占位符反馈信息。其中+ 字符表示该位置的字母正确,? 字符表示字母在单词中但位置错误,X 字符表示字母不在单词中。通过不断的反馈和尝试,最终猜测出正确的单词。

方案所需基础知识

什么是Amazon Q Developer?

Amazon Q Developer 是一款由亚马逊云科技推出的AI驱动的软件开发助手,用于帮助开发者重新构想整个软件开发生命周期的体验,使得在亚马逊云科技或其他平台上构建、保护、管理和优化代码的过程变得更加快捷。其中比较亮点的功能是Amazon Q Developer Agent,它一个特性开发代理,该代理可以在集成到VSCode等开发环境(IDE)中,通过该工具开发者只需要通过自然语言输入,就可以自动生成定制化代码项目、修复代码bug和漏洞以及单元测试。当开发者输入特定代码需求后,软件开发代理会分析开发者的代码库并制定实现代码计划。开发者可以接受该计划,或者要求代理对其进行迭代优化之前的项目版本。在计划被确认接受后,代理会自动生成基于开发者需求的代码更改。

Amazon Q Developer 通过生成式人工智能(AI)为所有开发者提供目前性能最佳的代码生成工具,目前Amazon Q Developer在SWE-bench排行榜上名列第一。SWE-bench是一个测试系统自动解决GitHub代码问题的开发工具评估数据集。接下来小李哥就会介绍如何开始使用软件开发代理开发游戏、概述代理的工作原理等。

本实践包括的内容

1. 分享生成式AI猜词小游戏Java源代码

2. 利用Amazon Q为猜词小游戏源代码进行解释、构建、Debug

本实践包括的内容

Amazon Q代码助手插件安装

1. 确认已经在VS Code IDE运行环境中安装了进行实操实验的必要依赖项

  • Amamzon CLI
  • Visual Studio Code 的 Amazon Q 扩展
  • Git
  • Java - Amazon Corretto -- 版本 8 和17
  • Maven

2. 导航到 VS Code IDE 左侧窗格中的扩展图标

3. 在搜索栏中,输入 Amazon Q 并点击 Install

登录亚马逊云科技开发者账户使用Amazon Q Developer

4. 在 Visual Studio Code 的 Amazon Q 扩展中,选择上方的”Use For Free“,并选择Continue继续。

5. 出现提示是否要使用代码打开外部网站,选择打开。

6. 将打开浏览器选项卡并显示登录开发者账户Builder ID页面,输入账户信息登录。

7. 登录成功后我们就可以看到左侧的Amazon Q Developer对话界面,我们输入测试问题”What is your name?“可以得到Amazon Q Developer的介绍

小游戏源代码项目

8. 小李哥在这里将分享部分源代码,其中游戏控制器代码如下:

package com.example.qwords.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.qwords.model.Word;
import com.example.qwords.model.GameStatus;
import com.example.qwords.service.WordSelectionService;

import io.micrometer.core.lang.Nullable;
import lombok.extern.log4j.Log4j2;

@Controller
@Log4j2
public class GameController {

    private WordSelectionService wordBank;
    private String selected;
    private Word word;

    @GetMapping("/game")
    public String index(@RequestParam String user, Model model) {

        wordBank = new WordSelectionService();
        selected = wordBank.getWord();
        word = new Word(selected);

        log.info(user);

        int attempts = getAttempts(model);

        String result = "";

        //Set View Attributes
        model.addAttribute("word", word.getWord());
        model.addAttribute("message", "Make your first guess!");
        model.addAttribute("attempts", attempts);
        model.addAttribute("result", result);
        model.addAttribute("status", GameStatus.INPROGRESS);

        return "game";
    }

    @PostMapping("/game")
    public String makeGuess(String guess, int attempts, Model model) {

        String result = word.getInfo(guess);
        attempts = addAttempt(attempts);
        
        model.addAttribute("result", result);
        model.addAttribute("attempts", attempts);
        model.addAttribute("guess", guess);

        if (word.isCorrect(guess.toCharArray())) {
            model.addAttribute("message", "Congratulations! You guessed correctly");
            model.addAttribute("status", GameStatus.SUCCESS);
        } else {

            if (attempts >= 5) {
                model.addAttribute("message", "Sorry, you've reached the maximum number of attempts.");
                model.addAttribute("status", GameStatus.FAILED);
            } else {
                model.addAttribute("message", "Try again. Your next guess:");
                model.addAttribute("status", GameStatus.INPROGRESS);
            }
        }

        return "game";
    }

    private int getAttempts(Model model) {
        Integer attempts = (Integer) model.getAttribute
        ("attempts");
        return (attempts != null) ? attempts : 0;
    }

    private int addAttempt(@Nullable Integer attempt) {
        return attempt + 1;
    }

}

 单词选择服务源代码如下:

package com.example.qwords.service;

import com.example.qwords.repository.WordList;

public class WordSelectionService {

    private WordList wordlist;
    private String selectedWord;

    public WordSelectionService() {

        this.wordlist = new WordList();
        this.selectedWord = wordlist.getRandomWord();
    }

    public String getWord() {
        return this.selectedWord;
    }
}

利用Amazon Q为源代码开发增效

9. 我们通过以下命令编译、运行该Java项目

mvn verify
java -jar target/QWordsService-0.0.1.jar

10. 运行结束后会将游戏进程绑定到8090端口,并弹出窗口询问是否要在浏览器中打开应用程序。选择 Open in Browser,就可以弹出以下游戏界面。

11. 目前源代码中存在一个小Bug,就是每次被猜测的词是固定的,让游戏没有挑战性,我们想让这个被猜测的词是随机的。接下来我们一步步通过Amazon Q的AI能力解决这个Bug。首先我们选中我们在第8步分享的游戏控制器代码,然后右键单击“Send to Amazon Q > Send to prompt”,将 源代码发送到 Q。。

12. 接下来在Amazon Q聊天框中对选中代码解释,输入以下内容获得具体解释。

How is the word selected for the game?

代码解释如下:

13. 接下来我们重复同样的操作,将步骤9中的单词选择服务源代码导入到Q中,并提问以下问题对代码进行解释。

How does the WordSelectionService select and return a word for the GameController?

得到的解释如下:在该回复中,Q理解出单词随机选择的逻辑是调用了WordList.getRandomWord 函数,我们需要修改该函数内的代码逻辑实现随机单词生成。

14. 我们将该函数的源代码导入到Q中,并输入以下问题,让Q帮我们生成修改后的正确代码。

My words are not selected in a random fashion. Can you troubleshoot and fix the code?

得到以下回复,回复中可以看到Amazon Q建议使用Math.random() 实现随机单词的生成。在 Amazon Q聊天框的回复中,有一个在光标处插入(insert at cursor)选项,单击此选项可以直接将原项目中的源代码一键替换掉,更高效的对代码进行修改。

15. 我们再进入到WordList.getRandomWord 函数源代码中,可以看到代码已经被替换掉了

package com.example.qwords.repository;
import java.util.ArrayList;
import java.lang.Math;

public class WordList {

    private ArrayList<String> wordlist;


    public WordList() {
        this.wordlist = new ArrayList<String>();
        this.wordlist.add("animal");
        this.wordlist.add("bakery");
        this.wordlist.add("cracks");

    }
    
    public String getRandomWord() {
        int randomIndex = (int) (Math.random() * wordlist.size());
        return this.wordlist.get(randomIndex);
    }


}

16. 我们运行如下命令,对小游戏项目进行重新构建和启动

mvn -U clean verify
java -jar target/QWordsService-0.0.1.jar

17. 再次打开游戏页面,输入猜测的单词,我们发现被猜的单词已经不是固定单词了,通过这种方式让游戏更有乐趣。

 

以上就是利用亚马逊AI代码开发/生成工具-Amazon Q Developer开发猜词小游戏的中篇内容。欢迎大家关注小李哥的亚马逊云科技AI服务深入调研系列,关注小李哥未来不要错过更多国际前沿的AWS云开发/云架构方案。 

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值