我正在尝试在 PowerShell 中开发刽子手游戏。我的脚本一切顺利,但游戏中有一个逻辑错误。我想检查一下,当用户输入相同的单词两次时。它简单地给出错误!例如:单词“足球”。如果用户输入字母“F”超过 1 次,它应该会给出错误消息。这是我的代码:
$random = Get-Random -Minimum 0 -Maximum 5;
$names="America","Iran","Poland","Cat","PowerShell ";
$arrname= $names[$random];
$arrname.ToCharArray();
$ntp="0","1";
$arrlen = $arrname.Length;
clear;
Write-Host "---------Guess the word--------------";
Write-Host "Length is : " $arrname.Length;
$life=3;
do{
Write-Host "Lifes Remain " $life;
$rnd= Read-Host "Guess the word ";
if($flag2 -le 1)
{
$flag=0;
}
else
{
$life--;
}
for($i=0;$i -lt $arrname.Length; $i++)
{
if($rnd -eq $arrname[$i] )
{
$flag =1;
$arrlen --;
}
}
if($flag -eq 0 )
{
$life--;
}
if($arrlen -eq 0)
{
Write-Host $arrname;
Write-Host " ************ You WIN ********************";
break;
}
if($life -eq 0)
{
Write-Host " ------------- You LOST ------------"
break;
}
$p++;
}
while(1);