Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Above are the matlab scripts I have. I\'m writing a code to create a tic tac toe

ID: 3860127 • Letter: A

Question

Above are the matlab scripts I have. I'm writing a code to create a tic tac toe game.

Need help to modify the CompMove code above (the second pic), which the computer knows how to block a winning move and makes a winning move when there is one, otherwise make a random move. The code above now is just the computer makes a vaild random move all the time.

Please help me with this, and make sure the answer you give could work with the scripts above. Thank you!

FILE NAVIGATE BREAKPOINTS RUN sTicTacToe main script that will call necessary function to play the game gameBoard [0,0,0; 0,0,0; 0,0,0]; 4 validMoves = 1; winner = 17; while (validMoves > 0) %USER gameBoarduserMove(gameBoard); winner -winnerLoserTie(gameBoard); if (winner= 0) Il (winner=-1) ll (winner= 1) 10- break; 12 13 - 14 15 - 16 - end validMoves = anyMovesRemaining(gameBoard); if (val!dMoves == 0) break end sComputer gameBoard compMove ( gameBoard) winner winnerLoserTie(gameBoard); if (winner= 0) ll (winner=-1) ll (winner= 1) 18 19 - 20- 21 - 22 - 23 - 24 25 26 - 27 - 28 - 29 30 - 31- 32 break; end va idMoves = anyMovesRemaining ( gameBoard); if (validMoves == 0) break end end %Display who won gameBoardgameBoard if (winner := 1) fprintf('You won!An) elseif (winner == -1)

Explanation / Answer

Function gameState = compMove(board

%computer makes a move at random

State = false

While (state ==false)

num=randi(9);

If((num>=1)&&(num<=3))

if¬(board(num,1)==-1)&& ¬(board(num,1)==1)

board(num,1)==-1;

state= true;

while sum(available(1:9)) < 900
    invalid = 0;
    move = input('Your Move: ');
    if available(move) < 100
    available(move) = 100;
    tttm(move) = 'X'; clc; disp(tttm)
    else
    disp('please enter a valid move')
    invalid = 1;
    end
    if any(sum(available) == 300) | any(sum(available') == 300) | sum(available([1,5,9])) == 300 | sum(available([3,5,7])) == 300
        disp('X wins!')
        available(1) = 2200;
    end
    if sum(available(1:9)) < 900 & invalid == 0
    compchoice = available(find(available(:)<100))
    compmove = compchoice(ceil(rand(1)*length(compchoice)));
    tttm(compmove) = 'O';
    available(compmove) = 101; clc
    disp(tttm)
    end
    if any(sum(available) == 303) | any(sum(available') == 303) | sum(available([1,5,9])) == 303 | sum(available([3,5,7])) == 303
        disp('O wins!')
        available(1) = 2200;
    elseif sum(available(:)) == 904
        disp('DRAW!')
    end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote