大头壳

大头大头 下雨不愁 人家有伞 我有大头
posts - 1, comments - 6, trackbacks - 0, articles - 22
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

吊死鬼游戏

Posted on 2008-03-28 10:00 王大头 阅读(710) 评论(1)  编辑 收藏 引用
perl版本

#!/opt/perl/bin/perl

@words=qw(internet answers printer program);
@guesses=();
$wrong=0;

$choice=$words[rand @words];
$hangman="0-|--<";

@letters=split(//,$choice);
@hangman=split(//,$hangman);
@blankword=(0) x scalar(@hangman);

OUTER
:
        
while($wrong<@hangman){
                
foreach $i (0..$#letters) {
                        if($blankword[$i]){
                                
printf("%s",$blankword[$i]);
                        }
else{
                                
printf("-");
                        }
                }
                
printf("\n");

                
printf("Your Guess: ");
                
$guess=<STDIN>;chomp $guess;
                
foreach(@guesses){
                        
next OUTER if($_ eq $guess);
                }

                
$right=0;
                
for($i=0;$i<@letters;$i++){
                        
if($letters[$i] eq $guess){
                                
$blankword[$i]=$guess;
                                
$right=1;
                        }
                }
                
$wrong++ unless(not $right);
                
if(join('',@blankword) eq $choice){
                        
printf("You got it right!\n");
                        
exit;
                }
        }
        
printf("%s\nSorry, the word was %s.\n",$hangman,$choice);


python版本

import string
import random

HANGMAN
="0-|--<"
dic
=["internet","program","printer","wangdatou"]
rand
=len(dic)*random.random()

hide_word
=dic[int(rand)]
word_len
=len(hide_word)

guess_word
=""
error_cnt
=0

for char in hide_word:
    guess_word
=guess_word + "-"

print guess_word
print "Please input your guess:"

while(1):
    input
=raw_input()
    guess_char
=input[0]
    char_idx
=-1
    match_flag
=0
    
    
for char in hide_word:
        char_idx
=char_idx + 1
        
if(guess_char==char):
            
if(char_idx==0):
                guess_word
=char+guess_word[1:word_len]
            
elif(char_idx==word_len):
                guess_word
=guess_word[0:(word_len-1)]
            
else:
                guess_word
=guess_word[0:char_idx] + char + guess_word[(char_idx+1):word_len]
            match_flag
=1

    
if(match_flag==0):
        error_cnt
=error_cnt+1

    
if(error_cnt>word_len):
        
print HANGMAN,"\nSorry,the word is:",hide_word
        exit

    
print guess_word

    
if(guess_word==hide_word):
        
print "You got it,the word is:",hide_word
        exit
    
else:
        
print "Please input your guess:"

shell版本

HIDEWORD=`awk '{printf "%s\n",$(rand*NF+1)}' hangman.dic`
HANGMAN
="O-|--<"
h_idx
=1
try_cnt
=0
error_cnt
=0
while [ $h_idx -le ${#HIDEWORD} ]
do
printf "-"
GUESS_WORD
=$GUESS_WORD"-"
h_idx
=`expr $h_idx + 1`
done
printf "\nPlease input your guess:\n";
while read guess
do
        guess_char
=`echo $guess|cut -c1-1`
        g_idx
=1
        match_flag
=0

        
while [ $g_idx -le ${#HIDEWORD} ]
        do
                r_char
=`echo $HIDEWORD|cut -c$g_idx-$g_idx`
                
if [ $guess_char = $r_char ]
                then
                        match_flag
=1
                        
if [ $g_idx -eq 1 ]
                        then    t
=`expr $g_idx + 1`
                                GUESS_WORD
=$r_char`echo $GUESS_WORD|cut -c$t-`
                        elif [ 
$g_idx -eq ${#HIDEWORD} ]
                        then    t=`expr $g_idx - 1`
                                GUESS_WORD
=`echo $GUESS_WORD|cut -c1-$t`$r_char
                        
else
                                t1
=`expr $g_idx - 1`
                                t2
=`expr $g_idx + 1`
                                GUESS_WORD
=`echo $GUESS_WORD|cut -c1-$t1`$r_char`echo $GUESS_WORD|cut -c$t2-`
                        fi
                        g_idx
=`expr $g_idx + 1`
                
else
                        g_idx
=`expr $g_idx + 1`
                fi
        done

        
if [ $match_flag -eq 0 ]
        then
                error_cnt
=`expr $error_cnt + 1`
                
if [ $error_cnt -ge ${#HIDEWORD} ]
                then    printf "%s\nSorry,the word is %s!\n" $HANGMAN $HIDEWORD
                        
exit
                fi
        fi

        
if [ $GUESS_WORD = $HIDEWORD ]
        then
                
printf "%s\nYou got it,the word is %s.\n" $GUESS_WORD $HIDEWORD
                
exit
        
else
                
printf "%s\nPlease input your guess:\n" $GUESS_WORD;
        fi

done

Feedback

# re: 吊死鬼游戏  回复  更多评论   

2009-06-10 08:21 by 霏霏
在哪里?

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理