// Utility class for Hangman // // DO NOT ADD METHODS TO THIS CLASS AND USE IT IN Hangman.java! // The version of this file we will use for grading will not have // your changes! // // WARNING: The only changes you may make is in the BODY of the // drawGame method. Do not change anything else. // import java.io.*; import java.util.*; import java.awt.*; public class HangmanUtils { // DO NOT CHANGE THESE public static DrawingPanel panel = null; public static String name = ""; public static ArrayList wordList = new ArrayList(); public static Random rand = new Random(); // for testing public static void main(String[] args) { drawGame("h e l l o ", "zcefdq"); } // DO NOT MODIFY THIS METHOD // constructs the word list from the given file name private static void constructWordList(String filename) { Scanner input = null; try { input = new Scanner(new File(filename)); } catch(FileNotFoundException e) { System.out.println("Can't open file: " + filename + " (is it missing?)"); System.exit(0); } wordList.clear(); while (input.hasNext()) { String line = input.nextLine(); boolean isWord = true; for (int i = 0; i < line.length(); i++) { if (!Character.isLetter(line.charAt(i))) { isWord = false; } } if (isWord) { wordList.add(line); } } } // DO NOT MODIFY THIS METHOD // gets a random word from a file called hangman-words.txt public static String getWord() { String filename = "hangman-words.txt"; if (!name.equals(filename)) { constructWordList(filename); } return wordList.get(rand.nextInt(wordList.size())).toUpperCase(); } // YOU MAY CHANGE THE BODY OF THIS METHOD, BUT DO NOT MODIFY THE METHOD HEADER // draws the hangman public static void drawGame(String formattedAnswer, String misses) { /////////////////////////////////////////////////////////// // YOU MAY ONLY CHANGE THE BODY OF THIS METHOD /////////////////////////////////////////////////////////// // gallows' parameters int topLeftX = 70; int topLeftY = 70; int nooseLength = 30; int topBarLength = 80; int gallowsHeight = 200; int bottomBarLength = topBarLength + 40; int barThickness = 10; int panelWidth = bottomBarLength * 5; int panelHeight = gallowsHeight * 2; if (panel == null) { panel = new DrawingPanel(panelWidth, panelHeight); } Graphics pen = panel.getGraphics(); pen.setColor(Color.WHITE); pen.fillRect(0, 0, panelWidth, panelHeight); pen.setColor(Color.BLACK); // draw gallows pen.fillRect(topLeftX, topLeftY, barThickness, nooseLength); // noose pen.fillRect(topLeftX, topLeftY, topBarLength, barThickness); // arm pen.fillRect(topLeftX + topBarLength, topLeftY, barThickness, gallowsHeight); // stand pen.fillRect(topLeftX - 10, topLeftY + gallowsHeight, bottomBarLength, barThickness); // base // draw guess so far String guessSoFar = formattedAnswer.toUpperCase(); pen.setFont(new Font("SanSerif", Font.PLAIN, 20)); pen.drawString("Word: ", topLeftX + bottomBarLength + 20, topLeftY + 30); pen.setFont(new Font("SanSerif", Font.BOLD, 20)); pen.drawString(guessSoFar, topLeftX + bottomBarLength + 100, topLeftY + 30); // draw misses (fencepost problem!) String formattedMisses = "Misses: "; misses = misses.toUpperCase(); if (misses.length() > 0) { formattedMisses += misses.charAt(0); for (int i = 1; i < misses.length(); i++) { formattedMisses += "," + misses.charAt(i); } } pen.setFont(new Font("SanSerif", Font.PLAIN, 20)); pen.drawString(formattedMisses, topLeftX + bottomBarLength + 20, topLeftY + 70); int numErrors = misses.length(); int headSize = 30; int bodyCenter = topLeftX + barThickness / 2; int shoulderY = topLeftY + nooseLength + headSize + 15; int bodyTop = topLeftY + nooseLength + headSize; int armY = bodyTop + 5; int bodyBottom = bodyTop + gallowsHeight / 3; int legY = bodyBottom + 30; // draw head if (numErrors >= 1) { pen.drawOval(topLeftX - headSize / 2 + barThickness / 2, topLeftY + nooseLength, 30, 30); } // draw body if (numErrors >= 2) { pen.drawLine(bodyCenter, bodyTop, bodyCenter, bodyBottom); } // draw right arm if (numErrors >= 3) { pen.drawLine(bodyCenter, shoulderY, bodyCenter - 30, armY); } // draw left arm if (numErrors >= 4) { pen.drawLine(bodyCenter, shoulderY, bodyCenter + 30, armY); } // draw right leg if (numErrors >= 5) { pen.drawLine(bodyCenter, bodyBottom, bodyCenter - 30, legY); } // draw left leg if (numErrors >= 6) { pen.drawLine(bodyCenter, bodyBottom, bodyCenter + 30, legY); endGame("YOU SUCK!", 100, topLeftY + gallowsHeight + 80); } else if (formattedAnswer.indexOf("_") < 0) { // answer has no blanks endGame("YOU WIN!", 100, topLeftY + gallowsHeight + 80); } } // DO NOT MODIFY THIS METHOD // ends the game and disposes of the DrawingPanel public static void endGame(String display, int x, int y) { Graphics pen = panel.getGraphics(); pen.setFont(new Font("SanSerif", Font.BOLD, 50)); pen.drawString(display, x, y); panel.sleep(3000); panel.dispose(); panel = null; // have to set to null, so new one // will be created for next game } }