Capturing Mouse Clicks on an array/patch

14 views (last 30 days)
Sarah Rasavanh
Sarah Rasavanh on 9 Dec 2019
Commented: Chris on 28 Nov 2022
clear
clc
[board, DrawRes, N, Theta, onerow, playerturn, FigSize] = setboard();
[Xline, Yline, BoardCenterX, BoardCenterY, radius] = Grid(onerow, FigSize)
[BoardColor] = DrawPiece(onerow, BoardCenterX, BoardCenterY, Theta, radius, board);
for i = 1:60
clc
disp(board)
while playerturn > 0
fprintf('Player %i turn\n', playerturn)
[x1,y1] = inputmoves();
if board(y1,x1) == 0
board(y1,x1) = playerturn;
playerturn = switchplayers(playerturn);
[BoardColor] = DrawPiece(onerow, BoardCenterX, BoardCenterY, Theta, radius, board);
end
end
end
I'm trying to make a game of tictactoe, and I have it set up where there are graphics and that there is a patch array which stores each place a player can go. my problem is that I don't know how to make it so when I click on the patch area it finds the patch coordinate and matches it with the one in the main array (in this case board) where the code then recognizes it as part of the board/ graphics. Right now I have it where the user inputs coordinates into the command window and then it goes to the board array but I want to be able to do this by clicking on the space I want.
  2 Comments
Ruger28
Ruger28 on 9 Dec 2019
Not an answer, but I have done similar things to this using uicontrol pushbuttons as an arrary. I actually just made the old game Snake using this method. You can determine which pushbutton was pressed, and get row/column coordinates via this method.
Ruger28
Ruger28 on 9 Dec 2019
Also worth noting, that groot could be of use capturing your mouse location
GROOT = groot;
sprintf('Current Mouse Location = [%f, %f]',GROOT.PointerLocation(1),GROOT.PointerLocation(2));
Be aware of the units GROOT is in. Not sure if this helps, but it is something.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 23 Apr 2020
Sarah - very late to this, but you can use hittest to capture mouse clicks on graphics objects (in your case, the nine different patches). For example, the following code for a 3x3 grid changes the patch a different colour whenever pressed (red or blue for player 1 or player 2 respectively):
function TicTacToeExample
hFigure = figure;
hAxes = axes;
axis equal;
axis off;
hold on;
squareEdgeSize = 5;
% create the board of patch objects
hPatchObjects = zeros(3,3);
for j = 3:-1:1
for k = 1:3
hPatchObjects(3 - j+ 1, k) = rectangle('Position', [k*squareEdgeSize,j*squareEdgeSize,squareEdgeSize,squareEdgeSize], 'FaceColor', [0 0.5 0.5],...
'EdgeColor', 'k', 'LineWidth', 3, 'HitTest', 'on', 'ButtonDownFcn', {@OnPatchPressedCallback, 3 - j+ 1, k});
end
end
gameBoard = zeros(3,3);
atPlayer = 1;
playerColours = [1 0 0; 0 0 1];
xlim([squareEdgeSize 4*squareEdgeSize]);
ylim([squareEdgeSize 4*squareEdgeSize]);
function OnPatchPressedCallback(hObject, eventdata, rowIndex, colIndex)
% remove callback so square can't be chosen again
set(hObject, 'ButtonDownFcn', []);
% change FaceColor to player colour
set(hObject, 'FaceColor', playerColours(atPlayer, :));
% update the game board
gameBoard(rowIndex,colIndex) = atPlayer;
end
Each patch is assigned the OnPatchPressedCallback callback function with the only difference being the row and column index (for the patch) into the game board. Once any player has selected a square (patch) then this callback fires and we change its colour, update the game board, and remove the callback from the patch object (so that no player can choose it again).
  1 Comment
Chris
Chris on 28 Nov 2022
Hello there,
Is there a way to retrieve the Board array once the user is done activating cells outside the function? I can't find a way to do this, even with a global statement.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!