Create PowerShell TicTacToe Game

Boredom induces the need to keep my mind busy. Imagine being stuck in a place with access to only a basic windows 10 workstation and heavily content filtered internet. Luckily, PowerShell is still enabled on the workstation. So you decide to spend your time creating a PowerShell TicTacToe game to pass the time.

Caveats and Warnings

This PowerShell TicTacToe game took me total 6 hours to research (Google search all the code formats) and put together. Warning, I tested it sparingly and I missed following basic coding best practices.

Looking back at my work there are areas of code that need to be shortened and more comments. Secondly, I prefer to consolidate code with numerous method calls instead of repeating code lines. However, for a small project that occupied my thoughts and passed the time, to small project was enjoyable.

PowerShell TicTacToe game source code

<# Tic Tac Toe Game
Created by powersjo. 
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Path = "$Home\Documents"
if(!(Test-Path -Path $Path\TicTacToe)){
    New-Item -Name TicTacToe -Path $Path -ItemType Directory    
}
if(!(Test-Path -Path $Path\TicTacToe\Board)){
    New-Item -Name Board -Path $Path\TicTacToe -ItemType Directory    
}
if(!(Test-Path -Path $Path\TicTacToe\Board\1.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 1.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\2.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 2.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\3.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 3.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\4.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 4.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\5.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 5.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\6.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 6.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\7.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 7.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\8.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 8.txt
}
if(!(Test-Path -Path $Path\TicTacToe\Board\9.txt)){
    New-Item $Path\TicTacToe\Board -ItemType file -Name 9.txt
}
if(!(Test-Path -Path $Path\TicTacToe\player.txt)){
    New-Item $Path\TicTacToe -ItemType file -Name player.txt
}

Set-Content $Path\TicTacToe\player.txt '1'
Set-Content $Path\TicTacToe\Board\1.txt '0'
Set-Content $Path\TicTacToe\Board\2.txt '0'
Set-Content $Path\TicTacToe\Board\3.txt '0'
Set-Content $Path\TicTacToe\Board\4.txt '0'
Set-Content $Path\TicTacToe\Board\5.txt '0'
Set-Content $Path\TicTacToe\Board\6.txt '0'
Set-Content $Path\TicTacToe\Board\7.txt '0'
Set-Content $Path\TicTacToe\Board\8.txt '0'
Set-Content $Path\TicTacToe\Board\9.txt '0'

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '380,290'
$Form.text                       = "Form"
$Form.TopMost                    = $false

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = ""
$Button1.width                   = 30
$Button1.height                  = 30
$Button1.location                = New-Object System.Drawing.Point(115,64)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.text                    = ""
$Button2.width                   = 30
$Button2.height                  = 30
$Button2.location                = New-Object System.Drawing.Point(187,64)
$Button2.Font                    = 'Microsoft Sans Serif,10'

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.text                    = ""
$Button3.width                   = 30
$Button3.height                  = 30
$Button3.location                = New-Object System.Drawing.Point(260,64)
$Button3.Font                    = 'Microsoft Sans Serif,10'

$Button4                         = New-Object system.Windows.Forms.Button
$Button4.text                    = ""
$Button4.width                   = 30
$Button4.height                  = 30
$Button4.location                = New-Object System.Drawing.Point(115,111)
$Button4.Font                    = 'Microsoft Sans Serif,10'

$Button5                         = New-Object system.Windows.Forms.Button
$Button5.text                    = ""
$Button5.width                   = 30
$Button5.height                  = 30
$Button5.location                = New-Object System.Drawing.Point(187,111)
$Button5.Font                    = 'Microsoft Sans Serif,10'

$Button6                         = New-Object system.Windows.Forms.Button
$Button6.text                    = ""
$Button6.width                   = 30
$Button6.height                  = 30
$Button6.location                = New-Object System.Drawing.Point(260,111)
$Button6.Font                    = 'Microsoft Sans Serif,10'

$Button7                         = New-Object system.Windows.Forms.Button
$Button7.text                    = ""
$Button7.width                   = 30
$Button7.height                  = 30
$Button7.location                = New-Object System.Drawing.Point(260,157)
$Button7.Font                    = 'Microsoft Sans Serif,10'

$Button8                         = New-Object system.Windows.Forms.Button
$Button8.text                    = ""
$Button8.width                   = 30
$Button8.height                  = 30
$Button8.location                = New-Object System.Drawing.Point(187,157)
$Button8.Font                    = 'Microsoft Sans Serif,10'

$Button9                         = New-Object system.Windows.Forms.Button
$Button9.text                    = ""
$Button9.width                   = 30
$Button9.height                  = 30
$Button9.location                = New-Object System.Drawing.Point(115,157)
$Button9.Font                    = 'Microsoft Sans Serif,10'

$Button10                        = New-Object system.Windows.Forms.Button
$Button10.text                   = "reset"
$Button10.width                  = 60
$Button10.height                 = 30
$Button10.location               = New-Object System.Drawing.Point(186,207)
$Button10.Font                   = 'Microsoft Sans Serif,10'

$Label1                        = New-Object System.Windows.Forms.Label
$Label1.text                   = "Turn: Player 1"
$Label1.width                  = 60
$Label1.height                 = 30
$Label1.location               = New-Object System.Drawing.Point(10,10)
$Label1.Font                   = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Button1,$Button2,$Button3,$Button4,$Button5,$Button6,$Button7,$Button8,$Button9,$Button10,$Label1))

$Button10.Add_Click({
    Set-Content $Path\TicTacToe\player.txt '1'
    Set-Content $Path\TicTacToe\Board\1.txt '0'
    Set-Content $Path\TicTacToe\Board\2.txt '0'
    Set-Content $Path\TicTacToe\Board\3.txt '0'
    Set-Content $Path\TicTacToe\Board\4.txt '0'
    Set-Content $Path\TicTacToe\Board\5.txt '0'
    Set-Content $Path\TicTacToe\Board\6.txt '0'
    Set-Content $Path\TicTacToe\Board\7.txt '0'
    Set-Content $Path\TicTacToe\Board\8.txt '0'
    Set-Content $Path\TicTacToe\Board\9.txt '0'
    $Button1.Text = ""
    $Button2.Text = ""
    $Button3.Text = ""
    $Button4.Text = ""
    $Button5.Text = ""
    $Button6.Text = ""
    $Button7.Text = ""
    $Button8.Text = ""
    $Button9.Text = ""
    $Label1.Text = "Turn: Player 1"
})

function Cycle-Player{
    $tempPlayer = Get-Content $Path\TicTacToe\player.txt 
    if($tempPlayer -eq 1){
        Set-Content $Path\TicTacToe\player.txt '2'
        $Label1.Text = "Turn: Player 2"
    }
    else{
        Set-Content $Path\TicTacToe\player.txt '1'
        $Label1.Text = "Turn: Player 1"
    }
}

$Button1.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\1.txt
    if($tempSpot -eq 0){
        if($tempPlayer -eq 1){
            $Button1.Text = "X"
            Set-Content $Path\TicTacToe\Board\1.txt '1'
        }
        ElseIf($tempPlayer -eq 2){
            $Button1.Text = "O"
            Set-Content $Path\TicTacToe\Board\1.txt '2'
        }
        Cycle-Player
    }
})

$Button2.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\2.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button2.Text = "X"
                Set-Content $Path\TicTacToe\Board\2.txt '1'
            }
            2{
                $Button2.Text = "O"
                Set-Content $Path\TicTacToe\Board\2.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button3.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\3.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button3.Text = "X"
                Set-Content $Path\TicTacToe\Board\3.txt '1'
            }
            2{
                $Button3.Text = "O"
                Set-Content $Path\TicTacToe\Board\3.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button4.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\4.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button4.Text = "X"
                Set-Content $Path\TicTacToe\Board\4.txt '1'
            }
            2{
                $Button4.Text = "O"
                Set-Content $Path\TicTacToe\Board\4.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button5.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\5.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button5.Text = "X"
                Set-Content $Path\TicTacToe\Board\5.txt '1'
            }
            2{
                $Button5.Text = "O"
                Set-Content $Path\TicTacToe\Board\5.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button6.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\6.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button6.Text = "X"
                Set-Content $Path\TicTacToe\Board\6.txt '1'
            }
            2{
                $Button6.Text = "O"
                Set-Content $Path\TicTacToe\Board\6.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button7.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\7.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button7.Text = "X"
                Set-Content $Path\TicTacToe\Board\7.txt '1'
            }
            2{
                $Button7.Text = "O"
                Set-Content $Path\TicTacToe\Board\7.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button8.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\8.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button8.Text = "X"
                Set-Content $Path\TicTacToe\Board\8.txt '1'
            }
            2{
                $Button8.Text = "O"
                Set-Content $Path\TicTacToe\Board\8.txt '2'
            }
        }
        Cycle-Player
    }
})

$Button9.Add_Click({
$tempPlayer = Get-Content $Path\TicTacToe\player.txt 
$tempSpot = Get-Content $Path\TicTacToe\Board\9.txt
    if($tempSpot -eq 0){
        switch ($tempPlayer){
            1{
                $Button9.Text = "X"
                Set-Content $Path\TicTacToe\Board\9.txt '1'
            }
            2{
                $Button9.Text = "O"
                Set-Content $Path\TicTacToe\Board\9.txt '2'
            }
        }
        Cycle-Player
    }
})

$Form.ShowDialog()

In conclusion, the end product is very bare bones. The featured image above gives a screenshot of the finished product. Additionally, feel free to copy and use or improve or disregard the code. Finally, just consider the work an open source contribution to the internet. Thanks for reading.

If you want to check out my previous blog post on code / scripting click here.

If you want to read a different topic try any of my book reviews.

Most of the help I found online was located here at StackOverflow.

Review of “8 Seconds of Courage: a Soldier’s Story from Immigrant to the Medal of Honor”

8 Seconds of Courage is short book with a deep inspiring story about Flo Groberg’s life from a child in France to the award ceremony for the Medal of Honor. Step by step you read about the events and people that influenced Flo’s life and decisions towards the Medal of Honor. Truthfully, the meritorious recognition is just the country’s way to recognize the commitment and sacrifice that Flo and his Soldiers already committed. The meat and substance lies in the journey to the heroism and then the recovery afterwards.

Key Components

This book really highlights the journey it takes to move on instinct to protect your comrades. Flo is positively influenced by his uncle, finishes college, then joins the army. He then completes Army ranger school and leads two tours in the Middle East. It’s the memories, good and bad, that help lead him to make the right choices that protect lives. However, the 8 seconds of heroism is not the end of the voyage.

After the 8 Seconds of Courage comes recovery. This is where the battle of regret, self worth, courage to face loss and heartbreak edges on for years. Not weeks or months, it’s years of pain. It’s easy to highlight the act of valor or the ceremony for the Medal of Honor. Yet I found the most value in the story of the path to the salute to courage and the dark recovery within.

I fully recommend this book. It is a quick read but I was not able to put it down. The progression features the important points that the reader needs to understand to appreciate the journey of Flo.

You can find 8 Seconds of Courage anywhere books are sold. Here it is, sold on Amazon: https://www.amazon.com/Seconds-Courage-Soldiers-Story-Immigrant/dp/1501165887

Check out my last book review here.

Download CPU App

Why a Download CPU app? Why would I create an app to Download CPU? Well, of course to enable everyone on Android phones to download CPU. “Isn’t that silly?” some people may ask. Yes, it is ridiculous, but I did it for fun.

I started off the Download CPU with just pictures in paint to get my concept and design down.

Once I had the concept down, I used this silly app to learn more things about developing Android apps. I mainly learned moving back and forth through Activities and putting in more logic in the app (such as flashing the background randomly). Changing the text proved to be challenging, I had to learn about the Handler() method and delaying posts. I also got better making a 3D image and working overall with the graphics of the app. This ended up being the finished product.

Overall, I’m happy with the finished product. I learned a lot more about developing an Android app and got to make a silly Download CPU in the process.

If you want to download this app you can here:
https://play.google.com/store/apps/details?id=com.powersjo.downloadcpu

I made a splash page for this app here, it has more pictures made from the 3D image that I created for the app.