[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
随机一种;因未考虑重复步骤。没有用循环。
  1. $one = @{'狼'='';'羊'='';'菜'=''};
  2. $two = @{};
  3. While ( $one.count -gt 0 ) {
  4. $ref1 = Get-Random @($one.keys) -count 1;
  5. $one.remove($ref1);
  6. if ( $two.kes -notcontains $ref1 ) { $two.add($ref1,'') };
  7. if ( ($one.keys -contains '狼' -and $one.keys -contains '羊') -or `
  8. ( $one.keys -contains '羊' -and $one.keys -contains '菜') ) {
  9. $one.add($ref1,'');
  10. $two.remove($ref1);
  11. } else {
  12. Write-host "人+$ref1 过河" -fore green;
  13. if ( $two.count -lt 3 ) {
  14. if ( ($two.keys -contains '狼' -and $two.keys -contains '羊') -or `
  15. ( $two.keys -contains '羊' -and $two.keys -contains '菜') ) {
  16. $ref2 = Get-Random @($two.keys) -count 1;
  17. $one.add($ref2,'');
  18. $two.remove($ref2);
  19. Write-host "人+$ref2 返回" -fore yellow;
  20. } else { Write-host "人 返回" }
  21. }
  22. }
  23. }
复制代码
每次运行都会有一种方法。把随机数改循环可以计算所有。
1

评分人数

TOP

本帖最后由 xczxczxcz 于 2019-3-22 15:58 编辑

补充完整,去重复。
  1. [Collections.Arraylist] $one = @('狼','羊','菜');
  2. [Collections.Arraylist] $two = @();
  3. $back = $null;
  4. While ( $one.count -gt 0 ) {
  5. $ref = Get-Random $one -count 1;
  6. if ( $ref -ne $back ) { $one.remove($ref) }
  7. if ( $two -notcontains $ref ) { $null = $two.add($ref) };
  8. if ( ($one -contains '狼' -and $one -contains '羊') -or `
  9. ( $one -contains '羊' -and $one -contains '菜') ) {
  10. $null = $one.add($ref);
  11. $two.remove($ref);
  12. } else {
  13. if ( $two.count -lt 3 ) {
  14. Write-host "人+$ref 过河 → " -fore green -NoNewLine;
  15. if ( $two.Count -eq 2 ) {
  16. if ( ($two -contains '狼' -and $two -contains '羊') -or `
  17. ( $two -contains '羊' -and $two -contains '菜') ) {
  18. if ( $two[0] -ne $ref ) { $back = $two[0] } else { $back = $two[1] };
  19. $null = $one.add($back);
  20. $two.remove($back);
  21. Write-host "人+$back 返回" -fore yellow;
  22. } else { Write-host "人 返回" }
  23. } else { Write-host "人 返回" }
  24. } else { Write-host "人+$ref 过河 Over" -fore red; break };
  25. }
  26. }
复制代码
随机一组,没有反复过程。编辑美化了一下。
1

评分人数

TOP

返回列表