斯坦福 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"));ArrayListlineList = 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
- 斯坦福 cs106A Assignment4
- CS106A Assignment4: Hangman
- 斯坦福 cs106A Assignment5
- 斯坦福 cs106A Assignment6
- 斯坦福 cs106A Assignment7
- CheckerboardKarel For Standord CS106A
- cs193p assignment4 遇到问题及解决过程2
- 斯坦福研究所
- 斯坦福学习
- 斯坦福大學將舉辦區塊鏈研討會
- CS106A Assignment3: Breakout——浅谈自上而下的设计体验
- Coding Style 转自Stanford CS106A Handout #7
- 斯坦福大学CS106A公开课笔记--启示环境配置
- cs106a学习笔记 - 搭建Karel the robot环境
- 斯坦福大学公开课:编程方法学 (cs106A) 资源
- 帮朋友的朋友做的作业题额。。就当练习吧。。<assignment4>
- 斯坦福iPhone开发课件
- 乔布斯斯坦福演讲
- 迭代器模式
- Codeforces Round #370 (Div. 2) D. Memory and Scores —— DP
- ViewDragHelper 深入解析
- 单例模式中为什么用枚举更好?
- 关于web前端学习
- 斯坦福 cs106A Assignment4
- CSU~1336: Interesting Calculator(spfa)
- SVN有冲突
- vs2015加载项目失败
- 字符、字节、字符集的小疑问
- php get_magic_quotes_gpc()函数用法介绍(防注入)
- 单词查找树
- 网路编程(二)UDP和TCP协议下传输数据
- mac上编译安装lnmp环境(php7.0)