Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing hangman game - Replace characters in a string
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
adriian




PostPosted: Fri Jan 11, 2019 2:13 pm   Post subject: 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.

Turing:


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
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Jan 12, 2019 9:12 am   Post subject: 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




PostPosted: Sun Jan 13, 2019 1:57 pm   Post subject: Re: RE:Turing hangman game - Replace characters in a string

Insectoid @ Sat Jan 12, 2019 9:12 am wrote:
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




PostPosted: Mon Jan 14, 2019 9:46 am   Post subject: 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




PostPosted: Yesterday at 1:06 pm   Post subject: Re: RE:Turing hangman game - Replace characters in a string

Insectoid @ Mon Jan 14, 2019 9:46 am wrote:
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 .. *))
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: