no extra info provided

SquareConqueror.cpp

Committer:
jh152
Date:
2021-04-24
Revision:
0:dd04a694fbdd

File content as of revision 0:dd04a694fbdd:

#include"SquareConqueror.h"
//struct MY_Flag getFlags;

///////////////////////////////// Class: Game_State ////////////////////////////////////////////
Game_State::Game_State(int dist_raw[], int ide_raw[], int rgb_raw[], struct MY_flags* getFlags) {
    // The constructor
    /*Dist_raw[4] = dist_raw[];
    Ide_raw[4] = ide_raw[];
    RGB_raw[4] = rgb_raw[];*/

    this->dist_raw = dist_raw;
    this->ide_raw = ide_raw;
    this->rgb_raw = rgb_raw;

    //getFlags = new MY_flags{ NOT_OPEN,NOT_OPEN,'k',false};
    this->getFlags = getFlags;
}

void Game_State::identityOperaor() {
    // Deal with data from photoelectric sensors, and give hard level and also judge whether administrator.
   /* int ide_raw[4];
    for (int count = 0; count < sizeof(ide_raw[4]); count++) {
        ide_raw[count] = int(ide_raw[count] < 0.1);
    }*/
    getFlags->hard_level = ((ide_raw[3] * 8 + ide_raw[2] * 4 + ide_raw[1] * 2 + ide_raw[0]) % 4);
    if (getFlags->hard_level == 0) {
        if (ide_raw[3] == 0 && ide_raw[2] == 0 && ide_raw[1] == 0 && ide_raw[0] == 0) {
            getFlags->is_administrator = false;
        }
        else {
            getFlags->is_administrator = true;
            getFlags->hard_level = 4;
        }
    }
    else {
        getFlags->is_administrator = false;
    }
}

void Game_State::distOperator() {
    // update the distnce matrex
    int dist_sum = 0;
    for (int count = 0; count < sizeof(dist_raw); count++) {
        dist_sum += dist_raw[count];
    }
    /*Dist_raw[DIST_READING_NUM] = (Dist_raw[DIST_READING_NUM] + 1) % DIST_READING_NUM;
    dist_sum = -Dist_raw[Dist_raw[DIST_READING_NUM]] + Dist_pre;
    Dist_raw[Dist_raw[DIST_READING_NUM]] = Dist_pre;*/

    // judge whether too far
    if (dist_sum >= (MAX_DIST * DIST_READING_NUM)) {
        if (getFlags->separation == NOT_OPEN)
            getFlags->separation = NOT_OPEN;
        else {
            getFlags->separation = TOO_FAR;
        }
    }
    else {
        getFlags->separation = LEGAL;
    }
}
void Game_State::bprOperator() {
    // Deal with data from color sensor, and give distance information
    int r = rgb_raw[1] * 255 / rgb_raw[0];
    int g = rgb_raw[2] * 255 / rgb_raw[0];
    int b = rgb_raw[3] * 255 / rgb_raw[0];
    if (r > RGB_DARK && g > RGB_DARK && b > RGB_DARK) {
        if (r > g && r > b)
        {
            getFlags->bpr_mark = 'r';

        }
        if (g > r && g > b)
        {
            getFlags->bpr_mark = 'g';
        }
        if (b > r && b > g)
        {
            getFlags->bpr_mark = 'b';
        }
    }
    else {
        getFlags->bpr_mark = 'k';
    }
}

bool Game_State::whetherContinue() {
    bool gameGo = false;
    if ((getFlags->hard_level != NOT_OPEN) && (getFlags->bpr_mark == 'g') && (getFlags->separation != NOT_OPEN)) {
        gameGo = true;
    }

    return gameGo;
}


///////////////////////////////// Class: SoltionsFinder ////////////////////////////////////////////
SoltionsFinder::SoltionsFinder(int i, int j, int m, int n, int k)
{
    c1 = m;
    c2 = n;
    u1 = i;
    u2 = j;
    m_situation = c1 * 1000 + c2 * 100 + u1 * 10 + u2 - 1;
    hard_level = k;
    //m_judger = 1;
    memset(m_pool, 0.0, sizeof(m_pool));
}

SoltionsFinder::~SoltionsFinder()
{
}

//float cost[10000][5];
void SoltionsFinder::SolPools(void)
{
    int num;
    float m_case1, m_case2, m_case3, m_case4;
    ifstream infile;   //输入流
    ofstream outfile;   //输出流

    int m_count = 0;

    infile.open("SolMap.txt", ios::in);
    if (!infile.is_open())
        cout << "Open file failure" << endl;
    while (!infile.eof())            // 若未到文件结束一直循环
    {
        infile >> num >> m_case1 >> m_case2 >> m_case3 >> m_case4;
        m_pool[m_count][0] = num;
        m_pool[m_count][1] = m_case1;
        m_pool[m_count][2] = m_case2;
        m_pool[m_count][3] = m_case3;
        m_pool[m_count][4] = m_case4;
        ++m_count;

    }
    infile.close();   //关闭文件

}
int SoltionsFinder::GetSolution() {
    ///////////////////////////////////// sort /////////////////////////////////////
    int orderList[4] = { 1, 2, 3, 4 };
    int orderTemp;
    int m_result;
/*    int pre_result;*/
    //float tempMax = m_pool[m_situation][orderList[0]];

    if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[1]]) {
        //tempMax = m_pool[m_situation][orderList[1]];
        orderTemp = orderList[0];
        orderList[0] = orderList[1];
        orderList[1] = orderTemp;
    }

    if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[2]]) {
        orderTemp = orderList[0];
        orderList[0] = orderList[2];
        orderList[2] = orderList[1];
        orderList[1] = orderTemp;
    }
    else if (m_pool[m_situation][orderList[0]] > m_pool[m_situation][orderList[2]] && m_pool[m_situation][orderList[1]] < m_pool[m_situation][orderList[2]]) {
        orderTemp = orderList[1];
        orderList[1] = orderList[2];
        orderList[2] = orderTemp;
    }

    if (m_pool[m_situation][orderList[0]] < m_pool[m_situation][orderList[3]]) {
        orderTemp = orderList[0];
        orderList[0] = orderList[3];
        orderList[3] = orderList[2];
        orderList[2] = orderList[1];
        orderList[1] = orderTemp;
    }
    else if (m_pool[m_situation][orderList[0]] > m_pool[m_situation][orderList[3]] && m_pool[m_situation][orderList[1]] < m_pool[m_situation][orderList[3]])
    {
        orderTemp = orderList[1];
        orderList[1] = orderList[3];
        orderList[3] = orderList[2];
        orderList[2] = orderTemp;
    }
    else if (m_pool[m_situation][orderList[1]] > m_pool[m_situation][orderList[3]] && m_pool[m_situation][orderList[2]] < m_pool[m_situation][orderList[3]]) {
        orderTemp = orderList[2];
        orderList[2] = orderList[3];
        orderList[3] = orderTemp;
    }
    // End sorting, now orderList[0] gives the code of the best solution
    do {

        switch (hard_level) {
        case 1:
            m_result = orderList[hard_level - 1];

            break;
        case 2:
            srand(time(NULL));
            if ((rand() % 3 < 2) || (m_pool[m_situation][orderList[1]] == -1)) {
                m_result = orderList[hard_level - 2];
            }
            else {
                m_result = orderList[hard_level - 1];
            }

            break;
        case 3:
            srand(time(NULL));
            if ((rand() % 3 == 1) || (m_pool[m_situation][orderList[1]] == -1)) {
                m_result = orderList[hard_level - 3];
            }
            else if ((rand() % 3 == 0) || (m_pool[m_situation][orderList[2]] == -1)) {
                m_result = orderList[hard_level - 2];
            }
            else {
                m_result = orderList[hard_level - 1];
            }
            break;
        default:
            break;
        }
    } while (m_pool[m_situation][m_result] == -1);
    m_result += 3;
    return m_result;
}