随机一种;因未考虑重复步骤。没有用循环。- $one = @{'狼'='';'羊'='';'菜'=''};
- $two = @{};
- While ( $one.count -gt 0 ) {
- $ref1 = Get-Random @($one.keys) -count 1;
- $one.remove($ref1);
- if ( $two.kes -notcontains $ref1 ) { $two.add($ref1,'') };
-
- if ( ($one.keys -contains '狼' -and $one.keys -contains '羊') -or `
- ( $one.keys -contains '羊' -and $one.keys -contains '菜') ) {
- $one.add($ref1,'');
- $two.remove($ref1);
- } else {
- Write-host "人+$ref1 过河" -fore green;
- if ( $two.count -lt 3 ) {
- if ( ($two.keys -contains '狼' -and $two.keys -contains '羊') -or `
- ( $two.keys -contains '羊' -and $two.keys -contains '菜') ) {
- $ref2 = Get-Random @($two.keys) -count 1;
- $one.add($ref2,'');
- $two.remove($ref2);
- Write-host "人+$ref2 返回" -fore yellow;
- } else { Write-host "人 返回" }
- }
- }
- }
复制代码 每次运行都会有一种方法。把随机数改循环可以计算所有。 |