Search code examples
matlab

How to get a figure to plot in each grid square in Matlab


I'm trying to create a connect four game through Matlab.

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.

  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 

Solution

  • Here are some fixes to the code.

    1. removed the line DrawBoard(nrRows, nrCols). Not sure if you put it there as a comment as you have already drawn the board or if DrawBoard is a separate function.
    2. Changed the calculation for r and c to give the center of the cell you wan the put the peg in. This is done by subtracting 0.5 from each.
    3. Changed the line x = cx + r .* cosd(angles); to x = c + 0.5*cosd(angles);. In the previous one, variable cx is undefined and instead of r being the radius of the peg, I used 0.5 you can replace it by appropriate variable. But the idea is to draw a circle of radius 0.5 (so that it fits in a cell) with the center offset by c along x-axis. Similar change for y to offset the peg along y-axis.
    4. Changed the color in plot command 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.
    5. I am guessing you are yet to implement gravity so that the peg moves down. Also you need to check is a cell is already filled. All these and other improvements (like removing unnecessary for-loops, better way to draw pegs, etc.) are left once you get to a working code.

    Here's a "working" code:

    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
    
            axis square;
            hold on;
    
        while nrMoves < nrRows * nrCols        %Computes ability to move polygon
            [x, y] = ginput(1); 
            r = ceil(y) - 0.5;
            c = ceil(x) - 0.5;
    
            angles = 0 : 1 : 360;
            x = c + 0.5*cosd(angles);
            y = r + 0.5*sind(angles);
    
            plot(x, y, 'Color', [0 0 0], 'LineWidth', 3);
        end 
    end