Programming.

Software is divided into two parts. Fisrt one is program for choosing the best move. Program having the board, simulate moves for each chequer, then respond of player, againg robots move and again players moove. So thanks to this simulation robot knows every possible move. Moves have it's value and on base of them program choose the best option. Program create a list of movements and killed enemy chequers. Second part is program related to making the robots move. Everything connected to engines and sensors.

#include <hFramework.h>
#include <Lego_Light.h>
#include <Lego_Touch.h>
#include <cstdlib>
#include <cstdio>

using namespace hSensors;

using namespace hFramework;


int symul[6][5];
char czas;
int tab[26][2];
int tab_temp[25][2];

int value;
int value_temp;
int index;
int los=1;


int moves[13][2];
int kills[12][2];

int x,y;
int X,Y;
bool is_open=true;


void open(){
    if(is_open==false){
        hMot4.rotRel(-1200,300,1);
        is_open=true;
    }
}

void close(){
    if(is_open==true){
        hMot4.rotRel(1200,300,1);
        is_open=false;
    }
}

void go_to_sensor(){
    hMot3.rotRel(-800,700,1);
    hMot5.rotRel(-170,500);
    hMot2.rotRel(-170,500,1);
}

void go_to_board(){

    hMot5.rotRel(-300,500);
    hMot2.rotRel(-300,500,1);

    hMot5.rotRel(3250,500);
    hMot2.rotRel(3250,500,1);

    X=0;
    Y=7;
}

void go_to(int poz_x, int poz_y){
//1 2
    int dirx,diry;
    dirx=(poz_x-X)*650;
    diry=(poz_y-Y)*812;

    hMot5.rotRel(dirx,500);
    hMot2.rotRel(dirx,500,1);
    hMot3.rotRel(diry,700,1);

    X=poz_x;
    Y=poz_y;


}


void go_back(){
    int disy=(7-Y)*812+200;
    hMot3.rotRel(disy,700,1);

    int disx=X*(-650)-3500;
    hMot5.rotRel(disx,500);
    hMot2.rotRel(disx,500,1);
}

void throw_out(){
    close();
    go_back();
    open();


}

int check(){


    Lego_Light sensor(hSens2);

    sensor.init ();
    int16_t val;
    int average=0;
    for (int i=0;i<20;i++) {

        val = sensor.readRaw();
        average=average+val;
        LED1.toggle();
        sys.delay_ms(10);
        //printf("val %5d average %d\r\n",val,average);
    }

    sensor.deinit ();
    average=average/20;
    printf("AVERAGE %d\r\n",average);
    if(average<1700){printf("czarny"); return -1;}
    else if(average<1920){printf("pusty"); return 0;}
    else {printf("bialy"); return 1; }


}

void check_board(int board[8][8]){
    go_to_board();
    hMot5.rotRel(50,500);
    hMot2.rotRel(50,500,1);
    go_to_sensor();
    int temp_x=0;
    for(int d=0;d<8;d++){
        for(int v=0;v<8;v++){
            board[d][v]=0;
        }
    }

    for(int d=0;d<4;d++){
        if(d!=0) hMot3.rotRel(-800,700,1);
        board[temp_x][7]=check();
        hMot3.rotRel(-1624,700,1);
        board[temp_x][5]=check();
        hMot3.rotRel(-1624,700,1);
        board[temp_x][3]=check();
        hMot3.rotRel(-1624,700,1);
        board[temp_x][1]=check();
        hMot3.rotRel(-812,700,1);
        hMot5.rotRel(650,500);
        hMot2.rotRel(650,500,1);
        temp_x=temp_x+1;
        board[temp_x][0]=check();
        hMot3.rotRel(1624,700,1);
        board[temp_x][2]=check();
        hMot3.rotRel(1624,700,1);
        board[temp_x][4]=check();
        hMot3.rotRel(1624,700,1);
        board[temp_x][6]=check();
        hMot3.rotRel(2000,700,1);
        temp_x=temp_x+1;
        if(d!=3){
            hMot5.rotRel(650,500);
            hMot2.rotRel(650,500,1);
        }

     }
     hMot5.rotRel(-8000,500);
     hMot2.rotRel(-8000,500,1);


}

void make_move(){

    int indexm=0;
    int indexk=0;


    for(int k=0;k<12;k++){
        moves[k][0]=-1;
        moves[k][1]=-1;
        kills[k][0]=-1;
        kills[k][1]=-1;
    }
    moves[12][0]=-1;
    moves[12][1]=-1;


    moves[0][0]=tab[0][0];
    moves[0][1]=tab[0][1];
    indexm=indexm+1;



   for(int c=1;c<26;c=c+2){

        if(tab[c][0]==-1) break;

        if(tab[c+1][0]!=-1){
            kills[indexk][0]=tab[c][0];
            kills[indexk][1]=tab[c][1];
            indexk=indexk+1;

            moves[indexm][0]=tab[c+1][0];
            moves[indexm][1]=tab[c+1][1];
            indexm=indexm+1;

        }
        else{
            moves[indexm][0]=tab[c][0];
            moves[indexm][1]=tab[c][1];
            indexm=indexm+1;
        }

    }

    printf("tab: \r\n");
        for(int h=0;h<26;h++){
            printf("%d\t%d\r\n",kills[h][0],kills[h][1]);
        }
        printf("tab: \r\n");
        for(int h=0;h<26;h++){
            printf("%d\t%d\r\n",moves[h][0],moves[h][1]);
        }

    go_to_board();
    go_to(moves[0][0],moves[0][1]);
    close();

    for(int k=1;k<13;k++){

        if(moves[k][0]==-1) break;
        else{

            go_to(moves[k][0],moves[k][...
                    
                        Read more »