Check win with "if statements"

조회 수: 2(최근 30일)
Nathan Ross
Nathan Ross 2021년 4월 17일
편집: Image Analyst 2021년 4월 18일
I have been working on different methods to check for a winner in my game of tic tac toe. My current approach is using "if" statements. My problem is I am not sure how to properly define my values to exectute my "winner" dialogue . Could I get some help on this? My code is below and What I am attempting to add is commented towards the bottom right before the three "ends".
%start of game file
TTCboard = zeros(3,3);% creates a 3x3 matrix of zeros
% for creating the standard tic-tac-toe board in blue and red
figure
plot([0 3],[-1 -1], 'b','linewidth',1);% creates top horizontal line in board
hold on
plot([2 2],[0 -3], 'b','linewidth',1)% creates right-most vertical line
plot([3 3],[0 -3], 'r','linewidth',2)% boxes in right-most lines
plot([0 3],[-3 -3], 'r','linewidth',2)% boxes in the bottom-most lines
plot([0 3],[0 0], 'r','linewidth',2)% boxes in top -most lines
plot([0 3],[-2 -2], 'b','linewidth',1)% creates bottom horizontal line
plot([0 0],[0 -3], 'r','linewidth',2 ...
)% boxes in left-most lines
plot([1 1],[0 -3], 'b','linewidth',1)% creates left-most vertical line
title('welcome to tic-tac-toe! Good Luck!');% creates title above the game
axis off % keeps the X & y-axis off, creating a better looking natural board
hold off% ensures later commands are added to existing board
xticks([0 1 2 3]); yticks([0 1 2 3]);
xticklabels([]); yticklabels([]);
player = 'X'; % designates the first player as X
while true % when it is player x's turn, the following holds true
fprintf('%s click to place an X\n', player); % creates X in the cell clicked on
[x, y] = ginput(1);
x = floor(x)+0.5 ;
y = floor(y)+0.5;
while TTCboard(x+0.5,-(y-0.5)) ~= 0
disp('Please pick an open square')% when more than one X or O attempts to be entered in the same space, the message will appear in command window prompting the player to try again
[x, y] = ginput(1);
x = floor(x)+0.5;
y = floor(y)+0.5;
end
text(x,y,player, 'horizontalalignment', 'center', 'FontSize',24);% gives specific parameters to the X on the board and centers it in the clicked on cell
TTCboard(x+0.5,-(y-0.5)) = 1;
xticks([0 1 2 3]); yticks([0 1 2 3]);
xticklabels([]); yticklabels([]);
player2 = 'O'; % Designates the 2nd players turn after player 1
while true %for when it is player 2's turn, the following holds true
fprintf('%s click to place an O \n', player2);% instructs the player to put down their O in one of the available cells
[x, y] = ginput(1);
x = floor(x)+0.5;
y = floor(y)+0.5;
while TTCboard(x+0.5,-(y-0.5)) ~= 0
disp('Please pick an open square')% when more than one X or O attempts to be entered in the same space, the message will appear in command window.
[x, y] = ginput(1);
x = floor(x)+0.5;
y = floor(y)+0.5;
end
text(x,y,player2, 'horizontalalignment', 'center', 'FontSize',24);% specifies the style and size of the O used in the game and centers it
TTCboard(x+0.5,-(y-0.5)) = 2;% assigns a value to the O cell
% if (1,1)=(1,2)==(1,3);
% waitfor(warndlg('player wins'));
% close all
end
end
end
%end of game file
% Thank you for playing
  댓글 수: 1
Nathan Ross
Nathan Ross 2021년 4월 17일
Sidenote. There should be a break; before the commented code

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

답변(1개)

Image Analyst
Image Analyst 2021년 4월 17일
Try this:
% Let's get 10 random boards and check to see which player won, if anyone.
for k = 1 : 10
% Get sample board.
TTCboard = randi([0, 1], 3, 3)
% See if 1 (X) won this board.
winnerIs1 = any(sum(TTCboard, 1) == 3) || ...
any(sum(TTCboard, 2) == 3) || ...
(TTCboard(1,1) == 1 && TTCboard(2,2) == 1 && TTCboard(3,3) == 1) || ...
(TTCboard(1,3) == 1 && TTCboard(2,2) == 1 && TTCboard(3,1) == 1);
if winnerIs1
fprintf('1 X won this board.\n\n');
end
% See if 0 (O) won this board.
winnerIs0 = any(sum(TTCboard, 1) == 0) || ...
any(sum(TTCboard, 2) == 0) || ...
(TTCboard(1,1) == 0 && TTCboard(2,2) == 0 && TTCboard(3,3) == 0) || ...
(TTCboard(1,3) == 0 && TTCboard(2,2) == 0 && TTCboard(3,1) == 0);
if winnerIs0
fprintf('O won this board.\n\n');
end
% If it was a draw, print that out.
if ~(winnerIs0 || winnerIs1)
fprintf('No one won this board -- it was a draw.\n\n');
end
end
  댓글 수: 4
Image Analyst
Image Analyst 2021년 4월 18일
편집: Image Analyst 2021년 4월 18일
What is the pattern that gave a false indication of a win? And why did you add in all those extra conditions for a win that were unneeded because they were handled by the "any" condition?

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

범주

Find more on Strategy & Logic in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by