Using vector as an array of strings for packman-type game

I neeed help with a packman-type game assignment

These are the guidelines

1. Change the map array to a vector of strings. Remove the pre-initlization/premade map (see #2)


2. Write a load functon that:

Asks user for file name

Loads the map from a file

The option must check for file existence and it being able to load the map, otherwise, it asks again.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include "gotoxy.h"


using namespace std;

bool moveTo(int x, int y, int &bite, string map[])
{
    bool flag = false;
    
    
    if (map[y][x] == ' ')
    {
        flag = true;
        map[y][x] = '@';
        
    }
    else if (map[y][x] == 'x')
    {
        flag =true;
        map [y][x] = '@';
        
        bite++;
        
    }
    
    return flag;
}


int main()
{
    
    
    string map[10] =
    {
        
        "###################",
        "#@                #",
        "#              x  #",
        "#                 #",   //This is how the map looks
        "#                 #",
        "#         x       #",
        "#                 #",
        "#                 #",
        "###################"
        
    };
    
    int x=1;
    int y=1;
    int bite= moveTo(x,y,bite,map);
    bool game_running = true;
    
    do{
        
        gotoxy(0,0); //uses the gotoxy file
        
        
        for (int i=0; i< 10 ; i++)
            cout << map [i] << endl;
        
        
        
        
        cout << "You ate " << bite << " bite(s)" << endl;
        
        system("pause>nul");
        
        if(GetAsyncKeyState(VK_DOWN) && moveTo(x, y+1,bite,map))
        {
            map [y][x]=' ';
            y++; //moves down
        }
        
        if(GetAsyncKeyState(VK_UP)  && moveTo(x, y-1,bite,map) )
        {
            map [y][x]=' ';
            y--; //moves up
        }
        
        if(GetAsyncKeyState(VK_RIGHT)  && moveTo(x+1,y,bite,map) )
        {
            map [y][x]=' ';
            x++; //moves right
        }
        
        if(GetAsyncKeyState(VK_LEFT) && moveTo (x-1,y,bite,map)  )
        {
            map [y][x]=' ';
            x--; //moves left
        }
        
        if(GetAsyncKeyState(VK_ESCAPE))
        {
            game_running = false;
        }
    }
    
    while(game_running == true);
    
    system("cls");
    cout << "Game over! Thanks for playing!";
    
    
    
    
    return 0;
}


This is my gotoxy.h file

1
2
3
4
5
6
7
8
9
#include "windows.h"


BOOL gotoxy(const WORD x, const WORD y) {
    COORD xy;
    xy.X = x;
    xy.Y = y;
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}

Last edited on
Please help, thanks in advance
Do you know how vectors work? The easiest, most comprehensive resource would be a reference (such as the one on this site - you can just search 'vector' in the search bar at the top of the page). For creating your map, you could do this:
35
36
37
38
39
40
41
42
43
44
45
    std::vector<std::string> map = {
        "###################",
        "#@                #",
        "#              x  #",
        "#                 #",   //This is how the map looks
        "#                 #",
        "#         x       #",
        "#                 #",
        "#                 #",
        "###################"
    };


I'll leave it to you to work out how to do the second part - you may want to look at fstream (the file in/out header for C++).
Topic archived. No new replies allowed.