0

所以我每天都面临脱发的问题,因为我第一年的任务需要这个子任务。

在程序的初始化阶段,我需要在坐标 [5][5] / 即板的中心处的数字周围打印一个“框”。

“盒子”由“ | ”组成,覆盖两侧,“ _ ”在坐标 [5][5]处的数字的顶部和底部。

当我执行这个程序时,板子显示但“盒子”没有。为什么会这样??

这是代码:

#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, flag;

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

        for ( rows = 0 ; rows < 9 ; rows++ )
        {

            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                flag = 0;

                do
               {
                    //generate random numbers from 2 - 8

                    randomNumber = rand() %7 + 2;

                    board[rows][columns] = randomNumber;

                        //Display the 'box' if rows and columns == 5 / i.e - board[5][5]
                        if ( rows == 5 && columns == 5 )
                        {   //Checks for 2 adjacent numbers
                            if  ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
                            {
                                flag = 0;
                                continue;
                            }
                            else
                            {
                                flag = 1;

                                //Print 'box'
                                marker( rows, columns );
                            }
                        }

                    //Checks for 2 adjacent numbers.
                    if  ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
                        {
                            flag = 0;
                            continue;
                        }

                    else
                    //Prints the correct board
                         {
                                flag = 1;
                                printf( "  %d  ", board[rows][columns] );
                         }

                } while ( flag == 0 ); //end outer do-while

            }//end inner for-loop

            printf("\n\n");

        }//end outer for-loop

}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, int** board )
  {
    board[x][y-1] == "\n _ ";
    board[x][y+1] == "\n _ ";
    board[x-1][y] == " |";
    board[x+1][y] == "| ";
  }

int main()
{
    drawBoard();
}

德州仪器

4

3 回答 3

0
void marker( int x, int y, int** board )
  {
    board[x][y-1] == "\n _ ";
    board[x][y+1] == "\n _ ";
    board[x-1][y] == " |";
    board[x+1][y] == "| ";
  }

这行不通。'==' 用于比较,而不是分配。你需要一个'='。但是,board 是一个 int 数组,您不能在 int 数组中分配一个const char*(因为那是什么"something")。你的板应该是一个字符数组,然后你可以像这样分配:

void marker( int x, int y, char** board )
  {
    board[x][y-1] = '_';
    board[x][y+1] = '_';
    board[x-1][y] = '|';
    board[x+1][y] = '|';
  }

另一个问题是棋盘的中心位于索引 [4][4] 处,即大小为 9 的表格的第五个元素。但这是一个较小的问题......

于 2013-10-24T15:30:34.707 回答
0

我知道你想在这里做什么,这不是我通常的想法,所以我可能没有帮助:s

首先,一个错误:在"drawboard": marker( rows, columns )

的原型"marker": void marker( int x, int y, int** board )。我不认为用标志编译会起作用(是的,标志很有帮助,试试"gcc -Wall -Wextra -Werror"

此外,我认为你应该重新考虑你的功能“标记”,它不能工作。

我认为board[x][y-1] = "\n _ "可以解决问题,如果您return(board[x][y-1])再次使用,我将使用一个char**,而不是整数制表符,并使用许多短函数(并用于write代替printf:p)

于 2013-10-24T15:55:01.450 回答
0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

//FUNCTION: Draw the Board
char tochar(int i)
{
    return i+'0';
}
void marker( int x, int y, char board[][19] );
int check(int x, int y, char board[][19])
{
    if(x>1&&board[2*x+1][2*y+1]==board[2*x-1][2*y+1])
        return 1;

    if(y>1&&board[2*x+1][2*y+1]==board[2*x+1][2*y-1])
        return 1;
   return 0;
}
int drawBoard()
{
    //Declare array size
    char board[19][19];

    memset(board,32, sizeof(board));
    //initialize variables
    int rows, columns, randomNumber, flag;

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

        for ( rows = 0 ; rows < 9 ; rows++ )
        {

            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                flag = 0;

                do
               {
                    //generate random numbers from 2 - 8

                    randomNumber = rand() %7 + 2;
                    board[2*rows+1][2*columns+1] = tochar(randomNumber);

                        //Display the 'box' if rows and columns == 5 / i.e - board[5][5]
                        if ( rows == 4 && columns == 4 )
                        {   //Checks for 2 adjacent numbers
                            if  ( check(rows, columns, board))
                            {
                                flag = 0;
                            }
                            else
                            {
                                flag = 1;

                                marker( 2*rows+1, 2*columns+1, board );
                                //Print 'box'
                            }
                        }
                    else
                    {
                    //Checks for 2 adjacent numbers.
                    if  ( check(rows, columns, board))
                        {
                            flag = 0;
                        }

                    else
                    //Prints the correct board
                         {
                            flag = 1;
//                            printf( "  %c  ", board[2*rows+1][2*columns+1] );
                         }
                    }
                } while ( flag == 0 ); //end outer do-while

            }//end inner for-loop

            printf("\n\n");

        }//end outer for-loop
    for(rows=0;rows<19;rows++)
    {
        for(columns=0;columns<19;columns++)
        {
            printf("%c",board[rows][columns]); 
        }
        printf("\n");
    }
}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, char board[][19] )
  {
    board[x][y-1] = '|';
    board[x][y+1] = '|';
    board[x-1][y] = '_';
    board[x+1][y] = '_';
   /* 
192
191
217
218
*/
  }

int main()
{
    drawBoard();
}
于 2013-10-24T15:55:39.930 回答