Jump to content

I made a simple powershell number guessing gane with the following code.  I have to add a few things to it.  How do i use the get-date function to time how  long the game takes, and second, I have to create a log and it needs to display:  longest and shortest game time, and number of games played.  the log needs to be displayed in notepad, but update the log within the program.  I would appreciate any help

 

$rand = new-object system.random
$number= $rand.next(0,11)
 
do { $a = read-host -prompt "what number do you choose"
    if ($a -gt $Number) {Write-Host "number is too high"}
elseif ($a -lt $Number) {Write-Host "number is too low"}
elseif ($a -eq $Number) {Write-Host "You did it!! It took you $x tries!"}
else {"You have to guess a number!!!"}
$x = $x + 1
   } while ($a -ne $number)
Link to comment
https://linustechtips.com/topic/471543-powershell/
Share on other sites

Link to post
Share on other sites

i think you have to create two variables, one that updates just once and the other keeps on updating, and substract them.

Sorry, it's been a while since I last worked with Powershell.

 

Spoiler

CPU:Intel Xeon X5660 @ 4.2 GHz RAM:6x2 GB 1600MHz DDR3 MB:Asus P6T Deluxe GPU:Asus GTX 660 TI OC Cooler:Akasa Nero 3


SSD:OCZ Vertex 3 120 GB HDD:2x640 GB WD Black Fans:2xCorsair AF 120 PSU:Seasonic 450 W 80+ Case:Thermaltake Xaser VI MX OS:Windows 10
Speakers:Altec Lansing MX5021 Keyboard:Razer Blackwidow 2013 Mouse:Logitech MX Master Monitor:Dell U2412M Headphones: Logitech G430

Big thanks to Damikiller37 for making me an awesome Intel 4004 out of trixels!

Link to comment
https://linustechtips.com/topic/471543-powershell/#findComment-6322663
Share on other sites

Link to post
Share on other sites

For the time, use this:

$stopwatch = New-Object System.Diagnostics.Stopwatch$stopwatch.Start()$stopwatch.Stop()Write-Host $stopwatch

And for the log, maybe this?

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0a6860f5-9b02-4e79-97fe-05829f2c5ab3/open-write-and-close-the-file-using-powershell

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/471543-powershell/#findComment-6324853
Share on other sites

Link to post
Share on other sites

For the time, use this:

$stopwatch = New-Object System.Diagnostics.Stopwatch$stopwatch.Start()$stopwatch.Stop()Write-Host $stopwatch.Elapsed

And for the log, maybe this?

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0a6860f5-9b02-4e79-97fe-05829f2c5ab3/open-write-and-close-the-file-using-powershell

FTFY

 

 

I made a simple powershell number guessing gane with the following code.  I have to add a few things to it.  How do i use the get-date function to time how  long the game takes, and second, I have to create a log and it needs to display:  longest and shortest game time, and number of games played.  the log needs to be displayed in notepad, but update the log within the program.  I would appreciate any help

 

$rand = new-object system.random$number = $rand.next(0,11)$x = 0do{    $a = read-host -prompt "what number do you choose"    if ($a -gt $Number) {        Write-Host "number is too high"    } else {        if ($a -lt $Number) {            Write-Host "number is too low"            } else {                if ($a -eq $Number) {                    Write-Host "You did it!! It took you $x tries!"                    } else {                        "You have to guess a number!!!"                    }                }            }    $x = $x + 1    } while ($a -ne $number)

Depends on how you want the log to function, do you want it to be a high level summary? number of games play, average time taken etc? or do you want it to be a console log that shows everything?

i'd say use a csv file for the first one, and then just load it up when the game loads and pipe each to a variable, do your thing, then overwrite the file with a new file every time one of these var's change.

For a console log, you can re-direct text to a log file by doing a '>> $logfile' at the end of the line you want to re-direct to the text file.

 

you can also use System.IO.StreamWriter if you feel like getting advanced.

 

ps: use powershell_ise you code is a mess, and you need to make sure your variables are pre-defined, $x was keeping it's value from previous runs.

Link to comment
https://linustechtips.com/topic/471543-powershell/#findComment-6328604
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×