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