活跃农民
- 积分
- 928
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-2-28
- 最后登录
- 1970-1-1
|
Hangman.java- /*
- * File: Hangman.java
- * ------------------
- * This program will eventually play the Hangman game from
- * Assignment #4.
- */
- import acm.graphics.*;
- import acm.program.*;
- import acm.util.*;
- import java.awt.*;
- public class Hangman extends ConsoleProgram {
-
- private HangmanCanvas canvas = new HangmanCanvas();
- private RandomGenerator rgen = RandomGenerator.getInstance();
- private HangmanLexicon word_index = new HangmanLexicon();
- private boolean flag;
-
- public void run() {
- /** Initialize */
- setLayout(new GridLayout(1,2));
- add(canvas);
- validate();
- canvas.reset();
-
- int time_left = 8;
- char guess_char;
- println("Welcome to Hangman!");
-
- /** Choose the word */
- String answer = word_index.getWord(rgen.nextInt(0,word_index.getWordCount()-1));
- String word_inpro = "";
- for(int i = 0; i < answer.length(); i++)
- word_inpro = word_inpro + "-";
- canvas.displayWord(word_inpro);
-
- /** Running main function */
- while(time_left > 0){
- println("The word now looks like this: "+ word_inpro);
- println("You have "+ time_left + " guesses left.");
- guess_char = Character.toUpperCase(readLine("Your guess: ").charAt(0));
- flag = false;
- word_inpro = replaceChar(answer, word_inpro,guess_char);
- if(flag == false){
- time_left--;
- canvas.noteIncorrectGuess(guess_char);
- }
- else canvas.displayWord(word_inpro);
- /** Correct answer */
- if(answer.equals(word_inpro)){
- println("That guess is correct.\nYou guessed the word: " +
- answer + "\nYou win.");
- break;
- }
- }
-
- if (time_left == 0) println("The word was: " + answer + "\nYou lose.");
- }
-
- private String replaceChar(String ans, String str, char ch){
- String result = "";
- for(int i = 0; i< str.length(); i++){
- if(ans.charAt(i) == ch){
- result += ch;
- flag = true;
- }
- else result += str.charAt(i);
- }
- return result;
- }
- }
复制代码 HangmanCanvas.java- /*
- * File: HangmanCanvas.java
- * ------------------------
- * This file keeps track of the Hangman display.
- */
- import acm.graphics.*;
- import java.awt.*;
- public class HangmanCanvas extends GCanvas {
- /** Resets the display so that only the scaffold appears */
- public void reset() {
- add(new GLine(50,50,50,50+SCAFFOLD_HEIGHT));
- add(new GLine(50,50,50+BEAM_LENGTH,50));
- add(new GLine(50+BEAM_LENGTH,50,50+BEAM_LENGTH,50+ROPE_LENGTH));
- }
- /**
- * Updates the word on the screen to correspond to the current
- * state of the game. The argument string shows what letters have
- * been guessed so far; unguessed letters are indicated by hyphens.
- */
- public void displayWord(String word) {
- if(getElementAt(50,100+SCAFFOLD_HEIGHT)!=null)
- remove(getElementAt(50,100+SCAFFOLD_HEIGHT));
- GLabel word_display = new GLabel(word,50,100+SCAFFOLD_HEIGHT);
- word_display.setFont(new Font("TimesRoman", Font.PLAIN, 30));
- add(word_display);
- }
- /**
- * Updates the display to correspond to an incorrect guess by the
- * user. Calling this method causes the next body part to appear
- * on the scaffold and adds the letter to the list of incorrect
- * guesses that appears at the bottom of the window.
- */
- private String error_guess="";
- private int body_part = 0;
- public void noteIncorrectGuess(char letter) {
- /** Wrong guesses at the bootom of the window */
- error_guess += letter;
- if(getElementAt(50,125+SCAFFOLD_HEIGHT)!=null)
- remove(getElementAt(50,125+SCAFFOLD_HEIGHT));
- GLabel error_display = new GLabel(error_guess,50,125+SCAFFOLD_HEIGHT);
- error_display.setFont(new Font("TimesRoman", Font.PLAIN, 20));
- add(error_display);
-
- /** Draw body part */
- switch(++body_part){
- case 1: add(new GOval(50+BEAM_LENGTH-HEAD_RADIUS,50+ROPE_LENGTH,HEAD_RADIUS*2,HEAD_RADIUS*2));
- break; //head
- case 2: add(new GLine(50+BEAM_LENGTH,LEG_BEGIN-BODY_LENGTH,50+BEAM_LENGTH,LEG_BEGIN));
- break; //body
- case 3: add(new GLine(50+BEAM_LENGTH,ARM_BEGIN,50+BEAM_LENGTH-UPPER_ARM_LENGTH,ARM_BEGIN));
- add(new GLine(50+BEAM_LENGTH-UPPER_ARM_LENGTH,ARM_BEGIN,50+BEAM_LENGTH-UPPER_ARM_LENGTH,
- ARM_BEGIN+LOWER_ARM_LENGTH));
- break; //left arm
- case 4: add(new GLine(50+BEAM_LENGTH,ARM_BEGIN,50+BEAM_LENGTH+UPPER_ARM_LENGTH,ARM_BEGIN));
- add(new GLine(50+BEAM_LENGTH+UPPER_ARM_LENGTH,ARM_BEGIN,50+BEAM_LENGTH+UPPER_ARM_LENGTH,
- ARM_BEGIN+LOWER_ARM_LENGTH));
- break; //right arm
- case 5: add(new GLine(50+BEAM_LENGTH,LEG_BEGIN,50+BEAM_LENGTH-HIP_WIDTH,LEG_BEGIN));
- add(new GLine(50+BEAM_LENGTH-HIP_WIDTH,LEG_BEGIN,50+BEAM_LENGTH-HIP_WIDTH,
- LEG_BEGIN+LEG_LENGTH));
- break; //left leg
- case 6: add(new GLine(50+BEAM_LENGTH,LEG_BEGIN,50+BEAM_LENGTH+HIP_WIDTH,LEG_BEGIN));
- add(new GLine(50+BEAM_LENGTH+HIP_WIDTH,LEG_BEGIN,50+BEAM_LENGTH+HIP_WIDTH,
- LEG_BEGIN+LEG_LENGTH));
- break; //right leg
- case 7: add(new GLine(50+BEAM_LENGTH-HIP_WIDTH,LEG_BEGIN+LEG_LENGTH,
- 50+BEAM_LENGTH-HIP_WIDTH-FOOT_LENGTH,LEG_BEGIN+LEG_LENGTH));
- break; //left foot
- case 8: add(new GLine(50+BEAM_LENGTH+HIP_WIDTH,LEG_BEGIN+LEG_LENGTH,
- 50+BEAM_LENGTH+HIP_WIDTH+FOOT_LENGTH,LEG_BEGIN+LEG_LENGTH));//right foot
- }
- }
- /* Constants for the simple version of the picture (in pixels) */
- private static final int SCAFFOLD_HEIGHT = 360;
- private static final int BEAM_LENGTH = 144;
- private static final int ROPE_LENGTH = 18;
- private static final int HEAD_RADIUS = 36;
- private static final int BODY_LENGTH = 144;
- private static final int ARM_OFFSET_FROM_HEAD = 28;
- private static final int UPPER_ARM_LENGTH = 72;
- private static final int LOWER_ARM_LENGTH = 44;
- private static final int HIP_WIDTH = 36;
- private static final int LEG_LENGTH = 108;
- private static final int FOOT_LENGTH = 28;
- private static final int LEG_BEGIN = 50+ROPE_LENGTH+HEAD_RADIUS*2+BODY_LENGTH;
- private static final int ARM_BEGIN = LEG_BEGIN-BODY_LENGTH+ARM_OFFSET_FROM_HEAD;
- }
复制代码 HangmanLexicon.java- /*
- * File: HangmanLexicon.java
- * -------------------------
- * This file contains a stub implementation of the HangmanLexicon
- * class that you will reimplement for Part III of the assignment.
- */
- import acm.util.*;
- import java.util.*;
- import java.io.*;
- public class HangmanLexicon {
- private ArrayList<String> dic = new ArrayList<String>();
-
- HangmanLexicon(){
- BufferedReader rd = null;
- try{
- rd = new BufferedReader(new FileReader("HangmanLexicon.txt"));
- String line = rd.readLine();
- while(line != null){
- dic.add(line);
- line = rd.readLine();
- }
- rd.close();
- } catch (IOException ex){System.out.println("NO FILE!");}
- }
- /** Returns the number of words in the lexicon. */
- public int getWordCount() {
- return dic.size();
- }
- /** Returns the word at the specified index. */
- public String getWord(int index) {
- return dic.get(index);
- }
- }
复制代码 |
|