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

댓글 수: 3

Madhav Mohanakrishnan
Madhav Mohanakrishnan 2020년 10월 12일
What do we enter as input ?
Caleb Sandridge
Caleb Sandridge 2021년 4월 5일
enter the row and column of where you want to place your marker
Sidhant
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
the cyclist 2016년 11월 25일

2 개 추천

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

Charly Perez
Charly Perez 2016년 11월 25일
I'm confused how does that make the board read it correctly if that can't be a winning possibility.
the cyclist
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.)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Strategy & Logic에 대해 자세히 알아보기

질문:

2016년 11월 25일

댓글:

2022년 11월 24일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by