fix words puzzle level2
Some checks failed
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Waiting to run
Build project / Publish to itch.io (StandaloneLinux64) (push) Blocked by required conditions
Build project / Publish to itch.io (StandaloneWindows64) (push) Blocked by required conditions
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Has been cancelled

This commit is contained in:
EthanPisani 2025-03-29 21:24:56 -04:00
parent 072d1a04fc
commit 1aa187a019

View File

@ -5,6 +5,7 @@ using System.Linq;
using TMPro; using TMPro;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using UnityEngine.UI; using UnityEngine.UI;
public enum WGameState public enum WGameState
{ {
Uninitialized, Uninitialized,
@ -12,70 +13,42 @@ public enum WGameState
Win, Win,
Lose Lose
} }
public class WordPuzzle : MonoBehaviour public class WordPuzzle : MonoBehaviour
{ {
private TMP_Text[] allLetterTexts; private TMP_Text[] allLetterTexts;
private List<TMP_Text> usedLetterTexts = new List<TMP_Text>(); private List<TMP_Text> usedLetterTexts = new List<TMP_Text>();
private List<string> words = new List<string>(); private List<string> words = new List<string>();
private int currentCount; private int currentCount;
private int currentSubmit = 0; private int currentSubmit = 0;
public GameObject congratulations; public GameObject congratulations;
public GameObject retry; public GameObject retry;
public TMP_Text retryText; public TMP_Text retryText;
public Color inWordColor = Color.yellow; public Color inWordColor = Color.yellow;
public Color perfectMatchColor = Color.green; public Color perfectMatchColor = Color.green;
public Color wrongColor = Color.red; public Color wrongColor = Color.red;
public string currentWord = string.Empty; public string currentWord = string.Empty;
public WGameState gameState = WGameState.Uninitialized; public WGameState gameState = WGameState.Uninitialized;
[SerializeField] private TextAsset textAsset = null; [SerializeField] private TextAsset textAsset = null;
public AudioClip loseSound; public AudioClip loseSound;
public AudioClip winSound; public AudioClip winSound;
private void Awake() private void Awake()
{ {
allLetterTexts = GetComponentsInChildren<TMP_Text>(); allLetterTexts = GetComponentsInChildren<TMP_Text>();
foreach (var text in allLetterTexts) foreach (var text in allLetterTexts)
{ {
text.text = string.Empty; text.text = string.Empty;
} }
// var textAsset = Resources.Load<TextAsset>("words_alpha");
words = Regex.Split(textAsset.text, Environment.NewLine).ToList(); words = Regex.Split(textAsset.text, Environment.NewLine).ToList();
if (currentWord == string.Empty) { if (string.IsNullOrEmpty(currentWord)) {
currentWord = words[UnityEngine.Random.Range(0, words.Count)]; currentWord = words[UnityEngine.Random.Range(0, words.Count)];
Debug.Log("Choose a random word: " + currentWord); Debug.Log("Chosen random word: " + currentWord);
} }
gameState = WGameState.Uninitialized; gameState = WGameState.Uninitialized;
}
private void HandleInput()
{
// Debug.Log("HandleInput");
foreach (char c in Input.inputString)
{
if (c == '\b')
{
RemoveChar();
}
else if ((c == '\n') || (c == '\r'))
{
SubmitButtonClick();
}
else if (char.IsLetter(c))
{
DoKeyPress(char.ToLower(c));
}
}
} }
private void Update() private void Update()
@ -84,8 +57,31 @@ public class WordPuzzle : MonoBehaviour
{ {
HandleInput(); HandleInput();
} }
} }
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
RemoveChar();
}
else if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
{
SubmitButtonClick();
}
else
{
foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(key) && key >= KeyCode.A && key <= KeyCode.Z)
{
DoKeyPress(key.ToString().ToLower()[0]);
break;
}
}
}
}
public void SetGameState(WGameState state) public void SetGameState(WGameState state)
{ {
gameState = state; gameState = state;
@ -99,7 +95,6 @@ public class WordPuzzle : MonoBehaviour
private void RemoveChar() private void RemoveChar()
{ {
print("remove");
if (!usedLetterTexts.Any() || currentCount <= 0) if (!usedLetterTexts.Any() || currentCount <= 0)
return; return;
@ -110,25 +105,21 @@ public class WordPuzzle : MonoBehaviour
private void DoKeyPress(char character) private void DoKeyPress(char character)
{ {
print(character);
if (usedLetterTexts.Count >= allLetterTexts.Length || currentCount >= 5) if (usedLetterTexts.Count >= allLetterTexts.Length || currentCount >= 5)
return; return;
var currentText = allLetterTexts[usedLetterTexts.Count]; var currentText = allLetterTexts[usedLetterTexts.Count];
currentText.text = character.ToString(); currentText.text = character.ToString();
usedLetterTexts.Add(currentText); usedLetterTexts.Add(currentText);
currentCount++; currentCount++;
} }
private void SubmitButtonClick() private void SubmitButtonClick()
{ {
print("submit");
if (currentCount != 5) if (currentCount != 5)
return; return;
var submittedWord = string.Empty; var submittedWord = string.Empty;
int ii = 0; int ii = 0;
for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++) for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++)
{ {
@ -137,47 +128,34 @@ public class WordPuzzle : MonoBehaviour
submittedWord += textsChar; submittedWord += textsChar;
texts.color = wrongColor; texts.color = wrongColor;
if (currentWord.Contains(textsChar)) if (currentWord.Contains(textsChar))
{ {
texts.color = inWordColor; texts.color = inWordColor;
if (textsChar == currentWord[ii]) if (textsChar == currentWord[ii])
texts.color = perfectMatchColor; texts.color = perfectMatchColor;
} }
ii++; ii++;
} }
print(submittedWord);
if (words.Contains(submittedWord)) if (words.Contains(submittedWord))
{ {
if(submittedWord == currentWord) if(submittedWord == currentWord)
{ {
congratulations.SetActive(true); congratulations.SetActive(true);
print("success!");
SetGameState(WGameState.Win); SetGameState(WGameState.Win);
SoundFXManager.instance.PlaySound(winSound, transform, 1.0f); SoundFXManager.instance.PlaySound(winSound, transform, 1.0f);
return; return;
} }
print("reset count");
currentSubmit++; currentSubmit++;
currentCount = 0; currentCount = 0;
} }
else else
{ {
print("fail!");
for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++) for (int i = 0 + (currentSubmit * 5); i < usedLetterTexts.Count; i++)
{ {
TMP_Text texts = usedLetterTexts[i]; TMP_Text texts = usedLetterTexts[i];
texts.color = Color.white; texts.color = Color.white;
} }
} }
if(currentSubmit == 6) if(currentSubmit == 6)
@ -192,25 +170,19 @@ public class WordPuzzle : MonoBehaviour
public void ResetPuzzle() public void ResetPuzzle()
{ {
allLetterTexts = GetComponentsInChildren<TMP_Text>(); allLetterTexts = GetComponentsInChildren<TMP_Text>();
retry.SetActive(false); retry.SetActive(false);
congratulations.SetActive(false); congratulations.SetActive(false);
usedLetterTexts.Clear(); usedLetterTexts.Clear();
foreach (var texts in allLetterTexts) foreach (var texts in allLetterTexts)
{ {
texts.text = string.Empty; texts.text = string.Empty;
texts.color = Color.white; texts.color = Color.white;
} }
currentSubmit = 0; currentSubmit = 0;
currentCount = 0; currentCount = 0;
if (string.IsNullOrEmpty(currentWord)) {
// currentWord = words[UnityEngine.Random.Range(0, words.Count)];
if (currentWord == string.Empty) {
currentWord = words[UnityEngine.Random.Range(0, words.Count)]; currentWord = words[UnityEngine.Random.Range(0, words.Count)];
Debug.Log("Choose a random word: " + currentWord); Debug.Log("Chosen random word: " + currentWord);
} }
SetGameState(WGameState.Playing); SetGameState(WGameState.Playing);
} }