连接四个matlab

时间:2012-12-06 02:31:50

标签: matlab

好的,现在我正在尝试通过Matlab编码创建一个连接四游戏;现在游戏仍然是婴儿,但我的问题是,我要么无法在每个网格中绘制图形,要么我无法得到“圆圈”图形。请以任何可能的方式提供帮助。此外,如果有人知道任何连接四个matlab教程,将不胜感激。

  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 

1 个答案:

答案 0 :(得分:2)

以下是对代码的一些修复。

  1. 删除了行DrawBoard(nrRows, nrCols)。由于您已经绘制了电路板或者DrawBoard是一个单独的功能,因此不确定您是否将其作为注释放在那里。
  2. 更改了rc的计算,以便为要放入引线的单元格的中心提供。这是通过从每个中减去0.5来完成的。
  3. 将行x = cx + r .* cosd(angles);更改为x = c + 0.5*cosd(angles);。在上一个中,变量cx未定义,而r不是peg的半径,我使用0.5您可以用适当的变量替换它。但想法是绘制一个半径为0.5的圆(使其适合单元格),其中心沿x轴偏移cy的类似变化是沿y轴偏移桩。
  4. plot命令中的颜色更改为[0 0 0],即黑色。 [1 1 1]是白色的,无法在白色背景上看到:)。我建议将'k'用于黑色,将'b'用于蓝色,依此类推。有关基本颜色规范,请参阅matlab文档。
  5. 我猜你还没有实施引力,以便钉子向下移动。您还需要检查是否已填充单元格。所有这些和其他改进(如删除不必要的for循环,更好的方法来绘制挂钩等)都会在你找到工作代码后留下。
  6. 这是一个“工作”代码:

    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