1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# Board layout $board = @( "_", "_", "_", "_", "_", "_", "_", "_", "_" ) # Function to display the board function Show-Board { Write-Host "" Write-Host " $($board[0]) | $($board[1]) | $($board[2]) " Write-Host "---+---+---" Write-Host " $($board[3]) | $($board[4]) | $($board[5]) " Write-Host "---+---+---" Write-Host " $($board[6]) | $($board[7]) | $($board[8]) " Write-Host "" } # Function to check if the game is won or tied function Check-Win { # Check rows for ($i = 0; $i -lt 9; $i += 3) { if ($board[$i] -eq $board[$i + 1] -and $board[$i + 1] -eq $board[$i + 2] -and $board[$i] -ne "_") { return $board[$i] } } # Check columns for ($i = 0; $i -lt 3; $i++) { if ($board[$i] -eq $board[$i + 3] -and $board[$i + 3] -eq $board[$i + 6] -and $board[$i] -ne "_") { return $board[$i] } } # Check diagonals if ($board[0] -eq $board[4] -and $board[4] -eq $board[8] -and $board[0] -ne "_") { return $board[0] } if ($board[2] -eq $board[4] -and $board[4] -eq $board[6] -and $board[2] -ne "_") { return $board[2] } # Check if the board is full if ($board -notcontains "_") { return "Tie" } return $null } # Main game loop $turn = "X" while ($true) { Show-Board Write-Host "Turn: $turn" Write-Host "Enter the index of the square to place your marker (0-8):" $move = Read-Host # Check if the move is valid if ($move -lt 0 -or $move -gt 8 -or $board[$move] -ne "_") { Write-Host "Invalid move, try again." continue } $board[$move] = $turn $result = Check-Win if ($result) { Show-Board Write-Host "The winner is $result" break } # Switch turns if ($turn -eq "X") { $turn = "O" } else { $turn = "X" } } |