当前位置:文档之家› 北邮国际学院JAVA大作业

北邮国际学院JAVA大作业

StartGame00.java/*** A class that takes in Hangman game in GUI interface, which extends StartGame.* @author Yuewen Li* @created 2013* @version 1.0* @version 1.1*/import javax.swing.*;import java.awt.*;import java.io.*;import java.util.*;public class StartGame00 extends StartGame{JPanel thePanel; //The new variable for painting.int miss=0; //This variable is to determine the steps of drawing the hangman. It increaces in each round of//guess with the wrong guess./*** Override the constructor of parent class. Add the initialization of thePanel.*/public StartGame00(){super();this.thePanel=new StartGameGUI();}/*** Override super.go(). Add two instructions in super.go() to repaint thePanel.*/public void go(){getDictionary();startGuess();System.out.println("Would you like to play again?");System.out.println("Yes----------------Enter 1");System.out.println("No-----------------Enter 2");Scanner myChoice=new Scanner(System.in);int choice=myChoice.nextInt();switch(choice){case 1:this.miss=0; //This is the new partthePanel.repaint(); //to repaint.go();case 2:System.out.println("Game over!");System.exit(1);}}/*** Override super.startGuess(). Add the new function to repaint thePanel for every wrong guess. */public void startGuess(){this.guessNum++;int missTime=0;String missLetter="";int allRightLetters=0;String questWord=this.dictionary.toLowerCase();ArrayList<Character> display=new ArrayList<Character>();ArrayList<Character> myScanner=new ArrayList<Character>();System.out.print("Word: ");for(int i=0;i<questWord.length();i++){display.add('_');}for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: ");System.out.print("Enter Guess: ");do{Scanner myGuess=new Scanner(System.in);String word=myGuess.next();word=check(word);word=word.toLowerCase();char theLetter=word.charAt(0);int rightLetter=0;for(int j=0;j<questWord.length();j++){if(theLetter==questWord.charAt(j)){int u=0;for(int y=0;y<myScanner.size();y++){if(theLetter==myScanner.get(y)){u++;}}if(u==0){rightLetter++;display.remove(j);display.add(j,this.dictionary.charAt(j));myScanner.add(theLetter);}}}allRightLetters+=rightLetter;if(rightLetter==0){missTime++;this.miss=missTime; //This is the new partthePanel.repaint(); //to repaint.missLetter+=word;System.out.print("Word: ");for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: "+missLetter);System.out.print("Enter Guess: ");}else{if(allRightLetters==questWord.length()){System.out.println("Congratulations, you win! You have guessed the word: "+this.dictionary+" in "+(missTime+1)+" guesses.");missTime=10;}else{System.out.print("Word: ");for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: "+missLetter);System.out.print("Enter Guess: ");}}}while(missTime<6);if(missTime!=10){System.out.println("");System.out.println("You have run out of guesses. The word was: "+this.dictionary+".");}}/*** Inner class to create thePanel and paint the hangman in GUI.*/public class StartGameGUI extends JPanel{public void paintComponent(Graphics g){g.setColor(Color.white);g.fillRect(0,0,300,500);g.setColor(Color.blue);g.drawLine(30,450,100,450);g.drawLine(65,450,65,50);g.drawLine(65,50,180,50);g.drawLine(180,50,180,80);g.setColor(Color.black);if(miss>=1){ //Steps of drawing with the wrong guess.g.drawOval(150,80,60,60);}if(miss>=2){g.drawLine(180,140,180,300);}if(miss>=3){g.drawLine(100,160,180,220);}if(miss>=4){g.drawLine(260,160,180,220);}if(miss>=5){g.drawLine(180,300,120,400);}if(miss>=6){g.drawLine(180,300,240,400);}}}}StartGame.java/*** A class that takes in Hangman game in command line interface.* @author Yuewen Li* @created 2013* @version 1.0* @version 1.1*/import java.io.*;import java.util.*;public class StartGame{String fileName="dictionary.txt";String dictionary=""; //The word for once guessstatic int wordNum=0; //The number of words in dictionary.txt.static int randomWord=0; //Generate a random word.static int guessNum=0; //Count how many rounds.static ArrayList<String> tempDictionary; //Generate a word list for game./*** Constructor. Read the file "dictionary.txt" and get an ArrayList of words, tempDictionary.*/public StartGame(){tempDictionary=new ArrayList<String>();try{FileReader fileReader=new FileReader(fileName);BufferedReader bufferedReader=new BufferedReader(fileReader);String aWord="";aWord=bufferedReader.readLine();while(aWord!=null){tempDictionary.add(aWord);this.wordNum++;aWord=bufferedReader.readLine();}bufferedReader.close();fileReader.close();}catch(IOException e){System.out.println("Error occured");System.exit(1);}}/*** A getDictionary() method to get a word randomly for game.*/public void getDictionary(){if(guessNum==wordNum){System.out.println("Words run out! Game over!");System.exit(1);}else{this.randomWord=(int)(Math.random()*(wordNum-1-guessNum));this.dictionary=tempDictionary.get(randomWord);tempDictionary.remove(randomWord); //This part is to generate a} //different word for every round.}/*** A startGuess() method to start one round of the game.*/public void startGuess(){this.guessNum++;int missTime=0;String missLetter="";int allRightLetters=0;String questWord=this.dictionary.toLowerCase();ArrayList<Character> display=new ArrayList<Character>();ArrayList<Character> myScanner=new ArrayList<Character>(); //This list, myScanner is toSystem.out.print("Word: "); //avoid that a same letter isfor(int i=0;i<questWord.length();i++){ //typed many times.display.add('_');}for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: ");System.out.print("Enter Guess: ");do{ //This loop is to help finishi one round ofScanner myGuess=new Scanner(System.in); //guess.String word=myGuess.next();word=check(word);word=word.toLowerCase();char theLetter=word.charAt(0);int rightLetter=0;for(int j=0;j<questWord.length();j++){if(theLetter==questWord.charAt(j)){int u=0;for(int y=0;y<myScanner.size();y++){ //myScanner works inif(theLetter==myScanner.get(y)){ //this part.u++;}}if(u==0){rightLetter++;display.remove(j);display.add(j,this.dictionary.charAt(j));myScanner.add(theLetter);}}}allRightLetters+=rightLetter; //That allRightLetters equals the length ofif(rightLetter==0){ //questWord indicates the user has won.missTime++;missLetter+=word;System.out.print("Word: ");for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: "+missLetter);System.out.print("Enter Guess: ");}else{if(allRightLetters==questWord.length()){System.out.println("Congratulations, you win! You have guessed the word: "+this.dictionary+" in "+(missTime+1)+" guesses.");missTime=10; //Make missTime the value of 10 to break} //the do-while loopelse{System.out.print("Word: ");for(int p=0;p<display.size();p++){System.out.print(display.get(p)+" ");}System.out.println("");System.out.println("Misses: "+missLetter);System.out.print("Enter Guess: ");}}}while(missTime<6);if(missTime!=10){System.out.println("");System.out.println("You have run out of guesses. The word was: "+this.dictionary+".");}}/*** A check(String) method to ensure the entered character is valid.*/public String check(String word){boolean a=word.matches("[A-z]{1}");if(!a){System.out.println("Words only contain letters, no number or other characters.");Scanner newGuess=new Scanner(System.in);word=newGuess.next();word=check(word);return word;}elsereturn word;}/*** A go() method to start and end the Hangman Game. Ask users to continue or end game.*/public void go(){this.getDictionary();this.startGuess(); //start a round of guess.System.out.println("Would you like to play again?");System.out.println("Yes----------------Enter 1");System.out.println("No-----------------Enter 2"); //Ask user whether to continue or not.Scanner myChoice=new Scanner(System.in);int choice=myChoice.nextInt();switch(choice){case 1:go(); //Start another guess and ask and choice again. This//loop is to control the whole game.case 2:System.out.println("Game over!");System.exit(1); //Exit the game.}}}HangmanCL.java/*** A class that starts Hangman game in command line interface. * @author Yuewen Li* @created 2013* @version 1.0* @version 1.1*/public class HangmanCL{/***A main method to start the Hangman game.*/public static void main(String[] args){StartGame a=new StartGame();System.out.println("Welcome to Hangman:");a.go();}}HangmanGUI.java/*** A class that starts Hangman game in GUI interface.* @author Yuewen Li* @created 2013* @version 1.0* @version 1.1*/import javax.swing.*;import java.awt.*;import java.util.*;public class HangmanGUI extends JFrame{/***A main method to start Hangman game in GUI interface.*/public static void main(String[] args){JFrame frame=new JFrame("Hangman");StartGame00 a=new StartGame00();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(a.thePanel);frame.setSize(300,500);frame.setVisible(true);System.out.println("Welcome to Hangman:");a.go();}}。

相关主题