Search Unity

3 char in string

Discussion in 'Scripting' started by gurkan, May 24, 2019.

  1. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    Hello there
    I want to get 4 letters from a word.

    used this code;
    Code (CSharp):
    1.  
    2. ////this mytext : hello
    3. ///h
    4. ///he
    5. ///hel
    6. string my_text = "hello";
    7. for(int t=0; t<2; t++)
    8. {
    9.   string one = my_text.Substring(0, t);
    10.   /////string one = my_text.Substring( change , t);
    11. }
    12.  
    if this variable works after the 3rd letter of the word.
    I want him to write.
    Code (CSharp):
    1. ///  l
    2. ///  lo
    "HELLO" will be dynamic.
    "change" will be dynamic again
    .

    How should I follow a path. Could you help?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,546
    Sorry, but I don't understand what you are asking.
     
    Joe-Censored likes this.
  3. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    It gives an error when it runs after the 3rd letter.
    I want this process to stop automatically when the word ends.

    Code (CSharp):
    1. string one = my_text.Substring( 3, t);
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,504
  5. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    14
    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):
    1. using UnityEngine;
    2.  
    3. public class MyClass : MonoBehaviour
    4. {
    5.     string my_text = "hello";
    6.     int splitIndex = 3;
    7.  
    8.     void Start()
    9.     {
    10.         // If you want to log each character from a string sepperatly
    11.         EachLetterSepperaltly(my_text);
    12.  
    13.         // If you want to log each character from a string building the word
    14.         BuildWord(my_text);
    15.  
    16.         // If you want to split the string first and build words from the remaining substrings
    17.         SplitAndBuildWord(my_text, splitIndex);
    18.     }
    19.  
    20.     void EachLetterSepperaltly (string s)
    21.     {
    22.         for (int i = 0; i < s.Length; i++)
    23.         {
    24.             Debug.Log(s[i]);
    25.         }
    26.     }
    27.  
    28.     void BuildWord(string s)
    29.     {
    30.         string temp = "";
    31.         for (int i = 0; i < s.Length; i++)
    32.         {
    33.             temp += s[i];
    34.             Debug.Log(temp);
    35.         }
    36.     }
    37.  
    38.     void SplitAndBuildWord(string s, int si)
    39.     {
    40.         string temp = "";
    41.         string substringA = s.Substring(0, si);
    42.         string substringB = s.Substring(si, s.Length - si);
    43.         for (int a = 0; a < substringA.Length; a++)
    44.         {
    45.             temp += substringA[a];
    46.             Debug.Log(temp);
    47.         }
    48.         temp = "";
    49.         for (int b = 0; b < substringB.Length; b++)
    50.         {
    51.             temp += substringB[b];
    52.             Debug.Log(temp);
    53.         }
    54.     }
    55. }
    56.  
     
  6. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    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):
    1.  
    2. void text_w()
    3.     {
    4.         int re_tex= My_question.Length -sel_char;
    5.         if(re_tex> 4)
    6.             re_tex= 4;
    7.         z = 0;
    8.         for(int t = 0; t <re_tex; t ++)
    9.         {
    10.             if(t == 0)
    11.                 answers [t] = "" + My_question [sel_char];
    12.             else
    13.                 answers [t] = My_question.Substring (sel_char, t + 1);
    14.             ++ z;
    15.         }
    16.     }
    17.  
    but in some words, he gave an error.
     
  7. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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.
     
  8. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    giving a word. player chooses letters from answers. The letters will complement the word, selecting 3 answers.
     
  9. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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".
     
  10. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    Yes that's right. but not spelling.
     
  11. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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?
     
  12. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    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.
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,356
    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):
    1. public static List<string> GetOptions(string remaining, int maxOptions)
    2. {
    3.     // TODO: check arguments
    4.  
    5.     var options = new List<string>(maxOptions);
    6.  
    7.     for (var i=0; i< maxOptions && i <remaining.Length; ++i)
    8.     {
    9.         options.Add(remaining.Substring(0, i + 1));
    10.     }
    11.  
    12.     return options;
    13. }
    The generated garbage shouldn't be a concern, since this is only going to be called when something is selected.
     
    gurkan likes this.
  14. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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.
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,356
    From what he has written so far it seems that's exactly what he wants.
     
    Last edited: May 24, 2019
  16. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    Something like this:

    upload_2019-5-24_14-6-57.png

    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".
     
    Last edited: May 24, 2019
  17. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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.
     
  18. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    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. o_O
    I'm thinking of writing the answers manually now. it will be a tiring solution for me.
    thanks for your help
     
    Last edited: May 25, 2019
  19. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    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):
    1. static void Main(string[] args)
    2.         {
    3.             string word = "Hello";                                      //THE WORD TO COMPLETE
    4.             List<string> choices = null;                                //STORES PARSED CHOICES
    5.             string remaining = word;                                    //REFERENCE THE WORD TO COMPLETE
    6.             int option = 0;                                             //STORES USER SELECTED CHOICE INDEX
    7.             string selection = null;                                    //STORES USER SELECTED CHOICE
    8.  
    9.             Console.WriteLine("Word: " + word);                         //PROMPT USER WITH WORD TO COMPLETE
    10.             while (remaining.Length > 0)                                //WHILE THERE ARE REMAINING LETTERS
    11.             {
    12.                 choices = GetOptions(remaining, 3);                     //GET CHOICES FROM REMAINING
    13.  
    14.                 Console.WriteLine("Select a choice: ");                 //PROMPT USER TO SELECT A CHOICE
    15.                 for (int i = 0; i < choices.Count; i++)                 //ITERATE ALL CHOICES
    16.                 {
    17.                     Console.WriteLine(i + ") " + choices[i]);           //PROMPT USER WITH CHOICE AT I'TH INDEX
    18.                 }
    19.  
    20.                 option = int.Parse(Console.ReadLine());                 //GET USER SELECTED INDEX
    21.                 selection = choices[option];                            //REPLACE SELECTION WITH CHOICE
    22.                 Console.WriteLine("Selected: " + selection);            //PROMPT USER WITH THE SELECTED CHOICE
    23.  
    24.                 remaining = remaining.Remove(0, selection.Length);      //REMOVE THE SELECTION FROM THE REMAINING LETTERS
    25.                 Console.WriteLine();                                    //LINE TERMINATOR
    26.                 Console.WriteLine("Remaining letters: " + remaining);   //PROMPT USER WITH REMAINING LETTERS IN WORD
    27.                 Console.WriteLine();                                    //LINE TEMRINATOR
    28.             }
    29.  
    30.             Console.WriteLine("You win!");                              //PROMPT USER WITH SUCCESSFUL COMPLETION
    31.         }
    upload_2019-5-25_3-28-57.png
     
  20. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,356
    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.
     
  21. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    11
    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...:)