您的位置:首页 > 编程语言

斯坦福大学开放课程——编程方法 作业4 Part I

2010-12-16 14:53 399 查看
把第四次作业的第一部分的功能实现了,先暂存在这里。

Part I—Playing a console-based game

To solve the problem, your program must be able to:

Choose a random word to use as the secret word. That word is chosen from a word list, as described in the following paragraph.

Keep track of the user’s partially guessed word, which begins as a series of dashes and then updated as correct letters are guessed.

Implement the basic control structure and manage the details (ask the user to guess a letter, keep track of the number of guesses remaining, print out the various messages, detect the end of the game, and so forth)

只修改了Hangman.java文件,另外两个文件未作修改,所以只贴上Hangman.java文件中的源代码

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 static final String strWelcome = "Welcome to Hangman!";
private static final String strTip = "The word now looks like this: ";

public void run() {
/* You fill this in */
gameInit();	//游戏初始化

gamePlay();	//进行游戏
}

/*
* ******************************************
* 		函数名:	gameInit
* 		功  能:	对游戏界面和相关变量进行
* 					初始化
* ******************************************
*/
private void gameInit() {
println(strWelcome);	//输出欢迎信息

/* 初始化目标单词和用户所猜单词变量 */
originWord = tmp.getWord(rgen.nextInt(0,tmp.getWordCount()-1));
guessWord = "-";
for(int i= 1;i < originWord.length();i++){
guessWord += "-";
}

////////////////////////////////////
//println(originWord);//输出所选单词
////////////////////////////////////
}
/*
* ******************************************
* 		函数名:	gameplay
* 		功  能:	游戏开始
* ******************************************
*/
private void gamePlay() {
boolean isLive = false;//判断是否在规定次数内成功
int leftTimes = originWord.length();	//剩余次数
String 	strCh = null,	//用户所猜字母
/* 猜测正确提示信息 */
guessRight = "That guess is correct";

while(!guessWord.equals(originWord)){
isLive = false;

println(strTip + guessWord);	//输出单词提示信息
println("You have " + leftTimes + " guesses left.");	//输出剩余次数提示信息

strCh = readLine("Your guess: ");	//读取用户所猜测的字母

while(strCh.length() > 1){
println("One character, one time.");
strCh = readLine("Your guess: ");	//读取用户所猜测的字母
}

strCh = strCh.toUpperCase();	//将读取到的字母统一为大写

/* 判断是否猜测正确 */
for(int index=0;index < originWord.length();index++){
if(strCh.charAt(0) == originWord.charAt(index)){
if(0 == index){
guessWord = strCh + guessWord.substring(1);
}
else if(originWord.length() == (index +1)){
guessWord = guessWord.substring(0, index) + strCh;
}
else{
guessWord = guessWord.substring(0, index) + strCh + guessWord.substring(index+1);
}

isLive = true;
}
}

/* 输出相应信息 */
if(isLive){
//猜测正确
println(guessRight);
}
else{
//猜测错误
println("There are no " + strCh +"'s in the word.");
leftTimes--;
}

if(0 == leftTimes) break;	//剩余次数用完,退出

}

if(isLive){
println("You guessed the word: " + originWord);
println("You win.");
}
else{
println("You're completely hung.");
println("The word was: " + originWord);
println("You lose");
}
}

/* 私有实例变量 */
private String originWord;
private String guessWord;
private HangmanLexicon tmp = new HangmanLexicon();
private RandomGenerator rgen = RandomGenerator.getInstance();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐