日期:2014-05-20  浏览次数:20872 次

编写一个刽子游戏,求各种答案,来强人
[b]随机产生一个单词,然后提示用户一次猜测一个字母,如运行示例,单词中的每个字母都以星号显示,当用户的猜测准确时,就显示实际的字母,当用户完成了一个单词,就显示猜错的次数,然后询问用户是否继续猜测另一个单词,声明一个数组来存储这些单词,如下所示:
String[] words={"write","program",……}
输出的结果:
  (guess) Enter a letter in word ******* > p 
  (guess) Enter a letter in word p****** > r
  (guess) Enter a letter in word pr**r** > p
  p is already in the word
  (guess) Enter a letter in word pr**r** > o 
  (guess) Enter a letter in word pro*r** > g
  (guess) Enter a letter in word progr** > n
  n is not in the word
  (guess) Enter a letter in word progr** > m
  (guess) Enter a letter in word progr*m > a
  the word is program.you missed 1 time

  Do you want to guess for another word? Enter y or n>
 

------解决方案--------------------
contains(CharSequence s) 
当且仅当此字符串包含 char 值的指定序列时,才返回 true。

第一个想到的String的方法
------解决方案--------------------
注意下细节就好了
Java code

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GuessGame {
    List<String> words = new ArrayList<String>();
    String word = "";
    String matchs = " ";
    int missTime = 0;
    GuessGame(String... args) {
        words.addAll(Arrays.asList(args));
    }

    public void randomWords() {
        int temp = (int) (Math.random() * words.size());
        word = words.get(temp);
    }

    public void guess() {
        System.out.print("Enter a letter in word "
                + word.replaceAll("[^" + matchs + "]", "*") + " >>>");
        BufferedInputStream in =new BufferedInputStream(System.in);
        char c = ' ';
        try {
            c = (char)in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (matchs.indexOf(c) != -1) {
            System.out.println(c + " is already in the word");
            guess();
            return;
        }
        else{
            if(word.indexOf(c) != -1){
                matchs+=String.valueOf(c);
                String temp = word.replaceAll("[^" + matchs + "]", "*");
                if(temp.indexOf('*') == -1){
                    System.out.println("the word is "+word+" .you missed "+ missTime +" time");
                }
                else{
                    guess();
                }
            }
            else{
                System.out.println(c+" is not in the word");
                missTime++;
                guess();
            }
        }
    }

    public static void main(String args[]){
        GuessGame gg=new GuessGame("welcome","helloword","niceshoot");
        
        boolean flag = true; 
        while(flag){
            gg.randomWords();
            gg.guess();
            System.out.println("Do you want to guess for another word ? Enter y or n>>>");
            BufferedInputStream in =new BufferedInputStream(System.in);
            try {
                if((char)in.read() == 'n'){
                    flag = false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            gg.missTime =0;
            gg.matchs = " ";
        }
    }
}

------解决方案--------------------
还是有点意思,所以写了个,你看看吧
Java code

package com.test;

import java.util.Random;
import java.util.Scanner;

public class Test {
    
    static char[] word;
    static char[] printWord;
    static String singleWord = "";
    static int count;
    
    public static void main(String[] args) {
        boolean over = false;
        word = randomWord().toCharArray();
        printWord = new char[word.length];
        for(int i = 0; i < word.length; i++){
            printWord[i] = '*';
        }
        Scanner scan = new Scanner(System.in);
        while(true){
            if(!over){
                System.out.println("(guess) Enter a letter in word " + new String(printWord) + " > ");
                String in = scan.next();
                if(in.length() == 1){
                    char c = in.charAt(0);
                    boolean haveChar = false;
                    if(singleWord.indexOf(c) == -1){
                        for(int i = 0; i < word.length; i++){
                                if(c == word[i]){
                                    printWord[i] = c;
                                    singleWord += String.valueOf(c);
                                    haveChar = true;
                                }
                        }
                    }else{
                        haveChar = true;
                        System.out.println(String.valueOf(c) + " is already in the word");
                    }
                    if(!haveChar){
                        count++;
                        System.out.println(String.valueOf(c) + " is not in the word");
                    }
                    if(String.valueOf(printWord).indexOf("*") == -1){
                        System.out.println("the word is " + String.valueOf(word));
                        System.out.println("the word is program.you missed " + count + " time");
                        System.out.println("Do you want to guess for another word? Enter y or n >");
                        over = true;
                    }
                }
            }else{
                String in = scan.next();
                if("y".equalsIgnoreCase(in)){
                    over = false;
                    word = randomWord().toCharArray();
                    printWord = new char[word.length];
                    singleWord = "";
                    count = 0;
                    for(int i = 0; i < word.length; i++){
                        printWord[i] = '*';
                    }
                }else if("n".equalsIgnoreCase(in)){
                    break;
                }else{
                    System.out.println("Do you want to guess for another word? Enter y or n >");
                    continue;
                }
            }
        }
        
    }
    
    public static String randomWord(){
        String[] words = new String[]{"hello", "word", "jack", "hehehehe", "honghe", "beibi"};
        Random random = new Random();
        return words[random.nextInt(6)];
    }
}