How to Write a Hangman Program in Java Substring

  Turing hangman game - Replace characters in a string
Author Message
adriian


Post Posted: 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




Post Posted: 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


Post Posted: 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




Post Posted: 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


Post Posted: Wed Jan 16, 2019 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 .. *))

Insectoid




Post Posted: Thu Jan 17, 2019 8:19 am Post subject: 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


Post Posted: Thu Jan 17, 2019 2:23 pm Post subject: Re: RE:Turing hangman game - Replace characters in a string

Insectoid @ Thu Jan 17, 2019 8:19 am wrote:

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------- <-- I need to not display this
m---m--- <-- Or this
m---mm-- <-- This is the correct output. Display this.

Thank you so so much for the help so far btw. I actually could not have done this without you

Insectoid




Post Posted: Fri Jan 18, 2019 11:49 am Post subject: RE:Turing hangman game - Replace characters in a string

Fixing that problem only needs a few tweaks to your code, I'm sure you'll figure out.

Sponsor
Sponsor
Sponsor
sponsor
adriian


Post Posted: Fri Jan 18, 2019 1:21 pm Post subject: Re: RE:Turing hangman game - Replace characters in a string

Insectoid @ Fri Jan 18, 2019 11:49 am wrote:

Fixing that problem only needs a few tweaks to your code, I'm sure you'll figure out.


I did in fact figure it out, I was outputting the word in a for loop.
Thanks again, I honestly really really REALLY appreciate the help.

Display posts from previous:  

How to Write a Hangman Program in Java Substring

Source: http://compsci.ca/v3/viewtopic.php?t=40686

0 Response to "How to Write a Hangman Program in Java Substring"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel