TEE-CSA / TEE-Frontend

Apache License 2.0
0 stars 0 forks source link

RaceCar Individual Issue - Emaad Mir #8

Open Emaad-Mir opened 7 months ago

Emaad-Mir commented 7 months ago

Emaad Mir's Individual Review Ticket

Name Team+Indi+Teacher Runtime Issues Key Commits Analytics Video
Emaad M. 0.75+0.82+x=1.57 Betting Issue On this review ticket Analytics Video Link

Frontend contributions: image

Key Commits:

setTimeout(() => {
                    if (raceResultFib) {
                        const winLossMessage = raceResultFib.didUserWin ? `You won ${betAmountFib * 2} points!` : `You lost ${betAmountFib} points.`;
                        alert(`Race finished! Winning algorithm: ${raceResultFib.selectedAlgorithm}. ${winLossMessage} Fun Fact: ${raceResultFib.funFact}`);
                        userScoreFib += raceResultFib.didUserWin ? betAmountFib * 2 : -betAmountFib;
                        document.getElementById("userScoreFib").textContent = userScoreFib;
                    }
                }, Math.max(...Object.values(algorithmTimes)) * 4000);
            } catch (error) {
                console.error('Error fetching algorithm times:', error);
            }
        }
async function placeBetFib() {
            const betInputValue = parseInt(document.getElementById("fibBetInput").value);
            if (isNaN(betInputValue) || betInputValue <= 0 || betInputValue > userScoreFib) {
                alert("Invalid bet amount for Fibonacci race");
                return;
            }

            betAmountFib = betInputValue;
            document.getElementById("betAmountFib").textContent = betAmountFib;

            try {
                const response = await fetch(`http://localhost:8085/api/fibonacci/bet?betAmount=${betAmountFib}&startingPoints=${userScoreFib}`);
                raceResultFib = await response.json(); // Store the race result
                // Do not start the race here. The race will start when "Start Fibonacci Race" button is clicked.
            } catch (error) {
                console.error('Error placing bet for Fibonacci race:', error);
            }
        }

Backend contributions: image

Key Commits:

// CarApiController.java
@GetMapping("/bet")

public Map<String, Object> betOnSortRace(@RequestParam(required = true) int betAmount,
                                         @RequestParam(required = true) int startingPoints) {
    Betting game = new Betting(startingPoints);

    Map<String, Integer> speeds = getAlgorithmSpeeds(null);

    // randomly select an algorithm
    List<String> algorithms = new ArrayList<>(speeds.keySet());
    String selectedAlgorithm = algorithms.get(new Random().nextInt(algorithms.size()));

    String fastestAlgorithm = speeds.entrySet().stream()
            .min(Map.Entry.comparingByValue())
            .map(Map.Entry::getKey)
            .orElse("none");

    boolean isGuessCorrect = selectedAlgorithm.equals(fastestAlgorithm);

    game.placeBet(betAmount, isGuessCorrect);

    Map<String, Object> result = new HashMap<>();
    result.put("message", game.getResultMessage());
    result.put("newScore", game.getPoints());
    result.put("selectedAlgorithm", selectedAlgorithm); // selected algorithm in the response

    Map<String, String> algorithmFacts = getAlgorithmFacts();
    result.put("fastestAlgorithm", fastestAlgorithm); // The actual winning algorithm
    result.put("funFact", algorithmFacts.get(fastestAlgorithm));

    return result;
}

private Map<String, String> getAlgorithmFacts() {
    Map<String, String> facts = new HashMap<>();
    facts.put("mergeSort", "Merge Sort is an efficient, stable, comparison-based, divide and conquer sorting algorithm. Most implementations produce a stable sort, meaning that the implementation preserves the input order of equal elements in the sorted output. Merge Sort also has a time complexity of O(nlogn), which is quite fast!");
    facts.put("insertionSort", "Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Insertion Sort has an average time complexity of O(n^2), making it quite inefficient for larger lists.");
    facts.put("bubbleSort", "Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Bubble Sort has a time complexity of O(n^2), also making it quite inefficient for larger lists.");
    facts.put("selectionSort", "Selection Sort is an in-place comparison sorting algorithm. It has an O(n^2) complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort.");
    return facts;
}

// FibonacciApiController.java

@GetMapping("/bet")
    public Map<String, Object> betOnFibonacciRace(@RequestParam(required = true) int betAmount,
                                                  @RequestParam(required = true) int startingPoints) {
        Betting game = new Betting(startingPoints);

        Map<String, Integer> speeds = getAlgorithmSpeeds(null);

        // Randomly select a Fibonacci algorithm
        List<String> algorithms = new ArrayList<>(speeds.keySet());
        String selectedAlgorithm = algorithms.get(new Random().nextInt(algorithms.size()));

        String fastestAlgorithm = speeds.entrySet().stream()
                .min(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse("none");

        boolean isGuessCorrect = selectedAlgorithm.equals(fastestAlgorithm);

        game.placeBet(betAmount, isGuessCorrect);

        Map<String, Object> result = new HashMap<>();
        result.put("message", game.getResultMessage());
        result.put("newScore", game.getPoints());
        result.put("selectedAlgorithm", selectedAlgorithm);
        result.put("fastestAlgorithm", fastestAlgorithm);

        Map<String, String> fibonacciFacts = getFibonacciFacts();
        result.put("funFact", fibonacciFacts.get(fastestAlgorithm));

        return result;
    }

    private Map<String, String> getFibonacciFacts() {
        Map<String, String> facts = new HashMap<>();
        facts.put("forLoopFibonacci", "For Loop Fibonacci is efficient for small sequences but becomes less efficient as the sequence grows due to its linear time complexity.");
        facts.put("whileLoopFibonacci", "While Loop Fibonacci is similar to the for-loop version but uses a while loop, making it slightly more versatile in certain contexts.");
        facts.put("recursionFibonacci", "Recursion Fibonacci is elegant but can be less efficient due to its exponential time complexity, especially for large sequences.");
        facts.put("matrixFibonacci", "Matrix Fibonacci uses matrix multiplication to calculate Fibonacci numbers, providing a more efficient approach for larger sequences.");
        return facts;
    }

Reflection

One significant (individual) improvement that I made was fixing the inconsistent functionality of the betting system for both the Fibonacci and the sorting races. At the time of our crossover review, we still had a few issues in getting the betting implementation to work for arrays of all sizes. What we have for our project now is definitely a large improvement from what we had a few days ago, as now users can play the racing game, place bets, and see the pop up message at the end informing them if they won or lost along with a fun fact about the sorting algorithm that won. One thing that I learned from doing this project was knowing the order of the different sorting algorithms from fastest to slowest. This was a very important part of our project, as we needed to ensure that the time it took for each sorting algorithm to travel across the whole track accurately reflected its efficiency compared to the other algorithms. For example, the sorting algorithms from fastest to slowest is usually merge sort, insertion sort, selection sort, and bubble sort. Therefore, we had to construct the race so that as efficiency increases, the time to cross the finish line decreases. As with any project for this class or any team-related activity, I learned about the absolute necessity of communication. My group members (Tay Kim and Ethan Tran) and I made a group chat for our scrum team, and us communicating ensured that we wouldn’t be running into issues such as merge conflicts. We would always inform each other if we were going to make any changes to certain files or commit some code. If something suddenly stopped working, one of us would always inform the group chat so that everyone was aware. Overall, I fully understand the significance of talking to your group mates/teammates, whether it is in this class or in situations beyond this class.

h4seeb-cmd commented 7 months ago

Individual Review: Mirza grading Emaad

Individuals Video, Issue(s), Commits(s)

FINAL SCORE: 0.82/0.9

Had every requirement, nice demos and explanations too! Good calling back to previous mistakes too.

NOTE: This didn't dock your score but I would try moving a little slower when presenting. Not a huge thing but it would help the quality of your presentations.

Glows:

Grows: