I have all files, how to combine them together? a. Add all the pieces from your
ID: 3535251 • Letter: I
Question
I have all files, how to combine them together?
a. Add all the pieces from your previous projects: alphabet panel, person, and guess phrase
b. Coordinate the user input to appropriately change the alphabet panel, the person, and the guess phrase
based on each letter pressed. Hint: you may need to make your HangmanGame a JFrame as well.
Modify AlphabetPanel to turn incorrect letters a different color (like red) after they have been
guessed. Similarly, correct letter guesses should also be turned a different color (like green) after being
entered by the user. Hint: you may need to move the AlphabetPanel’s key listener into your
HangmanGame class.
c. Ask the user if they want to play again. If they do, reset the guess phrase to a new phrase and reset the
rest of the game.
d. NOTES:
i. Clicking in the window should not advance the person (so delete the mouse event handling you
previously had in person)
ii. Only incorrect letters should make a part of the person show up and ONLY the first time the
incorrect letter is pressed (pressing the same incorrect letter over and over should not cause any
more body parts to show).
3. Be sure to comment everything
a. Each of your methods (including their parameters).
a. Anything else that needs explanation.
b. Use proper indentation
c. Use appropriate variable/field names
d. Group like things together (itemsthat are part of the same objects)
e. Use appropriate comments – including comments for each method
f. Check your curly braces
4. For this project you will submit the following (see “Saving and Submitting your Work†for details):
a. A general archive file (e.g. a zip)
b. A runnable JAR file
c. At least one JPG showing your application
5. EXTRA CREDIT: Add comments for correct/incorrect answers. NOTE: You can use two files for this and
reuse your RandomString class from Project 5. Display a corresponding comment from the person being
hung, negative if the user/player guessed wrong, positive or encouraging if the user/player got it right.
Hints / tips
• For the comments positive and negative you can reuse your RandomString class from Project 5
• When incorporating multiple panels in a single frame, you will need to set a layout scheme.
• As part of project 6 you need to make the logic of the game work properly as well as ask the user if they
would like to play again. You may find it useful to use the following logic:
if person or phrase is revealed
Explanation / Answer
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.lang.reflect.Array;
public class hangman extends Applet implements ActionListener{
static final int DEAD=13; // amount of errors till loss
private int errors; // amount of errors
private String message; // error or victorie
private String information; // information of the message
private String rword; // the rword
private StringBuffer gword;// the gword
private Button bStart; // Button "Restart"
private Button bGo; // Button "Go"
private TextField tfLetter; // letter box
private Font fnt; // common font
public void init(){
fnt = new Font( "Monospaced", 0, 12 );
setFont(fnt);
// Create textbox for guess letter
tfLetter = new TextField();
// Create buttons and labels
bStart = new Button("Restart");
bGo = new Button("Go");
// Add the graphical elements to the applet
add(bStart);
add(new Label("Guess a letter:"));
add(tfLetter);
add(bGo);
// Buttons are events:
bStart.addActionListener(this);
bGo.addActionListener(this);
// Start first game
initGame();
}
public void initGame(){
/* Setting the errors to 0 */
errors=0;
/* Enter the wordslist, separated by a | here: */
String str = "computer|radio|calculator|teacher|bureau|police|geometry|president|subject|country|enviroment|classroom|animals|province|month|politics|puzzle|instrument|kitchen|language|vampire|ghost|solution|service|software|virus25|security|phonenumber|expert|website|agreement|support|compatibility|advanced|search|triathlon|immediately|encyclopedia|endurance|distance|nature|history|organization|international|championship|government|popularity|thousand|feature|wetsuit|fitness|legendary|variation|equal|approximately|segment|priority|physics|branche|science|mathematics|lightning|dispersion|accelerator|detector|terminology|design|operation|foundation|application|prediction|reference|measurement|concept|perspective|overview|position|airplane|symmetry|dimension|toxic|algebra|illustration|classic|verification|citation|unusual|resource|analysis|license|comedy|screenplay|production|release|emphasis|director|trademark|vehicle|aircraft|experiment";
String[] temp;
/* delimiter */
String delimiter = "\|";
/* given string will be split by the argument delimiter provided. */
temp = str.split(delimiter);
/* Setting the seed */
Random randomGenerator = new Random();
/* Generating random number */
int randomInt = randomGenerator.nextInt(temp.length);
rword = new String(temp[randomInt]);
char positions[] = new char[rword.length()];
for (int i=0; i<rword.length(); i++) {
positions[i] = '.';
}
String s = new String(positions);
gword = new StringBuffer(s);
tfLetter.setText("");
// Delete the messages
message="";
information = "";
repaint();
}
public void paint(Graphics g) {
// Draw the hangman
int baseY = 250;
if (errors > 0){ // ground
g.drawLine(90, baseY,200,baseY);
}
if (errors > 1){ // bar up
g.drawLine(125,baseY,125,baseY-100);
}
if (errors > 2){
g.drawLine(110,baseY,125,baseY-15);
}
if (errors > 3){
g.drawLine(140,baseY,125,baseY-15);
}
if (errors > 4){ // side bar
g.drawLine(125,baseY-100,175,baseY-100);
}
if (errors > 5){
g.drawLine(125,baseY-85,140,baseY-100);
}
if (errors > 6){ // rope
g.drawLine(175,baseY-100,175,baseY-75);
}
if (errors > 7){ // body
g.drawOval(170,baseY-75,10,12);
}
if (errors > 8){
g.drawOval(170,baseY-65,15,25);
}
if (errors > 9){ // arms
g.drawLine(160,baseY-65,170,baseY-60);
}
if (errors > 10){
g.drawLine(183,baseY-60,193,baseY-65);
}
if (errors > 11){ // legs
g.drawLine(165,baseY-30,170,baseY-45);
}
if (errors > 12){
g.drawLine(183,baseY-45,193,baseY-30);
}
// Show the messages
g.drawString( message, 40, baseY+25 );
g.drawString( information, 25, baseY+45 );
g.drawString( new String (gword), 110, 60);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == bStart){
initGame();
}
if (e.getSource() == bGo){
processTurn();
// Delete the letter input box
tfLetter.setText("");
repaint();
}
}
private void processTurn(){
String s, t;
char a;
s = tfLetter.getText();
a = s.charAt(0);
if (!Character.isLetter(a)){
message="Only enter letters!";
return;
}
if (s.length()>1){
message="One letter at a time!";
return;
}
// Has the letter been guessed
t = new String(gword);
if (t.indexOf(s) != -1){
message="Letter has already been guessed";
return;
}
// If the letter doesn't occur in the rword
if (rword.indexOf(s) == -1){
message="";
errors++;
if (errors==DEAD){
message="You lost!";
information =
"Click on restart for another chance!";
}
return;
}
// Replace dots in gword with the found letter.
for (int i=0; i<rword.length(); i++){
if (rword.charAt(i) == a){
gword.setCharAt(i, a);
}
}
t = new String(gword);
// If all the dots have been filled, you win
if (t.indexOf('.') == -1){
message="You win!";
return;
}
// Delete message
message="";
repaint();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.