Hello there I want to get 4 letters from a word. used this code; Code (CSharp): ////this mytext : hello ///h ///he ///hel string my_text = "hello"; for(int t=0; t<2; t++) { string one = my_text.Substring(0, t); /////string one = my_text.Substring( change , t); } if this variable works after the 3rd letter of the word. I want him to write. Code (CSharp): /// l /// lo "HELLO" will be dynamic. "change" will be dynamic again. How should I follow a path. Could you help?
It gives an error when it runs after the 3rd letter. I want this process to stop automatically when the word ends. Code (CSharp): string one = my_text.Substring( 3, t);
You can just check the length of the string to make sure you don't try to go past it. Though I'm not sure if I really understand your question. https://docs.microsoft.com/en-us/dotnet/api/system.string.length?view=netframework-4.8
Hello, I think you want to split the string first and then log the 2 substrings character by character and build up the word ? Code (CSharp): using UnityEngine; public class MyClass : MonoBehaviour { string my_text = "hello"; int splitIndex = 3; void Start() { // If you want to log each character from a string sepperatly EachLetterSepperaltly(my_text); // If you want to log each character from a string building the word BuildWord(my_text); // If you want to split the string first and build words from the remaining substrings SplitAndBuildWord(my_text, splitIndex); } void EachLetterSepperaltly (string s) { for (int i = 0; i < s.Length; i++) { Debug.Log(s[i]); } } void BuildWord(string s) { string temp = ""; for (int i = 0; i < s.Length; i++) { temp += s[i]; Debug.Log(temp); } } void SplitAndBuildWord(string s, int si) { string temp = ""; string substringA = s.Substring(0, si); string substringB = s.Substring(si, s.Length - si); for (int a = 0; a < substringA.Length; a++) { temp += substringA[a]; Debug.Log(temp); } temp = ""; for (int b = 0; b < substringB.Length; b++) { temp += substringB[b]; Debug.Log(temp); } } }
not a complete answer. I'm doing a game for kids. my question is just "hello". answers: h - he - hel will have 3 options. If "he" was chosen. new answers: l - ll - llo. (remaining letters) So this is a completion game. My problem. short or long. or the last two letters, there must be only two answers. ( l - lo) a difficult logic. can not find a solution. This code helped me a little.: Code (CSharp): void text_w() { int re_tex= My_question.Length -sel_char; if(re_tex> 4) re_tex= 4; z = 0; for(int t = 0; t <re_tex; t ++) { if(t == 0) answers [t] = "" + My_question [sel_char]; else answers [t] = My_question.Substring (sel_char, t + 1); ++ z; } } but in some words, he gave an error.
The player is presented with the question 'hello'? I do not understand. Why is the following an answer? h - he - hel I am trying to understand your problem.
giving a word. player chooses letters from answers. The letters will complement the word, selecting 3 answers.
So the given word is "hello." How does h - he - hel compliment 'hello'? Moreover, if the player selects 'he'. Why are new answers l - ll - llo? This all looks like the player is spelling the given word "hello".
I am sorry, but I still do not understand the objective. Is the first answer ever wrong? Word: "Hello" Choices: "h, he, hel" Selection 1: "H" Choices: "e, el, ell" Selection: "el" Choices: "l, ll, llo" Selection "llo" You win?
Yeah. The game will work this way. I can't pull these answers dynamically. Word: "Hello" Choices: "h, he, hel" Selection 1: "H" Choices: "e, el, ell" Selection: "ell" ////////If this is selected. Choices: "o" ///answer is only one... this is my problem. not 3 answers Selection "o" You win? If one letter remains, there will be only one answer. I can't do it dynamically.
It seems you can never lose that game, since anything that's given as an "answer" is correct. Even though that's just for kids, what's the actual purpose? Anyways, quick and easy solution is the following: Code (CSharp): public static List<string> GetOptions(string remaining, int maxOptions) { // TODO: check arguments var options = new List<string>(maxOptions); for (var i=0; i< maxOptions && i <remaining.Length; ++i) { options.Add(remaining.Substring(0, i + 1)); } return options; } The generated garbage shouldn't be a concern, since this is only going to be called when something is selected.
Then there is a specific number of attempts to complete a word, otherwise you get to the point where there is only one or two choices. I am thinking the word can be extracted into a data tree, which would allow you to get the max number of selections before you lose, and it would also allow you to get a minimum for words to get a high score. This seems like an interesting problem. Ill play around with it a bit.
Something like this: If the child does not contain three elements then you lose. So, we know hot many solutions, if more than one exist, if the player selects the one of the choices. The letter 'H' in 'Hello' only has one possible solution. Since this is a game for children, it is up to the gameplay to break the bad news to them in a pleasant way. I do not know if you want a game over screen if the player selects "H"->"el".
Maybe you are right. However, In that implies that the player can never lose. Since it is for kids, maybe that is what is wanted.
Thanks for your help Yeah. Don't lose in the game. only, word and letter practice. but they have to complete the word with their choice. SUDDOHA : Your c # code. it's no different than what I wrote. but I still can't dynamically read the word completion. I'm thinking of writing the answers manually now. it will be a tiring solution for me. thanks for your help
SUDDOHA's loop works fine. Not the most optimal solution, but like SUDDOHA said, only processes when the user clicks. This is a console application for quick prototype, and I am not sure how you will present the data to the user. Code (CSharp): static void Main(string[] args) { string word = "Hello"; //THE WORD TO COMPLETE List<string> choices = null; //STORES PARSED CHOICES string remaining = word; //REFERENCE THE WORD TO COMPLETE int option = 0; //STORES USER SELECTED CHOICE INDEX string selection = null; //STORES USER SELECTED CHOICE Console.WriteLine("Word: " + word); //PROMPT USER WITH WORD TO COMPLETE while (remaining.Length > 0) //WHILE THERE ARE REMAINING LETTERS { choices = GetOptions(remaining, 3); //GET CHOICES FROM REMAINING Console.WriteLine("Select a choice: "); //PROMPT USER TO SELECT A CHOICE for (int i = 0; i < choices.Count; i++) //ITERATE ALL CHOICES { Console.WriteLine(i + ") " + choices[i]); //PROMPT USER WITH CHOICE AT I'TH INDEX } option = int.Parse(Console.ReadLine()); //GET USER SELECTED INDEX selection = choices[option]; //REPLACE SELECTION WITH CHOICE Console.WriteLine("Selected: " + selection); //PROMPT USER WITH THE SELECTED CHOICE remaining = remaining.Remove(0, selection.Length); //REMOVE THE SELECTION FROM THE REMAINING LETTERS Console.WriteLine(); //LINE TERMINATOR Console.WriteLine("Remaining letters: " + remaining); //PROMPT USER WITH REMAINING LETTERS IN WORD Console.WriteLine(); //LINE TEMRINATOR } Console.WriteLine("You win!"); //PROMPT USER WITH SUCCESSFUL COMPLETION }
That's what you think, but that's not true. The second snippet you posted and the one that I posted follow the same approach, but in a different way. I've written it in a way that it solely depends on its arguments, whereas yours takes fields into account as well. However yours definitely suffers from a small bug and you can also simplify it, that's what I've already done for you though. As for the small bug, you're iterating up to t=4. That is, you process the body of the loop for t=0, t=1, t=2, t=3, 4 times in total. The first thing that comes to mind is that your array is simply of length 3, since you only want 3 answers in total. So your loop would attempt to access the array with index 3, which is out of range. Perhaps that's the error? We don't know... The next potential source of errors is your variable sel_char, which is probably the number of selected characters (in total?). If there's a bug in that calculation, your method might fail as well. Perhaps this is the error? We don't know either... So, there are just wild guesses, you have to include error / exception messages / information. That'll make things a lot easier. All I can tell is that the code I posted definitely works. If it did not work for you, you've passed wrong arguments.
Yes it works. thank you so much. (SUDDOHA) I'm sorry. I found my fault. it works very nicely. if the word consists of 2 letters. getting error. I was getting this error in the SUDDOHA list operation. now works very nicely. thank you so much. SGM3 and SUDDOHA I will share with you when the application is finished...