Maze game help

Hello!
I'm making a maze game but I can't understand how to generate new levels. Please help
Code: http://pastebin.com/2PQ34XNE


You have a lot of repeated code in there, and sadly because of the way you are writing your code you will end up needing to write direction code for each level.

Anyway, to answer your question you should have a array for the currently played map, i.e. what is being updated by player movement, and a array for your levels where you can use as a lookup and copy from - what happens when the player in your current code wants to play again? - the array was already modified by the last game so no coins etc would be left.

You should try and brake down your code into functions.

Ive written a bit of code just to give you an idea what I mean, hope it helps :)

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#include <iostream>

void setLevel(int);
void displayLevel();

// this is where our map we are currently 
// playing will be copied to.
char Map[20][30];

// example level data.
char levels[60][30] = {
	"#############################",	// Level 1
	"#  #     c         ####     #",
	"#    ##       c    #        #",
	"# ## #  c          ##       #",
	"#    #   ####        c      #",
	"#  ###        c             #",
	"#            *       c      #",
	"# #        c c c            #",
	"# #        c @ c          c #",
	"#          c c c            #",
	"# # # c #          ##       #",
	"#c   ##### c     ########   #",
	"#  ##################  c    #",
	"#   c     #   !   c #       #",
	"#     ####### ######        #",
	"#           #  #       c    #",
	"#     c      # ##           #",
	"#     c      #     c        #",
	"#     c      #     c        #",
	"#############################",
	"#############################",	// Level 2
	"#                           #",
	"#       ##############      #",
	"#                           #",
	"#    #   ####   cccccc      #",
	"#  ###        c             #",
	"#       ccc  *       c      #",
	"# #        # # c  #         #",
	"# #        c @ #      #   c #",
	"#       #  c c #    ##      #",
	"#       #    #  #     ##    #",
	"#    ####         ####      #",
	"#   #    ###  ####          #",
	"#   #                       #",
	"#   ########                #",
	"#    #      #  #####   c    #",
	"#     c      # ##  #        #",
	"#     c## ## #    #c####    #",
	"#     c      # !   c        #",
	"#############################",
	"#############################",	// Level 3
	"#    cc   ##  cccc          #",
	"#    ## # #   c    #   ##   #",
	"#                      ##   #",
	"#    #   #### #####  c ##   #",
	"#  ###        c             #",
	"#            * ## #  c      #",
	"# #        c c c  #         #",
	"# #        c @ c  #       c #",
	"#          c c c  #         #",
	"# # # c #          ##       #",
	"#c   ##### c     ########   #",
	"#          # ##             #",
	"#   c #####   !#  c #####   #",
	"#     #       #    #        #",
	"#     #   ####    #    c    #",
	"#     #          #          #",
	"#     c#########   c        #",
	"#     c      #     c        #",
	"#############################",
};


int main()
{
	int level = 1;
	const int MAX_LEVELS = 3;

	// create a demo loop to show each level
	for (int i = 0; i < MAX_LEVELS; i++) {
		// set level
		setLevel(level);
		// display level
		displayLevel();
		// next level
		level++;
	}
	
	return 0;
}

void setLevel(int level)
{
	std::cout << std::endl << "Level: " << level << std::endl;
	// remember arrays start at 0 so level 1
	// would need to point to position 0.
	level--;
	// we use the level to create an offset to
	// next level
	level = 20 * level;
	// copy level data to map
	int mapDown = level, mapAcross = 0;
	for (int d = 0; d < 20; d++) 	{
		mapAcross = 0;
		for (int a = 0; a < 30; a++)		{
			Map[d][a] = levels[mapDown][mapAcross];
			mapAcross++;
		}
		mapDown++;
	}
}

// display level simply shows whats in map, map
// was written to by setLevel as per the current
// level you are playing on.
void displayLevel()
{
	for (int d = 0; d < 20; d++) {
		for (int a = 0; a < 30; a++) {
			std::cout << Map[d][a];
		}
		std::cout << std::endl;
	}
}


Level: 1
#############################
#  #     c         ####     #
#    ##       c    #        #
# ## #  c          ##       #
#    #   ####        c      #
#  ###        c             #
#            *       c      #
# #        c c c            #
# #        c @ c          c #
#          c c c            #
# # # c #          ##       #
#c   ##### c     ########   #
#  ##################  c    #
#   c     #   !   c #       #
#     ####### ######        #
#           #  #       c    #
#     c      # ##           #
#     c      #     c        #
#     c      #     c        #
#############################

Level: 2
#############################
#                           #
#       ##############      #
#                           #
#    #   ####   cccccc      #
#  ###        c             #
#       ccc  *       c      #
# #        # # c  #         #
# #        c @ #      #   c #
#       #  c c #    ##      #
#       #    #  #     ##    #
#    ####         ####      #
#   #    ###  ####          #
#   #                       #
#   ########                #
#    #      #  #####   c    #
#     c      # ##  #        #
#     c## ## #    #c####    #
#     c      # !   c        #
#############################

Level: 3
#############################
#    cc   ##  cccc          #
#    ## # #   c    #   ##   #
#                      ##   #
#    #   #### #####  c ##   #
#  ###        c             #
#            * ## #  c      #
# #        c c c  #         #
# #        c @ c  #       c #
#          c c c  #         #
# # # c #          ##       #
#c   ##### c     ########   #
#          # ##             #
#   c #####   !#  c #####   #
#     #       #    #        #
#     #   ####    #    c    #
#     #          #          #
#     c#########   c        #
#     c      #     c        #
#############################

Last edited on
Thank you very much :)

Welcome :)
Topic archived. No new replies allowed.