Generate board

This is a discussion on Generate board within the C Programming forums, part of the General Programming Boards category; I need to create a program that receives two numbers from the user (rows and columns) and then generates a ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Generate board

    I need to create a program that receives two numbers from the user (rows and columns) and then generates a board with arrows in random direction. In the middle of the board should be a diamond.

    This is my code. It does not generate arrows and does not create diamond in the middle.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    /* Function that generates the board */
    int boardgame(int row, int column) {
    int i,j, board2[row][column],num;
    float r,c;
    srand(time(NULL));
    r=row;
    c=column;
    
    for (i=0; i<row; i++) 
    {
        for (j=0; j<column; j++) 
        {
            if ((j==(c/2.0)) && (i==(r/2.0))) {
            board2[i][j]=35; }
           else if (((j-1)==(c/2.0)) && (i==(r/2.0))) {
            board2[i][j]=59; }
           else if ((j==(c/2.0)) && ((i-1)==(r/2.0))) {
            board2[i][j]=59; }
           else if (((j-1)==(c/2.0)) && ((i-1)==(r/2.0))) { 
            board2[i][j]=35; }
            else {
            num=(10 + rand()% 4);
            board2[i][j]=num; 
            }
            }
            }
            return (board2[row][column]); 
            }
    
    int main()
    {
        int z,x,row,column, q1=1;
        int board[20][20];
        
        /* Checks if the entered # of rows and columns is correct */
        while (q1==1) {
        printf("Please enter the even # of rows (4,6,8,10,12,14,16,18,20):");
        scanf("%d", &row);
        printf("Please enter the even # of columns (4,6,8,10,12,14,16,18,20):");
        scanf("%d", &column);
        if ((row>20) || (row<4) || (column>20) || (column<4) || !(row%2==0) || !(column%2==0)) 
                     printf("Invalid Data\n");
                     else
                     q1=0; }
       
     board[20][20]=boardgame(row,column);
     printf("\n\n\n");
        /* Prints the board */
        for (z=0; z<row; z++) {
            printf("%d", row-z);
        for (x=0; x<column; x++) {
                    printf("  %c  ", board[z][x]); }
                    printf("\n\n"); }
                    
                    printf(" ");
                    for (z=1; z<=column; z++)
                    printf("  %d  ", z);
                    printf("\n\n\n");
        
            system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't assign arrays, nor return them from a function.

    Code:
    board[20][20]=boardgame(row,column);
    This just assigns an integer to an item that also happens to be out of bounds.

    Instead you should pass the board into the function to be filled out there.

    Code:
    int boardgame(int row, int column, int board2[][20])
    The filling in part also looks messed up. I thought you wanted one of 4 random characters in each field, and then put a diamond in the middle.

    Code:
    char arrows[4] = {x, y, z, w}; //whichever values the arrows should be
    char random_arrow = arrows[rand() % 4];
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 07:02 AM
  2. Constructor problem
    By rebel in forum C++ Programming
    Replies: 22
    Last Post: 01-11-2006, 06:45 AM
  3. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 05:23 AM
  4. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21