Matlab Problem How to modify the following matlab script of the move computer, w
ID: 3860312 • Letter: M
Question
Matlab Problem
How to modify the following matlab script of the move computer, which makes it impossible to win the computer (the computer never loses).
This is tic tac toe game, thanks!
The 1- 2 pic are the main script of the game:
The following pics are the CompMove script
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);Explanation / Answer
> For an assignment I have to use Matlab to program a 'make move' function for a tic tac toe simulator.
>
> There is another function called check_win which will check to see if a player has won, 1 for player 1 and -1 for player 2.
>
> I need to write a 'make move' function that chooses the 'best' move to make. It needs to find the move that will win (if there is one) or choose a blocking move if the other opponent is one move away from winning (if there is one) otherwise make a random move.
>
> Here is my code, commented with what it should work as, and my coding comments. Any help would be appreciated to see what is wrong with it (not filling up when making vector one move away from winning) and there are no errors.
>
> I'm not sure if the break is used correctly, to escape the if statement and repeat the process.
>
> function board = make_move(board, player)
> %MAKE_MOVE Determine the next move of a player for tic tac toe
> % Given a board setup and a player, determine the "best" move for the
> % current player and update and return the board setup after this move
> % has been made.
> %
> % The process is :
> % 1. Find a winning position. If found, make the move and exit
> % the function;
> % 2. Find a position to block the other player from winning. If
> % found, make the move and exit the function;
> % 3. Otherwise, make a move to a random free location
> %
> n= size(board,1);
> m= size(board,1);
> rand1 = randi(n);
> rand2 = randi(m);
> done = 0;
>
> while done ~= 1; % Checking to see if while loop should still be running
> for i = 1:n % For each row
> for j = 1:m % For each column, in each row
> if board(i,j) == 0 % Is the vector 0, eg: empty?
> board(i,j) = player; % If it is, let's try making a move here with the player value
> check_win(board); % Is this a winning move? Has the game finished?
> if check_win(board) == player % If check_win says player value (1 or -1) has won, return
> return
> else % Let's try a blocking attempt
> board(j,i) = -1*player; % Switching the rows and column values around, and placing a move as opponent
> if check_win(board) == -1*player % Checking to see if this is a winning move for opponent
> board(j,i) = player; % If this is a winning move, block it by placing your move here
> return
> else % If cannot make winning move or cannot block a win
> if board(m,n) == 0 % Use rand to generate vector position, checking to see if it is 0 (empty)
> board(m,n) = player; % Place a move here
> return
> end
> end
> end
> else
> break
> done = 0; % Should restart the process again if the original vector is not empty
> end
> end
> end
> end
The glory of MATLAB is that the endless loops aren't necessary:
function out = makeMove(in)
%SCd
%09/13/2010
%
%in is a 3x3 matrix (1 for player; 0 for open; computer is -1);
out = in; %set out == in to modify it
%first see if you can win
any_rows = sum(in==-1,2)==2 & any(in==0,2);
any_cols = sum(in==-1,1)==2 & any(in==0,1);
diagLU = sum(in([3,5,7])) == 2 & any(in([3 5 7])==0,2); %lower to upper
diagUL = sum(in([1,5,9])) == 2 & any(in([1 5 9])==0,2); %upper to lower
if any_rows
out = out(any_rows,:)== -1;
elseif any_cols
out = out(:,any_cols)== -1;
elseif diagUL
out = out([1 5 9])== -1;
elseif diagLU
out = out([3 5 7])== -1;
%If you can't win; make sure to block
%elseif do you need to block?
%You write this!
else
%Find a random place == 0 and set it to -1
out(randsample(find(in==0),1)) = -1;
end
end
%%%%%
Break down what was done in the definition of any_rows, any_cols etc. If you can wrap your head around the logic used there