HELP with TICTACTOE code
조회 수: 15 (최근 30일)
이전 댓글 표시
I need help determining the winner it only reads the winner if its crisscross.
Here is what I have:
clear all
clc
close all
board = zeros(3,3);
% Draw Board
figure
plot([.5 3.5],[-1.5 -1.5], 'k','linewidth',2);
hold on
plot([.5 3.5],[-2.5 -2.5], 'k','linewidth',2)
plot([1.5 1.5],[-.5 -3.5], 'k','linewidth',2)
plot([2.5 2.5],[-.5 -3.5], 'k','linewidth',2)
hold off
axis off
% Obtain first move
move = input('enter player 1 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = 1;
text(c,-r,'X','horizontalalignment','center','fontsize',20)
% Start loop with 2nd player
for turn = 1:4
move = input('enter player 2 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = -1;
text(c,-r,'O','horizontalalignment','center','fontsize',20)
% Check for victory by 0, 2nd player
result = [sum(board), sum(board) ];
result(1) = board(1,1) + board(2,2) + board(1,3);
result(2) = board(2,1) + board(2,2) + board(2,3);
result(3) = board(3,1) + board(3,2) + board(3,3);
result(4) = board(1,1) + board(2,1) + board(3,1);
result(5) = board(2,1) + board(2,2) + board(2,3);
result(6) = board(1,3) + board(2,3) + board(3,3);
result(7) = board(1,1) + board(2,2) + board(3,3);
result(8) = board(1,3) + board(2,2) + board(3,1);
if any(result == -3)
disp('player 2 wins')
break
end
% Repeat for player 1
move = input('enter player 1 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = 1;
text(c,-r,'X','horizontalalignment','center','fontsize',20)
% Check for victory for X, 1st player
result = [sum(board), sum(board) ];
result(1) = board(1,1) + board(2,2) + board(1,3);
result(2) = board(2,1) + board(2,2) + board(2,3);
result(3) = board(3,1) + board(3,2) + board(3,3);
result(4) = board(1,1) + board(2,1) + board(3,1);
result(5) = board(2,1) + board(2,2) + board(2,3);
result(6) = board(1,3) + board(2,3) + board(3,3);
result(7) = board(1,1) + board(2,2) + board(3,3);
result(8) = board(1,3) + board(2,2) + board(3,1);
if any(result == 3)
disp('player 1 wins')
break
end
end
% Checks for draw
if all(result ~= 3) & (sum(sum(abs(board))) == 9)
disp('nobody wins')
end
댓글 수: 4
Sidhant
2022년 11월 24일
clear all
clc
close all
board = zeros(3,3);
% Draw Board
figure
plot([.5 3.5],[-1.5 -1.5], 'k','linewidth',2);
hold on
plot([.5 3.5],[-2.5 -2.5], 'k','linewidth',2)
plot([1.5 1.5],[-.5 -3.5], 'k','linewidth',2)
plot([2.5 2.5],[-.5 -3.5], 'k','linewidth',2)
hold off
axis off
% Obtain first move
move = input('enter player 1 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = 1;
text(c,-r,'X','horizontalalignment','center','fontsize',20)
% Start loop with 2nd player
for turn = 1:4
move = input('enter player 2 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = -1;
text(c,-r,'O','horizontalalignment','center','fontsize',20)
% Check for victory by 0, 2nd player
result = [sum(board), sum(board) ];
result(1) = board(1,1) + board(2,2) + board(1,3);
result(2) = board(2,1) + board(2,2) + board(2,3);
result(3) = board(3,1) + board(3,2) + board(3,3);
result(4) = board(1,1) + board(2,1) + board(3,1);
result(5) = board(2,1) + board(2,2) + board(2,3);
result(6) = board(1,3) + board(2,3) + board(3,3);
result(7) = board(1,1) + board(2,2) + board(3,3);
result(8) = board(1,3) + board(2,2) + board(3,1);
if any(result == -3)
disp('player 2 wins')
break
end
% Repeat for player 1
move = input('enter player 1 move [r,c] ');
r = move(1);
c = move(2);
board(r,c) = 1;
text(c,-r,'X','horizontalalignment','center','fontsize',20)
% Check for victory for X, 1st player
result = [sum(board), sum(board) ];
result(1) = board(1,1) + board(2,2) + board(1,3);
result(2) = board(2,1) + board(2,2) + board(2,3);
result(3) = board(3,1) + board(3,2) + board(3,3);
result(4) = board(1,1) + board(2,1) + board(3,1);
result(5) = board(2,1) + board(2,2) + board(2,3);
result(6) = board(1,3) + board(2,3) + board(3,3);
result(7) = board(1,1) + board(2,2) + board(3,3);
result(8) = board(1,3) + board(2,2) + board(3,1);
if any(result == 3)
disp('player 1 wins')
break
end
end
% Checks for draw
if all(result ~= 3) & (sum(sum(abs(board))) == 9)
disp('nobody wins')
end
답변 (1개)
the cyclist
2016년 11월 25일
I did not evaluate your entire code, but I can see that this line
result(1) = board(1,1) + board(2,2) + board(1,3)
should be this instead:
result(1) = board(1,1) + board(2,2) + board(3,3)
댓글 수: 2
the cyclist
2016년 11월 25일
편집: the cyclist
2016년 11월 25일
Oops, I see that you have that result, which is one of the diagonals, already listed as result(7).
However, your current result(1) is a mistake. It should actually be
result(1) = board(1,1) + board(1,2) + board(1,3)
which is a winning combination missing from your list.
(Sorry for my earlier error.)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!