Greetings,
I'm hoping that i can get some help on an exercise I'm doing dealing with multidimensional arrays.

I want to move around a board and calculate how many steps I've taken on the board.
Input asks for the seed number and how many steps to take.
I just don't know what to do to make a point move.
It starts from the middle and the user does not need to say which direction to move. The program does randomly.

output:

board:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

"How many steps? 3"
"Total steps taken: 3"

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0

"How many steps? 2"
"Total steps taken: 5"

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 2 2 0 0 0 0
0 0 0 0 0 0 0 0


That's basically what I'm trying to make the program do. I'm having trouble figuring it out so i would love the help :/

NOTE: It's all basics, I have not reached dealing with Pointers yet....

Here is my original code. So far I'm just displaying the boards and collecting input...

#include<iostream>
using namespace std;

void displayBoards(int numBoard[][10], char symbolBoard[][10]);
int getSeed(int& seed); 
int getSteps(int n);


int main()
{
    int numBoard[10][10] = {{0},{0}};
    char symbolBoard[10][10] = {"\0"};
    int seed;
    int n;

    
    
    
    getSeed(seed);

    while(n != 0){
            srand(seed);
            getSteps(n);
            displayBoards(numBoard, symbolBoard); }
    


system("PAUSE");
return 0;
}

int getSeed(int& seed){
    cout<<"Seed: ";
    cin>>seed;
}

int getSteps(int n){
    cout<<"Number of steps? (0 to end) ";
    cin>>n;
    cout<<endl;
}

void displayBoards(int numBoard[][10], char symbolBoard[][10]){
    
     for(int row=0; row<10; row++){
             for(int col=0;col<10;col++){
                     cout<<numBoard[row][col]<<" ";            
             }                            
    cout<<endl;
    }
    
    //char symbolBoard[10][10] = {{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',}, 
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',}, 
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',},
                                //{'.','.','.','.','.','.','.','.','.','.',}};
    

    
    for(int row=0; row<10; row++){
            for(int col=0;col<10;col++){
                    cout<<symbolBoard[row][col]<<".";             
             }                            
    cout<<endl;
    }
    cout<<endl;
}
//initialize a 2D array
char board[5][9];

//make everything '0'
for(int i=0; i<5; i++)
     for(int j=0; j<9; j++)
     {
          board[i][j] = '0';
     }

//prompt user for number of random steps to take from middle

//call a recursive function to populate the board with a random steps
void random_step_generator(int& turns, char& board[][], int& row, int& col)
{
     //RECURSIVE EXIT CASE
     //Test for ANY valid possible move, if none exit function
     if(!turns || board[row+1][col]=='1' || board[row][col+1]=='1'
        || board[row-1][col]=='1' || board[row][col-1]=='1')
     {
          cout << "\n\nNo more moves. ";
          return;
     }  

     //0=up, 1=right, 2=down, 3=left
     enum{up, right, down, left};
     direction = rand()%4;

     switch(direction)
     {
          case up:  
          board[row+1][col]=='0'?board[++row][col]=='1':random_step_generator(turns, board, row, col);
          break;

          case right:  
          board[row][col+1]=='0'?board[row][++col]=='1':random_step_generator(turns, board, row, col);
          break;

          case down:  
          board[row-1][col]=='0'?board[--row][col]=='1':random_step_generator(turns, board, row, col);
          break;

          case left:  
          board[row][col-1]=='0'?board[row][--col]=='1':random_step_generator(turns, board, row, col);
          break;
     }

      //Display the move
     display();

     //Recursive call
     random_step_generator(--turns, board, row, col);
}

Edited 5 Years Ago by Clinton Portis: n/a

this may not be perfect, but it's better than that crap i just gave you. tweak it, debug it, take the concepts and run with it:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

void random_step_generator(int& turns, char board[5][9], int& row, int& col);
void display(char board[5][9]);

int main()
{
     char board[5][9];
     int turns = 0;
     int row = 2;
     int col = 4;
     char ans = '\0';

     srand(time(NULL));

     do{
          //make everything '0'
          for(int i=0; i<5; i++)
            for(int j=0; j<9; j++)
            {
                 board[i][j] = '0';
            }

         //starting position
         board[row][col] = '*';

         //prompt user for number of random steps to take from middle
         cout << "\nEnter number of random steps to take ";
         cin >> turns;

         random_step_generator(turns, board, row, col);

         display(board);

         cout << "Would ye' like to try again? (Y/N) ";
         cin >> ans;

         row = 2;
         col = 4;

         }while(ans == 'y' || ans == 'Y');

     return 0;
}

//call a recursive function to populate the board with a random steps
void random_step_generator(int& turns, char board[5][9], int& row, int& col)
{
     int direction = 0;

     //RECURSIVE EXIT CASE
     //Test for ANY valid possible move, if none exit function
     if(!turns || (board[row+1][col]=='*' && board[row][col+1]=='*'
        && board[row-1][col]=='*' && board[row][col-1]=='*'))
     {
          cout << "\nNo more available moves. " << endl << endl;
          return;
     }

     //0=up, 1=right, 2=down,3=left
     enum{up, right, down, left};
     direction = rand()%4;

     if(direction == up && board[row+1][col]=='0' && row)
     {
        board[++row][col] = '*';
     }
     else if(direction == right && board[row][col+1]=='0'&& col<8)
     {
        board[row][++col] = '*';
     }
     else if(direction == down && board[row-1][col]=='0' && row<4)
     {
        board[--row][col] = '*';
     }
     else if(direction == left && board[row][col-1]=='0'&& col)
     {
          board[row][--col] = '*';
     }
     else //try again
     {
         random_step_generator(turns, board, row, col);
         return;
     }

    //make another random step
    random_step_generator(--turns, board, row, col);
}

void display(char board[5][9])
{
    for(int i=0; i<5; i++)
    {
         for(int j=0; j<9; j++)
         {
             cout << board[i][j];
         }

         cout << endl;
    }

    cout << endl;
}

Edited 5 Years Ago by Clinton Portis: n/a

this would actually be a better way to do it once you get to the boundaries of the array:

//RECURSIVE EXIT CASE
     //Test for ANY valid possible move, if none exit function
     if(!turns || (board[row+1][col]!='0' && board[row][col+1]!='0'
        && board[row-1][col]!='0' && board[row][col-1]!='0'))
     {
          cout << "\nNo more available moves. " << endl << endl;
          return;
     }

Thank you so much for the help! I seem to understand. My whole problem was ignoring the direction. Sorry I have not been responding but i have been implementing your method. I would like to know how to keep count of all the steps I input into it. For that, I need to keep the graph continuing until I hit the edge of the board....

I purposely made this close to what you want, but not exactly what you wanted... just so you can attempt to resolve minor issues like this on your own. you have my idea of a concept i think you should use, which is 90% of the task. make an attempt. make some psuedo code of what you want to do, and translate it into c++.

if nothing else, show us your attempts, and we will kinda point ye' in the right direction.

all of this is to ensure that we do not have to figuratively spoon feed you, or hold your hand when crossing the street.

you are a big boy now.

Edited 5 Years Ago by Clinton Portis: make a counter and pass it into the random_step_generator( ) function. increment the counter on each recursive iteration.

This article has been dead for over six months. Start a new discussion instead.