1

作为我第一年任务的一部分,我正在开发一个简单的糖果迷恋游戏。

所以我被困在这部分,在初始化过程中,3个相同的数字不能彼此相邻。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//FUNCTION: Draw the Board
int drawBoard()
{
    //Declare array size
    int board[9][9];

    //initialize variables
    int rows, columns, randomNumber;

    //random number seed generator
    srand(time(NULL));

        for ( rows = 0 ; rows < 9 ; rows++ )
        {
            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                //generate random numbers from 2 - 9
                randomNumber = rand() %7 + 2;

                board[rows][columns] = randomNumber;

                printf( "%d\t", board[rows][columns] );

            }//end inner for-loop

            printf("\n\n");
        }//end outer for-loop

}//end FUNCTION drawBoard

int checkAdjacent()
{

}

int main()
{
    drawBoard();
}

我想也许在函数drawBoard()中调用函数checkAdjacent(),然后使用这样的代码:

if ( board[rows][columns] == randomNumber && 
     board[rows][columns] == randomNumber && 
     board[rows][columns] == randomNumber )

也许我必须在 drawBoard 函数中执行此操作,但我只是越来越困惑自己,同时在这里挠头:/

有什么提示吗?

4

3 回答 3

0

此代码可以帮助您

            if((columns > 1) && board[rows][columns - 1] == board[rows][columns - 2]
                           && board[rows][columns - 1] == randomNumber) {
                do {
                    randomNumber = rand() %7 + 2;
                } while (randomNumber == board[rows][columns - 1]);
            }

它不漂亮,但它有效

于 2013-10-23T10:54:44.063 回答
0

我不确定这个游戏,但试试这个:我在插入时检查邻接。由于它是一块小板,因此复杂性不会受到太大影响。此外,由于其随机性,具有相同相邻元素的概率也较小。

    int flag;

    for ( rows = 0 ; rows < 9 ; rows++ )
    {
        for ( columns = 0 ; columns < 9 ; columns++ )
        {
            //generate random numbers from 2 - 9

            flag = 0;
            do{
               randomNumber = rand() % 7 + 2;
               board[rows][columns] = randomNumber;
               if ((board[rows][columns] == board[rows-1][columns]) || (board[rows][columns] == board[rows][columns-1]))
                 flag = 0;
               else 
                 flag = 1; 
            //printf( "%d\t", board[rows][columns] );
             }while(flag == 0);

        }//end inner for-loop

        printf("\n\n");
    }//end outer for-loop
于 2013-10-23T11:00:29.137 回答
0

不是最优化的代码......但你会明白的!

# include <iostream>
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <conio.h>

//FUNCTION: Draw the Board
void drawBoard()
{
    //Declare array size
    int board[9][9];

    //initialize variables
    int rows, columns, randomNumber;
    int x, y, z;

    //random number seed generator
    srand(time(NULL));

        for ( rows = 0 ; rows < 9 ; rows++ )
        {
            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                //generate random numbers from 2 - 9
                randomNumber = rand() %7 + 2;
                board[rows][columns] = randomNumber;
                printf( "%d\t", board[rows][columns] );
            } //end inner for-loop
            printf("\n\n");
        } //end outer for-loop

        // Checking triple values - Horizontal values
        for ( rows = 0 ; rows < 9 ; rows++ )
        {
            printf ("\n");
            for ( columns = 0 ; columns < 7 ; columns++ )
            {
                printf ("%d %d %d - ", board[rows][columns], board[rows][columns+1], board[rows][columns+2]);
                x = board[rows][columns];
                y = board[rows][columns+1];
                z = board[rows][columns+2];

                if ((x == y) && (y == z))
                    printf ("Found\n");
                else
                    printf ("Nop\n");
                if (columns == 7)
                    break;
            } //end inner for-loop
        } //end outer for-loop

}//end FUNCTION drawBoard

int main()
{
    drawBoard();
    _getch();
}
于 2013-10-23T11:21:49.357 回答