Clearing the main

Hi all
Im currently working on a c++ maze game. At the minute my main is full of code that does not need to be there. I want to move the code from the main to my maze.cpp class and just have my main class call the information from the new class that will be made in maze.cpp.

Im not sure how to go about this can somebody please help.

this is my current main class...as you can see it needs work.

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
#include <iostream>
#include <cstdlib>
#include "maze.h" 

using namespace std;



int main(int argc, char *argv[])
{
system("color 78"); 

cout << "     _|      _|    _|_|    _|_|_|_|_|  _|_|_|_| \n" 
     << "     _|_|  _|_|  _|    _|        _|    _|       \n" 
     << "     _|  _|  _|  _|_|_|_|      _|      _|_|_|   \n" 
     << "     _|      _|  _|    _|    _|        _|       \n" 
     << "     _|      _|  _|    _|  _|_|_|_|_|  _|_|_|_| \n\n\n"                                                                                 
     << "         _|_|_|    _|_|    _|      _|  _|_|_|_|   \n"
     << "       _|        _|    _|  _|_|  _|_|  _|         \n"
     << "       _|  _|_|  _|_|_|_|  _|  _|  _|  _|_|_|     \n"
     << "       _|    _|  _|    _|  _|      _|  _|         \n"
     << "         _|_|_|  _|    _|  _|      _|  _|_|_|_|   \n";  
    
cout << "              Welcome to the Maze Game\n";   

system("PAUSE");

    
//create rooms

Room *a = new Room('A');
Room *b = new Room('B');
Room *c = new Room('C');
Room *d = new Room('D');
Room *e = new Room('E');
Room *f = new Room('F');
Room *g = new Room('G');
Room *h = new Room('H');
Room *i = new Room('I');
Room *j = new Room('J');
Room *k = new Room('K');
Room *l = new Room('L');

//set directions

a->setDirection(NULL, b, e, NULL);
b->setDirection(NULL, NULL, f, a);
c->setDirection(NULL, d, g, NULL);
d->setDirection(NULL, NULL, c, NULL);
e->setDirection(a, NULL, i, NULL);
f->setDirection(b, g, NULL, NULL);
g->setDirection(c, h, k, f);
h->setDirection(NULL, NULL, l, g);
i->setDirection(e, j, NULL, NULL);
j->setDirection(NULL, NULL, i, NULL);
k->setDirection(g, NULL, NULL, NULL);
l->setDirection(h, NULL, NULL, NULL);


int Counter=0;
int Score=0;
Room *current = a;
Room *next;
char userChoice;

do
 {
 current->roomProperties();
 cin >> userChoice;
 
 next = current->moveDirection(userChoice);
 Counter++;
 if (NULL != next)
    current = next;
    
 }
 
 while (current->getName() != 'L');
 
 if (Counter= 5) Score = 1000;
 if (Counter >5 && Counter <8) Score = 600;
 if (Counter >8 && Counter <12) Score = 300;
 if (Counter >12 && Counter <15) Score = 150;
 if (Counter >15) Score = 0;
 
 
 cout << "********************************************************\n"
      << "*      YOU HAVE MADE IT TO ROOM L.....YOU WIN!!        *\n"
      << "********************************************************\n"
      << "*            You made it to Room L in " << Counter << " moves          *\n"
      << "********************************************************\n"
      << "*                Your Score is " << Score << "                    *\n" 
      << "********************************************************\n"
      << "*         _._._                       _._._            *\n"
      << "*        _|   |_                     _|   |_           *\n"
      << "*        | ... |_._._._._._._._._._._| ... |           *\n"
      << "*        | ||| |  o    FREEDOM    o  | ||| |           *\n"
      << "*   ())  |[-|-]| [-|-]  [-|-]  [-|-] |[-|-]|  ())      *\n"
      << "*  (())) |     |---------------------|     | (()))     *\n"
      << "* (()))()|[-|-]|  :::   .-\"-.   :::  |[-|-]|(()))()    *\n"
      << "* ()))(()|     | |~|~|  |_|_|  |~|~| |     |()))(()    *\n"
      << "*    ||  |_____|_|_|_|__|_|_|__|_|_|_|_____|  ||       *\n"
      << "* ~ ~^^               /=======\\               ^^~ ~    *\n"
      << "*      ^~^~                                ~^~^        *\n" 
      << "*                                                      *\n"
      << "********************************************************\n";
      
       
      
 system("PAUSE");

  return 0;
 }
You coud consider letting your Maze class contain the create-maze and init-maze things you have in main(), but other than that...i dunno
I would recommend putting all of your class/functions declaration in a header file. Their definition in a functions definition file. And basically just call them from the main. Just spilt them up, such as create room, set directions could all be a member of class Maze. Ex:

Main.cpp
1
2
3
4
5
6
7
8
9
#include "file.h"

int main(){
	Maze Test; 
	Test.printMaze();
                Test.CreateRoom(); 
                Test.SetDirection();
}


file.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>
#include <fstream>
using namespace std; 

class Maze
{
	public:
	Maze();
	void printTable(); 
                void Test.CreateRoom(); 
                void Test.SetDirection();
}; 


definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "file.h"
void Maze::printTable()	 
{
code here...
}

void Maze::CreateRoom()
{
code here
}
 
void Maze::SetDirection()
{
code here
}
Last edited on
thanks for your help i will give that a go
Topic archived. No new replies allowed.