PoshCode Logo PowerShell Code Repository

tic tac toe demo by HeyNow 4 days ago (modification of post by silvia view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6807"></script>download | new post

update $cur variable scope to $script:cur

  1. function frmMain_Show {
  2.   Add-Type -Assembly System.Windows.Forms
  3.   [Windows.Forms.Application]::EnableVisualStyles()
  4.  
  5.   $ico = [Drawing.Icon]::ExtractAssociatedIcon(($PSHome + '\powershell.exe'))
  6.  
  7.   $script:cur = $false #users switch
  8.   $btn = New-Object "Windows.Forms.Button[,]" 3, 3
  9.  
  10.   $frmMain = New-Object Windows.Forms.Form
  11.   $btnPlay = New-Object Windows.Forms.Button
  12.   #
  13.   #btnPlay
  14.   #
  15.   $btnPlay.Location = New-Object Drawing.Point(78, 225)
  16.   $btnPlay.Text = "New Play"
  17.   $btnPlay.Add_Click({
  18.     $script:cur = $false
  19.     $btn | % {$_.Text = [String]::Empty}
  20.   })
  21.   #
  22.   #frmMain
  23.   #
  24.   $frmMain.ClientSize = New-Object Drawing.Size(231, 260)
  25.   $frmMain.Controls.Add($btnPlay)
  26.   $frmMain.FormBorderStyle = [Windows.Forms.FormBorderStyle]::FixedSingle
  27.   $frmMain.Icon = $ico
  28.   $frmMain.MainMenuStrip = $mnuMain
  29.   $frmMain.MaximizeBox = $false
  30.   $frmMain.MinimizeBox = $False
  31.   $frmmain.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
  32.   $frmMain.Text = "Tic Tac Toe"
  33.   $frmMain.Add_Load({
  34.     for ($i = 0; $i -lt 3; $i++) {
  35.       for ($j = 0; $j -lt 3; $j++) {
  36.         $btn[$i, $j] = New-Object Windows.Forms.Button
  37.         $btn[$i, $j].Parent = $this
  38.         $btn[$i, $j].Left = 10 + $j * 70
  39.         $btn[$i, $j].Top = 10 + $i * 70
  40.         $btn[$i, $j].Size = New-Object Drawing.Size(70, 70)
  41.         $btn[$i, $j].Font = New-Object Drawing.Font("Microsoft Sans Serif", 27, [Drawing.FontStyle]::Bold)
  42.         $btn[$i, $j].Add_Click({
  43.           if ([String]::IsNullOrEmpty($this.Text)) {
  44.             switch ($script:cur) {
  45.               $true {
  46.                 $this.ForeColor = [Drawing.Color]::Crimson
  47.                 $this.Text = "O"
  48.                 $script:cur = $false
  49.               }
  50.               default {
  51.                 $this.ForeColor = [Drawing.Color]::DarkGreen
  52.                 $this.Text = "X"
  53.                 $script:cur = $true
  54.               }
  55.             } #switch
  56.           }
  57.         }) #btnX_Click
  58.       } #for
  59.     } #for
  60.   })
  61.  
  62.   [void]$frmmain.ShowDialog()
  63. }
  64.  
  65. frmMain_Show

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