TicTacToe in Powershell

So for fun I thought I would create a two player game of tictactoe using Powershell.  It was a good excuse to mess with a multidimensional array, as you will see below.  The variable for the multidimensional array is called $ticTacMatrix, it is how I set up the grid for the game.

$ticTacMatrix=@();
$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$gridCount = 9;

function playGame(){
    ##This function populates the matrix
    param($gridPlace1,$gridPlace2,$placeValue);
    $gridPlace1 = Read-Host "Position 1";
    $gridPlace2 = Read-Host "Position 2";
    if($placeValue -notmatch "[X,O]"){
        Write-Output "invalid";
    }elseif([int]$gridPlace1 -gt 2 -or [int]$gridPlace2 -gt 2){
        Write-Output "outofrange";
    }elseif($ticTacMatrix[$gridPlace1][$gridPlace2] -match "[X,O]"){
        Write-Output "inuse";
    } else {
        $ticTacMatrix[$gridPlace1][$gridPlace2] = $placeValue;
    }
}

function checkwin(){
    ##This function checks to see if there is a winner
    param($gameBoard);
    $winner = '';
    $firstColumn=@();
    $firstColumn = $gameBoard[0][0],$gameBoard[1][0],$gameBoard[2][0];
    $secondColumn=@();
    $secondColumn = $gameBoard[0][1],$gameBoard[1][1],$gameBoard[2][1];
    $thirdColumn=@();
    $thirdColumn = $gameBoard[0][2],$gameBoard[1][2],$gameBoard[2][2];

    $firstDiagonal=@();
    $firstDiagonal = $gameBoard[0][0],$gameBoard[1][1],$gameBoard[2][2];
    $secondDiagonal=@();
    $secondDiagonal = $gameBoard[0][2],$gameBoard[1][1],$gameBoard[2][0];

    if(($gameBoard[0] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[1] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[2] -match 'X').count -eq 3){$winner='X';}
    if(($firstColumn -match 'X').count -eq 3){$winner='X';}
    if(($secondColumn -match 'X').count -eq 3){$winner='X';}
    if(($thirdColumn -match 'X').count -eq 3){$winner='X';}
    if(($firstDiagonal -match 'X').count -eq 3){$winner='X';}
    if(($secondDiagonal -match 'X').count -eq 3){$winner='X';}

    if(($gameBoard[0] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[1] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[2] -match 'O').count -eq 3){$winner='O';}
    if(($firstColumn -match 'O').count -eq 3){$winner='O';}
    if(($secondColumn -match 'O').count -eq 3){$winner='O';}
    if(($thirdColumn -match 'O').count -eq 3){$winner='O';}
    if(($firstDiagonal -match 'O').count -eq 3){$winner='O';}
    if(($secondDiagonal -match 'O').count -eq 3){$winner='O';}

    if($winner -eq 'X' -or $winner -eq 'O'){
        Write-Output $winner;
    }
}

Clear-Host;
Write-Host "Welcome to Powershell based TicTacToe" -ForegroundColor 'Green';
Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
Write-Host '';

Foreach($row in $ticTacMatrix){$row -join '|';}

$inputVal = 1;
$everyOther = 'no';
$player = 'X';
while($inputVal -le $gridCount){
    Write-Host '';
    Write-Host "Player: $player" -ForegroundColor 'Cyan';
    $gamePlayValue = playGame -placeValue $player;
    if($gamePlayValue -eq 'inuse'){
        Write-Host "Already in use" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }elseif($gamePlayValue -eq 'outofrange'){
        Write-Host "Position out of range" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }elseif($gamePlayValue -eq 'invalid'){
        Write-Host 'Invalid player' -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }
    Clear-Host;
    Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
    Write-Host '';
    Foreach($row in $ticTacMatrix){$row -join '|';}
    $gameOver = checkWin -gameBoard $ticTacMatrix;
    if($gameOver -eq 'X'){
        Write-Host '';
        Write-Host 'X Wins' -ForegroundColor 'Green';
        Break;
    }
    if($gameOver -eq 'O'){
        Write-Host '';
        Write-Host 'O Wins' -ForegroundColor 'Green';
        Break;
    }
    if($everyOther -eq 'no'){
        $player = 'O';
        $everyOther = 'yes';
    }else{
        $player = 'X';
        $everyOther = 'no';
    }
    $inputVal++;
}

if($inputVal -eq 10){
    Write-Host "No Winners" -ForegroundColor 'Red';
}

 

Leave a comment