AleoNet/aleo-education

Logic Flow

effizzieng opened this issue · 0 comments

I noticed that some parts of the code have incomplete logic flow or missing implementations, such as the valid_char function and parts of the guess_letter function.

Here's a revised version. Kindly review and let me know if there is anything you want me to add. I am looking forward for a meaningful contribution.

Please feel free to correct is I'm wrong.

circuit Hangman {
    commitment: Point,
    revealed: [char; 20],
    used_guesses: [char; 10],
    guesses_left: u32,
    victory: bool,

    // Function to check if a character is a lowercase English alphabet letter
    function valid_char(c: char) -> bool {
        const valid_chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        let is_valid_char = false;

        for char in valid_chars {
            if c == char {
                is_valid_char = true;
                break;
            }
        }

        return is_valid_char;
    }
    
    // Function to initialize a new game of Hangman
    function new_game(word: [char; 20], const word_length: u32) -> Self {
        const digest: Point = Point { x: 0field, y: 258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177field };
        const x: field = 7810607721416582242904415504650443951498042435501746664987470571546413371306field;
        const y: field = 1867362672570137759132108893390349941423731440336755218616442213142473202417field;
        
        let hasher = PedersenHash::new(digest, [x; 256], [y; 256]);
        let hash: Point = hasher.hash([true; 256]);

        let mut valid_word = true;
        for char in word {
            if !Hangman::valid_char(char) {
                valid_word = false;
                break;
            }
        }

        if word_length > 20 {
            valid_word = false;
        }

        if !valid_word {
            return Self {
                commitment: Point { x: 0field, y: 0field },
                revealed: ['_'; 20],
                used_guesses: ['_'; 10],
                guesses_left: 0u32,
                victory: false,
            };
        }

        return Self {
            commitment: hash,
            revealed: ['_'; 20],
            used_guesses: ['_'; 10],
            guesses_left: 10u32,
            victory: false,
        };
    }

    // Function to handle a letter guess
    function guess_letter(mut self, letter: char) -> Self {
        let mut is_valid_guess = true;

        // Check if the guessed letter is valid
        if !Hangman::valid_char(letter) {
            is_valid_guess = false;
        }

        // Check if the guessed letter has already been used
        for used_letter in self.used_guesses {
            if letter == used_letter {
                is_valid_guess = false;
                break;
            }
        }

        // Update the game state based on the guess
        if is_valid_guess {
            let mut found_letter = false;
            for (i, char) in self.revealed.iter_mut().enumerate() {
                if letter == self.word[i] {
                    *char = letter;
                    found_letter = true;
                }
            }

            if !found_letter {
                self.guesses_left -= 1;
            }
        }

        // Check for victory condition
        if self.revealed.iter().all(|&c| c != '_') {
            self.victory = true;
        }

        // Update used guesses
        for (i, used_letter) in self.used_guesses.iter_mut().enumerate() {
            if *used_letter == '_' {
                *used_letter = letter;
                break;
            }
        }

        return self;
    }
}