0

我正在尝试在 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); 
4

2 回答 2

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 ------------"
}
于 2013-11-15T14:20:38.183 回答
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 ------------"
}
于 2021-05-10T20:05:29.130 回答