Terminating a game of tic tac toe if two entries are entered in the same cell
조회 수: 1 (최근 30일)
이전 댓글 표시
As the title suggests, i am finishing up making a game of tic tac toe , and want to write some code at the end that will terminae/end the game if the players try and put more than one X or o in each space. This is what I have currently .
TTCboard = zeros(3,3);
% for creating the standard tic-tac-toe board in green
figure
plot([0 3],[-1 -1], 'g','linewidth',1);% creates top horizontal line in board
hold on
plot([2 2],[0 -3], 'g','linewidth',1)% creates right-most vertical line
plot([0 3],[-2 -2], 'g','linewidth',1)% creates bottom horizontal line
plot([1 1],[0 -3], 'g','linewidth',1)% creates left-most vertical line
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,
fprintf('%s click to place a piece\n', player); % creates X in the cell clicked on
[x, y] = ginput(1);
x = floor(x)+0.43 ;
y = floor(y)+0.5;
text(x,y,player, 'horizontalalignment', 'center', "FontSize",24);% gives specific parameters to the X on the board
xticks([0 1 2 3]); yticks([0 1 2 3]);
xticklabels([]); yticklabels([]);
player2 = 'O' % Designates the 2nd players turn
while true
fprintf('%s click to place a piece\n', player2);
[x, y] = ginput(1);
x = floor(x)+0.43 ;
y = floor(y)+0.5;
text(x,y,player2, 'horizontalalignment', 'center', "FontSize",24);
break;
end
end
댓글 수: 1
John Chilleri
2021년 3월 15일
One possibility is to create an additional 3x3 matrix of zeros, and whenever an X or O is added, change the corresponding entry to 1. Then implement a simple check before placing Xs and Os that ensures the matrix entry is 0 (meaning available).
채택된 답변
Bob Thompson
2021년 3월 15일
John has a pretty good idea with that.
If you put in a logic check and a loop, you don't even need to actually quit the game, just tell the player to pick an open spot.
Sample below, but beware that I have not tested it yet. Also, note that P1 has squares marked as 1, and P2 has squares marked as 2. I imagine that will be useful for determining who owns which squares and if somebody wins.
while true % when it is player x's turn,
fprintf('%s click to place a piece\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')
[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
% Close space for later use
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
while true
fprintf('%s click to place a piece\n', player2);
[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')
[x, y] = ginput(1);
x = floor(x)+0.5;
y = floor(y)+0.5;
end
text(x,y,player2, 'horizontalalignment', 'center', 'FontSize',24);
% Close space for later use
TTCboard(x+0.5,-(y-0.5)) = 2;
break;
end
end
댓글 수: 3
Bob Thompson
2021년 3월 15일
편집: Bob Thompson
2021년 3월 15일
Really? I took exactly what I put down and didn't have any issues with it. Did you make other adjustments to it? I don't think I changed anything outside of what I posted.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!