活跃农民
- 积分
- 355
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-3-23
- 最后登录
- 1970-1-1
|
第三部分脑残了半天都写不出来,参照了这贴才发现其实很简单,教授反而讲的是高层面的……第二部分写的特别难看,不过好像大家都没好哪去- /*
- * 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 acm.program.*;
- import java.io.*;
- import java.util.*;
- import java.awt.*;
- public class HangmanLexicon extends ConsoleProgram {
-
- private ArrayList<String> Strlist=new ArrayList<String>();
- private BufferedReader rd;
-
- /* private BufferedReader openfile(String prompt){
- BufferedReader rd=null;
- while(rd==null){
- try{
- String name=readLine(prompt);
- rd=new BufferedReader(new FileReader(name));
- } catch (IOException ex){
- println("Bad File");
- }
- }
- return rd;
- }
- */
-
- public HangmanLexicon(){
-
- // BufferedReader rd=openfile("Please Enter Filename");
- try{
- rd=new BufferedReader(new FileReader("ShorterLexicon.txt"));
- while(true){
- String line=rd.readLine();
- if(line==null)break;
- Strlist.add(line);
- ///store in
- }
- rd.close();
- } catch (IOException ex){
- throw new ErrorException(ex);
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
- /** Returns the number of words in the lexicon. */
- public int getWordCount() {
- return Strlist.size();
- //return 10;
- }
- /** Returns the word at the specified index. */
- public String getWord(int index) {
-
- return(Strlist.get(index));
-
- /*
- *
- switch (index) {
- case 0: return "BUOY";
- case 1: return "COMPUTER";
- case 2: return "CONNOISSEUR";
- case 3: return "DEHYDRATE";
- case 4: return "FUZZY";
- case 5: return "HUBBUB";
- case 6: return "KEYHOLE";
- case 7: return "QUAGMIRE";
- case 8: return "SLITHER";
- case 9: return "ZIRCON";
- default: throw new ErrorException("getWord: Illegal index");
- }
- */
- };
-
-
- }
复制代码- /*
- * File: HangmanCanvas.java
- * ------------------------
- * This file keeps track of the Hangman display.
- */
- import acm.graphics.*;
- import java.awt.*;
- public class HangmanCanvas extends GCanvas {
-
- public void draw(){
- count++;
-
-
- GOval head=new GOval(getWidth()/2-HEAD_RADIUS,getHeight()/8+ROPE_LENGTH,HEAD_RADIUS*2,HEAD_RADIUS*2);
- GLine body=new GLine(getWidth()/2,getHeight()/8+ROPE_LENGTH+2*HEAD_RADIUS,getWidth()/2,getHeight()/8+ROPE_LENGTH+2*HEAD_RADIUS+BODY_LENGTH);
- int arm=ARM_OFFSET_FROM_HEAD+getHeight()/8+ROPE_LENGTH+2*HEAD_RADIUS;
- GLine leftarm=new GLine(getWidth()/2,arm,getWidth()/2-UPPER_ARM_LENGTH,arm);
- GLine rightarm=new GLine(getWidth()/2,arm,getWidth()/2+UPPER_ARM_LENGTH,arm);
- GLine lefthand=new GLine(getWidth()/2-UPPER_ARM_LENGTH,arm,getWidth()/2-UPPER_ARM_LENGTH,arm+LOWER_ARM_LENGTH);
- GLine righthand=new GLine(getWidth()/2+UPPER_ARM_LENGTH,arm,getWidth()/2+UPPER_ARM_LENGTH,arm+LOWER_ARM_LENGTH);
- int hip=getHeight()/8+ROPE_LENGTH+2*HEAD_RADIUS+BODY_LENGTH;
- GLine lefthip=new GLine(getWidth()/2,hip,getWidth()/2-HIP_WIDTH,hip);
- GLine righthip=new GLine(getWidth()/2,hip,getWidth()/2+HIP_WIDTH,hip);
- GLine leftleg=new GLine(getWidth()/2-HIP_WIDTH,hip,getWidth()/2-HIP_WIDTH,hip+LEG_LENGTH);
- GLine rightleg=new GLine(getWidth()/2+HIP_WIDTH,hip,getWidth()/2+HIP_WIDTH,hip+LEG_LENGTH);
- GLine leftfoot=new GLine(getWidth()/2-HIP_WIDTH,hip+LEG_LENGTH,getWidth()/2-HIP_WIDTH-FOOT_LENGTH,hip+LEG_LENGTH);
- GLine rightfoot=new GLine(getWidth()/2+HIP_WIDTH,hip+LEG_LENGTH,getWidth()/2+HIP_WIDTH+FOOT_LENGTH,hip+LEG_LENGTH);
-
-
-
-
- if(count==1){
- add(head);
- }
- if(count==2){
- add(body);
- }
- if(count==3){
- add(leftarm);
- add(lefthand);
- }
- if(count==4){
- add(rightarm);
- add(righthand);
- }
- if(count==5){
- add(lefthip);
- add(leftleg);
- }
- if(count==6){
- add(righthip);
- add(rightleg);
- }
- if(count==7){
- add(leftfoot);
- }
- if(count==8){
- add(rightfoot);
- }
-
-
-
-
-
-
-
-
- }
-
- /** Resets the display so that only the scaffold appears */
- public void reset() {
- GLine rope=new GLine(getWidth()/2,getHeight()/8,getWidth()/2,getHeight()/8+ROPE_LENGTH);
- GLine pillar=new GLine(getWidth()/2-BEAM_LENGTH,getHeight()/8,getWidth()/2-BEAM_LENGTH,getHeight()/8+SCAFFOLD_HEIGHT);
- add(pillar);
- GLine beam=new GLine(getWidth()/2-BEAM_LENGTH,getHeight()/8,getWidth()/2,getHeight()/8);
- add(beam);
- add(rope);
- /* You fill this in */
-
- }
- /**
- * 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) {
- /* You fill this in */
- GLabel displayw=new GLabel(word);
- GRect cover=new GRect(displayw.getWidth(),displayw.getAscent());
- cover.setFilled(true);
- cover.setColor(Color.WHITE);
-
- add(cover,getWidth()/2,getHeight()/2+200-displayw.getAscent());
- add(displayw,getWidth()/2,getHeight()/2+200);
-
-
-
-
- }
- /**
- * 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 word) {
- /* You fill this in */
- result=result+word;
- GLabel incorrect= new GLabel(result);
- add(incorrect,getWidth()/2,getHeight()/2+220);
-
- }
-
-
-
- private int count=0;
- private String result="";
- /* 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;
- }
复制代码 有因为怕写错把之前的code改成注释之后又懒得删的习惯,所以看起来一个比一个长…… |
|