[玩转系统] PowerShell 左-中-右
作者:精品下载站 日期:2024-12-14 07:58:00 浏览:14 分类:玩电脑
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 类来定义玩家对象。每个回合都会显示当前状态以及玩家滚动的内容。
游戏继续在每个玩家之间循环,直到只剩下一个玩家有筹码。
我还添加了一个名为自动播放的参数,它将以 1 秒的间隔自动循环播放游戏。
我很高兴把这些放在一起,并且花了比我应该花的更多的时间。即便如此,它还远非完美。事实上,如果您想尝试扩展代码,我已经留下了一些增强建议。我希望你玩得开心。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag