PoshCode Logo PowerShell Code Repository

Target Game by Dan In Philly 3 weeks ago
View followups from Dan In Philly | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6882"></script>download | new post

This was the first game I wrote in BASIC as a 12-yr old. Here it is again in PowerShell. :)

  1. Clear
  2. # Draw a vertical line on the right side of the console window
  3.     $position = $Host.UI.RawUI.CursorPosition
  4.     $height = $Host.UI.RawUI.WindowSize.height
  5.     $bottom = ($height - 2)
  6. For($y=2; $y -lt $bottom; $y++){
  7.     $position.x = ($Host.UI.RawUI.WindowSize.width - 5) # Horizontal position
  8.     $position.y = $y # Vertical position
  9.     $Host.UI.RawUI.CursorPosition = $position
  10.     $vertchar = [char]9553
  11. Write-Host $vertchar}
  12.  
  13. # Create a point on the line
  14.     $point = Get-Random -Minimum 4 $bottom
  15.     $position.y = $point
  16.     $Host.UI.RawUI.CursorPosition = $position
  17.     $pointchar = [char]9571
  18. Write-Host $pointchar
  19.  
  20. # User input to guess the position of the point
  21.     $position.x = 0
  22.     $position.y = ($height-2)
  23.     $Host.UI.RawUI.CursorPosition = $position
  24.     $dispBtm = $bottom - 1
  25. $guess = Read-Host "Guess where the point is between 3 and $dispBtm"
  26.  
  27. # Draw a horizontal line at the point of the guess
  28.     $horizChar = [char]9552
  29.     $eraseChar = [char]32
  30.     $width = $Host.UI.RawUI.WindowSize.width
  31. For($x=0; $x -lt ($width - 5); $x++){
  32.     $position = $Host.UI.RawUI.CursorPosition
  33.     $position.x = $x
  34.     $position.y = $guess
  35.     $Host.UI.RawUI.CursorPosition = $position
  36. Write-Host $horizChar
  37. sleep -Milliseconds 25
  38.  
  39. # Reset the horizontal character two spaces behind the current position
  40.     $erase = $x - 1
  41.     If($erase -gt 0){$position.x = $erase}
  42.     $Host.UI.RawUI.CursorPosition = $position
  43. Write-Host $eraseChar}
  44.  
  45. # Show point location
  46.     $position.x = ($width - 3)
  47.     $position.y = $point
  48.     $Host.UI.RawUI.CursorPosition = $position
  49. If($guess -eq $point) {Write-Host $point -ForegroundColor Green}
  50.     Else {Write-Host $point -ForegroundColor Red}
  51.  
  52. # Indicate win or lose and query for another round
  53.     $position.x = 0
  54.     $Host.UI.RawUI.CursorPosition = $position
  55. If($guess -eq $point) {Write-Host "   WINNER!" -ForegroundColor Green}
  56.     Else {Write-Host " $guess is a LOSER" -ForegroundColor Red}
  57. $repeat = Read-Host " Again (y/n)"
  58. If($repeat -ne "y" -or $repeat -ne "Y")
  59.     {Clear
  60.     Exit}
  61. Else {.\target.ps1}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me