A list,

Puzzles based on MATLAB

Ii. Source code

function pintu1(a)% Pin Tu % 3x3 square puzzle % Arrange the numbers 1~8 in order % Complete the puzzle, the end of the game % producer Yuxuan A= gen () %1~9Random number G = [1 2 3;4 5 6;7 8 0]; Drawmap (A); % picture puzzlewhile 1% Player action [xpos,ypos] = ginput(1);
    col = ceil(xpos);
    row = 3-ceil(ypos)+1; num = A(row,col); % handle player actionsif row>1&A(row- 1,col)==0
        A(row- 1,col) = num;
        A(row,col) = 0;
    end
    if row<3&A(row+1,col)==0
        A(row+1,col) = num;
        A(row,col) = 0;
    end
    if col>1&A(row,col- 1) = =0
        A(row,col- 1) = num;
        A(row,col) = 0;
    end
    if col<3&A(row,col+1) = =0
        A(row,col+1) = num;
        A(row,col) = 0;
    end
    drawmap(A)
    zt = abs(A-G); % jigsaw operationif sum(zt(:))= =0
        msgbox('win')
        breakEnd function drawmap(A) % drawmap;hold on
line([0 3], [0 0].'linewidth'.4);
line([3 3], [0 3].'linewidth'.4);
line([0 3], [3 3].'linewidth'.4);
line([0 0], [0 3].'linewidth'.4); % to draw3X3 gridfor i = 1:3
    for j = 1:3
        drawrect([j- 1 3-i],[j 3-i],[j 3-i+1],[j- 1 3-i+1].'y',A(i,j)); Axis equal% Cancel auto zoom change AXIS OFF % Hide coordinatesfunction drawrect(x1,x2,x3,x4,color,num)Draw each cell, and fill in x= [x1(1) x2(1) x3(1) x4(1)];
y = [x1(2) x2(2) x3(2) x4(2)]; The fill (x, y, color) % colouringif num==0
    text(0.5*(x1(1)+x2(1)),0.5*(x1(2)+x4(2)),' '.'fontsize'.24)
else
    text(0.5*(x1(1)+x2(1))0.05.0.5*(x1(2)+x4(2)),num2str(num),'fontsize'.24Function y = gen() %1~9Y = INF *ones(1.9); % generates nine Spacesfor i = 1:9
    while 1
        a = randint(1.1.9); % in1~9Generate a random numberif isempty(find(y==a))% finds if the same number appearsy(i) = a;
Copy the code

3. Operation results

Fourth, note

Version: 2014 a