c++ maze game assignment help

Pages: 12
keyType.h
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
#ifndef keyType_H
#define keyType_H 

#include "locationType.h"
   
class keyType {
	
  public:
	
    // Post-condition: currentLocation will be equal to newLocation
    void setLocation(locationType newLocation);
	  
    // Creates a keyType with a random combination code
    //    The currentLocation should be (0,0);
    keyType();
	  
    // Creates a keyType with a random combination code
    //    The currentLocation should set tp the startingLocation.
    keyType(locationType startingLocation);

    // Returns the Combination Code
    int getCombinationCode() const;

    // Returns the Current Location
    locationType getCurrentLocation() const;
	   
  private:

    int combinationCode; // Will Be Random Number Between 1 and 10,000
    locationType currentLocation;
	  
    // Creates a random integer between 1 and 10,000 and assigns it to combinationCode.
    void generateRandomCombinationCode();
	  
};

#endif

lockType.h
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
#ifndef lockType_H
#define lockType_H 

#include "keyType.h"

class lockType {
	
  public:
	   
    // Post-condition: locked will be true.
    void lock();
	   
    // Post-condition: locked will be false if key has right combination
    //                 The function will return true if the key has the right combination
    bool unlock(const keyType &key);
	   
    // Post-condition: combination code of this lock will be changed to match combination of key
    void keyLock(const keyType &key);  // Like a Locksmith Would Do
	   
    // Post-condition: returns locked boolean
    bool isLocked() const;
	   
    // Creates a locktype with combination code of 0
    // Lock should start out as unlocked.
    lockType();
	   
    // Creates a locktype with combination matching that of key.
    // Lock should start out as unlocked.
    lockType(const keyType &key);
	
  private:
    int combinationCode; 
    bool locked;
	
};

#endif
bump
I've been looking at this. A couple more question. It looks like a door doesn't lead anywhere. Is that right? In that case the algorithm should probably be done in 2 passes:
- find all keys and doors. Pick up the keys
- find all doors and open them.

You've made one big mistake in designing your data. It would make more sense to have the board be a 2D array of rooms where each room includes info like a vector of keys and an optional door. I've been working with the data as-is but I thought I'd mention this.

Is this an assignment? Can you post the full text?
Okay, I've got something that seems to work. I changed boardType::isDoorAtThisLocation() so it returns true only if the door is locked:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Return true if a LOCKED (dmh) door is here
bool
boardType::isDoorAtThisLocation(locationType location) const
{
    for (std::vector < doorType >::size_type i = 0; i != doors.size(); i++) {
        if (location.x == doors[i].getCurrentLocation().x
            && location.y == doors[i].getCurrentLocation().y &&
            !doors[i].isOpen()) {
            return true;
        }
    }
    return false;
}


I think all other changes were in the main program. Search for "dmh" in the comments. Some highlights:
- In winGame you have to pass board and mainPlayer by reference because winGame modifies both.
- That also means that the recursive calls to winGame must pass mainPlayer, not a modified copy. That makes the recursive calls a little awkward: move right, make recursive call, move left, repeat for up, down, and right.
- I check for a won game after each recursive call.
- If there's a door in a room, then winGame tries to open the door a second time, after the recursive calls. After all, but that time, it might have the key that opens it.
- I added a printTour() function to print the current tour for debugging. To use it, just comment out the return statement on the first line.
- I added some debugging statements so you can see when winGame() finds a key or opens a door.
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include "personType.h"
#include "boardType.h"
#include "doorType.h"
#include "keyType.h"
#include "lockType.h"
#include "locationType.h"

using namespace std;

const int N = 8;
int numberOfMoves = 0;

// dmh pass by ref
bool winGame(boardType &board, personType &mainPlayer, int tour[N][N]);
bool isNextMoveValid(int tour[N][N], int x, int y);
int highestNumberOnBoard(int tour[N][N], int i, int j);
bool allSameNumber(int tour[N][N], int i, int j);

// Test function to print the current tour
void printTour(int tour[N][N])
{
    return;
    for (unsigned i=0; i<N; ++i) {
	for (unsigned j=0; j<N; ++j) {
	    cout << ' ' << tour[j][i] << ' ';
	}
	cout << '\n';
    }
    cout << '\n';
}
	
int
main()
{

    srand(time(NULL));

    int tour[N][N] = { {0} };

    personType mainPlayer(N, N);

    boardType board(N, N, N);

    board.printBoard();

    if (winGame(board, mainPlayer, tour) == true) {
	cout << "I opened all the doors!" << endl;
	cout << "It took me " << numberOfMoves << " moves." << endl;
    } else {
	cout << "Oops.  Well, that didn't work." << endl;
    }

    return 0;
}

bool
isNextMoveValid(int tour[N][N], int x, int y)
{
    if (allSameNumber(tour, x, y))
	return true;
    else if (tour[x][y] == highestNumberOnBoard(tour, x, y))
	return false;
    else
	return true;
}

int
highestNumberOnBoard(int tour[N][N], int i, int j)
{
    int max = tour[0][0];
    for (int i = 0; i < N; i++) {
	for (int j = 0; j < N; j++) {
	    if (tour[i][j] > max)
		max = tour[i][j];
	}
    }

    return max;
//what is the highest position ive traversed on board
}

bool
allSameNumber(int tour[N][N], int i, int j)
{
//checks to see if been here already
//i could create a flag method
    int test = tour[0][0];

    for (int i = 0; i < N; i++) {
	for (int j = 0; j < N; j++) {
	    if (tour[i][j] != test)
		return false;
	}
    }
    return true;
}

// dmh: drop x and y. Use location of mainPlayer
// dmh: pass board by ref. Otherwise the keys regenerate
// The tour value is set to 1 when you enter winGame and 2 when you leave
bool
winGame(boardType &board, personType &mainPlayer, int tour[N][N])
{
    numberOfMoves++;
    if (board.victory())
	return true;

    locationType currentPlayerLocation = mainPlayer.getCurrentLocation();

    if (tour[currentPlayerLocation.x][currentPlayerLocation.y]) {
	// You've already explored this spot
	return false;
    }
    
    // Indicate that you're working on this location
    tour[currentPlayerLocation.x][currentPlayerLocation.y]++;
    printTour(tour);
    if (board.isKeyAtThisLocation(currentPlayerLocation)) {
	cout << "Found key at " << currentPlayerLocation.x
	     << ',' << currentPlayerLocation.y << '\n';
	mainPlayer.pickupKey(board.pickupKey(currentPlayerLocation));
    }
    bool tryDoorAgain = false;
    if (board.isDoorAtThisLocation(currentPlayerLocation)) {
	cout << "Found door at " << currentPlayerLocation.x
	     << ',' << currentPlayerLocation.y << '\n';
	if (mainPlayer.openDoor(board.getDoor(currentPlayerLocation))) {
	    cout << "unlocked it\n";
	} else {
	    cout << "Can't unlock it\n";
	    tryDoorAgain = true;
	}
    }

    // dmh check for winGame each time.
    // also, you have to use the mainPlayer here because winGame
    // modifies the player (their keychain)
    if (mainPlayer.moveRight()) {
	if (winGame(board, mainPlayer, tour)) {
	    return true;
	}
	mainPlayer.moveLeft();	// return to previous position
    }

    if (mainPlayer.moveLeft()) {
	if (winGame(board, mainPlayer, tour)) {
	    return true;
	}
	mainPlayer.moveRight();	// return to previous position
    }

    if (mainPlayer.moveUp()) {
	if (winGame(board, mainPlayer, tour)) {
	    return true;
	}
	mainPlayer.moveDown();	// return to previous position
    }

    if (mainPlayer.moveDown()) {
	if (winGame(board, mainPlayer, tour)) {
	    return true;
	}
	mainPlayer.moveUp();	// return to previous position
    }

    // If you get here then you didn't win yet, but you
    // should have all the keys. If there's a door, then
    // try it again.
    if (tryDoorAgain) {
	cout << "Trying door at " << currentPlayerLocation.x
	     << ',' << currentPlayerLocation.y << " again.\n";
	if (mainPlayer.openDoor(board.getDoor(currentPlayerLocation))) {
	    cout << "unlocked it\n";
	    // and see if you've now won.
	    if (board.victory()) return true;
	} else {
	    cout << "Can't unlock it\n";
	    tryDoorAgain = true;
	}
    }
    // Indicate that you've already tried this location
    tour[currentPlayerLocation.x][currentPlayerLocation.y]++;
    return false;
}

yeh the doors lead nowhere the game is complete when all doors are opened
Topic archived. No new replies allowed.
Pages: 12