Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

HELP: Tabs Instead of spaces

Discussion in 'General Discussion' started by EilexInteractive, Nov 20, 2019.

  1. EilexInteractive

    EilexInteractive

    Joined:
    Dec 6, 2018
    Posts:
    5
    Hi All!

    I'm making a hangman game for a uni project. I'm storing the characters of the word & the characters guessed in list. When I print the list I'm printing it to a string. when the string prints the the letter is wrong I add a space however instead of the string just having 1 space it seems to be added 4 or more.

    can somebody help me with this URGENTLY
    Code (CSharp):
    1. public void SetCharUI() {
    2.         WrongL = "";
    3.         Debug.Log(GuessedWrong.Count);
    4.         for (int i = 0; i < GuessedWrong.Count; i++) {
    5.            
    6.             WrongL += GuessedWrong[i] + " ";
    7.            
    8.         }
    9.  
    10.         GuessedWord = "";
    11.         for (int j = 0; j < Characters.Count; j++) {
    12.             string c = Characters[j].ToString();
    13.             for (int n = 0; n < GuessedCorrect.Count; n++) {
    14.                 string a = GuessedCorrect[n].ToString();
    15.                 if (c == a) {
    16.                     GuessedWord += c;
    17.                 } else {
    18.                     GuessedWord += " ";
    19.                 }
    20.             }
    21.         }
    22.  
    23.         DisplayedWord.text = GuessedWord;
    24.         WrongLetters.text = WrongL;
    25.  
    26.     }
     
  2. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    Nested for loop could be the issue I feels.
     
  3. EilexInteractive

    EilexInteractive

    Joined:
    Dec 6, 2018
    Posts:
    5
    What do you think the best way to fix it is?
     
  4. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    Don't know for sure but maybe echo the space printout " " in the first for loop rather than the nested one.
     
  5. EilexInteractive

    EilexInteractive

    Joined:
    Dec 6, 2018
    Posts:
    5
    Sorry I've said print. I meant that I'm placing the string into a text object, would that make a difference?
     
  6. sxa

    sxa

    Joined:
    Aug 8, 2014
    Posts:
    741
    So for each letter in the original string
    you compare it against each and every letter of the guess string.
    And if it doesnt match the you print a space.

    But you dont know why you have more than one space?

    If you have 4 letters in the words, how many times do you check the individual letters of your guess against the first letter of the hidden word?

    How many times could it match?
    How many times could it not match?
     
    angrypenguin likes this.