Scilab - from factorial search to tic-tac-toe

Published on September 06, 2013

Scilab - from factorial search to tic-tac-toe

    Good evening friends, today is Friday and by tradition I would like to share my findings with you.
    A funny situation. Those few programmers (people associated with programming) whom I personally know have never written games, and people who are not much like the polar bear from the principles of humanism are no more than a polar bear from the notion of “professional programmer”, no, no, they’ll make some terrible unplayable under the tree.
    One of the above people is I.
    In this article I want to kill two birds with one stone to tell people a little about the Scilab package of applied mathematical programs , and at the same time to demonstrate its functions in a non-standard way
    Today I will tell you about how I made tic-tac-toe in Scilab . For details, you are welcome under cat.

    image

    Thank you for the picture DrZugrik


    To begin with, I'm not a programmer, not at all a programmer, very very very not a programmer.
    That is why I propose not to accept this article as a guide to action. This post I write only "just for fun" and partly to popularize Scilab.
    The source script file is uploaded to GitHub , so if someone wants to improve the code, I’m only for it.

    So for starters, a quick reference. What is Scilab?
    A detailed answer to this question can be found on Wikipedia or on the official community website
    If you say in your own words - then Scilab, it is a discovery analogue of Matlab. With very similar syntax, with similar procedures (many Matlab procedures have their own counterpart in Scilab), the interface of the latest Scilab 5.4.1, remotely resembles Matlab five years ago, now it is quite comfortable to work in it. Well, and most importantly, Scilab has similar capabilities, certainly less than the commercial giant, but still for a student, a beginner scientist or just a curious engineer, it’s quite enough. Scilab has its own analogue of Simulink (although it is clearly inferior), you can also connect various modules, for example Maxima for symbolic calculations (it didn’t work very reliably, but it worked) .Scilab is under Windows and Linux, there are sources, it is released under an open license. What is no substitute for pirated copies of Matlab,
    An example of the operation of a package of applied mathematical programs can be seen in the screenshots under the spoiler.

    Interface, graphs and factorials
    image
    graphs in Scilab

    image
    example function - finding factorial


    Certainly to master Scilab, perhaps it will be a little more difficult. It is less debugged, there are bugs and the methods of working with it are certainly less than with a big brother, but from time to time more and more answers to potential questions can be found on the Internet and in the program manuals.
    By the way, we are now on hand and stocking up with guides. To write tic tac toe, I needed a SCILAB Package Guide .
    Creating graphical applications in the Scilab environment,

    well , Scilab still does not hurt. Solving engineering and mathematical problems

    What was the task.
    Using the ability to create graphical interfaces in Scilab, write simple tic tac toe for playing with a computer.

    Source code (uploaded toGitHub ) is presented under the spoiler.

    If you briefly describe it, then first a graphic window is created, 9 buttons of the playing field, a text label and a restart button are placed on it. When you click on the button on the playing field, the game begins. Artificial intelligence, purely from the ceiling, takes a value from 1 to 9, and if it freely "goes" there, if not digs further. At the end of each click handler, a verification of the fulfillment of the victory condition is called up (we represent the field cells as a matrix and scoff at it in every way). As a result, we got simple tic tac toe for playing with AI, which sometimes unexpectedly beat me during the tests.

    It looks something like this:

    Source
    // Tick-tack-toe
    //Tic Tac Toe, on the basis of scilab
    //Start here
    global ffield;  
    global Win;
    ffield=0;
    //the creation of the game window
    mw=figure();   
    set(mw,'position',[20,40,500,450]);
    set(mw,'figure_name','Tick-tack-toe');
    //status Bar
    label1=uicontrol('Style', 'text', 'Position', [80,400,250,20], 'String',..
    'Game in progress (click on the square button)','HorizontalAlignment','left');
    //the creation of the restart button
    ubutton_c=uicontrol(mw,'Style','pushbutton','position',[80,380,120,20]..
    ,'String','Restart game','CallBack','newgame()');
    //the creation of the playing field
    ubutton=list([1:9]);
    for i=1:9 
        num=string(i)
        y=ceil(i/3);
        x=i-((y-1)*3);
        ubutton(i)=uicontrol(mw,'Style','pushbutton','position',[80*x+2,80*y,80,80]..
        ,'String',' ','CallBack','press_button('+num+')');
    end
    function y=press_button(button_num)
        //gaming activities
        global ffield;
        global Win;
        // Human
        Button_Value=get(ubutton(button_num),'String');
        if Button_Value==" " then
            set(ubutton(button_num),'String',"X");
            ffield=ffield+1;
        end
        // AI 
        if ffield<9 then 
            ct=comp_turn();  //get random action of AI
            buf=get(ubutton(ct),'String');
            while buf ~= " " do
                ct=comp_turn();  //get random action of AI
                buf=get(ubutton(ct),'String');
            end
            set(ubutton(ct),'String',"0"); // Ai chooses cell
        end
            ffield=ffield+1;  //counter filled cells
            Winner() //find game winner
    endfunction
    function R=comp_turn()
        // Computer opponent action (random action)
        R = grand(1,1,"poi",4);
        if R>9 then R=9;    end;
        if R<1 then R=1;    end;
    endfunction
    function Winner()
        //find game winner function
        res=0;
        pw=0;
        cw=0;
        plfield=hypermat([3,3]);   // results matrix for human
        cmfield=hypermat([3,3]);   // results matrix for AI
        // check game field
        for ck=1:9 Button_Value=get(ubutton(ck),'String');
            j=ceil(ck/3);
            i=ck-((j-1)*3);
            if  Button_Value=="X"  then plfield(j,i)=1;  end 
            if  Button_Value=="0" then cmfield(j,i)=1; end 
        end   
        // check human results
        pb=pob_diag(plfield,3) ;//processing second diag.
        sm=prod(plfield,1)+prod(plfield,2)';//processing  matrix
        if sum(sm)==1 then res=1;   end; //check winning the hor. and vert. field
        if diag(plfield,0)==1 then res=1;   end; //check winning field main diag.
        if  pb==1 then res=1;   end; //check winning field second diag.
        // check AI results
        pb=pob_diag(cmfield,3) //processing second diag.
        sm=prod(cmfield,1)+prod(cmfield,2)';//processing matrix
        if sum(sm)==1 then res=2;   end; // check winning the hor. and vert. field
        if diag(cmfield,0)==1 then res=2;   end; //check winning field main diag.
        if  pb==1 then res=2;   end; //check winning field second diag.
        //  Deciding the winner
        if  res==1 then set(label1,'String',"You Win");   end 
        if  res==2 then set(label1,'String',"Computer Win");   end 
        if  (ffield>=9) & (res==0) then set(label1,'String',"No Winner");   end 
    endfunction
    function mult=pob_diag(A,N)
          //  analysis of the secondary diagonal matrix
        mult=1;
        for i=1:N
            mult=mult*A(i,N+1-i);
        end
    endfunction
    function mult=find_one(Win)
       // analysis of the columns of the matrix
        mult=1;
        prod(Win,1)+prod(Win,2)'
        for i=1:2
            mult=mult+i;
        end
    endfunction
    function newgame()
        // restart game (reset values)
        global ffield;
        global Win;
        global res;
        for i=1:9 
            y=ceil(i/3);
            x=i-((y-1)*3);
            set(ubutton(i),'String',' ');
        end;
        ffield=0;
        res=0
        Win=0;
        set(label1, 'String','Game in progress (click on the square button)');
    endfunction
    



    image

    Of course the code is bad, but I hope that I can motivate someone to study this certainly useful math tool.
    Have a nice Friday and a great weekend :)