aishere
aishere

Reputation: 53

How to check each element in array?

I'm trying to analyze each element that has been placed into my array from a txt file of chars for a maze game. The array must be checked so we can determine where in the maze the walls are, players are, ghosts, keys and ladders, entrance, etc. So I need to check for certain characters (such as #, P, K, D, G, E). I'm not sure how to set this up though?

Here are the functions to get the Floor files and place each character in the array called tiles.

const int ROWS = 20;

void Floor::Read (const char * filename)
{
size_t row = 0;
ifstream input(filename);

while (row < ROWS && input >> tiles[row++]);

}
void Game::readFloors()
{
m_floor[0].Read("FloorA.txt");
m_floor[1].Read("FloorB.txt");
m_floor[2].Read("FloorC.txt");

}

Then here is what one of the floors looks like with each char:

##############################
#      K                     #
#  ############## ### ###  # #
#      K    #   # #C# #K#  # #
# ######### # A # # # # #  # #
#      K    #   #          # #
# ############D#####D####### #
#                            #
#   C         G        C     #
#                            #
# ######D##############D#### #
#      #  C         #K#    # #
# #### ######## #   # #      #
# #K   #      # ### # # #### #
# # ## # #### #   # # #    # #
E # ## # #    ### # # #### # #
# #    # #K           D    # #
# #D#### ################### #
#                    K       #
##############################

header file with the two classes used with the functions above:

class Floor {
char tiles[20][30]; 
void printToScreen();
public:
Floor();
void Read (const char * filename);
};

class Game {
private:
Floor m_floor [3];
vector<Character> characters; 
public:
void readFloors();
};

Looked for similar questions, but most aren't analyzing many different options. Thanks for the help!

Upvotes: 0

Views: 220

Answers (2)

Gabe
Gabe

Reputation: 971

I've put together a simple reader that puts the map (based on your example) into a matrix you can juggle with.

#include <iostream>
#include <fstream>
#include <string>

std::ifstream f("/path/../txt.txt");
std::string str;

int cols = 0;
int rows = 0;

char mat[100][100];

int main(){
    int line = 0;
    while(std::getline(f, str)){
        const char * chars = str.c_str();
        for(int i=0; i<str.length(); i++){
            mat[line][i] = chars[i];
        }

        cols = str.length();
        line++;
        rows++;
    }

    for(int i=0; i<line; i++){
        for(int j=0; j<rows; j++){
            std::cout<<mat[i][j]<<" ";
        }
        std::cout<<std::endl;
    }
    std::cout<<"Cols: "<<cols<<" Rows: "<<rows;
    return 0;
}

This way, you can get the position of each player, wall block etc. by a two point coordinates just by performing simple character comparisons.

Upvotes: 1

ZachariahRS
ZachariahRS

Reputation: 65

You can use a for loop inside of a for loop, one to check the columns in the array and another to check the rows. Them you'd just check each element if it equals k,c,g,etc.

Upvotes: 1

Related Questions