Chatbot Implementation - UQcsse3200/2024-studio-2 GitHub Wiki

ChatbotService Documentation

Overview

The ChatbotService class provides a simple chatbot interface for your game. It offers predefined responses based on keywords in the player's message. The chatbot helps answer common gameplay-related queries, such as objectives, controls, and game features.

image

Chatbot Feature

Overview

The Chatbot feature in the main menu provides users with interactive assistance by responding to their in-game questions. It helps players quickly access information about controls, objectives, game progression, and other key features. This chatbot simplifies navigation and enhances the user experience by allowing players to interact with predefined questions or type their own queries.

Features

  • Predefined Responses: A set of predefined questions and answers to commonly asked queries.
  • User Input Processing: Players can type their own questions to interact with the chatbot.
  • Dynamic Response Handling: The chatbot responds to user input with predefined keyword-based responses.
  • Chatbot Icon: An easily accessible icon in the bottom-right corner that opens the chatbot dialog.
  • Custom Dialog Design: The chatbot dialog features a well-designed user interface with an interactive input field and response label.

Implementation

ChatbotService Class

The ChatbotService class handles the chatbot's functionality. It stores a map of keyword-response pairs and processes user input to return appropriate responses.

public class ChatbotService {
    private final Map<String, String> responses;

    public ChatbotService() {
        responses = new HashMap<>();
        initializeResponses();
    }

    private void initializeResponses() {
        responses.put("objective", "Your goal is to defeat all the animals in each kingdom and ultimately become the overlord.");
        responses.put("start", "Simply click the \"Start\" button on the main menu to begin your adventure!");
        // Add more predefined responses here
    }

    public String getResponse(String userMessage) {
        if (userMessage == null || userMessage.isEmpty()) {
            return "I didn't catch that. Can you say something?";
        }

        String lowerCaseMessage = userMessage.toLowerCase();

        for (Map.Entry<String, String> entry : responses.entrySet()) {
            if (lowerCaseMessage.contains(entry.getKey())) {
                return entry.getValue();
            }
        }

        return "I'm sorry, I don't understand. Can you ask something else?";
    }
}

Test Cases

public class ChatbotServiceTest {

    private ChatbotService chatbotService;

    @BeforeEach
    public void setUp() {
        chatbotService = new ChatbotService();
    }

Test Case for Matching Keyword

    @Test
    public void testResponseWithMatchingKeyword() {
        String userMessage = "Tell me about the objective of the game.";
        String expectedResponse = "Your goal is to defeat all the animals in each kingdom and ultimately become the overlord.";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }

Test Case for Response with Different Keyword Casing

    @Test
    public void testResponseWithDifferentKeywordCasing() {
        String userMessage = "What are the CONTROLS?";
        String expectedResponse = "Use the WASD keys to move: W for up, A for left, S for down, and D for right.";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }

With No Keyword that Matches in User Input

    @Test
    public void testResponseWithNoMatchingKeyword() {
        String userMessage = "Tell me more about the game.";
        String expectedResponse = "I'm sorry, I don't understand. Can you ask something else?";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }

Test Case with Null Input

    @Test
    public void testResponseWithNullInput() {
        String expectedResponse = "I didn't catch that. Can you say something?";
        String actualResponse = chatbotService.getResponse(null);

        assertEquals(expectedResponse, actualResponse);
    }

Test Case with Empty Input

    @Test
    public void testResponseWithEmptyInput() {
        String userMessage = "";
        String expectedResponse = "I didn't catch that. Can you say something?";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }

Test Case with Many Matching Keywords

    @Test
    public void testResponseWithMultipleMatchingKeywords() {
        String userMessage = "Can you explain the objective and the kingdoms?";
        String expectedResponse = "Your goal is to defeat all the animals in each kingdom and ultimately become the overlord.";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }

Test Case with Partial Keyword Match

    @Test
    public void testResponseWithPartialKeywordMatch() {
        String userMessage = "How do I start the game?";
        String expectedResponse = "Simply click the \"Start\" button on the main menu to begin your adventure!";
        String actualResponse = chatbotService.getResponse(userMessage);

        assertEquals(expectedResponse, actualResponse);
    }
}