生命边界游戏 - 检查活细胞

时间:2014-05-02 21:01:59

标签: c++ conways-game-of-life

我正在为我正在参加的C ++课程制作康威的生命游戏,而我似乎无法让它保持在边界的范围内。我们正在使用一个古老的类,所以它的解释如下。

<lvp\matrix.h> - 允许重新调整大小的二维数组

用法 -

声明 - matrix<itemType> itemName(numRows, numColumns, fillValue)

传递给函数 - (itemName)

函数中的

参数 - (const matrix<itemType> &name)或前面没有'const'的参数。

所以这是一个问题的图片(向量是一个基于矩阵的类,它生成数组,并且有方法可以很容易地重新调整大小)。

http://imageshack.com/a/img835/2531/03gx.png

请记住我是一名初学程序员,所以请对你的批评保持温和:)

提前谢谢你。

所以这是迄今为止的代码:

#include <lvp\matrix.h>
#include <iostream.h>

//--------------------------------------------------------------

void displayBoard(const matrix<char> &board)
{
    cout << "   1  2  3  4  5  6  7" << endl;
    for( int row=0; row<board.numrows(); row++)
    {
        cout << row+1 << " ";
        for( int col = 0; col < board.numcols(); col++)
            cout<<"["<<board[row][col]<<"]";
        cout<<endl;
    }
}

//--------------------------------------------------------------

void placeLiveCells(matrix<char> &board, matrix<char> &check)
{
    //Test function
    board[3][3]='X';
    board[3][4]='X';
    board[3][2]='X';
    check[3][3]='X';
    check[3][4]='X';
    check[3][2]='X';
}

//--------------------------------------------------------------



//--------------------------------------------------------------

void checkNeighbors(matrix<char> &board, int row, int col)
{
    int count=0;
    for(int i=0; i<2; i++)
    {
        if(board[row][col+1]=='X')
            count++;
        if(board[row][col-1]=='X')
            count++;
        if(board[row+1][col+1]=='X')
            count++;
        if(board[row-1][col-1]=='X')
                count++;
        if(board[row+1][col-1]=='X')
            count++;
        if(board[row-1][col+1]=='X')
            count++;
        if(board[row+1][col]=='X')
            count++;
        if(board[row-1][col]=='X')
            count++;
        if(count<2)
            board[row][col]='O';
        if(count>3)
            board[row][col]='O';
    }
}

//--------------------------------------------------------------

void findX(matrix<char> &board)
{
    int numspots=board.numrows()*board.numcols(), r=0, c;
    for(int j=0; j<numspots; j++)
    {
        if(board[r][c]=='X')
            checkNeighbors(board, r, c);  /*This is where I think the 
problem is, the code was working ok before I added this function*/
        if(c==6)
        {
            c=0;
            r++;
        }
    }
}

//--------------------------------------------------------------

int main()
{
    int row=0, col=0;
    matrix<char> check(7, 7, 'O');
    matrix<char> board(7, 7, 'O');
    displayBoard(board);
    placeLiveCells(board, check);
    displayBoard(board);
    findX(board);
    checkNeighbors(board, row, col);

    return(0);
}

我对checkNeighbors的下一次尝试包括检查超出范围的错误&lt; 0,但仍然无效:

void checkNeighbors(matrix<char> &board, int row, int col)
{
    int count=0;
    for(int i=0; i<2; i++)
    {
        if((row-1>=0) && (col-1>=0))
        {
            if(board[row][col+1]=='X')
                count++;
            if(board[row][col-1]=='X')
                count++;
            if(board[row+1][col+1]=='X')
                count++;
            if(board[row-1][col-1]=='X')
                count++;
            if(board[row+1][col-1]=='X')
                count++;
            if(board[row-1][col+1]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row-1][col]=='X')
                count++;
            if(count<2)
                board[row][col]='O';
            if(count>3)
                board[row][col]='O';
        }
        else if((row-1<0) && (col-1>=0))
        {
            if(board[row][col+1]=='X')
                count++;
            if(board[row][col-1]=='X')
                count++;
            if(board[row+1][col+1]=='X')
                count++;
            if(board[row][col-1]=='X')
                count++;
            if(board[row+1][col-1]=='X')
                count++;
            if(board[row][col+1]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row][col]=='X')
                count++;
            if(count<2)
                board[row][col]='O';
            if(count>3)
                board[row][col]='O';
        }
        else if((row-1>=0) && (col-1<0))
        {
            if(board[row][col+1]=='X')
                count++;
            if(board[row][col]=='X')
                count++;
            if(board[row+1][col+1]=='X')
                count++;
            if(board[row-1][col]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row-1][col+1]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row-1][col]=='X')
                count++;
            if(count<2)
                board[row][col]='O';
            if(count>3)
                board[row][col]='O';
        }
        else if((row-1<0) && (col-1<0))
        {
            if(board[row][col+1]=='X')
                count++;
            if(board[row][col]=='X')
                count++;
            if(board[row+1][col+1]=='X')
                count++;
            if(board[row][col]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row][col+1]=='X')
                count++;
            if(board[row+1][col]=='X')
                count++;
            if(board[row][col]=='X')
                count++;
            if(count<2)
                board[row][col]='O';
            if(count>3)
                board[row][col]='O';
        }
    }
}

所以我在这里所做的就是用if if语句重新编写checkNeighbors并使用if语句来检查其他单元格。他们现在做到这一点,如果rowcol是负数,在检查相邻单元格时,它将不会-1到它将产生负数。

0 个答案:

没有答案