Question:
Help getting started with a C++ program?
1970-01-01 00:00:00 UTC
Help getting started with a C++ program?
Three answers:
2012-01-15 11:36:14 UTC
If it was visual basic I could have helped you.
2012-01-15 11:24:29 UTC
if you are using Turing, i could probably help you with some parts
cja
2012-01-16 08:12:37 UTC
You need to start with a good data structure for your game board. Having 5 separate arrays of int, each hardcoded to a length of 4, is about the worst choice - inflexible and hard to work with. It would be good if you could allow the player to choose the difficulty, then create the game board of size pre-defined for that level, or even just let the player to choose the dimensions. This should give you some ideas:



#include

#include

#include

#include

#include

#include

#include

#include



using namespace std;



int random(double min, double max);

int adjCount(const vector< deque* >&, int, int);

bool validRowCol(const vector< deque* >&, int, int);

typedef struct {

    int row, col;

} coord_t;



int main(int argc, char *argv[]) {

    string in;

    stringstream ss;

    int rows, cols, N, x, y;

    char c;

    time_t t;



    srand((unsigned)time(&t));

    do {

        cout << "Enter (rows, cols): ";

        getline(cin, in);

        ss.clear(); ss.str(in);

    } while ((!(ss >> rows >> c >> cols)) || (rows < 1) || (cols < 1));

   

    vector< deque* > board(rows);

    vector< deque* >::iterator i;

    N = random(1, cols * rows / 2);



    // initialize

    for (i = board.begin(); i != board.end(); i++) {

        *i = new deque(cols);

    }



    // randomize

    for (int n = 0; n < N; n++) {

        while (((*board[y=random(0, rows-1)])[x=random(0, cols-1)]) == true);

        (*board[y])[x] = 1;

    }



    // display

    for (i = board.begin(); i != board.end(); i++) {

        copy((*i)->begin(), (*i)->end(), ostream_iterator(cout, " "));

        cout << endl;

    }



    // show counts of adjacent filled cells

    while (true) {

        cout << "Enter (row, col): ";

        getline(cin, in);

        ss.clear(); ss.str(in);

        if (ss >> y >> c >> x) {

            cout << adjCount(board, y, x) << endl;

        }

    }

    return 0;

}



int adjCount(const vector< deque* >&b, int row, int col) {

    static const coord_t coord[] = {

        {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0 }, {1, -1} };

    static const size_t N = sizeof(coord) / sizeof(coord[0]);

    int count = 0, y, x;



    if (validRowCol(b, row, col)) {

        for (size_t i = 0; i < N; i++) {

            y = row + coord[i].row; x = col + coord[i].col;

            if (validRowCol(b, y, x) && ((*b[y])[x] == true)) {

                ++count;

            }

        }

    }

    return count;

}



bool validRowCol(const vector< deque* >&b, int row, int col) {

    int nRows = b.size(), nCols;

    return (row >= 0) && (row < nRows) &&

                  (col >= 0) && (col < (nCols = (*b[row]).size()));

}



int random(double rangeMin, double rangeMax) {

    assert(rangeMin <= rangeMax);

    double maxN = rangeMax - rangeMin + 1;

    return static_cast(((rand() / (double)RAND_MAX) * maxN) + rangeMin);

}



#if 0



Sample run:



Enter (rows, cols): 5, 4

0 1 0 0

1 1 0 0

0 1 0 1

0 0 0 1

1 1 0 0

Enter (row, col): 0, 0

3

Enter (row, col): 3, 3

1

Enter (row, col): 3,2

4

Enter (row, col): 0, 3

0



#endif


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...