我正在尝试在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);
答案 0 :(得分:1)
在这里,这应该可以帮助您顺利完成,跟踪字母将它们添加到数组$guesses=@()
然后检查它们是否已使用包含字母$guesses -contains $guessLetter
的字母
$random = Get-Random -Minimum 0 -Maximum 5;
$names="America","Iran","Poland","Cat","PowerShell";
$targetWord = $names[$random];
[Char[]]$wordProgress = "_" * $targetWord.Length
clear;
Write-Host "---------Guess the word--------------";
$life=3;
$guesses=@()
do
{
do
{
Write-Host "[$($targetWord.Length)] $wordProgress";
Write-Host "Lifes Remain " $life
$guessLetter = Read-Host "Guess a letter:"
if ($guesses -contains $guessLetter)
{
"Try another letter!"
}
} while ($guesses -contains $guessLetter)
$guesses+=$guessLetter
$guesses -join ','
$foundLetter = $false
for($i=0;$i -lt $targetWord.Length; $i++)
{
if($guessLetter -like $targetWord[$i] )
{
$wordProgress[$i] = $guessLetter
$foundLetter=$true
}
}
if(!$foundLetter)
{
$life--;
}
if($($wordProgress -join '') -like $targetWord)
{
Write-Host $targetWord;
Write-Host " ************ You WIN ********************";
break;
}
}
while($life -gt 0)
if ($life -eq 0)
{
Write-Host " ------------- You LOST ------------"
}
答案 1 :(得分:1)
这是我对编码的小改动,我决定给 5 个生命并在词库中添加 5 个单词(这是我第一次在不需要帮助的情况下这样做终于。)!如果它有错误并且不会让您运行它,请尝试复制原始答案而不添加我的代码,然后它会运行,或者在您完成编辑代码后转到底部而不单击并尝试运行它。
$random = Get-Random -Minimum 0 -Maximum 10;
$names="America","Iran","Poland","Cat","PowerShell","Cow","How","Such","Dog","Dough";
$targetWord = $names[$random];
[Char[]]$wordProgress = "_" * $targetWord.Length
clear;
Write-Host "---------Guess the word--------------";
$life=5;
$guesses=@()
do
{
do
{
Write-Host "[$($targetWord.Length)] $wordProgress";
Write-Host "Lifes Remain " $life
$guessLetter = Read-Host "Guess a letter:"
if ($guesses -contains $guessLetter)
{
"Try another letter!"
}
} while ($guesses -contains $guessLetter)
$guesses+=$guessLetter
$guesses -join ','
$foundLetter = $false
for($i=0;$i -lt $targetWord.Length; $i++)
{
if($guessLetter -like $targetWord[$i] )
{
$wordProgress[$i] = $guessLetter
$foundLetter=$true
}
}
if(!$foundLetter)
{
$life--;
}
if($($wordProgress -join '') -like $targetWord)
{
Write-Host $targetWord;
Write-Host " ************ You WIN ********************";
break;
}
}
while($life -gt 0)
if ($life -eq 0)
{
Write-Host " ------------- You LOST ------------"
}