This site requires JavaScript, please enable it in your browser!
Greenfoot back

Comments for Hangman

Return to Hangman

danpostdanpost

2014/4/24

@Peach, actually I have two World subclasses: Hangman and TextFileViewer (to display the text files of the code). The only Actor subclass is the Letter class which I have as an inner class of Hangman and one Object subclass (the Words class). I used the Letter class only for the 26 Letter object that can be clicked on (the ones that turn gray after being chosen). The guessed letter (and the letter that appear in red after a lose) as well as the images used to show the drawing of the one who is being hanged are also Actor objects; but of no particular class (un-named Actor objects). I have a method in the Hangman class that is used to created and return an un-named actor object (getGenericActor).
PeachPeach

2014/4/29

sorry i don't really understand, so you don't have different classes for each body part of the hangman? eg. (right arm, right leg, head, etc)? instead, that is under the letter class?
danpostdanpost

2014/4/29

@Peach, I do not have separate classes for each body part; however, even though they are created from within the Letter class, they are not Letter actors. In fact. they are not of any particular type of Actor (they are un-named, or generic). I felt no need to place them in separate classes as once they are drawn, their type is un-important (no collision checking or anything would need to know what class they are) and there is no further action for them to take (no act, fields or methods). Their only purpose is for image display and there was only a need for Actor class methods to accomplish that. Look at the 'getGenericActor' method, which is used to return a new generic Actor object. Also, look at some of my other recent uploads and view the codes within them for more examples of creating these un-named Actor object; as well as some that are named, but coded within another class (inner classes).
PeachPeach

2014/4/30

where do you declare the variable "word"?
PeachPeach

2014/4/30

is the word class is an actor class
PeachPeach

2014/4/30

i don't understand this part of the code: static String[] words = {< WordsOmiited>} static java.util.Random r and = new java.util.Random(System.currentTimeMillis());
danpostdanpost

2014/4/30

@Peach, they are just field declaration statements for the array of words and the random number generator which is used to produce a random index for the word in the array to use in the puzzle. Getting the current time produces a 'random' seed for the random number generator. Check the java.util.Random API documentation for additional information on this type of Object. Having these field as 'static' causes them to be set prior to any Objects being created from the class (actually, they are created during compilation; and resetting does not alter them); and as these two fields will not change throughout the running of the scenario, they should probably be made 'final' as well.
PeachPeach

2014/5/1

oh ok another question, where do you declare the variable "word"? do you have a word class?
danpostdanpost

2014/5/1

The 'word' field is declared third from last before the Hangman class constructor.
PeachPeach

2014/5/5

what's an objects class
danpostdanpost

2014/5/5

@Peach, please refer to the following webpage: http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
PeachPeach

2014/5/14

how do you declare the method showLetter
PeachPeach

2014/5/14

sorry ignore my previous comment
PeachPeach

2014/5/14

how do you declare a color variable eg. black or blue
danpostdanpost

2014/5/14

@Peach, please start a new discussion if you have a question that does not pertain to a specific scenario.
PeachPeach

2014/5/14

i don't understand what this means "param alphaChar the letter to display on this new image" what is the parameter supposed to be?
danpostdanpost

2014/5/14

@Peach, you are talking about a parameter in the 'getLetterImage' method. '@param' means that the following word is the name of a parameter. 'alphaChar' is the name I gave the String parameter. 'the letter to display on this new image' tells you that the new image created within the method will contain the glyph of the letter contained in the 'alphaChar' string. The calling method, with the expressions for the arguments already evaluated, would look like this: getLetterImage("L", java.awt.Color.black); You were asking about declaring a color variable. You declare one just like you declare any other object. java.awt.Color color = null; // declare variable to hold a color color = java.awt.Color.blue; // assignment example one color = new java.awt.Color(255, 64, 40); // assignment example two If using 'import java.awt.Color;', the previous would be: Color color = null; color = Color.blue; color = new Color(255, 64, 40);
PeachPeach

2014/5/16

so I would need 26 getLetterImage methods for all 26 letters of the alphabet? eg. getLetterImage("A", java.awt.Color.black); getLetterImage("B", java.awt.Color.black); getLetterImage("C", java.awt.Color.black); etc thanks
PeachPeach

2014/5/16

when I write this, private GreenfootImage getLetterImage("A",java.awt.Color.black) a message saying "illegal start of type" keeps popping up. how do I fix it? also, in the method "return new GreenfootImage("A", 36, black, null);" what does the number 36 represent? Also,
danpostdanpost

2014/5/16

@Peach, it looks like you are missing a comma between "A" and java.awt.Color.black. And, no, you do not have to have 26 methods, one for each letter. The parameter 'alphaChar' can be any one of the alpha characters that is passed to the 'getLetterImage' method.. As far as the '36', refer to the following link: http://www.greenfoot.org/files/javadoc/greenfoot/GreenfootImage.html#GreenfootImage(java.lang.String, int, java.awt.Color, java.awt.Color)
danpostdanpost

2014/5/16

@Peach, maybe you are not missing a comma (the comma is so close to the 'j', it appears to be part of the 'j'). Please start a discussion on this issue showing how you are trying to use 'private GreenfootImage getLetterImage("A", java.awt.Color.black)'. Without context it is hard to figure out what you are trying to do.
PeachPeach

2014/5/19

I don't really understand this part of the code, could you please explain it? word = words.nextWord(); int baseX = getWidth()/2 - word.length() * 18 + 9; bg.setColor(Color.black); for (int i=0; i<word.length(); i++) { bg.fillRect(baseX+36*i, getHeight()-10, 24, 3); what is baseX?
danpostdanpost

2014/5/19

@Peach, the code you are showing gets a new random word and draws the underlines on the world background for each letter in the new word. 'baseX' is assigned the calculated x-coordinate for where the underline for the first letter in the new word is to go. Once that value is determined, the 'for' loop can draw all the underlines for all the letters in the new word in the proper positions so the word is centered along the base of the screen.
PeachPeach

2014/5/19

do you need to write "private int baseX;" under "public class WhiteBoard extends World"
danpostdanpost

2014/5/19

@Peach, what is 'WhiteBoard'? my world class is called 'Hangman'.
PeachPeach

2014/5/20

sorry typo. i meant to type "public class hangman extends World"
danpostdanpost

2014/5/20

@Peach, did I do so? if not, then no.
WatchbWatchb

2014/5/21

This is a very nice game! I was just wondering in your method "LetterPressed" what is the parameter? what is a string key? and what does this part of the code mean: letterClicked(letters[(int)key.charAt(0)-65]) In your next method, "private void letterClicked(Letter actor)" what is the parameter "letter actor"?
danpostdanpost

2014/5/21

@Watchb, in the middle section of the act method, you will see where I detect a keystroke, make it uppercase and call the 'letterClicked' method if the key is between "A" and "Z" inclusive. key.charAt(0) returns the ascii code value for the first (and only) character in the 'key' String object ('A' = 65 and 'Z' = 90); subtracting 65 from that gives a range of 0 to 25, which is the index of the Letter actor in the 'letters' array that contains the image of the 'key' letter. The 'letters' array contains the Letter actors that can be clicked on near the bottom of the screen. The Letters class can be found at the end of the Hangman class code.
KellyEmmerichKellyEmmerich

2014/5/22

Danpost, where I can see your code?
danpostdanpost

2014/5/22

@KellyEmmerich, click on the 'Run' button and click on one of the buttons in the top right.
WatchbWatchb

2014/5/23

Can I download this game onto greenfoot? I would like to take a look at your world classes and actor classes!
danpostdanpost

2014/5/23

@Watchb, just run the scenario and click on the buttons near the top-right of the window to look at the classes.
WatchbWatchb

2014/5/23

Danpost, what is the getGeneric actor method? i cannot find it in the greenfoot API
danpostdanpost

2014/5/23

@Watchb, the 'getGenericActor' method is within the Hangman class.
KellyEmmerichKellyEmmerich

2014/5/23

@Thank you very much :D
PeachPeach

2014/5/24

@Danpost, in this line of the code "addObject(letters[i] = new Letter(i), 24+38*(i%13),510+30*(i/13));" what does "24+38*(i%13),510+30*(i/13))" mean?
danpostdanpost

2014/5/24

@Peach, obviously it is a call to 'addObject', a method in the World class that uses the signature 'addObject(actor, int, int)'. The actor is a new Letter object that is being placed within the array of letters declared near the top of the class. This line is found within a 'for' loop that iterates 'i' through the numbers 0 to 25 (for each letter of the alphabet). Notice in the scenario how I split the 26 letters into two rows of 13 letters each. The first int (the x-coordinate of where to place the letter actor) is '24+38*(i%13)'. This says to start at x-coordinate 24 and add 'i' factors of 38 (the distance between each letter). Taking the modulus 13 of 'i' starts the factor back at zero again for the second row. The second int (the y-coordinate of where to place the letter actor) is '510+30*(i/13)'. This says to start at y-coordinate 510 and add a factor of 30 for the second row ('i/13' will return 0 until 'i' reaches 13; then it will return one for the rest of the loop).
PeachPeach

2014/5/24

@danpost, thank you for the explanation. If I wanted to have all the the 26 letters in 1 row, what would the code be like? do I just delete "(i%13)" and "(i/13)" from the code? so like this: addObject(letters[i] = new Letter(i), 24+38, 510+30; thank you!
danpostdanpost

2014/5/24

@Peach, if the world was wide enough to hold them, it would be 24+38*i, 510. The world width would have to be about 1000.
WatchbWatchb

2014/5/24

Danpost, is it possible to not use the getGenericActor method in the game? For example, is it possible to just create actor classes for each body part (head, right arm, right leg, body, etc)?
danpostdanpost

2014/5/24

@Watchb, absolutely.
WatchbWatchb

2014/5/24

danpost, could you please explain how you can do that? (I am quite new to java programming), thank you.
danpostdanpost

2014/5/24

Basically, you would just put the animation code for each within the 'addedToWorld' method of each class.
TotalBOSS01TotalBOSS01

2014/6/27

ill admit i failed to guess syzygy... i had never heard of that word
olenzolenz

2015/6/8

As my laptop does not run the Java plugin in a browser, I cannot access the scenario. Unfortunately, I am also unable to find the scenarios source code. Where can I find it?
danpostdanpost

2015/6/8

@olenz, the source for this scenario was not provided for download. It is, however, viewable while running the scenario. Did you try using a different browser (maybe IE)?
olenzolenz

2015/6/8

Nah, the trouble is that running a Java plugin on my laptop is disallowed administratively. So there is no difference between IE, Chrome or Firefox.
olenzolenz

2015/6/8

I managed to get it running, but I do not see how to get the source code, or even view it...
olenzolenz

2015/6/8

Oh, OK, I found it. Sorry.