当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 左-中-右

作者:精品下载站 日期:2024-12-14 07:58:00 浏览:14 分类:玩电脑

PowerShell 左-中-右


[玩转系统] PowerShell 左-中-右

通常情况下,我会在周五有趣的文章中留下这样的帖子。但考虑到这些天的情况,我想你们中的一些人可能希望以一点乐趣来开始新的一周。甚至可能在此过程中学习一点 PowerShell。

如果你的家人和我的一样,你希望能花更多的时间参加家庭游戏之夜。我们喜欢的游戏之一是 LCR,它代表 Left-Center-Right。这是一款有趣的游戏,不需要策略或思考,因为一切都依赖于随机掷 3 个骰子。您可以使用常规骰子或获取一组特定的 LCR 骰子。我从亚马逊购买了一款游戏。

每个玩家获得 3 个筹码。您可以掷骰子,筹码数最多为 3 个。每个骰子作为一个 L、C 和 R 点,指示通过您的筹码之一的方式:左、中或右。剩余的点是中性的,这意味着您可以保留筹码。游戏继续进行,直到只剩下一名玩家剩余筹码。他们赢了并将底池放在中间。请参阅 https://www.wikihow.com/Play-LCR 了解更多信息。

不管怎样,我决定编写一个 PowerShell 版本的游戏。该游戏包含在一个脚本文件中,您可以在 Github 上找到该脚本文件的要点。

LCR.ps1:

#requires -version 5.1



# Usage: .\lcr.ps1 -playername Fred,Wilma,Barney,Betty

[cmdletbinding()]
Param(
    [Parameter(Position = 0, Mandatory, HelpMessage = "Enter the name of the people playing at least 3 and no more than 6.")]
    [ValidateNotNullOrEmpty()]
    [string[]]$PLayerName,
    [Parameter(HelpMessage = "Auto play the game with 1 second round intervals")]
    [switch]$AutoPlay
)

if ($playerName.count -lt 3 -OR $playerName.count -gt 6) {
    Write-Warning "This game needs between 3 and 6 players."
    #bail out
    Return
}

#the character to display for Hold
$hold = [char]0x25cf  # "H"

# Game title to be displayed
$title = @"
  _      _____ _____
 | |    / ____|  __ \
 | |   | |    | |__) |
 | |   | |    |  _  /
 | |____ |____| | \ \
 |______\_____|_|  \_\

"@

# helper function to display a colorized fancy title
Function fancyTitle {
    Param([string]$TitleText = $Title)
    $s = $titleText.Split("`n")
    $colors = "Yellow", "Red", "Cyan", "White"
    $c = 0
    for ($i = 0; $i -le $s.count; $i += 2) {
        $color = $colors[$c]
        Write-Host $s[$i] -ForegroundColor $color
        Write-Host $s[$i + 1] -ForegroundColor $color
        $c++
    }
}

#I could also use one giant array and randomly select 3
#but I decided to create 3 die to better simulate a roll
$d1 = "L", "C", "R", "*", "*", "*"
$d2 = "L", "C", "R", "*", "*", "*"
$d3 = "L", "C", "R", "*", "*", "*"
$dice = $d1, $d2, $d3

#A PowerShell class to define a player object
class Player {
    [string]$Name
    [int]$Chips = 3
    #this will indicate the player to the left
    [int]$Left
    #this will indicate the plaer to the right
    [int]$Right
    #this will indicate the players postion in the circle
    [int]$Position

    [void]UpdatePartners([int]$TotalNumberPlayers) {
        #Left
        if ($this.Position + 1 -ge $TotalNumberPlayers) {
            $this.left = $this.Position - ($TotalNumberPlayers - 1)
        }
        else {
            $this.left = $this.position + 1
        }
        #Right
        if ($this.position - 1 -lt 0 ) {
            $this.right = ($TotalNumberPlayers - 1)
        }
        else {
            $this.right = $this.position - 1
        }
    } #update partners

    #the constructor
    player ([string]$Name, [int]$Position) {
        $this.name = $Name
        $this.Position = $Position
    }
} #player class

#a helper function to create a new player object
Function New-Player {
    [cmdletbinding()]
    Param(
        [string]$Name,
        [ValidateRange(1, 6)]
        [int]$PlayerNumber,
        [ValidateRange(3, 6)]
        [int]$TotalPlayers = 4
    )

    $p = [player]::new($Name, ($PlayerNumber - 1))
    $p.UpdatePartners($TotalPlayers)
    $p
} #New-Player function

#create the array of players
$n = 0
$players = @()
foreach ($name in $PlayerName) {
    $n++
    $players += New-Player -name $name -PlayerNumber $n -TotalPlayers $PLayerName.count
}

#initialize some counters
$center = 0
#an index number used to identify the player
$i = 0
#a counter for the total number of game rounds
$c = 0

#Each loop is a round of the game. Continue until only 1 player has chips remaining.
#That player is the winner
do {

    $c++
    Clear-Host
    #Write-Host $title -ForegroundColor Green
    fancyTitle

    #display current status
    $players | Format-Table -GroupBy @{Name = "Center"; Expression = {$center}} -Property Name, Chips

    #get the next player to roll
    $i++
    $current = $players.Item($i - 1)

    Write-Host "$($current.name) rolls:" -NoNewline

    #how many dice to roll depends on number of chips.
    #You can only roll a number of dice less than or equal to your total chip count
    if ($current.Chips -ge 3) {
        $roll = $dice | Get-Random -count 3
    }
    elseif ($current.chips -ge 2) {
        $roll = $dice[0..1] | Get-Random -count 2
    }
    elseif ($current.chips -eq 1) {
        $roll = $dice[0] | Get-Random -count 1
    }
    else {
        #You can't roll if you have no chips
        Write-Host " Skipping $($current.name)" -ForegroundColor yellow
        $roll = $null
    }

    #process the roll - moving chips as dictated by the roll
    if ($roll) {
        foreach ($die in $roll) {

            Switch ($die) {
                "L" {
                    Write-Host " L " -NoNewline -ForegroundColor Yellow
                    $Current.chips--
                    $($players.where( {$_.position -eq $current.left })).chips++
                }
                "C" {
                    Write-Host " C " -NoNewline -ForegroundColor Red
                    $current.chips--
                    $center++
                }
                "R" {
                    Write-Host " R " -NoNewline -ForegroundColor cyan
                    $Current.chips--
                    $($players.where( {$_.position -eq $current.Right })).chips++
                }
                Default {
                    Write-Host " $hold " -NoNewline -ForegroundColor green
                }
            }
        }
        Write-Host " "
    } #if $roll

    #reset counter
    If ($i -eq $players.count) {
        $i = 0
    }

    if ($AutoPlay) {
        Start-Sleep -Seconds 1
    }
    Else {
        #press enter for the next round
        Write-Host " "
        Pause
    }
} until ($players.where( {$_.chips -ne 0}).count -eq 1)

$winner = $players.where( {$_.chips -ne 0})
Write-Host "The winner in $c rounds is $($winner.name) with a chip total of $($winner.chips)." -ForegroundColor Green

游戏需要至少 3 名玩家。从技术上讲,没有最大值,但我的版本将其限制为 6。

C:\scripts\lcr.ps1 -playername John,Paul,George,Ringo

该脚本文件使用 PowerShell 类来定义玩家对象。每个回合都会显示当前状态以及玩家滚动的内容。

[玩转系统] PowerShell 左-中-右

游戏继续在每个玩家之间循环,直到只剩下一个玩家有筹码。

[玩转系统] PowerShell 左-中-右

我还添加了一个名为自动播放的参数,它将以 1 秒的间隔自动循环播放游戏。

我很高兴把这些放在一起,并且花了比我应该花的更多的时间。即便如此,它还远非完美。事实上,如果您想尝试扩展代码,我已经留下了一些增强建议。我希望你玩得开心。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯