Wordle in AGS?

Started by tilapisha, Wed 22/03/2023 15:27:35

Previous topic - Next topic

tilapisha

Hi, I am trying to create Wordle in my game. I've finished most of it thanks to this [link] https://www.adventuregamestudio.co.uk/forums/beginners-technical-questions/wordle-in-ags/msg636645648/#msg636645648 [/link] However, if you do not guess the word by the last row, I'd like the game to reset.

Code: ags
// main global script file
#define WORD_LENGTH 5
#define MAX_GUESSES 6
 
// This represents a row in the Wordle grid (one guess)
struct WordleGuess
{
   Button* LetterField[WORD_LENGTH];
   import String ToString();  
};
 
String WordleGuess::ToString()
{
  return String.Format("%s%s%s%s%s", this.LetterField[0].Text, this.LetterField[1].Text, this.LetterField[2].Text, this.LetterField[3].Text, this.LetterField[4].Text); 
}

enum WordleFieldState
{
  eWordleFieldBlank = 0,      // Nothing entered
  eWordleFieldEntry,          // Filled field in current row during editing
  eWordleFieldInvalid,        // Feedback that the word is invalid
  eWordleFieldGray,           // A gray field in a submitted row (letter not in word)
  eWordleFieldYellow,         // A yellow field in a submitted row (letter in word in other position)
  eWordleFieldGreen           // A green field in a submitted row (letter in word in same position)
};

// This is our wordle grid: a set of Wordle rows
WordleGuess WordleGrid[MAX_GUESSES];
 
int currentGuess;
int currentInputLetter;

// This function just links up the buttons of the GUI with our Wordle grid
void SetupWordle()
{
  int control;
  for(int i=0; i<MAX_GUESSES; i++)
  {
    for(int j=0; j<WORD_LENGTH; j++)
    {
      WordleGrid[i].LetterField[j] = gWordle.Controls[control].AsButton;
      control++;
    }
  }
}

       bool checkValid(String guess)
      {
        // TODO: Just a placeholder. Need to add check against dictionary of valid guesses
        return true;
       }
       
  WordleFieldState[] checkGuess(String guessWord, String solutionWord)
{
  if(guessWord.Length != WORD_LENGTH || solutionWord.Length != WORD_LENGTH)
    AbortGame("Invalid Wordle data");
 
  WordleFieldState format[];
  format = new WordleFieldState[WORD_LENGTH];
 
  // Make sure we ignore case:
  guessWord = guessWord.LowerCase();
  solutionWord = solutionWord.LowerCase();
 
  // Color the green fields (and the other fields gray):
  for(int i=0; i<WORD_LENGTH; i++)
  {
    if(guessWord.Chars[i] == solutionWord.Chars[i])
    {
      format[i] = eWordleFieldGreen;
      solutionWord = solutionWord.ReplaceCharAt(i, ' ');  // Remove the letter from the solution so we don't match on it again
    }
    else
      format[i] = eWordleFieldGray;
  }
 
  // Color the yellow fields:
  for(int i=0; i<WORD_LENGTH; i++)
  {
    if(format[i] != eWordleFieldGreen) // Don't process letters that are already matches
    {
      int hitIndex = solutionWord.IndexOf(String.Format("%c",guessWord.Chars[i]));
      if(hitIndex != -1) // if letter in word
      {
        format[i] = eWordleFieldYellow;
        solutionWord = solutionWord.ReplaceCharAt(hitIndex, ' ');
      }
    }
  }
 
  return format;
}

if(gWordle.Visible)
  {
    int j=0;
    
    if(keycode >= eKeyA && keycode <= eKeyZ) // Typed a letter
    {
      if(currentInputLetter<WORD_LENGTH)
      {
        WordleGrid[currentGuess].LetterField[currentInputLetter].Text = String.Format("%c", keycode);
        //WordleGrid[currentGuess].LetterField[currentInputLetter].BackgroundColor = Game.GetColorFromRGB(0, 0, 0);
        currentInputLetter++;
      }
    }
    else if(keycode == eKeyBackspace) // Remove the last letter
    {
      if(currentInputLetter>0)
      {
        currentInputLetter--;
        WordleGrid[currentGuess].LetterField[currentInputLetter].Text = "";        
      }
    }
   else if(keycode == eKeyReturn)
    {
      if(currentInputLetter == WORD_LENGTH) 
      {
        String solution = "GUMMY";
        String guess = WordleGrid[currentGuess].ToString();
        if(checkValid(guess))
        {
          // Update state
          WordleFieldState formatRow[] = checkGuess(guess, solution);
          for(int i=0; i<WORD_LENGTH; i++)
          {
            switch(formatRow[i])
            {
              case eWordleFieldGreen:
                WordleGrid[currentGuess].LetterField[i].TextColor = Game.GetColorFromRGB(0, 128, 0);
                break;
              case eWordleFieldYellow:
                WordleGrid[currentGuess].LetterField[i].TextColor = Game.GetColorFromRGB(128, 128, 0);
                break;
              case eWordleFieldGray:
              default:
                WordleGrid[currentGuess].LetterField[i].TextColor = Game.GetColorFromRGB(200, 200, 200);
                break;
            }
          }
        
        if(guess.UpperCase() == solution.UpperCase())
        {
          SetTimer(1, 30);
          cGoby.LoseInventory(iSnowglobe0);
          cGoby.LoseInventory(iSnowglobe1);
          cGoby.LoseInventory(iSnowglobe2);
          cGoby.LoseInventory(iSnowglobe3);
          cGoby.LoseInventory(iSnowglobe4);
          cGoby.LoseInventory(iSnowglobe5);
          cGoby.AddInventory(iSnowglobes);
          cGoby.AddInventory(iSwimGear);
          aAlert.Play(eAudioPriorityLow);
          
        }
        
          currentGuess++;
          currentInputLetter=0;
          
      
          if(currentGuess >= MAX_GUESSES)
          {
      // THIS IS WHERE I NEED IT TO RESET, I can reset currentGuess to 0 here, but also need to reformat

          }
        }
      }
    }
  }
}


Snarky

To reset the game, I think you just have to blank out all the WordleGuesses in WordleGrid:

Code: ags
for(int i=0; i<MAX_GUESSES;i++)
{
  for(int j=0; j<WORD_LENGTH; j++)
  {
    Button* b = WordleGrid[i].LetterField[j];
    b.Text = "";
    b.TextColor = Game.GetColorFromRGB(0, 0, 0); // Set this to whatever color you want to type with
  }
}

tilapisha

Yes, this worked! Thank you. Just had to change the variable j to k, and also make currentGuesses = 0.

SMF spam blocked by CleanTalk