斯坦福 cs106A Assignment4

来源:互联网 发布:stc89c52单片机驱动 编辑:程序博客网 时间:2024/05/29 11:53

在学斯坦福cs106A时编程任务没有习题答案,百度上也难找到4-7的答案, 所有放上自己写的代码(能运行并符合题目要求,因为初学,写的不好看,)供人参考,

Assignment4


因为是初学,英文又不好,代码中用了很多中文,随着学习的深入,个人觉得还是用中文比较好。


/* * File: Hangman.java * ------------------ * This program will eventually play the Hangman game from * @author zyw23 * Assignment #4. */import acm.program.*;import acm.util.*;public class Hangman extends ConsoleProgram { /* private Constants  */private static final int 几次机会 = 8;public void init() {图形窗口 = new HangmanCanvas();    add(图形窗口);       }    public void run() {        游戏是否重新开始 = "yes";    while (游戏是否重新开始.equals("yes")) {        游戏初始化();            开始游戏();                 while (true) {                游戏是否重新开始 = readLine("Does the game start again? \"input yes or no\" ");        if (游戏是否重新开始.equals("yes") || 游戏是否重新开始.equals("no")) break; else 游戏是否重新开始 = readLine("Does the game start again? \"input yes or no\" ");                    }      } }        //游戏初始化        private void 游戏初始化() {        setFont("serif-20");        println("Welcome to Hangman!");        剩余次数 = 几次机会;    是否过关 = false;    字母位置 = -1;    图形窗口.reset();    单词列表 = new HangmanLexicon();        正确答案 = 单词列表.getWord(rgen.nextInt(0, 单词列表.getWordCount()));      猜中位置 = new int[正确答案.length()];     for (int i = 0; i < 正确答案.length(); i++) {    猜中位置[i] = -1;    }    println("The word now looks like this: " + 提示答案(正确答案));    println("You have " + 剩余次数 + " guesses left.");    图形窗口.displayWord(提示答案(正确答案));               }        //开始游戏    private void 开始游戏() {            while (剩余次数 > 0 && !是否过关) {        while (true) {        猜测的字母 = readLine("Your guess: ");         //!猜测的字母.equals("")这样能忽略没任何输入直径回车后程序出现(Thread.java:745) 错误提示              if (!猜测的字母.equals("") && (猜测的字母.toUpperCase().charAt(0) >= 'A' && 猜测的字母.toUpperCase().charAt(0) <= 'Z')) {        字母 = 猜测的字母.toUpperCase().charAt(0);             break;                } else {        println("Please input a letter. ");        }                        }        if (是否猜中(字母) == -1) 没有猜中(); else 猜中();  //等于-1为没猜中否则为猜中.          }        }  //判断有没有猜中    private void 没有猜中() {    剩余次数--;if (剩余次数 == 0) {    图形窗口.noteIncorrectGuess(字母);图形窗口.displayWord(正确答案);println("There are no " + 字母 + "'s in the word.");    println("You'ar completely hung.");    println("The word was: " + 正确答案);        println("You lose.");} else {图形窗口.noteIncorrectGuess(字母);            println("There are no " + 字母 + "'s in the word.");    println("The word now looks like this: " + 提示答案(正确答案));    if (剩余次数 == 1) {    println("You have only one guesses left.");     } else {    println("You have " + 剩余次数 + " guesses left.");    }    }      }        //判断有没有猜中    private void 猜中() {        图形窗口.displayWord(提示答案(正确答案));println("That guess is correct.");if (提示答案(正确答案).indexOf('-') != -1) {println("The word now looks like this: " + 提示答案(正确答案));} else {println("You guessed the word: " + 提示答案(正确答案));println("You win.");是否过关 = true;}        }    //答案提示    private String 提示答案(String word) {String result = "";for(int i = 0; i < word.length(); i++) {if (猜中位置[i] == -1) {                  //未被猜中过则显示"-".result += '-';} else {                                 //被猜中过则显示字母.result += 正确答案.charAt(i);}}return result;}         // 是否猜中 返回-1表示没猜中,不是-1表示猜中.    private int 是否猜中(char 字母) {        字母位置 = 正确答案.indexOf(字母);                        if (字母位置 == -1) {                                  //等于-1表示没有猜中        } else if (猜中位置[字母位置] == -1) {                 //猜中位置[n]等于-1表示此n位置未被猜中过,表示猜中有效猜中位置[字母位置] = 字母位置;} else {                                              //猜中位置[n]不等于-1表示此n位置未被猜中过,表示猜中无效字母位置 = 正确答案.indexOf(字母, 字母位置 + 1);   //继续判断下一个索引位是否猜中.while (字母位置 != -1) {                         //直到确定单词所有索引位都没猜中后终止.if (猜中位置[字母位置] == -1) {猜中位置[字母位置] = 字母位置;break;} else {字母位置 = 正确答案.indexOf(字母, 字母位置 + 1); //继续判断下一个索引位是否猜中.} }}    return 字母位置;        }       /* Private instance variables */private RandomGenerator rgen = RandomGenerator.getInstance();      //随机数实例;private HangmanCanvas  图形窗口;private HangmanLexicon 单词列表;private int            剩余次数;private String         正确答案;private String         猜测的字母;private String         游戏是否重新开始;private char           字母;private int[]          猜中位置;private int            字母位置;private boolean        是否过关;}/* * File: HangmanCanvas.java * ------------------------ * This file keeps track of the Hangman display. */import acm.graphics.*;public class HangmanCanvas extends GCanvas {/** Resets the display so that only the scaffold appears */public void reset() {removeAll();图形窗口初始化();}/** * 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) {正确答案.setLabel(word);}/** * 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. */public void noteIncorrectGuess(char letter) {猜错几次++;显示部分人体(猜错几次);玩家答案.setLabel(玩家答案.getLabel() + Character.toUpperCase(letter));}//图形窗口初始化private void 图形窗口初始化() {猜错几次 = 0;正确答案 = new GLabel("");正确答案.setFont("SansSerif-bold-36");add(正确答案, getWidth() / 2 - BEAM_LENGTH - HEAD_RADIUS,     getHeight() / 8 + SCAFFOLD_HEIGHT + UPPER_ARM_LENGTH);玩家答案 = new GLabel("");玩家答案.setFont("SansSerif-bold-24");add(玩家答案, getWidth() / 2 - BEAM_LENGTH - HEAD_RADIUS,              getHeight() / 8 + SCAFFOLD_HEIGHT + UPPER_ARM_LENGTH + 玩家答案.getHeight());//绞刑架add(new GLine(getWidth() / 2 - BEAM_LENGTH, getHeight() / 8, getWidth() / 2 - BEAM_LENGTH, getHeight() / 8 + SCAFFOLD_HEIGHT));add(new GLine(getWidth() / 2 - BEAM_LENGTH, getHeight() / 8, getWidth() / 2, getHeight() / 8));add(new GLine(getWidth() / 2, getHeight() / 8, getWidth() / 2, getHeight() / 8 + ROPE_LENGTH));}//显示部分人体private void 显示部分人体(int index) {switch (index) {case 1: add(new GOval(HEAD_RADIUS * 2, HEAD_RADIUS * 2), getWidth() / 2 - HEAD_RADIUS, getHeight() / 8 + ROPE_LENGTH); break;case 2: add(new GLine(getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2, getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH));break;case 3: add(new GLine(getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD, getWidth() / 2 - UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD)); add(new GLine(getWidth() / 2 - UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD, getWidth() / 2 - UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD + LOWER_ARM_LENGTH)); break;case 4: add(new GLine(getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD, getWidth() / 2 + UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD)); add(new GLine(getWidth() / 2 + UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD, getWidth() / 2 + UPPER_ARM_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + ARM_OFFSET_FROM_HEAD + LOWER_ARM_LENGTH)); break;case 5: add(new GLine(getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH, getWidth() / 2 - HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH)); add(new GLine(getWidth() / 2 - HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH, getWidth() / 2 - HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH)); break;case 6: add(new GLine(getWidth() / 2, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH, getWidth() / 2 + HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH)); add(new GLine(getWidth() / 2 + HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH, getWidth() / 2 + HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH)); break;case 7: add(new GLine(getWidth() / 2 - HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH, getWidth() / 2 - HIP_WIDTH  - FOOT_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH)); break;case 8: add(new GLine(getWidth() / 2 + HIP_WIDTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH, getWidth() / 2 + HIP_WIDTH + FOOT_LENGTH, getHeight() / 8 + ROPE_LENGTH + HEAD_RADIUS * 2 + BODY_LENGTH + LEG_LENGTH)); break;}}/* 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 instance variables */private GLabel 正确答案;private GLabel 玩家答案;private int    猜错几次;}/* * File: HangmanLexicon.java * ------------------------- * This file contains a stub implementation of the HangmanLexicon * class that you will reimplement for Part III of the assignment. * @author zyw23 */import java.io.*;import acm.util.*;import java.util.ArrayList;public class HangmanLexicon {/** This is the hangamLexicon constructor */public HangmanLexicon() {try {BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt"));ArrayList lineList = new ArrayList(); while (true) {String line = rd.readLine();if (line == null) break;lineList.add(line);}rd.close();单词数组 = new String[lineList.size()];for (int i = 0; i < 单词数组.length; i++) {单词数组[i] = (String) lineList.get(i);}} catch (IOException ex) {throw new ErrorException(ex);}}/** Returns the number of words in the lexicon. */public int getWordCount() {return 单词数组.length;}/** Returns the word at the specified index. */public String getWord(int index) {return 单词数组[index];}/* Private instance variables */private String[] 单词数组;}




0 0