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

Matlab Problem This is a TIC-TAC-TOE game. I need to have a easy mode(makes rand

ID: 3866141 • Letter: M

Question

Matlab Problem

This is a TIC-TAC-TOE game. I need to have a easy mode(makes random move) and a moderate(knows how to block and makes a winning move, otherwise random)

Please help me mofidy the script of the computer move into the moderate mode, so that it could make a winning move and knows how to block the user from making a winning move (The computer script now just makes random move)

I have attached all the scripts needed.

I will rate the answer if it works correctly. Thanks

Script of the Computer Move

Valid Move script

Main script

FILE NAVIGATE EDT BREAKPOINTS function gamestate = compMove (board) %Computer makes a move at random state = false; while (state == false) num - randi(9); if ((num >= 1) && (num = 4) && (num = 7) && (num

Explanation / Answer

Solution=============================================

Assuming 1 means user's mark and -1 means machine's mark

Feature:

It will fill its mark, in case there are 2 marks already row/column or diaognal wise by user. So as to try avoiding getting the third one.

For optimum attack, you can copy the code in the while loop, and compare it with -2, as -2 will determine where there are already two marks made in a row/column or diagonal by computer. And so the final -1 can be put in to win. I am not adding, as it will make the code look complex inside the while loop(while its preety easy to do). Once you understand, just replicate the conditions inside and compare it with -2 instead with 2.

You can call your random function at the end of it, if there are no better choices left. And random is the way to go..

Do comment, if you have trouble understanding any piece of this code..

function gameState = compMoveModerate(board)

rowTotal=zeros(3,1);
columnTotal=zeros(3,1);

for i=1:size(board,1)
    for ii=1:size(board,2)
            rowTotal(i)=rowTotal(i)+board(i,ii);
    end
end

for i=1:size(board,1)
    for ii=1:size(board,2)
            columnTotal(i)=columnTotal(i)+board(ii,i);
    end
end

leftDiagonalTotal = board(1,1) + board(2,2)+ board(3,3);
rightDiagonalTotal = board(3,1) + board(2,2)+ board(1,3);

state=false;
while(state==false)

    for i=1:3
        if(state==true)
            break;
        end
        if (rowTotal(i) == 2)
            for j=1:3
                if (board(i,j)==0)
                    board(i,j)=-1;
                    state=true;
                    break;
                end
            end
        end
    end
  
        for i=1:3
            if(state==true)
                break;
            end;
            if (columnTotal(i)==2)
                for j=1:3
                    if (board(j,i)==0)
                        board(j,i)=-1;
                        state=true;
                        break;
                    end
                end
            end
        end  


    if(state==false)
  
        if (leftDiagonalTotal==2)
            if(board(1,1)==0)
                board(1,1)=-1;
            elseif(board(2,2)==0)
                board(2,2)=-1;
            else board(3,3)=-1;
            end
            state=true;
          
        elseif (rightDiagonalTotal==2)
            if(board(3,1)==0)
                board(3,1)=-1;
            elseif(board(2,2)==0)
                board(2,2)=-1;
            else board(1,3)=-1;
            end
            state=true;
        end
    end
end
gameState=board;
end


gameBoard =[1,1,0;
            0,0,0;
            0,1,0];

compMoveModerate(gameBoard)