c++ maze game assignment help

Pages: 12
So essentially we've been assigned to complete finish out game.cpp file based on a few other header files. The issue i have is that while i can make the character move and store to the tour[N][N] but, im not sure how to avoid forever going over the same path. i need to know that ive gone over the space already but i need to be able to go back over it if it leads to a door once the matching key has been gathered for that door.
game objective: a maze with multiple keys and multiple doors when all doors are opened the game is won. the professor suggested at one point implimenting a rnadom number generator to avoid having to keep trying the same path over and over again and that way it would eventually try all options without likely getting stuck in a loop of the same path over and over again, but for whatever reason the file didnt save correctly so i need to mess with that 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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include "personType.h"
#include "boardType.h"
using namespace std;

const int N = 8;
int numberOfMoves = 0;

bool winGame(boardType board, personType player, int tour[N][N]);
bool isNextMoveValid(int tour[N][N], int x, int y);
int highestNumberOnBoard(int tour[N][N]);
bool allSameNumber(int tour[N][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)) {
    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(x >= 0 && x < N && y >= 0 && y < N && tour[x][y] == 1)
        return true;

    return false;
}

int highestNumberOnBoard(int tour[N][N])
{
//what is the highest position ive traversed on board
}

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

}

bool winGame(boardType board, personType player, int tour[N][N], int x, int y)
{
 numberOfMoves++;
 if(board.victory())
    return true;
 {
     locationType currentPlayerLocation = player.getCurrentLocation();
     if(board.isKeyAtThisLocation(currentPlayerLocation))
        player.pickupKey(board.pickupKey(currentPlayerLocation));
     if(board.isDoorAtThisLocation(currentPlayerLocation))
        player.openDoor(board.getDoor(currentPlayerLocation));
     personType movePlayerRight(player);
     personType movePlayerLeft(player);
     personType movePlayerUp(player);
     personType movePlayerDown(player);
     
     
     if(movePlayerRight.moveRight())
        return winGame(board, movePlayerRight, tour[N][N])
    else if(movePlayerLeft.moveLeft())
        return winGame(board, movePlayerLeft, tour[N][N])
    else if(movePlayerUp.moveUp())
        return winGame(board, movePlayerUp, tour[N][N])
    else if(movePlayerDown.moveDown())
        return winGame(board, movePlayerDown, tour[N][N])
    return board.victory();
 }
}
I see that a move to position x,y is valid if tour[x][y]==1. So inside wingame(), you need to set tour[x][y]=0 upon entry to indicate that the current location is on the tour. Here x,y are the current location. When wingame() returns false, you have to set tour[x][y] back to 1. This might require rewriting the end of wingame() to something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
    bool result = false;
     if(movePlayerRight.moveRight())
        result = winGame(board, movePlayerRight, tour[N][N])
    else if(movePlayerLeft.moveLeft())
        result = winGame(board, movePlayerLeft, tour[N][N])
    else if(movePlayerUp.moveUp())
        result = winGame(board, movePlayerUp, tour[N][N])
    else if(movePlayerDown.moveDown())
        result = winGame(board, movePlayerDown, tour[N][N])
    if (!result) {
        tour[x][y] = 1;
    }
    return result;
this is what i have now though im still running into some errors particulalry line 34 says to few arguments and something about trouble passing int

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
#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;

bool winGame(boardType& board, personType& player, int tour[N][N], int x, int y);
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);

int main() {

  srand(time(NULL));

  int tour[N][N] = { {0},{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))
    return true;
    else if (tour[x][y] == highestNumberOnBoard(tour))
        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; i++)
    {
        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; i++)
    {
        if (tour[i][j] != test)
            return false;
    }
}
    return true;
}

bool winGame(boardType& board, personType& player, int tour[N][N], int x, int y)
{
 numberOfMoves++;
 if(board.victory())
    return true;
 {
     locationType currentPlayerLocation = player.getCurrentLocation();
     tour[currentPlayerLocation.x][currentPlayerLocation.y]++;
     if(board.isKeyAtThisLocation(currentPlayerLocation)){
        player.pickupKey(board.pickupKey(currentPlayerLocation));}
     if(board.isDoorAtThisLocation(currentPlayerLocation)){
        player.openDoor(board.getDoor(currentPlayerLocation));}
     personType movePlayerRight(player);
     personType movePlayerLeft(player);
     personType movePlayerUp(player);
     personType movePlayerDown(player);




      if(movePlayerRight.moveRight() && isNextMoveValid (tour, currentPlayerLocation.x+1, currentPlayerLocation.y)){
        return winGameboard, movePlayerRight, tour);}
    else if(movePlayerLeft.moveLeft() && isNextMoveValid (tour, currentPlayerLocation.x-1, currentPlayerLocation.y)){
        return winGame(board, movePlayerLeft, tour);}
    else if(movePlayerUp.moveUp()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y+1)){
        return winGame(board, movePlayerUp, tour);}
    else if(movePlayerDown.moveDown()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y-1)){
        return  winGame(board, movePlayerDown, tour);}

    {
        return (board.victory());
    }

 }
}
@mpvick

Your not passing the x and y variables to the winGame function, on line 34
@whitenite1

would i just throw an & infront of the int x and y such as int& x , int& y?
finally get a print screen but im pretty sure im passing the values of x and y wrong or my wingame function isnt complete as i get oops well that didnt work back. should i set x and y equal to zero in main?


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
#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;

bool winGame(boardType board, personType mainPlayer, int tour[N][N], int x, int y);
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);

int main() {

int x;
int y;
  srand(time(NULL));

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

  personType mainPlayer(N,N);

  boardType board(N, N, N);

  board.printBoard();

  if (winGame(board,mainPlayer,tour, x, y) == 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;
}

bool winGame(boardType board, personType mainPlayer, int tour[N][N], int x, int y)
{
 numberOfMoves++;
 if(board.victory())
    return true;
 {
     locationType currentPlayerLocation = mainPlayer.getCurrentLocation();
     tour[currentPlayerLocation.x][currentPlayerLocation.y]++;
     if(board.isKeyAtThisLocation(currentPlayerLocation)){
        mainPlayer.pickupKey(board.pickupKey(currentPlayerLocation));}
     if(board.isDoorAtThisLocation(currentPlayerLocation)){
        mainPlayer.openDoor(board.getDoor(currentPlayerLocation));}
     personType movePlayerRight(mainPlayer);
     personType movePlayerLeft(mainPlayer);
     personType movePlayerUp(mainPlayer);
     personType movePlayerDown(mainPlayer);




      if(movePlayerRight.moveRight() && isNextMoveValid (tour, currentPlayerLocation.x+1, currentPlayerLocation.y)){
        return winGame(board, movePlayerRight, tour, x ,y);}
    else if(movePlayerLeft.moveLeft() && isNextMoveValid (tour, currentPlayerLocation.x-1, currentPlayerLocation.y)){
        return winGame(board, movePlayerLeft, tour, x ,y);}
    else if(movePlayerUp.moveUp()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y+1)){
        return winGame(board, movePlayerUp, tour,x ,y);}
    else if(movePlayerDown.moveDown()&& isNextMoveValid (tour, currentPlayerLocation.x, currentPlayerLocation.y-1)){
        return  winGame(board, movePlayerDown, tour,x,y);}

    {
        return (board.victory());
    }

 }
}
        // locationType doorLocations[N]

       // under 250 moves
It's hard to tell what's wrong since you haven't posted these files:
1
2
3
4
5
6
#include "personType.h"
#include "boardType.h"
#include "doorType.h"
#include "keyType.h"
#include "lockType.h"
#include "locationType.h" 


should i set x and y equal to zero in main?
Currently you're passing x and y uninitialized into winGame(). What do the x and y parameters to winGame represent? Is it okay to pass uninitialized values? Figure out what the parameters are supposed to mean. Document that with a comment at the top of winGame(). Then make sure you're passing the right arguments when you call it.

I see that you incrment tour near the top of winGame, but you never unset it when discover a dead-end path. You probably need to decrement it any time you return false from winGame. I suggested something similar to this in my previous post.

Line 30: It seems odd to me that a person would need to know how big the board is.

Line 34: Consider renaming printBoard() to just print(). Adding "board" to the name of a board method is redundant.

Lines 49 & 50: Why does having the same number in every space on the board make the move valid? I would think it's only valid to move to a space that you haven't visited already (i.e., one that isn't on the current tour).

I think there are other logic errors in the code, but it's difficult to say without the missing header files so I can compile the program myself.
locationType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef locationType_H
#define locationType_H 
   
struct locationType {
  int x;
  int y;

  locationType();
  locationType(int, int);
};

#endif
personType.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef personType_H
#define personType_H 

#include <vector>
#include "locationType.h"
#include "keyType.h"
#include "doorType.h"
#include "boardType.h"
   
class personType {
	
  public:
	
    // Post-condition: The y-coordinate increases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
    bool moveUp();
	  
    // Post-condition: The y-coordinate decreases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
    bool moveDown();
	  
    // Post-condition: The x-coordinate decreases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
    bool moveLeft();
	  
    // Post-condition: The x-coordinate increases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
    bool moveRight();
	  
    // Post-condition: The key is added to person's keyChain.
    void pickupKey(keyType key);
	  
    // Post-condition: The door is opened by trying every key.
    //   If the door was opened, the function returns true.
    bool openDoor(doorType &door);

    // Post-condition: Returns the current location of the Person
    locationType getCurrentLocation() const;
	 
    // Creates a person with an empty keyChain and the
    //   currentLocation of (0,0)
    personType(int h, int w);
	  
    // Creates a person with an empty keyChain and the
    //   currentLocation is set to startingLocation.
    personType(locationType startingLocation, int h, int w);

  private:
	
    int boardHeight;
    int boardWidth;
    locationType currentLocation;
    std::vector<keyType> keyChain;
};

#endif
boardType.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef H_BOARDTYPE
#define H_BOARDTYPE

#include <vector>
#include "locationType.h"
#include "keyType.h"
#include "doorType.h"

class boardType {
  
  public:
     
     // Accessor Only
     // Returns if the Move Proposed Is Valid
     bool isValidSpace(locationType location) const;

     // Initialize the board
     // Sets Height to h
     // Sets Width to w
     // Creates a vector of doors and keys of size doorNum
     boardType(int h, int w, int doorNum);

     // Returns the First Key At This Location If There Is One
     // If There is No Key, Return NULL 
     // Remove the key from the vector "keys"
     keyType pickupKey(locationType location);

     // Accessor Only
     // Returns True if Any Key Is At The Present Location 
     bool isKeyAtThisLocation(locationType location) const;

     // Accessor Only
     // Returns True if Any Door Is At The Present Location
     bool isDoorAtThisLocation(locationType location) const;

     // Return Actual Door At Location Provided
     doorType& getDoor(locationType Location);

     // Accessor Only
     // Returns True if All Doors Are Open
     bool victory() const;

     // Accessor Only (Prints Out K or D for Key or Door, 0 for Neither)
     void printBoard() const;

  private:

     int height;
     int width;
     int doorNumber;

     std::vector<doorType> doors;
     std::vector<keyType> keys;

     // Adds Doors and Keys (They Will be Geographically Randomized)
     void setBoard();
};

#endif
I notice the argument list for function winGame is (board, persontype, tour[][],int, int)

But sometime you try to call winGame leaving out the two last ints from the argument list.
I belive in the most up to date one I include them when calling winGame

I'll double check though
I should have asked for the implementation (cpp) files for those classes too. Can you post all the headers and cpp files so we can compile the code ourselves?
for sure
locationType

1
2
3
4
5
6
7
8
9
10
11
#include "locationType.h"

locationType::locationType() {
  x = 0;
  y = 0;
}

locationType::locationType(int leftToRight, int bottomToTop) {
  x = leftToRight;
  y = bottomToTop;
}

personType

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
#include "locationType.h"
#include "keyType.h"
#include "doorType.h"
#include "personType.h"

	using namespace std;
   
    // Post-condition: The y-coordinate increases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
	bool personType::moveUp()
	{
		if(currentLocation.y < boardHeight-1)
		{
			currentLocation.y++;
                        return true;
		}
		else
		{
		        return false;
		}
	}
	  
    // Post-condition: The y-coordinate decreases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
	bool personType::moveDown()
	{
		if(currentLocation.y > 0)
		{
			currentLocation.y--;
                        return true;
		}
		else
		{
		        return false;
		}
	}
	  
    // Post-condition: The x-coordinate decreases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
	bool personType::moveLeft()
	{
		if(currentLocation.x > 0)
		{
			currentLocation.x--;
                        return true;
		}
		else
		{
		        return false;
		}
	}
	  
    // Post-condition: The x-coordinate increases by 1 if allowed by board size.
    //    This returns true if the move was allowed.
	bool personType::moveRight()
	{
		if(currentLocation.x < boardWidth-1)
		{
			currentLocation.x++;
                        return true;
		}
		else
		{
		        return false;
		}
	}

    // Post-condition: The key is added to person's keyChain.
	void personType::pickupKey (keyType key) 
	{
		keyChain.push_back(key);
	}
	  
    // Post-condition: The door is opened by trying every key.
    //   If the door was opened, the function returns true.
	bool personType::openDoor(doorType &door) 
	{
  		for(std::vector<keyType>::size_type i = 0; i != keyChain.size(); i++) 
		{
      			if (door.unlock(keyChain[i])) 
			{
          			door.open();
          			return true;
      			}
  		}
  		return false;
	}

    // Post-condition: Returns the current location of the Person
	locationType personType::getCurrentLocation() const
	{
		return currentLocation;
	}
	 
    // Creates a person with an empty keyChain and the
    //   currentLocation of (0,0)
	personType::personType(int h, int w) 
	{
    		currentLocation.x = 0;
    		currentLocation.y = 0;
                boardHeight = h;
                boardWidth = w;
	}
	
	  
    // Creates a person with an empty keyChain and the
    //   currentLocation is set to startingLocation.
	personType::personType(locationType startingLocation, int h, int w) 
	{
		currentLocation = startingLocation;
                boardHeight = h;
                boardWidth = w;
	}
boardType
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
#include "boardType.h"
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

boardType::boardType(int h, int w, int doorNum) {
  height = h;
  width = w;
  doorNumber = doorNum;
  setBoard();
}

bool boardType::isValidSpace(locationType location) const {
  return (location.x >= 0 && location.x < width && location.y >=0 && location.y < height);
}

keyType boardType::pickupKey(locationType location) {
  keyType foundKey;
  for(std::vector<keyType>::size_type i = 0; i != keys.size(); i++) {
    if (location.x == keys[i].getCurrentLocation().x && location.y == keys[i].getCurrentLocation().y) {
       foundKey = keys[i];
       keys.erase(keys.begin() + i);
       return foundKey;
    }
  }
  return foundKey;
}

bool boardType::isKeyAtThisLocation(locationType location) const {
  for(std::vector<keyType>::size_type i = 0; i != keys.size(); i++) {
    if (location.x == keys[i].getCurrentLocation().x && location.y == keys[i].getCurrentLocation().y) {
       return true;
    }
  }
  return false;
}
 
doorType& boardType::getDoor(locationType location) {
  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) {
       return doors[i];
    }
  }
  return doors[0];
}

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) {
       return true;
    } 
  }
  return false;
}

bool boardType::victory() const {
  for(std::vector<doorType>::size_type i = 0; i != doors.size(); i++) {
    if (!doors[i].isOpen()) {
      return false;
    }
  }
  return true;
}

void boardType::printBoard() const {
  for(int j=0; j<height; j++) {
    for (int i=0; i<width; i++) {
      locationType location(i,j);
      if (isKeyAtThisLocation(location)) 
        cout << 'K';
      else
        cout << '-';
      if (isDoorAtThisLocation(location))
        cout << 'D';
      else
        cout << '-';
      cout << ' ';
    }
    cout << endl;
  } 
}

void boardType::setBoard() {
  
  for (int i=0; i<doorNumber; i++) {
    locationType doorLocation;
    locationType keyLocation;
    do {
      locationType tempDoor(rand() % (width-1), rand() % (height-1));
      locationType tempKey(rand() % (width-1), rand() % (height-1));
      doorLocation = tempDoor;
      keyLocation = tempKey;
    }
    while (isDoorAtThisLocation(doorLocation) || isKeyAtThisLocation(keyLocation));

    keyType key(keyLocation);
    lockType lock(key);

    doorType door(lock, doorLocation);
    door.close();
    door.lock();
 
    keys.push_back(key);
    doors.push_back(door);

  }

}
keyType
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
#include "keyType.h"
#include <cstdlib>

void keyType::setLocation(locationType newLocation) {
  currentLocation = newLocation;
}

keyType::keyType() {
  currentLocation.x = 0;
  currentLocation.y = 0;
  generateRandomCombinationCode();
}

int keyType::getCombinationCode() const {
  return combinationCode;
}

keyType::keyType(locationType startingLocation) {
  currentLocation = startingLocation;
  generateRandomCombinationCode();
}

void keyType::generateRandomCombinationCode() {
  combinationCode = rand() % 10000+1;
}

locationType keyType::getCurrentLocation() const {
  return currentLocation;
}
lockType
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
#include "keyType.h"
#include "lockType.h"

void lockType::lock() {
  locked = true;
}

bool lockType::unlock(const keyType &key) {
  if (combinationCode == key.getCombinationCode()) {
    locked = false;
    return true;
  } else {
    return false;
  }
}

void lockType::keyLock(const keyType &key) {
  combinationCode = key.getCombinationCode();
}

bool lockType::isLocked() const {
  return locked;
}

lockType::lockType() {
  combinationCode = 0;
  locked = false;
}

lockType::lockType(const keyType &key) {
  keyLock(key);
  locked = false;
}
doorType

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
//This is the implementation for door type.

#include "doorType.h"
#include "lockType.h"
#include "keyType.h"

using namespace std;



bool doorType::unlock(const keyType &key)
{
 if (doorLock.unlock(key)) {
    return true;
 }
 else
    return false;
}

locationType doorType::getCurrentLocation() const {
  return currentLocation;
}

bool doorType::lock()
{
    if (!doorIsOpen)
    {
        doorLock.lock();
        return true;
    }
    else
        return false;


}


bool doorType::open()
{
    if (doorLock.isLocked())
        return false;
    else
    {
       doorIsOpen = true;
       return true;
    }

}

void doorType::close()
{
    doorIsOpen = false;
}

bool doorType::isOpen() const
{
   return doorIsOpen;
}

void doorType::replaceLock (lockType newLock)
{
    doorLock = newLock;
}

void doorType::setLocation(locationType newLocation)
{
    currentLocation = newLocation;
}

doorType::doorType(lockType startingLock)
{
    doorLock = startingLock;
    currentLocation.x = 0;
    currentLocation.y = 0;
}


doorType::doorType(lockType startingLock, locationType startingLocation)
{
    doorLock = startingLock;
    currentLocation = startingLocation;
}
doorType.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef doorType_H
#define doorType_H 

#include "locationType.h"
#include "lockType.h"
   
class doorType {

  public:
    // Post-condition: lock will be unlocked if key has right combination
    //                 The function will return true if the key has the right combination
    bool unlock(const keyType &key);
		
    // Post-condition: This lock will be locked if it is closed;
    //                 The function will return true if the door was closed.	
    bool lock();

    // Post-condition: The door will be opened only if the lock is unlocked.	
    //     The function will return true if the door was opened.		
    bool open();  // Will Only Work if The Lock is Unlocked
		
    // Post-condition: This lock will be closed;
    void close();

    // Returns true if the door is open.
    bool isOpen() const;
		
    // Post-condition: This function will set lock to newLock
    void replaceLock(lockType newLock);
		
    // Post-condition: This function will set currentLocation to newLocation.
    void setLocation(locationType newLocation);

    // Returns Current Location of the Door
    locationType getCurrentLocation() const;	
    
    // Creates a new door that is closed and sets lock to startingLock
    //   The door is put in location (0,0)
    doorType(lockType startingLock);
		
    // Creates a new door that is closed and sets lock to startingLock
    //   The currentLocation is set to startingLocation;
    doorType(lockType startingLock, locationType startingLocation);
	
  private:
	   
    lockType doorLock;
    bool doorIsOpen;
    locationType currentLocation;
};

#endif
Pages: 12