- Home /
Need Help Ending A Game - Alphabet Game
I am new to programming and I have followed a video on how to create an Alphabet game in unity which displays a letter on the screen, when 5 of those letters are selected it moves onto the next letter. The issue that I am facing is that game continues on with various characters after the letter Z.
How can I get my game to end once the player has completed the letter Z rather than continuing to other characters?
Here's my code below:
public class GameController : MonoBehaviour {
public char Letter = 'a';
int _correctAnswers = 5;
int _correctClicks;
void OnEnable()
{
GenerateBoard();
UpdateDiplayLetters();
}
void GenerateBoard()
{
var clickables = FindObjectsOfType<ClickableLetter>();
List<char> charsList = new List<char>();
for (int i = 0; i < _correctAnswers; i++)
charsList.Add(Letter);
for (int i = _correctAnswers; i < clickables.Length; i++)
{
var chosenLetter = ChooseInvalidRandomLetter();
charsList.Add(chosenLetter);
}
charsList = charsList
.OrderBy(t => UnityEngine.Random.Range(0, 10000))
.ToList();
for (int i = 0; i < clickables.Length; i++)
{
clickables[i].SetLetter(charsList[i]);
}
FindObjectOfType<RemainingCounterText>().SetRemaining(_correctAnswers - _correctClicks);
}
internal void HandleCorrectLetterClick(bool upperCase)
{
_correctClicks++;
FindObjectOfType<RemainingCounterText>().SetRemaining(_correctAnswers - _correctClicks);
if (_correctClicks >= _correctAnswers)
{
Letter++;
UpdateDiplayLetters();
_correctClicks = 0;
GenerateBoard();
}
}
private void UpdateDiplayLetters()
{
foreach (var displayletter in FindObjectsOfType<DisplayLetter>())
{
displayletter.SetLetter(Letter);
}
}
private char ChooseInvalidRandomLetter()
{
int a = UnityEngine.Random.Range(0, 26);
var randomLetter = (char)('a' + a);
while (randomLetter == Letter)
{
a = UnityEngine.Random.Range(0, 26);
randomLetter = (char)('a' + a);
}
return randomLetter;
}
}
Answer by sacredgeometry · Dec 01, 2020 at 03:12 AM
You could put all your letters/ chars in a queue instead of a list and then just dequeue them until you have an empty queue.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1?view=net-5.0
Here is an example implementation with some testing code, take it with a pinch of salt as you should avoid Linq in unity where possible, it was just here for terseness:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class AlphabetGame : $$anonymous$$onoBehaviour
{
private readonly IList<char> _alphabet = "ABCDEFGHIJKL$$anonymous$$NOPQRSTUVWXYZ".ToCharArray().ToList();
private Queue<char> _letters;
void Start()
{
_letters = new Queue<char>(_alphabet.OrderBy( x => Random.value ));
}
void LogQueue()
{
Debug.Log($"Queued: {string.Join(",", _letters)}");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(_letters.Any())
{
LogQueue();
var nextLetter = _letters.Dequeue();
Debug.Log($"Next Letter: {nextLetter}");
}
else
{
Debug.Log("There are no more letters!");
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613020658im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Magnet effect to draw coins,Magnetic effect to draw coins 5 Answers
Don't Destroy on Load not working for me 1 Answer
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer