Tic-tac-toe winner check
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nathan Ross
le 16 Avr 2021
Commenté : Nathan Ross
le 16 Avr 2021
I have designed a game of tic tac toe and am now trying to implement some way of displaying a winner for either player . I know there are multiple ways of going about this but am unsure of which route to take. Should I be using "if" statements of some kind ? And how should i be defining my X or O in that scope? I have put my code below.
%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
break;
end
end
%end of game file
0 commentaires
Réponse acceptée
Walter Roberson
le 16 Avr 2021
xmask = TTCboard == 1;
if all(xmask,1) || all(xmask,2) || all(diag(xmask)) || TEST_FOR_THE_OTHER_DIAGONAL
x wins
end
5 commentaires
Walter Roberson
le 16 Avr 2021
That would be because you have not defined TEST_FOR_THE_OTHER_DIAGONAL which is obviously a placeholder in the code I posted that you are intended to rewrite into code that tests the anti-diagonal.
Also, when a player wins, you need to leave the overall for loop.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Labels and Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!