mercredi 6 mai 2015

Numberz Algorithm

Hi I'm need to make a solver for this game or actually an auto-game http://ift.tt/1Rez6z9 The game It is to find a sum of numbers around 10 with vertical or horizantal direction with some exceptions. But I wondering if anyone know the algorithm, because is taking me too long developing.

//
//  main.m
//  Console
//


#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <unistd.h>

using namespace std;

int characterCount(std::string in)
{
    int count = 0;
    for(unsigned int i = 0; i < in.size(); ++i) {
        if (!isspace(in[i]))
            ++count;
    }
    return count;
}

struct cube{
    int num;
    bool color;
};

//struct centers{
//    int col;
//    int row;
//    cube cube;
//};

bool isodd(int x){
    return (x % 2);
}

//void d(vector<centers> center){
//    for (int i = 0; i < center.size(); i++) {
//        centers c = center.back();
//        cout<<c.cube.num<<endl;
//    }
//}

int main() {
    ifstream inFile("datos.txt");
    int rows = 0;
    std::string unused;
    while ( std::getline(inFile, unused) )
        ++rows;

    int count = characterCount(unused);

    cube data[count][rows];

    int rows2 = 0;
    int col = 0;

    std::string line;

    ifstream in("datos.txt");
    while (getline(in, line)) {
        istringstream stream(line);
        int x;
        col = 0;
        while (stream >> x) {
            data[col][rows2].num = x;
            data[col][rows2].color = false;
            col++;
        }
        rows2++;
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < count; j++) {
            cout<<data[i][j].num<<" ";
        }
        cout<<endl;
    }


    int step = 1;
    bool finish = false;
    while (!finish) {
        if (step == 1) {
            int rowStep1 = (rows + 2 - 1) / 2;
            int rowStart;
            if(isodd(rows)){
                rowStart = rowStep1-1;
            }else{
                rowStart = rowStep1/2;
            }

            int colStep1 = (count + 2 - 1) / 2;
            int colStart;
            if(isodd(rows)){
                colStart = rowStep1-1;
            }else{
                colStart = rowStep1/2;
            }

            //cout<<"rowStart= "<<rowStep1<<" rowStep1= "<<rowStart<<endl;
           // cout<<"colStep1= "<<colStep1<<" colStart= "<<colStart<<endl;

            //vector<centers> center;
            for (int i = rowStart; i < rowStep1; i++) {
                for (int j = colStart; j < colStep1; j++) {
//                    centers c;
//                    c.col = i;
//                    c.row = j;
//                    c.cube = data[i][j];
//                    center.push_back(c);
                    bool wrongNumber = false;
                    int searchCol = i;
                    int searchRow = j;
                    int movePosition = 0;
                    int count10 = data[i][j].num;
                    bool hasChange = false;
                    while (!wrongNumber) {
                        if (searchCol-1 == 0 && movePosition == 0) {
                            movePosition = 2;
                            hasChange = true;
                        }else if (searchCol-1 != -1 && movePosition == 0) {
                            searchCol -= 1;
                        }else if (searchCol-1 == -1 && movePosition == 0) {
                            movePosition = 2;
                            hasChange = true;
                        }else if(searchCol+1 == count && movePosition == 1){
                            movePosition = 2;
                            hasChange = true;
                        }else if(searchCol+1 != count && movePosition == 1){
                            searchCol += 1;
                        }else if(searchCol+1 != count+1 && movePosition == 1){
                            movePosition = 2;
                            hasChange = true;
                        }else if(searchRow-1 != 0 && movePosition == 2){
                            movePosition = 1;
                            hasChange = true;
                        }else if(searchRow-1 != -1 && movePosition == 2){
                            searchRow -= 1;
                        }else if(searchRow-1 == -1 && movePosition == 2){
                            movePosition = 1;
                            hasChange = true;
                        }else if(searchRow+1 == rows && movePosition == 3){
                            movePosition = 1;
                            hasChange = true;
                        }else if(searchRow+1 != rows && movePosition == 3){
                            searchRow += 1;
                        }else if(searchRow+1 != rows+1 && movePosition == 3){
                            movePosition = 1;
                            hasChange = true;
                        }

                        if (!hasChange) {
                            count10 += data[searchCol][searchRow].num;
                            if (count10 == 10) {
                                //data[i][j].color
                                //stop
                            }else if (count10 > 10){
                                count10 -= data[searchCol][searchRow].num;
                                if (movePosition == 0) {
                                    searchCol+=1;
                                    movePosition = 2;
                                }
                            }
                        }

                    }

                }
                //cout<<endl;
            }

        }
    }

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire