A functioning, visual game of tic-tac-toe between the human and the computer. (M
ID: 3533686 • Letter: A
Question
A functioning, visual game of tic-tac-toe between the human and
the computer.
(MUST CREATE A EXUCUTABLE MFILE THAT CAN DO THIS TO GET A 5STAR RATING)
(MUST FOLLOW THE DIRECTIONS TO THE LETTER)
Write one function or script to accomplish objectives.
To those not familiar with the game tic-tac-toe, it is played by two players on a
3-by-3 grid. Each player has a unique marker, commonly X and O, but it may be
convenient to use numbers in MATLAB. Players take turns placing their marker on
an open square on the grid. The first player to obtain three-in-a-row (considering
rows, columns, and the two main diagonals) is the winner.
Your task is to create a playable version of tic-tac-toe in MATLAB. It will be
played between the human and the computer, but you must ask the human to see
who goes first. The game must be tracked visually, using a 3x3 image with three
color states, blank, human, or computer (it should begin as the uniform blank color).
The human input MUST BE OBTAINED BY CLICKING THE MOUSE ON THE
BOARD. The computer input may be randomly generated, but try to program a
winning strategy.The image of the board must be updated after every turn.
Only legal turns may occur; neither you nor the computer are allowed to play on
an occupied square, and you CANNOT skip turns; the player must keep trying until
a legal turn is made, and the program should prompt if an illegal turn is attempted
by the human.
Victory must be declared as soon as possible, so check for it after every turn (you
do not have to exclude the first two turns each, ie- it
Explanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.