Ok, right now i'm trying to create a connect four game through Matlab coding; now the game is still infant but my problem is that I either can't get the figure to plot in each grid square or I can't get the 'circle' figure to plot at all. Please help in any way possible. Also if anyone knows about any connect four matlab tutorials, it would be greatly appreciated.
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
DrawBoard(nrRows, nrCols)
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y); % convert to row index
c = ceil(x); % convert to column index
angles = 0 : 1 : 360;
x = cx + r .* cosd(angles);
y = cy + r .* sind(angles);
plot(x, y, 'Color', [1 1 1], 'LineWidth', 3);
axis square;
end
end
Here are some fixes to the code.
DrawBoard(nrRows, nrCols). Not sure if you put it there as a comment as you have already drawn the board or ifDrawBoardis a separate function.randcto give the center of the cell you wan the put the peg in. This is done by subtracting 0.5 from each.x = cx + r .* cosd(angles);tox = c + 0.5*cosd(angles);. In the previous one, variablecxis undefined and instead ofrbeing the radius of the peg, I used0.5you can replace it by appropriate variable. But the idea is to draw a circle of radius0.5(so that it fits in a cell) with the center offset bycalong x-axis. Similar change foryto offset the peg along y-axis.plotcommand to[0 0 0], which is black.[1 1 1]is white and is impossible to see on white background :). I would suggest using'k'for black,'b'for blue and so on. See matlab documentation for basic color specifications.Here's a "working" code: