function drawgrid { ######################################### # update display # ######################################### write-host $line1 = " "+$row1[0]+" | "+$row1[1]+" | "+$row1[2] $line1 $dividerline = " ----------" $dividerline $line2 = " "+$row2[0]+" | "+$row2[1]+" | "+$row2[2] $line2 $dividerline $line3 = " "+$row3[0]+" | "+$row3[1]+" | "+$row3[2] $line3 write-host } ######################################### # setup and instructions # ######################################### $gameover = 'false' $row1= @("7","8","9") $row2 = @("4","5","6") $row3 = @("1","2","3") write-host write-host "tic-tac-toe" write-host "Enter a number (1-9) to place an X in that square" write-host "Press Q to give up, R to restart" drawgrid ######################################### # game loop # ######################################### while ($gameover -ne 'true') { ######################################### # get user input # ######################################### $target = Read-Host "Enter the grid number: " if (($target -ge 1) -and ($target -le 9)) { ##### need to add checks for R and Q inputs ######################################################## # check that the user input points to an empty square # ######################################################## switch ($target) { 1 { if($row3[0] -eq "1") { $row3[0] = "X" } } 2 { if($row3[1] -eq "2") { $row3[1] = "X" } } 3 { if($row3[2] -eq "3") { $row3[2] = "X" } } 4 { if($row2[0] -eq "4") { $row2[0] = "X" } } 5 { if($row2[1] -eq "5") { $row2[1] = "X" } } 6 { if($row2[2] -eq "6") { $row2[2] = "X" } } 7 { if($row1[0] -eq "7") { $row1[0] = "X" } } 8 { if($row1[1] -eq "8") { $row1[1] = "X" } } 9 { if($row1[2] -eq "9") { $row1[2] = "X" } } } ########################### # redraw the grid # ########################### drawgrid } if ($target -eq 'Q') { $gameover = 'true' } if ($target -eq 'R') { $row1= @("7","8","9") $row2 = @("4","5","6") $row3 = @("1","2","3") drawgrid } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)