
-----------------------------------
adriian
Fri Jan 11, 2019 2:13 pm

Turing hangman game - Replace characters in a string
-----------------------------------
What is it you are trying to achieve?
I'm very new to coding and I'm trying to make a hangman game.


What is the problem you are having?
I can't figure out how to replace certain characters in the variable "dashes" with the correct letters that the user inputs.


Describe what you have tried to solve this problem
Nothing as I don't know where to go with this.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please see the comments in my code below for further details.



var word, letter, displayAnswer, dashes : string
displayAnswer := ""
dashes := ""
put "Welcome to Hangman by Samson."
put "What word would you like to guess? " ..
get word
for i : 1 .. length (word)
    dashes := dashes + "-" %Dashes will be replaced with the correct letters that they guess
end for
loop
    exit when (displayAnswer = word)

    put "Please enter a letter to guess " ..
    get letter

    for x : 1 .. length (word)
        if letter = word (x) then
            dashes := dashes + letter %Here, I want to make it so that if their letter is within the word chosen, it will replace the dashes with their letter at the same position.
            put dashes

        else
            displayAnswer := displayAnswer + "-"
            put displayAnswer

        end if
    end for
end loop




Please specify what version of Turing you are using
Turing 4.1.1

-----------------------------------
Insectoid
Sat Jan 12, 2019 9:12 am

RE:Turing hangman game - Replace characters in a string
-----------------------------------
Look up substring in the Turing documentation to learn how to break a string into pieces. From there, it's as simple as dashes := a piece of word + letter + another piece of word

-----------------------------------
adriian
Sun Jan 13, 2019 1:57 pm

Re: RE:Turing hangman game - Replace characters in a string
-----------------------------------
Look up substring in the Turing documentation to learn how to break a string into pieces. From there, it's as simple as dashes := a piece of word + letter + another piece of word
I looked into the substring page and I swear to god I've been trying to figure this out for 30+ minutes but I really can't understand how to do this.

-----------------------------------
Insectoid
Mon Jan 14, 2019 9:46 am

RE:Turing hangman game - Replace characters in a string
-----------------------------------
Haha, welcome to computer science. 30 minutes is nothing in this world. 

I did make a mistake in my previous post, I should have written dashes := a piece of dashes + letter + another piece of dashes.

-----------------------------------
adriian
Wed Jan 16, 2019 1:06 pm

Re: RE:Turing hangman game - Replace characters in a string
-----------------------------------
Haha, welcome to computer science. 30 minutes is nothing in this world. 

I did make a mistake in my previous post, I should have written dashes := a piece of dashes + letter + another piece of dashes.
Okay I came up with this, but it's saying "Left bound of substring is less than 1"

if letter = word (x) then 
dashes := (dashes (x .. x - 1)) + letter + (dashes (x - 1 .. *))

-----------------------------------
Insectoid
Thu Jan 17, 2019 8:19 am

RE:Turing hangman game - Replace characters in a string
-----------------------------------
If x=1, what is dashes (x..x-1)?

Don't you think you should start from the beginning of dashes instead of the xth letter?

-----------------------------------
adriian
Thu Jan 17, 2019 2:23 pm

Re: RE:Turing hangman game - Replace characters in a string
-----------------------------------
If x=1, what is dashes (x..x-1)?

Don't you think you should start from the beginning of dashes instead of the xth letter?

You are very right! I finally figured it out with the help of:
dashes := (dashes (1 .. x - 1)) + letter + (dashes (x + 1 .. *))

The only issue is when you use a word with multiple letters that are the same, it prints off multiple copies with only one being right.
For example:
What word would you like to guess?
Mohammed
Letter?
M

Output: 
m-------   