Assume that the human player makes the first move against the computer in a game
ID: 3851607 • Letter: A
Question
Assume that the human player makes the first move against the computer in a game of Tic Tac Toe, which has 3 x 3 grid. write a MATLAB function that lets the computer respond to that move. The functions input argument should be cell location of the human players move. the functions ouput should be the cell location of computers first move. Label the cells as 1, 2,3 across the top row; 4, 5, 6, across the middle row and 7, 8, 9 across the bottom row.
!!!!USING MATLAB!!!
Can you plz explain as simple as possible.
thankyou
Explanation / Answer
Answer for Question:
See the below matlab code for 3x3 tic tac tie game with compuetr and humen.
This code is also have the comments.
function tictactoegame(tries)
X = 1;
O = 2;
NOWIN = 3;
result = [0 0 0]; % [X_wins, O_wins, No_Winner]
for k = 1:tries
window = zeros(3);
player = X;
winner = NOWIN;
for moves = 1:9
illegal = 1;
while illegal % Find a random, empty space.
m = ceil(rand*3);
n = ceil(rand*3);
illegal = window(m,n); % Is this space already taken?
end
window(m,n) = player; % No, place the X or O here.
% Starting with the 5th alternating move [X O X O X],
% if the current player occupies 3 squares across, or
% 3 down, or 3 on diagonal, or 3 on / diagonal, ...
if moves > 4 && ...
(window(m,1) == player && window(m,2) == player && ...
window(m,3) == player) || ...
(window(1,n) == player && window(2,n) == player && ...
window(3,n) == player) || ...
(window(1,1) == player && window(2,2) == player && ...
window(3,3) == player) || ...
(window(1,3) == player && window(2,2) == player && ...
window(3,1) == player)
% ... then that player wins this round.
winner = player;
break;
end
% Give the other player a turn.
if player == O, player = X; else player = O; end
end
% Keep score for each round.
result(winner) = result(winner) + 1;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.