Demo entry 6687503

perl

   

Submitted by anonymous on Dec 28, 2017 at 16:02
Language: Perl. Code size: 4.0 kB.

#成语能力测试
#电脑给出成语,用户回答,若成语出现频次较低(≤20)则得高分,出现频率较高(>20)则得低分。

print "*************************************\n";
print "*************************************\n";
print "* Welcome to Idioms Solitaire Game! *\n";
print "*************************************\n";
print "*************************************\n";
print "\n";
print "* Please input ok to start game!\n";
print "\n";

$In=<stdin>;
chomp($In);

while ($In eq 'ok')
	{
		print "\n";
		print "* GOOD LUCK!\n";
		print "\n";
		game();
	}




sub game 
{
	Init();			#准备成语
	play();			#开始游戏
}



sub Init
{
	open (In,"fruq.txt");				
	
	while(<In>)
	{
			chomp;
			if(/(\S+) (\d+)/)
		{
			$IdiomFreq{$1}=$2;			#获取频次表中的成语和频次:成语为关键字,频次为值的哈希表
		}
	}
	
	close (In);

	open (In,"IdiomPY.txt");			#导入成语拼音库
	
	while(<In>)
	{
		chomp;
		if(/(\S+) ([^_]+)\d.*_([^_]+)\d$/)
		{
			$First{$1}=$2;					#成语为关键字,首拼音为值的哈希表
			$Last{$1}=$3;					#成语为关键字,尾拼音为值的哈希表
			push(@Idioms,$1);				#将所有成语加入数组
		}
	}
	
close (In);			

	foreach $Idiom1(sort keys %First)
	{
		foreach $Idiom2(sort keys %Last)
		{
			if($Last{$Idiom2} eq $First{$Idiom1})
			{
				push(@{$Solitaire{$Idiom2}},$Idiom1);		#Solitaire的关键字是原成语,值是它可接龙的成语的数组
			}
		}
	}

}

sub play
{
	$JQ=$Idioms[int(rand()*@Idioms)]; 	#电脑随机给出成语
	$flag=1;
	$scores=0;
	print "Computer: $JQ(No.$flag/10)\n";
	
	
		while($flag<10)		#游戏进行一次flag+1,共进行10次
		{
			while(@{$Solitaire{$JQ}}==0)		#当所给成语无可接龙时,重新给出一个随机成语
			{
				$JQ=$Idioms[int(rand()*@Idioms)];
				print "There is no idiom to continue!\n  The Computer gives you an new Idiom: $JQ(No.$flag/10)\n";
			}
			
				print "Your scores:\t$scores\n";
				print "please input your answer:(q to exit)\t";
				print "\n";
				$In=<stdin>;
				chomp($In);
			
			if($In eq "q")
			{
				last;
			}
			
			else{if(defined $Solitaire{$In})
			{
				$users=$In;
				if($IdiomFreq{$In}>20)			#词频高(大于20次)的成语加分低
					{$scores+=10;
					 print "Bingo!(+10)\n";
					 print "\n";}
				else
					{$scores+=15;				#词频低(小于等于20次)的成语加分高
					 print "Bingo!(+15)\n";
					 print "\n";} #加十分
			
			@An=@{$Solitaire{$In}};
			$JQ=$An[int(rand()*@An)];
			}
			
			else{
				    print "Error! please choose right answer:\n";
			
					if(@{$Solitaire{$JQ}}<10)		#可接龙成语小于十个时,全部显示;大于十个时,显示前十个
					{
						for($i=0;$i<@{$Solitaire{$JQ}};$i++)
								{
									$j=$i+1;
									print "$j: ";
									print "${$Solitaire{$JQ}}[$i]\n";
								}
			
			
					$CH=<stdin>;			#玩家输入数字来选择答案
					chomp($CH);
					$num=$CH-1;
					$users=${$Solitaire{$JQ}}[$num];
						print "Your choice: ${$Solitaire{$JQ}}[$num]\n";
						print "\n";
										}
										
			elsif(@{$Solitaire{$JQ}}==0)
			{
				last;
			}
			
			else
				{for($i=0;$i<10;$i++)
					{
						$j=$i+1;
						print "$j: ";
						print "${$Solitaire{$JQ}}[$i]\n"; 	#显示可选择成语列表
					}
						$CH=<stdin>;
						chomp($CH);
						$num=$CH-1;
						$users=${$Solitaire{$JQ}}[$num];
						print "Your choice: ${$Solitaire{$JQ}}[$num]\n";	#显示玩家所选择的成语
						print "\n";
				}
				}
			}
			
			$flag+=1;
			
				if(@{$Solitaire{$users}}==0)				#当所给成语无可接龙时,重新给出一个随机成语
					{
						$JQ=$Idioms[int(rand()*@Idioms)];
						print "There is not an Idiom to continue!";
						print "The Computer gives a new Idiom: $JQ(No.$flag/10)\n"
					}
					
			else
			{		
			@us=@{$Solitaire{$users}};
			$JQ=$us[int(rand()*@us)];
			print "Computer: $JQ(No.$flag/10)\n";
			}
		}
		
		
			if($flag>=10)
			{
				print"\n";
				print"\n";
				print "*************************************\n";
				print "*************************************\n";
				print "*********Mission all clear!**********\n";
				print "*************************************\n";
				print "*************************************\n";
				print"\n";
				print"\n";
				print "Your final scores : $scores\n";
			}
			print"\n";
			print"\n";
			print "press <Enter> to exit..."; 
			<STDIN> 
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).