Indonesia Institute of Arts, Denpasar**We aren't endorsed by this school
Course
ART 2022
Subject
Computer Science
Date
Dec 26, 2024
Pages
4
Uploaded by CwEUlahu
tictactoe.ps1RawThis file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the filein an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersShow hidden characters1# Tic Tac Toe in Windows Powershell, by Adam Spencer - Source on GitHub Gists23# Debug4function Display-DebugText () {5Write-Host Player: $Global:player6Write-Host Board: $Global:board7Write-Host Finished: $Global:finished8Write-Host win_row_top: $win_row_top9Write-Host win_row_mid: $win_row_mid10Write-Host win_row_low: $win_row_low11Write-Host win_col_lef: $win_col_lef12Write-Host win_col_mid: $win_col_mid13Write-Host win_col_rig: $win_col_rig14Write-Host win_dia_lef: $win_dia_lef15Write-Host win_dia_rig: $win_dia_rig16}1718# Print the board to the console19function Print-Board {20Write-Host (-join("[", (Interpret-Integer($Global:board[0])), "][", (Interpret-Integer($Global:board[1])), "][", (Interpret-Integer($Global:board[2])), "]"))21Write-Host (-join("[", (Interpret-Integer($Global:board[3])), "][", (Interpret-Integer($Global:board[4])), "][", (Interpret-Integer($Global:board[5])), "]"))22Write-Host (-join("[", (Interpret-Integer($Global:board[6])), "][", (Interpret-Integer($Global:board[7])), "][", (Interpret-Integer($Global:board[8])), "]"))23}2425# Takes and integer and returns the character value for it: 1->O, 2->X, else SPACE26function Interpret-Integer ($int) {2728if($int -eq 1) {29Return "O"30}31if($int -eq 2) {32Return "X"33}3435Return " "36}3738# Get user input from host39function Get-UserInput {4041$Local:character = Interpret-Integer ($Global:player)4243$user_input = Read-Host (-join("Input for player ", $Global:player, ". (You are ", $Local:character, ")"))44$user_input -= 14546if (Is-Numeric($user_input) -eq 0) {47Write-Host "Provide the numeric index of the square you want to claim."48return Get-UserInput49}5051if ($Global:board[$user_input] -ne 0) {
52Write-Host "You cannot claim that square!"53return Get-UserInput54}5556Return $user_input57}5859# Is the value a number60function Is-Numeric ($Value) {6162if($Value -match "^[\d\.]+$"){63Return 064}6566return 167}6869# Claim the given square (place an X or O in it)70function Claim-Square ($square_index) {71$Global:board[$square_index] = $Global:player72}7374# Change the active player turn75function Increment-Player {76if ($Global:player -eq 1) {77$Global:player = 278} else {79$Global:player = 180}818283Write-Host (-join("It is now player ", $player, "'s turn"))84}8586# Check win conditions87function Check-Wins {88$Local:conditions = $Global:win_row_top, $Global:win_row_mid, $Global:win_row_low, $Global:win_col_lef, $Global:win_col_mid, $Global:win_col_rig, $Global:8990# For each condition91foreach ($condition in $Local:conditions) {9293# Only need to check the player who just moved9495if (Is-ConditionValid($condition) -eq 1) {96return 197} 98}99100# No win condition is fulfilled! Is the board full?101if (Is-BoardFull -eq 1) {102return 2103}104105return 0106}
106}107108function Is-BoardFull {109110foreach ($i in $Global:board) {111if ($i -eq 0) {112return 0113}114}115116return 1117}118119# Check whether the given win condition is fulfilled120function Is-ConditionValid ($condition) {121122Write-Host 123124# Check each index of the condition125for ($i = 0; $i -lt 9; $i++) {126127# Compare condition index to board index128# If there is something other than the current player at any of the condition's indices, it is false129# If all indices in the condition/board match, it is true130131# If this index matters to the condition132if ($condition[$i] -eq 1) {133134# If this index on the board is not the current player135136if ($board[$i] -ne $Global:player) {137return 0138}139}140141}142143return 1144}145146# Game logic147148clear149150# Declarations151$Global:player = 1152$Global:board = 0,0,0,0,0,0,0,0,0153$Global:finished = 0154155# Win conditions156$Global:win_row_top = 1,1,1,0,0,0,0,0,0157$Global:win_row_mid = 0,0,0,1,1,1,0,0,0158$Global:win_row_low = 0,0,0,0,0,0,1,1,1159160$Global:win_col_lef = 1,0,0,1,0,0,1,0,0
161$Global:win_col_mid = 0,1,0,0,1,0,0,1,0162$Global:win_col_rig = 0,0,1,0,0,1,0,0,1163164$Global:win_dia_lef = 1,0,0,0,1,0,0,0,1165$Global:win_dia_rig = 0,0,1,0,1,0,1,0,0166167# Display-DebugText168169170while ($finished -eq 0) {171Print-Board172$user_input = Get-UserInput173Claim-Square($user_input)174$Local:winner = Check-Wins175176if ($Local:winner -eq 1) {177clear178Print-Board179Write-Host (-join("Player ", $Global:player, " has won!"))180pause181exit182}183184if ($Local:winner -eq 2) {185clear186Print-Board187Write-Host (-join("Stalemate! All positions are filled and there is no winner!"))188pause189exit190}191192Increment-Player193194Clear-Host195}