looking for creator of specific code
이전 댓글 표시
clear
clf
% set up gameboard
A = zeros(3,3);
n = 3;
plot(0,0)
axis([0 3 0 3]);
hold on
% Create the crosslines
x = [0, 3];
y = [1, 1];
plot(x,y, 'k-')
x1 = [0, 3];
y1 = [2, 2];
plot(x1,y1, 'k-')
x2 = [1, 1];
y2 = [0, 3];
plot(x2,y2, 'k-')
x3 = [2, 2];
y3 = [0, 3];
plot(x3,y3, 'k-')
% Allow the player to click the gameboard
k = 0;
CreateBoard(n)
for i = 1:9
k = k + 1;
% Get location
[x,y] = ginput(1);
[x1,y1] = DrawXY(x,y);
[x2,y2] = LocXY(x,y);
% Draw X
if A(ceil(y1),ceil(x1)) == 0
if rem(k,2) == 0
DrawX(x1,y1);
A(y2,x2) = 1;
else
DrawO(x1,y1);
A(y2,x2) = 5;
end
else
k = k - 1;
end
% Determine who the winner is
winner = CheckWinner(A);
if winner ~= 0
disp(['Player ', num2str(winner), ' wins!']);
break;
elseif i == 9
disp('It''s a draw!');
end
end
% It's a draw!
title('Tic-Tac-Toe', 'FontSize', 20)
hold off
function CreateBoard(n)
for i = 1:n-1
plot([0 n],[i i],'k');
plot([i i],[0 n],'k');
end
end
% Create an X
function DrawX(x, y)
L1x = [x-0.4, x+0.4];
L1y = [y-0.4, y+0.4];
L2x = [x-0.4, x+0.4];
L2y = [y+0.4, y-0.4];
plot(L1x, L1y, 'b');
plot(L2x, L2y, 'b');
end
% Create a Y
function DrawO(x,y)
th = linspace(0,2*pi,100);
r = 0.4;
cX = r * cos(th) + x;
cY = r * sin(th) + y;
plot(cX,cY, 'r')
end
function [x,y] = DrawXY(x,y)
x = floor(x) + .5;
y = floor(y) + .5;
end
function [x,y] = LocXY(x,y)
x = ceil(x);
y = ceil(y);
end
function winner = CheckWinner(board)
for i = 1:3
if sum(board(i,:)) == 3
winner = 1;
return;
elseif sum(board(i,:)) == 15
winner = 2;
return;
end
end
for i = 1:3
if sum(board(:,i)) == 3
winner = 1;
return;
elseif sum(board(:,i)) == 15
winner = 2;
return;
end
end
if board(1,1) + board(2,2) + board(3,3) == 3 || board(1,3) + board(2,2) + board(3,1) == 3
winner = 1;
return;
elseif board(1,1) + board(2,2) + board(3,3) == 15 || board(1,3) + board(2,2) + board(3,1) == 15
winner = 2;
return;
end
winner = 0;
end
댓글 수: 2
John D'Errico
2026년 5월 26일
편집: John D'Errico
2026년 5월 26일
You want to know who wrote this specific code? Why, for god sakes? It is not something of which to be terribly proud to have written. That is to say, it is not terribly well written, though it is not too poor either. Just nothing special, a basic tic tac toe game code. So why do you care who wrote the code? Is a student passing it off as theirs, you are their teacher, and you suspect something? I would note that I did not find a source using a Google search, but that may just reflect a less than diligent search on my part.
Walter Roberson
2026년 5월 26일
I do not find any source for it using DuckDuckGo or Bing either.
답변 (1개)
John D'Errico
2026년 5월 26일
편집: John D'Errico
2026년 5월 26일
1 개 추천
The codes I did find were a bit more sophisticated in general, and I did not expect to find anything. HOWEVER, when I looked on the FEX, and then started checking individual files, the author joey pops out with a nearly identical code.
21 downloads. Probably one of them was to a student in your class. Sorry I did not find it more quickly, as the FEX is where I should have looked immediately.
카테고리
도움말 센터 및 File Exchange에서 Board games에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!