Question: Matlab problem: I need to create a move for the computer in the tic-tac-toe game, which the computer will never loses. plz modify the codes

Matlab problem:

I need to create a move for the computer in the tic-tac-toe game, which the computer will never loses. plz modify the codes below to create a script for this.

(This question is urgent, PLZ Treat this question correctly and seriously, if you don't know, plz don't answer. Don't copied and pasted some random codes in other languages to mess around my question)

Hint: you need to put some sort of strategy into the code.

Main script

Matlab problem: I need to create a move for the computer in

the tic-tac-toe game, which the computer will never loses. plz modify the

(Below is the moving code, plz modify this code to create a move which the computer will never lose in the tic-tac-toe game)

function [P1move_index]=MyMove(board)

turn=(board~=0);

x1=(board==2)*-1;

x2=(board==1);

win_or_block = x1+x2;

if sum(sum(turn))==0

P1move_index=1;

return

elseif sum(sum(turn))==1

if(board(5)==0)

P1move_index=5;

return

else

P1move_index=1;

return

end

elseif sum(sum(turn))==2

if board(5)~=0

P1move_index = 9;

return

elseif board(9)~= 0

P1move_index = 3;

return

else

P1move_index = 5;

return

end

end

for i=1:3

if sum(win_or_block(i,:))==2

temp = [ find(win_or_block(i,:)==0)];

P1move_index = (temp-1)*3+i;

return

elseif sum(win_or_block(:,i))==2

temp =[find(win_or_block(:,i)==0)];

P1move_index= (i-1)*3 + temp;

return

end

end

if sum(win_or_block([1 5 9]))==2

if find(win_or_block([1 5 9])==0)==3

P1move_index = 9;

return

elseif find(win_or_block([1 5 9])==0)==2

P1move_index = 5;

return

elseif find(win_or_block([1 5 9])==0)==1

P1move_index = 1;

return

end

end

if sum(win_or_block([3 5 7]))==2

if find(win_or_block([3 5 7 ])==0)==3

P1move_index = 7;

return

elseif find(win_or_block([3 5 7])==0)==2

P1move_index = 5;

return

elseif find(win_or_block([3 5 7])==0)==1

P1move_index = 3;

return

end

end

for i=1:3

if sum(win_or_block(:,i))==-2

temp =[find(win_or_block(:,i)==0)];

P1move_index= (i-1)*3 + temp;

elseif sum(win_or_block(i,:))==-2

temp = [ find(win_or_block(i,:)==0)];

P1move_index = (temp-1)*3+i;

return

end

end

if sum(win_or_block([1 5 9]))==-2

if find(win_or_block([1 5 9])==0)==3

P1move_index = 9;

return

elseif find(win_or_block([1 5 9])==0)==2

P1move_index = 5;

return

elseif find(win_or_block([1 5 9])==0)==1

P1move_index = 1;

return

end

end

if sum(win_or_block([3 5 7]))==-2

if find(win_or_block([3 5 7 ])==0)==3

P1move_index = 7;

return

elseif find(win_or_block([3 5 7])==0)==2

P1move_index = 5;

return

elseif find(win_or_block([3 5 7])==0)==1

P1move_index = 3;

return

end

end

P1move_index = 1+min(find(board==0));

P1move_index = min(find(board==0));

end

FILE NAVIGATE EDIT BREAKPOINTS RUN board = zeros(3,3); % Determines starting player randomly % playerturn 1 indicates Player 1 starting % playe rturn 2 indicates Player 2 starting playerturn = round (rand ) +1 ; step = 0; winplayer = 0; playing=1; %initialize %Display message disp Welcome to E7 tic-tac-toe.'); disp (board); if playe rturn = 1 4 12 disp( 'Player 1 will start first' else 15 disp( Player 2 will start first' end %Iterate the game while playing == 1 8 disp(['Player num2str(playerturn)'1) % Player1's turn if playe rturn 1 1 zeroind find ( board-=0); P1move-index = Human (board); % Change Player 1 function here if -any ( ze ro ind P1move-index P1move-index) zero!nd (1); = end board (P|move-index) playe rturn = 2; currentplayer = 1; step = step + 1; 1; = 28 % Player2's turn 2else zeroind = find ( board=0)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To create a move for the computer in tictactoe that will never lose youll need to implement a strategy based on the minimax algorithm The minimax algo... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!