How to store grid layout in array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • buddyfoeva
    New Member
    • Jan 2011
    • 1

    #1

    How to store grid layout in array?

    ok i need to code a game that has a rows and cols place in an array with every 5th one be a + with every other one using a period like this
    ............... .....
    ............... .....
    ............... .....
    ............... .....
    ....+.........+ .....
    ............... .....
    ............... .....
    ............... .....
    ............... .....
    ....+.........+ .....

    i can get that layout using loops but i cant figure out how to store it as an array. There are two global constants GRID_WIDTH and GRID_HEIGHT. how exactly could i store it so that i could transfer it to another module? so that when a buoy is place i can change the char in that array to the new char? here is that part of the code even though idk if it will help much
    Code:
    		for (int j = 1; j <= GRID_HEIGHT; j++)
    		{
    
    			if(GRID_HEIGHT < 1)
    			{
    				cout << setw(1) << j;
    			}
    
    			else
    			{
    				cout << setw(2) << j;
    			}
    
    
    
    			if(j % 5)
    			{
    				for(int y = 1; y <= GRID_WIDTH; y++)
    				{
    					cout << EMPTY_SPACE;
    				}
    			}
    
    			else
    			{
    
    
    				for(i = 1; i <= GRID_WIDTH; i++)
    				{
    			
    					if(i % 5)
    					{
    						cout << EMPTY_SPACE;
    					}
    
    					else
    					{
    					cout << INTERSECTION;
    					}
    				}//end for
    
    
    
    			}//end else
    
    
    
    			if(GRID_HEIGHT < 10)
    			{
    				cout << setw(1) << j;
    			}
    
    			else
    			{
    				cout << setw(2) << j;
    			}
    
    			cout << endl;
    		}
    oh the numbers are in there so you can tell where the buoy is place there are numbers on top that i didnt figure were important
  • tdlr
    New Member
    • Jan 2011
    • 22

    #2
    If I understood correctly, you want to save the current row setup of the game to modify it or to transfer it to somewhere else.
    For a single row, you could use a std string:
    Code:
    #include <string>
    #include <iostream>
    
    string row;
    	row.append(".");
    	cout<<"row is now: "<<row<<endl;
    	row[0] = '+';
    	cout<<"row is now: "<<row<<endl;
    Of course, before accessing a single character of the string, check its length().

    So if you wanted to save a complete row setup, just build an array or better a std::vector to save more than one row:
    Code:
    #include <vector>
    std::vector<string> rows;
    	string row1 = ".............";
    	rows.push_back(row1);
    	string row2 = "++.......+...";
    	rows.push_back(row2);
    	// Oh, we need to change position 4 in row 2:
    	rows[1][4] = '+';
    	//Print all rows:
    	for (std::vector<string>::iterator i = rows.begin(); i != rows.end(); ++i)
    		 std::cout<<(*i)<<std::endl;
    
    	/*Output:
    	.............
    	++..+....+...
    	*/

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      I have retitled your question from 'Help Me Now' to something a bit more appropriate.

      Bytes cannot assist with assignment questions, so I'll close the thread. As poster tdlr has already assisted you I'll leave it at that, but other moderators may simply delete it instead.

      MODERATOR

      Comment

      Working...