Last active
April 11, 2023 20:43
-
-
Save itspluxstahre/38c471826c12da4fe6a49fcbc53affcb to your computer and use it in GitHub Desktop.
Stupid TicTacToe in Powershell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
A simple PowerShell script to play Tic Tac Toe against the computer. | |
.DESCRIPTION | |
The script creates a 3x3 Tic Tac Toe board and allows the user to play against the computer. The computer will make | |
optimal moves to win or block the player's winning moves. | |
#> | |
# Display-Board function | |
<# | |
.SYNOPSIS | |
Displays the current state of the Tic Tac Toe board. | |
.DESCRIPTION | |
Clears the console and prints the Tic Tac Toe board with row and column separators. | |
.PARAMETER board | |
A 3x3 matrix representing the Tic Tac Toe board state. | |
.EXAMPLE | |
Display-Board $board | |
#> | |
function Display-Board { | |
param($board) | |
Clear-Host | |
for ($i = 0; $i -lt 3; $i++) { | |
for ($j = 0; $j -lt 3; $j++) { | |
Write-Host -NoNewline (" " + $board[$i][$j]) | |
if ($j -lt 2) { | |
Write-Host -NoNewline " |" | |
} | |
} | |
if ($i -lt 2) { | |
Write-Host "" | |
Write-Host "-----------" | |
} | |
} | |
Write-Host "" | |
} | |
# Check-Winner function | |
<# | |
.SYNOPSIS | |
Checks if the current board state has a winner. | |
.DESCRIPTION | |
Examines the Tic Tac Toe board to determine if there is a winner by checking rows, columns, and diagonals for a | |
winning combination. | |
.PARAMETER board | |
A 3x3 matrix representing the Tic Tac Toe board state. | |
.OUTPUTS | |
Returns true if a winning combination is found, false otherwise. | |
.EXAMPLE | |
$result = Check-Winner $board | |
#> | |
function Check-Winner { | |
param($board) | |
for ($i = 0; $i -lt 3; $i++) { | |
if (($board[$i][0] -eq $board[$i][1]) -and ($board[$i][1] -eq $board[$i][2])) { | |
return $true | |
} | |
if (($board[0][$i] -eq $board[1][$i]) -and ($board[1][$i] -eq $board[2][$i])) { | |
return $true | |
} | |
} | |
if (($board[0][0] -eq $board[1][1]) -and ($board[1][1] -eq $board[2][2])) { | |
return $true | |
} | |
if (($board[0][2] -eq $board[1][1]) -and ($board[1][1] -eq $board[2][0])) { | |
return $true | |
} | |
return $false | |
} | |
# Get-ComputerMove function | |
<# | |
.SYNOPSIS | |
Determines the computer's next move. | |
.DESCRIPTION | |
Analyzes the current board state and selects the computer's next move based on the following priority: | |
1. Winning move | |
2. Blocking opponent's winning move | |
3. Random available move | |
.PARAMETER board | |
A 3x3 matrix representing the Tic Tac Toe board state. | |
.OUTPUTS | |
Returns the computer's selected move as an integer from 1 to 9. | |
.EXAMPLE | |
$move = Get-ComputerMove $board | |
#> | |
function Get-ComputerMove { | |
param($board) | |
function Test-Move { | |
param($board, $move, $playerSymbol) | |
$row = [math]::Ceiling($move / 3) - 1 | |
$col = ($move - 1) % 3 | |
$testBoard = @(@(, $board[0][0], $board[0][1], $board[0][2]), @(, $board[1][0], $board[1][1], $board[1][2]), @(, $board[2][0], $board[2][1], $board[2][2])) | |
$testBoard[$row][$col] = $playerSymbol | |
return (Check-Winner $testBoard) | |
} | |
$availableMoves = @() | |
$winningMove = 0 | |
$blockingMove = 0 | |
for ($i = 0; $i -lt 3; $i++) { | |
for ($j = 0; $j -lt 3; $j++) { | |
if ($board[$i][$j] -ne "X" -and $board[$i][$j] -ne "O") { | |
$move = $i * 3 + $j + 1 | |
$availableMoves += , $move | |
if (Test-Move $board $move $playerSymbols[1]) { | |
$winningMove = $move | |
} | |
elseif (Test-Move $board $move $playerSymbols[0]) { | |
$blockingMove = $move | |
} | |
} | |
} | |
} | |
if ($winningMove -ne 0) { | |
return $winningMove | |
} | |
elseif ($blockingMove -ne 0) { | |
return $blockingMove | |
} | |
else { | |
return $availableMoves | Get-Random | |
} | |
} | |
# Main game loop | |
$board = @(@(1, 2, 3), @(4, 5, 6), @(7, 8, 9)) | |
$playerSymbols = @("X", "O") | |
$currentPlayer = 0 | |
while ($true) { | |
Display-Board $board | |
if ($currentPlayer -eq 0) { | |
$move = Read-Host ("Player " + ($currentPlayer + 1) + " (" + $playerSymbols[$currentPlayer] + "), enter your move (1-9)") | |
if (-not ($move -match "^[1-9]$")) { | |
Write-Host "Invalid move. Please enter a number between 1 and 9." | |
continue | |
} | |
} | |
else { | |
$move = Get-ComputerMove $board | |
Write-Host "Player 2 (computer) played at position:" $move | |
Start-Sleep -Seconds 2 | |
} | |
$row = [math]::Ceiling($move / 3) - 1 | |
$col = ($move - 1) % 3 | |
if ($board[$row][$col] -eq "X" -or $board[$row][$col] -eq "O") { | |
Write-Host "This position is already taken. Please choose another one." | |
continue | |
} | |
$board[$row][$col] = $playerSymbols[$currentPlayer] | |
if (Check-Winner $board) { | |
Display-Board $board | |
Write-Host "Player " ($currentPlayer + 1) " (" $playerSymbols[$currentPlayer] ") wins!" | |
break | |
} | |
$draw = $true | |
for ($i = 0; $i -lt 3; $i++) { | |
for ($j = 0; $j -lt 3; $j++) { | |
if ($board[$i][$j] -ne "X" -and $board[$i][$j] -ne "O") { | |
$draw = $false | |
break | |
} | |
} | |
} | |
if ($draw) { | |
Display-Board $board | |
Write-Host "It's a draw!" | |
break | |
} | |
$currentPlayer = 1 - $currentPlayer | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment