加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

练习perl的数组操作 hangman 猜字母

发布时间:2020-12-16 00:31:58 所属栏目:大数据 来源:网络整理
导读:#!/user/bin/perl -w@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:
#!/user/bin/perl -w
@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]){
				print $blankword[$i];
			}else{
				print"-";
			}
		}
		print "n";
		if($wrong){
			print @hangman[0..$wrong-1];
		}
		
		print"n 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){
			print "You got t right!n";
			exit;
		}
	}
	print "$hangmann Sorry,the word was $choice.n";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读