Hi.
I know it's now rly allowed to post homework and stuff. But i just need some help, like hints, or examples. =]
Don't fully understand how arrays(marked as mass in prog.) work yet. Arrays = massivs (in the countries language where i live)


[ The point is to have the X (character) walk around the playing field, and collect something (stars -> '*' ), that are invisible to the player until he walks over them ]

So what i need some help with is:

1) The x ( the so called character), goes beyond the playing field...
How to make it so, when the next "move" would go onto the filed line, to stop it, and say like "Can't go that way".

2)Need to the make the (*) invisible until he walks over em. And when he gets all of em, the game stops and shows the game is over message.
=========================================
This is what i got so far.

#include <iostream>
#include <iomanip>
using namespace std;

    
    const int height = 18;                                    // tak kak pole 16, a 2 stroki liniji
    const int width = 18;
    void Gamefield( char mass[][width + 1] );
    
    int main()
{
    int k = 0;
    char location;
    int x = 5;                                                     // Na4alo igri
    int y = 5; 
    
     char mass [ height ][ width + 1 ] = {
        
         "|---------------|",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|               |",
         "|---------------|"   };
         
         
         
    mass[ x ][ y ] =  'X';                                           // Player
    mass[ 8 ][ 5 ] =  '*';   
    mass[ 2 ][ 14 ] =  '*';                                        //Objects
    mass[ 10 ][ 3 ] =  '*';
    mass[ 15 ][ 15 ] =  '*';
    mass[ 15 ][ 7 ] =  '*';
    mass[ 3 ][ 2 ] =  '*';
    
    cout << "     Welcome to the game!" << endl
    << "Use the W/S/A/D buttons to move! " << endl << endl;
     
     for(int j=1;j>0;j++)  {
             
    Gamefield (mass);
    
    
    cout << "Which side to go? ";
    cin >> location;
    
    if (location == 'w') {
           mass[ x-1 ][ y ] = 'x';
           mass[ x ][ y ] = ' ';
            x--;
            }
            else if (location == 's') {
                 mass[ x+1 ][ y ] = 'x';
                 mass[ x ][ y ] = ' ';
                 x++;
                 }
                 else if (location == 'd') {
                      mass[ x ][ y +1 ] = 'x';
                      mass[ x ][ y ] = ' ';
                      y++;
                      }
                      else if (location == 'a') {
                           mass[ x ][ y-1 ] = 'x';
                           mass[ x ][ y ] = ' ';
                           y--;
                           }
                           else {
                                cout << endl << endl << "You typed in a wrong direction!" 
                               << " (W/S/A/D are the direction keys)"
                                << endl << "Which direction do you want to go now?: ";
                                 cin >> location;
                                     }
                                     
   
            
            system("CLS");                                       // Clear screen
}
     
     return 0;
}

void Gamefield( char mass [ height ][ width + 1 ] ){
         for (int i=0; i<height; i++){
             cout << mass[i] << endl;
             }
             cout << endl << endl;
}

1) This is called edge detection which is often part of a validation process. If the x or y coordinate is above 17 or under 0 then that move is invalid so you can tell the user and ask for alternate input. Keep doing that in a loop until a valid move has been made.

2)
Option 1:
keep an extra playingfield internally to the program that isn't revealed to the user. Use the indexes in the visualized field to see if a * or whatever is in the internal, not visualized, playingfield cell indicated by the players move and if it is, do whatever you have to do.

Option 2:
Declare mass to be a 2 dimensional array of user defined type, rather a 2 dimensinal array of type char. The user type could contain one variable to display to the user and another that contains information to be hidden from the user until the user inputs the appropriate coordinates. Other variables could be used to indicate whether this cell has been visited before, can't be visited for some reason, whatever. You can design the makeup of the user defined type to do what you want it to.

Thx. ^^

The 1st problem got solved, why didn't i try that myself >.<'
But the 2nd part still having problems. Will try to solve this myself, if not hope u can help me out again.
Got 6 more days to finish this. xD

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